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