r82202 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82201‎ | r82202 | r82203 >
Date:22:32, 15 February 2011
Author:reedy
Status:resolved (Comments)
Tags:
Comment:
Cannot fathom out why the loop was even needed

getLanguageLinks returns an "Associative array mapping language code to the page name"

Switch to using accessor, check the wanted language link exists in the array returned, if so, do what we were supposed to do before
Modified paths:
  • /trunk/extensions/DoubleWiki/DoubleWiki_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/DoubleWiki/DoubleWiki_body.php
@@ -63,55 +63,53 @@
6464 }
6565 $this->addMatchingTags ( $text, $match_request );
6666
67 - foreach ( $out->mLanguageLinks as $l ) {
68 - $nt = Title::newFromText( $l );
69 - $iw = $nt->getInterwiki();
 67+ $langLinks = $out->getLanguageLinks();
7068
71 - if ( $iw === $match_request ) {
72 - $url = $nt->getFullURL();
73 - $myURL = $out->getTitle()->getLocalURL();
74 - $languageName = $wgContLang->getLanguageName( $nt->getInterwiki() );
75 - $myLanguage = $wgLang->getLanguageName( $wgContLanguageCode );
 69+ if ( isset( $langLinks[$match_request] ) ) {
 70+ $nt = Title::newFromText( $langLinks[$match_request] );
7671
77 - $translation = Http::get( wfAppendQuery( $url, array( 'action' => 'render' ) ) );
78 - if ( $translation !== null ) {
79 - /* first find all links that have no 'class' parameter.
80 - * these links are local so we add '?match=xx' to their url,
81 - * unless it already contains a '?'
82 - */
83 - $translation = preg_replace(
84 - "/<a href=\"http:\/\/([^\"\?]*)\"(([\s]+)(c(?!lass=)|[^c\>\s])([^\>\s]*))*\>/i",
85 - "<a href=\"http://\\1?match={$wgContLanguageCode}\"\\2>", $translation );
86 - // now add class='extiw' to these links
87 - $translation = preg_replace(
88 - "/<a href=\"http:\/\/([^\"]*)\"(([\s]+)(c(?!lass=)|[^c\>\s])([^\>\s]*))*\>/i",
89 - "<a href=\"http://\\1\" class=\"extiw\"\\3>", $translation );
90 - // use class='extiw' for images too
91 - $translation = preg_replace(
92 - "/<a href=\"http:\/\/([^\"]*)\"([^\>]*)class=\"image\"([^\>]*)\>/i",
93 - "<a href=\"http://\\1\"\\2class=\"extiw\"\\3>", $translation );
 72+ $url = $nt->getFullURL();
 73+ $myURL = $out->getTitle()->getLocalURL();
 74+ $languageName = $wgContLang->getLanguageName( $nt->getInterwiki() );
 75+ $myLanguage = $wgLang->getLanguageName( $wgContLanguageCode );
9476
95 - // add prefixes to internal links, in order to prevent duplicates
96 - $translation = preg_replace( "/<a href=\"#(.*?)\"/i", "<a href=\"#l_\\1\"",
97 - $translation );
98 - $translation = preg_replace( "/<li id=\"(.*?)\"/i", "<li id=\"l_\\1\"",
99 - $translation );
100 - $text = preg_replace( "/<a href=\"#(.*?)\"/i", "<a href=\"#r_\\1\"", $text );
101 - $text = preg_replace( "/<li id=\"(.*?)\"/i", "<li id=\"r_\\1\"", $text );
 77+ $translation = Http::get( wfAppendQuery( $url, array( 'action' => 'render' ) ) );
 78+ if ( $translation !== null ) {
 79+ /**
 80+ * first find all links that have no 'class' parameter.
 81+ * these links are local so we add '?match=xx' to their url,
 82+ * unless it already contains a '?'
 83+ */
 84+ $translation = preg_replace(
 85+ "/<a href=\"http:\/\/([^\"\?]*)\"(([\s]+)(c(?!lass=)|[^c\>\s])([^\>\s]*))*\>/i",
 86+ "<a href=\"http://\\1?match={$wgContLanguageCode}\"\\2>", $translation );
 87+ // now add class='extiw' to these links
 88+ $translation = preg_replace(
 89+ "/<a href=\"http:\/\/([^\"]*)\"(([\s]+)(c(?!lass=)|[^c\>\s])([^\>\s]*))*\>/i",
 90+ "<a href=\"http://\\1\" class=\"extiw\"\\3>", $translation );
 91+ // use class='extiw' for images too
 92+ $translation = preg_replace(
 93+ "/<a href=\"http:\/\/([^\"]*)\"([^\>]*)class=\"image\"([^\>]*)\>/i",
 94+ "<a href=\"http://\\1\"\\2class=\"extiw\"\\3>", $translation );
10295
103 - // add ?match= to local links of the local wiki
104 - $text = preg_replace( "/<a href=\"\/([^\"\?]*)\"/i",
105 - "<a href=\"/\\1?match={$match_request}\"", $text );
 96+ // add prefixes to internal links, in order to prevent duplicates
 97+ $translation = preg_replace( "/<a href=\"#(.*?)\"/i", "<a href=\"#l_\\1\"",
 98+ $translation );
 99+ $translation = preg_replace( "/<li id=\"(.*?)\"/i", "<li id=\"l_\\1\"",
 100+ $translation );
 101+ $text = preg_replace( "/<a href=\"#(.*?)\"/i", "<a href=\"#r_\\1\"", $text );
 102+ $text = preg_replace( "/<li id=\"(.*?)\"/i", "<li id=\"r_\\1\"", $text );
106103
107 - // do the job
108 - $text = $this->matchColumns ( $text, $myLanguage, $myURL, $wgContLanguageCode,
109 - $translation, $languageName, $url, $match_request );
110 - }
 104+ // add ?match= to local links of the local wiki
 105+ $text = preg_replace( "/<a href=\"\/([^\"\?]*)\"/i",
 106+ "<a href=\"/\\1?match={$match_request}\"", $text );
111107
112 - //Break once we've matched a language link, no point in continuing processing
113 - break;
 108+ // do the job
 109+ $text = $this->matchColumns ( $text, $myLanguage, $myURL, $wgContLanguageCode,
 110+ $translation, $languageName, $url, $match_request );
114111 }
115112 }
 113+
116114 return true;
117115 }
118116

Follow-up revisions

RevisionCommit summaryAuthorDate
r82229Partial revert to r82202. Readd the loop, keep a couple of the improvements t...reedy10:22, 16 February 2011

Comments

#Comment by 😂 (talk | contribs)   05:02, 16 February 2011

getLanguageLinks() documentation is incorrect, it actually returns an array of iw-prefixed titles, eg:

array( 'fr:Some page', 'es:Some other page' );

So the iteration and title construction was at least somewhat necessary (although we can still bail once we've got a hit, like we did in r82199.

#Comment by Reedy (talk | contribs)   09:52, 16 February 2011

Fu-----

I'll fix it in the next few minutes

Status & tagging log