Index: trunk/extensions/DoubleWiki/DoubleWiki_body.php |
— | — | @@ -19,12 +19,12 @@ |
20 | 20 | |
21 | 21 | class DoubleWiki { |
22 | 22 | |
23 | | - /* |
| 23 | + /** |
24 | 24 | * Tags that must be closed. (list copied from Sanitizer.php) |
25 | 25 | */ |
26 | 26 | 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'; |
27 | 27 | |
28 | | - /* |
| 28 | + /** |
29 | 29 | * Read the list of matched phrases and add tags to the html output. |
30 | 30 | */ |
31 | 31 | function addMatchingTags ( &$text, $lang ) { |
— | — | @@ -34,7 +34,7 @@ |
35 | 35 | return; |
36 | 36 | } |
37 | 37 | $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'; |
39 | 39 | $items = array(); |
40 | 40 | preg_match_all( $line_pattern, $m[1], $items, PREG_SET_ORDER ); |
41 | 41 | foreach ( $items as $n => $i ) { |
— | — | @@ -66,6 +66,7 @@ |
67 | 67 | foreach ( $out->mLanguageLinks as $l ) { |
68 | 68 | $nt = Title::newFromText( $l ); |
69 | 69 | $iw = $nt->getInterwiki(); |
| 70 | + |
70 | 71 | if ( $iw === $match_request ) { |
71 | 72 | $url = $nt->getFullURL(); |
72 | 73 | $myURL = $out->getTitle()->getLocalURL(); |
— | — | @@ -74,22 +75,23 @@ |
75 | 76 | |
76 | 77 | $translation = Http::get( wfAppendQuery( $url, array( 'action' => 'render' ) ) ); |
77 | 78 | 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 | + */ |
81 | 83 | $translation = preg_replace( |
82 | 84 | "/<a href=\"http:\/\/([^\"\?]*)\"(([\s]+)(c(?!lass=)|[^c\>\s])([^\>\s]*))*\>/i", |
83 | 85 | "<a href=\"http://\\1?match={$wgContLanguageCode}\"\\2>", $translation ); |
84 | | - # now add class='extiw' to these links |
| 86 | + // now add class='extiw' to these links |
85 | 87 | $translation = preg_replace( |
86 | 88 | "/<a href=\"http:\/\/([^\"]*)\"(([\s]+)(c(?!lass=)|[^c\>\s])([^\>\s]*))*\>/i", |
87 | 89 | "<a href=\"http://\\1\" class=\"extiw\"\\3>", $translation ); |
88 | | - # use class='extiw' for images too |
| 90 | + // use class='extiw' for images too |
89 | 91 | $translation = preg_replace( |
90 | 92 | "/<a href=\"http:\/\/([^\"]*)\"([^\>]*)class=\"image\"([^\>]*)\>/i", |
91 | 93 | "<a href=\"http://\\1\"\\2class=\"extiw\"\\3>", $translation ); |
92 | 94 | |
93 | | - # add prefixes to internal links, in order to prevent duplicates |
| 95 | + // add prefixes to internal links, in order to prevent duplicates |
94 | 96 | $translation = preg_replace( "/<a href=\"#(.*?)\"/i", "<a href=\"#l_\\1\"", |
95 | 97 | $translation ); |
96 | 98 | $translation = preg_replace( "/<li id=\"(.*?)\"/i", "<li id=\"l_\\1\"", |
— | — | @@ -97,14 +99,17 @@ |
98 | 100 | $text = preg_replace( "/<a href=\"#(.*?)\"/i", "<a href=\"#r_\\1\"", $text ); |
99 | 101 | $text = preg_replace( "/<li id=\"(.*?)\"/i", "<li id=\"r_\\1\"", $text ); |
100 | 102 | |
101 | | - # add ?match= to local links of the local wiki |
| 103 | + // add ?match= to local links of the local wiki |
102 | 104 | $text = preg_replace( "/<a href=\"\/([^\"\?]*)\"/i", |
103 | 105 | "<a href=\"/\\1?match={$match_request}\"", $text ); |
104 | 106 | |
105 | | - # do the job |
| 107 | + // do the job |
106 | 108 | $text = $this->matchColumns ( $text, $myLanguage, $myURL, $wgContLanguageCode, |
107 | 109 | $translation, $languageName, $url, $match_request ); |
108 | 110 | } |
| 111 | + |
| 112 | + //Break once we've matched a language link, no point in continuing processing |
| 113 | + break; |
109 | 114 | } |
110 | 115 | } |
111 | 116 | return true; |
— | — | @@ -134,12 +139,12 @@ |
135 | 140 | $tag = $left_tags[1][$i]; |
136 | 141 | $left_chunk .= $left_slices[$i]; |
137 | 142 | |
138 | | - # if we are at the end of the loop, finish quickly |
| 143 | + // if we are at the end of the loop, finish quickly |
139 | 144 | if ( $i == $leftSliceCount - 1 ) { |
140 | 145 | $right_chunk .= $right_text; |
141 | 146 | $found = true; |
142 | 147 | } else { |
143 | | - # look for requested tag in the text |
| 148 | + // look for requested tag in the text |
144 | 149 | $a = strpos ( $right_text, $tag ); |
145 | 150 | if ( $a ) { |
146 | 151 | $found = true; |
— | — | @@ -151,8 +156,6 @@ |
152 | 157 | $right_chunk .= $m[0]; |
153 | 158 | $right_text = substr( $right_text, strlen( $m[0] ) ); |
154 | 159 | } |
155 | | - # } else { |
156 | | - # print "<br/>tag not found ".$tag; |
157 | 160 | } |
158 | 161 | } |
159 | 162 | |
— | — | @@ -161,7 +164,6 @@ |
162 | 165 | $left_bits = $this->find_paragraphs( $left_chunk ); |
163 | 166 | $right_bits = $this->find_paragraphs( $right_chunk ); |
164 | 167 | |
165 | | - // $body .= "<tr style=\"background-color:#ffdddd;\"><td>".count($left_bits)."</td><td>".count($right_bits)."</td></tr>\n"; |
166 | 168 | // Do not align paragraphs if counts are different |
167 | 169 | if ( count( $left_bits ) != count( $right_bits ) ) { |
168 | 170 | $left_bits = array( $left_chunk ); |
— | — | @@ -196,11 +198,11 @@ |
197 | 199 | return $head . $body . "</table>" ; |
198 | 200 | } |
199 | 201 | |
200 | | - /* |
| 202 | + /** |
201 | 203 | * Split text and return a set of html-balanced paragraphs |
202 | 204 | */ |
203 | 205 | function find_paragraphs( $text ) { |
204 | | - $result = Array(); |
| 206 | + $result = array(); |
205 | 207 | $bits = preg_split( $this->tags, $text ); |
206 | 208 | $m = array(); |
207 | 209 | preg_match_all( $this->tags, $text, $m, PREG_SET_ORDER ); |
— | — | @@ -226,7 +228,7 @@ |
227 | 229 | return $result; |
228 | 230 | } |
229 | 231 | |
230 | | - /* |
| 232 | + /** |
231 | 233 | * Split text and return a set of html-balanced slices |
232 | 234 | */ |
233 | 235 | function find_slices( $left_text ) { |
— | — | @@ -237,7 +239,7 @@ |
238 | 240 | preg_match_all( $tag_pattern, $left_text, $left_tags, PREG_PATTERN_ORDER ); |
239 | 241 | $n = count( $left_slices ); |
240 | 242 | |
241 | | - /* |
| 243 | + /** |
242 | 244 | * Make slices that are full paragraphs |
243 | 245 | * If two slices correspond to the same paragraph, the second one will be empty |
244 | 246 | */ |
— | — | @@ -250,7 +252,7 @@ |
251 | 253 | } |
252 | 254 | } |
253 | 255 | |
254 | | - /* |
| 256 | + /** |
255 | 257 | * Keep only slices that contain balanced html |
256 | 258 | * If a slice is unbalanced, we merge it with the next one. |
257 | 259 | * The first and last slices are compensated. |