Removed usage of extract() in LinkHolderArray::doVariants()
[lhc/web/wiklou.git] / includes / parser / LinkHolderArray.php
1 <?php
2 /**
3 * Holder of replacement pairs for wiki links
4 *
5 * @file
6 */
7
8 /**
9 * @ingroup Parser
10 */
11 class LinkHolderArray {
12 var $internals = array(), $interwikis = array();
13 var $size = 0;
14 var $parent;
15 protected $tempIdOffset;
16
17 function __construct( $parent ) {
18 $this->parent = $parent;
19 }
20
21 /**
22 * Reduce memory usage to reduce the impact of circular references
23 */
24 function __destruct() {
25 foreach ( $this as $name => $value ) {
26 unset( $this->$name );
27 }
28 }
29
30 /**
31 * Don't serialize the parent object, it is big, and not needed when it is
32 * a parameter to mergeForeign(), which is the only application of
33 * serializing at present.
34 *
35 * Compact the titles, only serialize the text form.
36 */
37 function __sleep() {
38 foreach ( $this->internals as $ns => &$nsLinks ) {
39 foreach ( $nsLinks as $key => &$entry ) {
40 unset( $entry['title'] );
41 }
42 }
43 unset( $nsLinks );
44 unset( $entry );
45
46 foreach ( $this->interwikis as $key => &$entry ) {
47 unset( $entry['title'] );
48 }
49 unset( $entry );
50
51 return array( 'internals', 'interwikis', 'size' );
52 }
53
54 /**
55 * Recreate the Title objects
56 */
57 function __wakeup() {
58 foreach ( $this->internals as $ns => &$nsLinks ) {
59 foreach ( $nsLinks as $key => &$entry ) {
60 $entry['title'] = Title::newFromText( $entry['pdbk'] );
61 }
62 }
63 unset( $nsLinks );
64 unset( $entry );
65
66 foreach ( $this->interwikis as $key => &$entry ) {
67 $entry['title'] = Title::newFromText( $entry['pdbk'] );
68 }
69 unset( $entry );
70 }
71
72 /**
73 * Merge another LinkHolderArray into this one
74 * @param $other LinkHolderArray
75 */
76 function merge( $other ) {
77 foreach ( $other->internals as $ns => $entries ) {
78 $this->size += count( $entries );
79 if ( !isset( $this->internals[$ns] ) ) {
80 $this->internals[$ns] = $entries;
81 } else {
82 $this->internals[$ns] += $entries;
83 }
84 }
85 $this->interwikis += $other->interwikis;
86 }
87
88 /**
89 * Merge a LinkHolderArray from another parser instance into this one. The
90 * keys will not be preserved. Any text which went with the old
91 * LinkHolderArray and needs to work with the new one should be passed in
92 * the $texts array. The strings in this array will have their link holders
93 * converted for use in the destination link holder. The resulting array of
94 * strings will be returned.
95 *
96 * @param $other LinkHolderArray
97 * @param $text Array of strings
98 * @return Array
99 */
100 function mergeForeign( $other, $texts ) {
101 $this->tempIdOffset = $idOffset = $this->parent->nextLinkID();
102 $maxId = 0;
103
104 # Renumber internal links
105 foreach ( $other->internals as $ns => $nsLinks ) {
106 foreach ( $nsLinks as $key => $entry ) {
107 $newKey = $idOffset + $key;
108 $this->internals[$ns][$newKey] = $entry;
109 $maxId = $newKey > $maxId ? $newKey : $maxId;
110 }
111 }
112 $texts = preg_replace_callback( '/(<!--LINK \d+:)(\d+)(-->)/',
113 array( $this, 'mergeForeignCallback' ), $texts );
114
115 # Renumber interwiki links
116 foreach ( $other->interwikis as $key => $entry ) {
117 $newKey = $idOffset + $key;
118 $this->interwikis[$newKey] = $entry;
119 $maxId = $newKey > $maxId ? $newKey : $maxId;
120 }
121 $texts = preg_replace_callback( '/(<!--IWLINK )(\d+)(-->)/',
122 array( $this, 'mergeForeignCallback' ), $texts );
123
124 # Set the parent link ID to be beyond the highest used ID
125 $this->parent->setLinkID( $maxId + 1 );
126 $this->tempIdOffset = null;
127 return $texts;
128 }
129
130 protected function mergeForeignCallback( $m ) {
131 return $m[1] . ( $m[2] + $this->tempIdOffset ) . $m[3];
132 }
133
134 /**
135 * Get a subset of the current LinkHolderArray which is sufficient to
136 * interpret the given text.
137 */
138 function getSubArray( $text ) {
139 $sub = new LinkHolderArray( $this->parent );
140
141 # Internal links
142 $pos = 0;
143 while ( $pos < strlen( $text ) ) {
144 if ( !preg_match( '/<!--LINK (\d+):(\d+)-->/',
145 $text, $m, PREG_OFFSET_CAPTURE, $pos ) )
146 {
147 break;
148 }
149 $ns = $m[1][0];
150 $key = $m[2][0];
151 $sub->internals[$ns][$key] = $this->internals[$ns][$key];
152 $pos = $m[0][1] + strlen( $m[0][0] );
153 }
154
155 # Interwiki links
156 $pos = 0;
157 while ( $pos < strlen( $text ) ) {
158 if ( !preg_match( '/<!--IWLINK (\d+)-->/', $text, $m, PREG_OFFSET_CAPTURE, $pos ) ) {
159 break;
160 }
161 $key = $m[1][0];
162 $sub->interwikis[$key] = $this->interwikis[$key];
163 $pos = $m[0][1] + strlen( $m[0][0] );
164 }
165 return $sub;
166 }
167
168 /**
169 * Returns true if the memory requirements of this object are getting large
170 */
171 function isBig() {
172 global $wgLinkHolderBatchSize;
173 return $this->size > $wgLinkHolderBatchSize;
174 }
175
176 /**
177 * Clear all stored link holders.
178 * Make sure you don't have any text left using these link holders, before you call this
179 */
180 function clear() {
181 $this->internals = array();
182 $this->interwikis = array();
183 $this->size = 0;
184 }
185
186 /**
187 * Make a link placeholder. The text returned can be later resolved to a real link with
188 * replaceLinkHolders(). This is done for two reasons: firstly to avoid further
189 * parsing of interwiki links, and secondly to allow all existence checks and
190 * article length checks (for stub links) to be bundled into a single query.
191 *
192 * @param $nt Title
193 */
194 function makeHolder( $nt, $text = '', $query = array(), $trail = '', $prefix = '' ) {
195 wfProfileIn( __METHOD__ );
196 if ( ! is_object($nt) ) {
197 # Fail gracefully
198 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
199 } else {
200 # Separate the link trail from the rest of the link
201 list( $inside, $trail ) = Linker::splitTrail( $trail );
202
203 $entry = array(
204 'title' => $nt,
205 'text' => $prefix.$text.$inside,
206 'pdbk' => $nt->getPrefixedDBkey(),
207 );
208 if ( $query !== array() ) {
209 $entry['query'] = $query;
210 }
211
212 if ( $nt->isExternal() ) {
213 // Use a globally unique ID to keep the objects mergable
214 $key = $this->parent->nextLinkID();
215 $this->interwikis[$key] = $entry;
216 $retVal = "<!--IWLINK $key-->{$trail}";
217 } else {
218 $key = $this->parent->nextLinkID();
219 $ns = $nt->getNamespace();
220 $this->internals[$ns][$key] = $entry;
221 $retVal = "<!--LINK $ns:$key-->{$trail}";
222 }
223 $this->size++;
224 }
225 wfProfileOut( __METHOD__ );
226 return $retVal;
227 }
228
229 /**
230 * FIXME: update documentation. makeLinkObj() is deprecated.
231 * Replace <!--LINK--> link placeholders with actual links, in the buffer
232 * Placeholders created in Skin::makeLinkObj()
233 * Returns an array of link CSS classes, indexed by PDBK.
234 */
235 function replace( &$text ) {
236 wfProfileIn( __METHOD__ );
237
238 $colours = $this->replaceInternal( $text );
239 $this->replaceInterwiki( $text );
240
241 wfProfileOut( __METHOD__ );
242 return $colours;
243 }
244
245 /**
246 * Replace internal links
247 */
248 protected function replaceInternal( &$text ) {
249 if ( !$this->internals ) {
250 return;
251 }
252
253 wfProfileIn( __METHOD__ );
254 global $wgContLang;
255
256 $colours = array();
257 $sk = $this->parent->getOptions()->getSkin( $this->parent->mTitle );
258 $linkCache = LinkCache::singleton();
259 $output = $this->parent->getOutput();
260
261 wfProfileIn( __METHOD__.'-check' );
262 $dbr = wfGetDB( DB_SLAVE );
263 $page = $dbr->tableName( 'page' );
264 $threshold = $this->parent->getOptions()->getStubThreshold();
265
266 # Sort by namespace
267 ksort( $this->internals );
268
269 $linkcolour_ids = array();
270
271 # Generate query
272 $query = false;
273 $current = null;
274 foreach ( $this->internals as $ns => $entries ) {
275 foreach ( $entries as $entry ) {
276 $title = $entry['title'];
277 $pdbk = $entry['pdbk'];
278
279 # Skip invalid entries.
280 # Result will be ugly, but prevents crash.
281 if ( is_null( $title ) ) {
282 continue;
283 }
284
285 # Check if it's a static known link, e.g. interwiki
286 if ( $title->isAlwaysKnown() ) {
287 $colours[$pdbk] = '';
288 } elseif ( $ns == NS_SPECIAL ) {
289 $colours[$pdbk] = 'new';
290 } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
291 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
292 $output->addLink( $title, $id );
293 $linkcolour_ids[$id] = $pdbk;
294 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
295 $colours[$pdbk] = 'new';
296 } else {
297 # Not in the link cache, add it to the query
298 if ( !isset( $current ) ) {
299 $current = $ns;
300 $query = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len, page_latest";
301 $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN(";
302 } elseif ( $current != $ns ) {
303 $current = $ns;
304 $query .= ")) OR (page_namespace=$ns AND page_title IN(";
305 } else {
306 $query .= ', ';
307 }
308
309 $query .= $dbr->addQuotes( $title->getDBkey() );
310 }
311 }
312 }
313 if ( $query ) {
314 $query .= '))';
315
316 $res = $dbr->query( $query, __METHOD__ );
317
318 # Fetch data and form into an associative array
319 # non-existent = broken
320 foreach ( $res as $s ) {
321 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
322 $pdbk = $title->getPrefixedDBkey();
323 $linkCache->addGoodLinkObj( $s->page_id, $title, $s->page_len, $s->page_is_redirect, $s->page_latest );
324 $output->addLink( $title, $s->page_id );
325 # FIXME: convoluted data flow
326 # The redirect status and length is passed to getLinkColour via the LinkCache
327 # Use formal parameters instead
328 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
329 //add id to the extension todolist
330 $linkcolour_ids[$s->page_id] = $pdbk;
331 }
332 unset( $res );
333 }
334 if ( count($linkcolour_ids) ) {
335 //pass an array of page_ids to an extension
336 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
337 }
338 wfProfileOut( __METHOD__.'-check' );
339
340 # Do a second query for different language variants of links and categories
341 if($wgContLang->hasVariants()) {
342 $this->doVariants( $colours );
343 }
344
345 # Construct search and replace arrays
346 wfProfileIn( __METHOD__.'-construct' );
347 $replacePairs = array();
348 foreach ( $this->internals as $ns => $entries ) {
349 foreach ( $entries as $index => $entry ) {
350 $pdbk = $entry['pdbk'];
351 $title = $entry['title'];
352 $query = isset( $entry['query'] ) ? $entry['query'] : array();
353 $key = "$ns:$index";
354 $searchkey = "<!--LINK $key-->";
355 $displayText = $entry['text'];
356 if ( $displayText === '' ) {
357 $displayText = null;
358 }
359 if ( !isset( $colours[$pdbk] ) ) {
360 $colours[$pdbk] = 'new';
361 }
362 $attribs = array();
363 if ( $colours[$pdbk] == 'new' ) {
364 $linkCache->addBadLinkObj( $title );
365 $output->addLink( $title, 0 );
366 $type = array( 'broken' );
367 } else {
368 if ( $colours[$pdbk] != '' ) {
369 $attribs['class'] = $colours[$pdbk];
370 }
371 $type = array( 'known', 'noclasses' );
372 }
373 $replacePairs[$searchkey] = $sk->link( $title, $displayText,
374 $attribs, $query, $type );
375 }
376 }
377 $replacer = new HashtableReplacer( $replacePairs, 1 );
378 wfProfileOut( __METHOD__.'-construct' );
379
380 # Do the thing
381 wfProfileIn( __METHOD__.'-replace' );
382 $text = preg_replace_callback(
383 '/(<!--LINK .*?-->)/',
384 $replacer->cb(),
385 $text);
386
387 wfProfileOut( __METHOD__.'-replace' );
388 wfProfileOut( __METHOD__ );
389 }
390
391 /**
392 * Replace interwiki links
393 */
394 protected function replaceInterwiki( &$text ) {
395 if ( empty( $this->interwikis ) ) {
396 return;
397 }
398
399 wfProfileIn( __METHOD__ );
400 # Make interwiki link HTML
401 $sk = $this->parent->getOptions()->getSkin( $this->parent->mTitle );
402 $output = $this->parent->getOutput();
403 $replacePairs = array();
404 foreach( $this->interwikis as $key => $link ) {
405 $replacePairs[$key] = $sk->link( $link['title'], $link['text'] );
406 $output->addInterwikiLink( $link['title'] );
407 }
408 $replacer = new HashtableReplacer( $replacePairs, 1 );
409
410 $text = preg_replace_callback(
411 '/<!--IWLINK (.*?)-->/',
412 $replacer->cb(),
413 $text );
414 wfProfileOut( __METHOD__ );
415 }
416
417 /**
418 * Modify $this->internals and $colours according to language variant linking rules
419 */
420 protected function doVariants( &$colours ) {
421 global $wgContLang;
422 $linkBatch = new LinkBatch();
423 $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
424 $output = $this->parent->getOutput();
425 $linkCache = LinkCache::singleton();
426 $sk = $this->parent->getOptions()->getSkin( $this->parent->mTitle );
427 $threshold = $this->parent->getOptions()->getStubThreshold();
428 $titlesToBeConverted = '';
429 $titlesAttrs = array();
430
431 // Concatenate titles to a single string, thus we only need auto convert the
432 // single string to all variants. This would improve parser's performance
433 // significantly.
434 foreach ( $this->internals as $ns => $entries ) {
435 foreach ( $entries as $index => $entry ) {
436 $pdbk = $entry['pdbk'];
437 // we only deal with new links (in its first query)
438 if ( !isset( $colours[$pdbk] ) ) {
439 $title = $entry['title'];
440 $titleText = $title->getText();
441 $titlesAttrs[] = array(
442 'ns' => $ns,
443 'key' => "$ns:$index",
444 'titleText' => $titleText,
445 );
446 // separate titles with \0 because it would never appears
447 // in a valid title
448 $titlesToBeConverted .= $titleText . "\0";
449 }
450 }
451 }
452
453 // Now do the conversion and explode string to text of titles
454 $titlesAllVariants = $wgContLang->autoConvertToAllVariants( $titlesToBeConverted );
455 $allVariantsName = array_keys( $titlesAllVariants );
456 foreach ( $titlesAllVariants as &$titlesVariant ) {
457 $titlesVariant = explode( "\0", $titlesVariant );
458 }
459 $l = count( $titlesAttrs );
460 // Then add variants of links to link batch
461 for ( $i = 0; $i < $l; $i ++ ) {
462 foreach ( $allVariantsName as $variantName ) {
463 $textVariant = $titlesAllVariants[$variantName][$i];
464 if ( $textVariant != $titlesAttrs[$i]['titleText'] ) {
465 $variantTitle = Title::makeTitle( $titlesAttrs[$i]['ns'], $textVariant );
466 if( is_null( $variantTitle ) ) {
467 continue;
468 }
469 $linkBatch->addObj( $variantTitle );
470 $variantMap[$variantTitle->getPrefixedDBkey()][] = $titlesAttrs[$i]['key'];
471 }
472 }
473 }
474
475 // process categories, check if a category exists in some variant
476 $categoryMap = array(); // maps $category_variant => $category (dbkeys)
477 $varCategories = array(); // category replacements oldDBkey => newDBkey
478 foreach( $output->getCategoryLinks() as $category ){
479 $variants = $wgContLang->autoConvertToAllVariants( $category );
480 foreach($variants as $variant){
481 if($variant != $category){
482 $variantTitle = Title::newFromDBkey( Title::makeName(NS_CATEGORY,$variant) );
483 if(is_null($variantTitle)) continue;
484 $linkBatch->addObj( $variantTitle );
485 $categoryMap[$variant] = $category;
486 }
487 }
488 }
489
490
491 if(!$linkBatch->isEmpty()){
492 // construct query
493 $dbr = wfGetDB( DB_SLAVE );
494 $varRes = $dbr->select( 'page',
495 array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect', 'page_len' ),
496 $linkBatch->constructSet( 'page', $dbr ),
497 __METHOD__
498 );
499
500 $linkcolour_ids = array();
501
502 // for each found variants, figure out link holders and replace
503 foreach ( $varRes as $s ) {
504
505 $variantTitle = Title::makeTitle( $s->page_namespace, $s->page_title );
506 $varPdbk = $variantTitle->getPrefixedDBkey();
507 $vardbk = $variantTitle->getDBkey();
508
509 $holderKeys = array();
510 if( isset( $variantMap[$varPdbk] ) ) {
511 $holderKeys = $variantMap[$varPdbk];
512 $linkCache->addGoodLinkObj( $s->page_id, $variantTitle, $s->page_len, $s->page_is_redirect );
513 $output->addLink( $variantTitle, $s->page_id );
514 }
515
516 // loop over link holders
517 foreach( $holderKeys as $key ) {
518 list( $ns, $index ) = explode( ':', $key, 2 );
519 $entry =& $this->internals[$ns][$index];
520 $pdbk = $entry['pdbk'];
521
522 if(!isset($colours[$pdbk])){
523 // found link in some of the variants, replace the link holder data
524 $entry['title'] = $variantTitle;
525 $entry['pdbk'] = $varPdbk;
526
527 // set pdbk and colour
528 # FIXME: convoluted data flow
529 # The redirect status and length is passed to getLinkColour via the LinkCache
530 # Use formal parameters instead
531 $colours[$varPdbk] = $sk->getLinkColour( $variantTitle, $threshold );
532 $linkcolour_ids[$s->page_id] = $pdbk;
533 }
534 }
535
536 // check if the object is a variant of a category
537 if(isset($categoryMap[$vardbk])){
538 $oldkey = $categoryMap[$vardbk];
539 if($oldkey != $vardbk)
540 $varCategories[$oldkey]=$vardbk;
541 }
542 }
543 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
544
545 // rebuild the categories in original order (if there are replacements)
546 if(count($varCategories)>0){
547 $newCats = array();
548 $originalCats = $output->getCategories();
549 foreach($originalCats as $cat => $sortkey){
550 // make the replacement
551 if( array_key_exists($cat,$varCategories) )
552 $newCats[$varCategories[$cat]] = $sortkey;
553 else $newCats[$cat] = $sortkey;
554 }
555 $output->setCategoryLinks($newCats);
556 }
557 }
558 }
559
560 /**
561 * Replace <!--LINK--> link placeholders with plain text of links
562 * (not HTML-formatted).
563 *
564 * @param $text String
565 * @return String
566 */
567 function replaceText( $text ) {
568 wfProfileIn( __METHOD__ );
569
570 $text = preg_replace_callback(
571 '/<!--(LINK|IWLINK) (.*?)-->/',
572 array( &$this, 'replaceTextCallback' ),
573 $text );
574
575 wfProfileOut( __METHOD__ );
576 return $text;
577 }
578
579 /**
580 * Callback for replaceText()
581 *
582 * @param $matches Array
583 * @return string
584 * @private
585 */
586 function replaceTextCallback( $matches ) {
587 $type = $matches[1];
588 $key = $matches[2];
589 if( $type == 'LINK' ) {
590 list( $ns, $index ) = explode( ':', $key, 2 );
591 if( isset( $this->internals[$ns][$index]['text'] ) ) {
592 return $this->internals[$ns][$index]['text'];
593 }
594 } elseif( $type == 'IWLINK' ) {
595 if( isset( $this->interwikis[$key]['text'] ) ) {
596 return $this->interwikis[$key]['text'];
597 }
598 }
599 return $matches[0];
600 }
601 }