r82199 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82198‎ | r82199 | r82200 >
Date:22:11, 15 February 2011
Author:reedy
Status:ok
Tags:
Comment:
Break out of the foreach loop in addMatchedText after we've matched the target language and done whatever we needed to do. Per chad

Remove some commented out/debug code

Normalise comments
Modified paths:
  • /trunk/extensions/DoubleWiki/DoubleWiki_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/DoubleWiki/DoubleWiki_body.php
@@ -19,12 +19,12 @@
2020
2121 class DoubleWiki {
2222
23 - /*
 23+ /**
2424 * Tags that must be closed. (list copied from Sanitizer.php)
2525 */
2626 var $tags = '/<\/?(b|del|i|ins|u|font|big|small|sub|sup|h1|h2|h3|h4|h5|h6|cite|code|em|s|strike|strong|tt|tr|td|var|div|center|blockquote|ol|ul|dl|table|caption|pre|ruby|rt|rb|rp|p|span)([\s](.*?)>|>)/i';
2727
28 - /*
 28+ /**
2929 * Read the list of matched phrases and add tags to the html output.
3030 */
3131 function addMatchingTags ( &$text, $lang ) {
@@ -34,7 +34,7 @@
3535 return;
3636 }
3737 $text = str_replace( $m[1], '', $text );
38 - $line_pattern = "/\s*([^:\n]*?)\s*:\s*([^:\n]*?)\s*\n/i";
 38+ $line_pattern = '/\s*([^:\n]*?)\s*:\s*([^:\n]*?)\s*\n/i';
3939 $items = array();
4040 preg_match_all( $line_pattern, $m[1], $items, PREG_SET_ORDER );
4141 foreach ( $items as $n => $i ) {
@@ -66,6 +66,7 @@
6767 foreach ( $out->mLanguageLinks as $l ) {
6868 $nt = Title::newFromText( $l );
6969 $iw = $nt->getInterwiki();
 70+
7071 if ( $iw === $match_request ) {
7172 $url = $nt->getFullURL();
7273 $myURL = $out->getTitle()->getLocalURL();
@@ -74,22 +75,23 @@
7576
7677 $translation = Http::get( wfAppendQuery( $url, array( 'action' => 'render' ) ) );
7778 if ( $translation !== null ) {
78 - # first find all links that have no 'class' parameter.
79 - # these links are local so we add '?match=xx' to their url,
80 - # unless it already contains a '?'
 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+ */
8183 $translation = preg_replace(
8284 "/<a href=\"http:\/\/([^\"\?]*)\"(([\s]+)(c(?!lass=)|[^c\>\s])([^\>\s]*))*\>/i",
8385 "<a href=\"http://\\1?match={$wgContLanguageCode}\"\\2>", $translation );
84 - # now add class='extiw' to these links
 86+ // now add class='extiw' to these links
8587 $translation = preg_replace(
8688 "/<a href=\"http:\/\/([^\"]*)\"(([\s]+)(c(?!lass=)|[^c\>\s])([^\>\s]*))*\>/i",
8789 "<a href=\"http://\\1\" class=\"extiw\"\\3>", $translation );
88 - # use class='extiw' for images too
 90+ // use class='extiw' for images too
8991 $translation = preg_replace(
9092 "/<a href=\"http:\/\/([^\"]*)\"([^\>]*)class=\"image\"([^\>]*)\>/i",
9193 "<a href=\"http://\\1\"\\2class=\"extiw\"\\3>", $translation );
9294
93 - # add prefixes to internal links, in order to prevent duplicates
 95+ // add prefixes to internal links, in order to prevent duplicates
9496 $translation = preg_replace( "/<a href=\"#(.*?)\"/i", "<a href=\"#l_\\1\"",
9597 $translation );
9698 $translation = preg_replace( "/<li id=\"(.*?)\"/i", "<li id=\"l_\\1\"",
@@ -97,14 +99,17 @@
98100 $text = preg_replace( "/<a href=\"#(.*?)\"/i", "<a href=\"#r_\\1\"", $text );
99101 $text = preg_replace( "/<li id=\"(.*?)\"/i", "<li id=\"r_\\1\"", $text );
100102
101 - # add ?match= to local links of the local wiki
 103+ // add ?match= to local links of the local wiki
102104 $text = preg_replace( "/<a href=\"\/([^\"\?]*)\"/i",
103105 "<a href=\"/\\1?match={$match_request}\"", $text );
104106
105 - # do the job
 107+ // do the job
106108 $text = $this->matchColumns ( $text, $myLanguage, $myURL, $wgContLanguageCode,
107109 $translation, $languageName, $url, $match_request );
108110 }
 111+
 112+ //Break once we've matched a language link, no point in continuing processing
 113+ break;
109114 }
110115 }
111116 return true;
@@ -134,12 +139,12 @@
135140 $tag = $left_tags[1][$i];
136141 $left_chunk .= $left_slices[$i];
137142
138 - # if we are at the end of the loop, finish quickly
 143+ // if we are at the end of the loop, finish quickly
139144 if ( $i == $leftSliceCount - 1 ) {
140145 $right_chunk .= $right_text;
141146 $found = true;
142147 } else {
143 - # look for requested tag in the text
 148+ // look for requested tag in the text
144149 $a = strpos ( $right_text, $tag );
145150 if ( $a ) {
146151 $found = true;
@@ -151,8 +156,6 @@
152157 $right_chunk .= $m[0];
153158 $right_text = substr( $right_text, strlen( $m[0] ) );
154159 }
155 - # } else {
156 - # print "<br/>tag not found ".$tag;
157160 }
158161 }
159162
@@ -161,7 +164,6 @@
162165 $left_bits = $this->find_paragraphs( $left_chunk );
163166 $right_bits = $this->find_paragraphs( $right_chunk );
164167
165 - // $body .= "<tr style=\"background-color:#ffdddd;\"><td>".count($left_bits)."</td><td>".count($right_bits)."</td></tr>\n";
166168 // Do not align paragraphs if counts are different
167169 if ( count( $left_bits ) != count( $right_bits ) ) {
168170 $left_bits = array( $left_chunk );
@@ -196,11 +198,11 @@
197199 return $head . $body . "</table>" ;
198200 }
199201
200 - /*
 202+ /**
201203 * Split text and return a set of html-balanced paragraphs
202204 */
203205 function find_paragraphs( $text ) {
204 - $result = Array();
 206+ $result = array();
205207 $bits = preg_split( $this->tags, $text );
206208 $m = array();
207209 preg_match_all( $this->tags, $text, $m, PREG_SET_ORDER );
@@ -226,7 +228,7 @@
227229 return $result;
228230 }
229231
230 - /*
 232+ /**
231233 * Split text and return a set of html-balanced slices
232234 */
233235 function find_slices( $left_text ) {
@@ -237,7 +239,7 @@
238240 preg_match_all( $tag_pattern, $left_text, $left_tags, PREG_PATTERN_ORDER );
239241 $n = count( $left_slices );
240242
241 - /*
 243+ /**
242244 * Make slices that are full paragraphs
243245 * If two slices correspond to the same paragraph, the second one will be empty
244246 */
@@ -250,7 +252,7 @@
251253 }
252254 }
253255
254 - /*
 256+ /**
255257 * Keep only slices that contain balanced html
256258 * If a slice is unbalanced, we merge it with the next one.
257259 * The first and last slices are compensated.

Status & tagging log