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