Don't display multiple language links to the same language
[mediawiki.git] / includes / parser / LinkHolderArray.php
blobd9356b48bea674993149b19ddbf0ffe281a83057
1 <?php
2 /**
3 * Holder of replacement pairs for wiki links
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.
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.
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
20 * @file
21 * @ingroup Parser
24 /**
25 * @ingroup Parser
27 class LinkHolderArray {
28 var $internals = array(), $interwikis = array();
29 var $size = 0;
30 var $parent;
31 protected $tempIdOffset;
33 function __construct( $parent ) {
34 $this->parent = $parent;
37 /**
38 * Reduce memory usage to reduce the impact of circular references
40 function __destruct() {
41 foreach ( $this as $name => $value ) {
42 unset( $this->$name );
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.
51 * Compact the titles, only serialize the text form.
52 * @return array
54 function __sleep() {
55 foreach ( $this->internals as &$nsLinks ) {
56 foreach ( $nsLinks as &$entry ) {
57 unset( $entry['title'] );
60 unset( $nsLinks );
61 unset( $entry );
63 foreach ( $this->interwikis as &$entry ) {
64 unset( $entry['title'] );
66 unset( $entry );
68 return array( 'internals', 'interwikis', 'size' );
71 /**
72 * Recreate the Title objects
74 function __wakeup() {
75 foreach ( $this->internals as &$nsLinks ) {
76 foreach ( $nsLinks as &$entry ) {
77 $entry['title'] = Title::newFromText( $entry['pdbk'] );
80 unset( $nsLinks );
81 unset( $entry );
83 foreach ( $this->interwikis as &$entry ) {
84 $entry['title'] = Title::newFromText( $entry['pdbk'] );
86 unset( $entry );
89 /**
90 * Merge another LinkHolderArray into this one
91 * @param $other LinkHolderArray
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;
102 $this->interwikis += $other->interwikis;
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.
113 * @param $other LinkHolderArray
114 * @param $texts Array of strings
115 * @return Array
117 function mergeForeign( $other, $texts ) {
118 $this->tempIdOffset = $idOffset = $this->parent->nextLinkID();
119 $maxId = 0;
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;
129 $texts = preg_replace_callback( '/(<!--LINK \d+:)(\d+)(-->)/',
130 array( $this, 'mergeForeignCallback' ), $texts );
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;
138 $texts = preg_replace_callback( '/(<!--IWLINK )(\d+)(-->)/',
139 array( $this, 'mergeForeignCallback' ), $texts );
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;
147 protected function mergeForeignCallback( $m ) {
148 return $m[1] . ( $m[2] + $this->tempIdOffset ) . $m[3];
152 * Get a subset of the current LinkHolderArray which is sufficient to
153 * interpret the given text.
154 * @return LinkHolderArray
156 function getSubArray( $text ) {
157 $sub = new LinkHolderArray( $this->parent );
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 ) )
165 break;
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] );
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;
179 $key = $m[1][0];
180 $sub->interwikis[$key] = $this->interwikis[$key];
181 $pos = $m[0][1] + strlen( $m[0][0] );
183 return $sub;
187 * Returns true if the memory requirements of this object are getting large
188 * @return bool
190 function isBig() {
191 global $wgLinkHolderBatchSize;
192 return $this->size > $wgLinkHolderBatchSize;
196 * Clear all stored link holders.
197 * Make sure you don't have any text left using these link holders, before you call this
199 function clear() {
200 $this->internals = array();
201 $this->interwikis = array();
202 $this->size = 0;
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.
211 * @param $nt Title
212 * @param $text String
213 * @param $query Array [optional]
214 * @param $trail String [optional]
215 * @param $prefix String [optional]
216 * @return string
218 function makeHolder( $nt, $text = '', $query = array(), $trail = '', $prefix = '' ) {
219 wfProfileIn( __METHOD__ );
220 if ( ! is_object($nt) ) {
221 # Fail gracefully
222 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
223 } else {
224 # Separate the link trail from the rest of the link
225 list( $inside, $trail ) = Linker::splitTrail( $trail );
227 $entry = array(
228 'title' => $nt,
229 'text' => $prefix.$text.$inside,
230 'pdbk' => $nt->getPrefixedDBkey(),
232 if ( $query !== array() ) {
233 $entry['query'] = $query;
236 if ( $nt->isExternal() ) {
237 // Use a globally unique ID to keep the objects mergable
238 $key = $this->parent->nextLinkID();
239 $this->interwikis[$key] = $entry;
240 $retVal = "<!--IWLINK $key-->{$trail}";
241 } else {
242 $key = $this->parent->nextLinkID();
243 $ns = $nt->getNamespace();
244 $this->internals[$ns][$key] = $entry;
245 $retVal = "<!--LINK $ns:$key-->{$trail}";
247 $this->size++;
249 wfProfileOut( __METHOD__ );
250 return $retVal;
254 * @todo FIXME: Update documentation. makeLinkObj() is deprecated.
255 * Replace <!--LINK--> link placeholders with actual links, in the buffer
256 * Placeholders created in Skin::makeLinkObj()
257 * Returns an array of link CSS classes, indexed by PDBK.
259 function replace( &$text ) {
260 wfProfileIn( __METHOD__ );
262 $colours = $this->replaceInternal( $text );
263 $this->replaceInterwiki( $text );
265 wfProfileOut( __METHOD__ );
266 return $colours;
270 * Replace internal links
272 protected function replaceInternal( &$text ) {
273 if ( !$this->internals ) {
274 return;
277 wfProfileIn( __METHOD__ );
278 global $wgContLang;
280 $colours = array();
281 $linkCache = LinkCache::singleton();
282 $output = $this->parent->getOutput();
284 wfProfileIn( __METHOD__.'-check' );
285 $dbr = wfGetDB( DB_SLAVE );
286 $threshold = $this->parent->getOptions()->getStubThreshold();
288 # Sort by namespace
289 ksort( $this->internals );
291 $linkcolour_ids = array();
293 # Generate query
294 $queries = array();
295 foreach ( $this->internals as $ns => $entries ) {
296 foreach ( $entries as $entry ) {
297 $title = $entry['title'];
298 $pdbk = $entry['pdbk'];
300 # Skip invalid entries.
301 # Result will be ugly, but prevents crash.
302 if ( is_null( $title ) ) {
303 continue;
306 # Check if it's a static known link, e.g. interwiki
307 if ( $title->isAlwaysKnown() ) {
308 $colours[$pdbk] = '';
309 } elseif ( $ns == NS_SPECIAL ) {
310 $colours[$pdbk] = 'new';
311 } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
312 $colours[$pdbk] = Linker::getLinkColour( $title, $threshold );
313 $output->addLink( $title, $id );
314 $linkcolour_ids[$id] = $pdbk;
315 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
316 $colours[$pdbk] = 'new';
317 } else {
318 # Not in the link cache, add it to the query
319 $queries[$ns][] = $title->getDBkey();
323 if ( $queries ) {
324 $where = array();
325 foreach( $queries as $ns => $pages ){
326 $where[] = $dbr->makeList(
327 array(
328 'page_namespace' => $ns,
329 'page_title' => $pages,
331 LIST_AND
335 $res = $dbr->select(
336 'page',
337 array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect', 'page_len', 'page_latest' ),
338 $dbr->makeList( $where, LIST_OR ),
339 __METHOD__
342 # Fetch data and form into an associative array
343 # non-existent = broken
344 foreach ( $res as $s ) {
345 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
346 $pdbk = $title->getPrefixedDBkey();
347 $linkCache->addGoodLinkObjFromRow( $title, $s );
348 $output->addLink( $title, $s->page_id );
349 # @todo FIXME: Convoluted data flow
350 # The redirect status and length is passed to getLinkColour via the LinkCache
351 # Use formal parameters instead
352 $colours[$pdbk] = Linker::getLinkColour( $title, $threshold );
353 //add id to the extension todolist
354 $linkcolour_ids[$s->page_id] = $pdbk;
356 unset( $res );
358 if ( count($linkcolour_ids) ) {
359 //pass an array of page_ids to an extension
360 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
362 wfProfileOut( __METHOD__.'-check' );
364 # Do a second query for different language variants of links and categories
365 if($wgContLang->hasVariants()) {
366 $this->doVariants( $colours );
369 # Construct search and replace arrays
370 wfProfileIn( __METHOD__.'-construct' );
371 $replacePairs = array();
372 foreach ( $this->internals as $ns => $entries ) {
373 foreach ( $entries as $index => $entry ) {
374 $pdbk = $entry['pdbk'];
375 $title = $entry['title'];
376 $query = isset( $entry['query'] ) ? $entry['query'] : array();
377 $key = "$ns:$index";
378 $searchkey = "<!--LINK $key-->";
379 $displayText = $entry['text'];
380 if ( $displayText === '' ) {
381 $displayText = null;
383 if ( !isset( $colours[$pdbk] ) ) {
384 $colours[$pdbk] = 'new';
386 $attribs = array();
387 if ( $colours[$pdbk] == 'new' ) {
388 $linkCache->addBadLinkObj( $title );
389 $output->addLink( $title, 0 );
390 $type = array( 'broken' );
391 } else {
392 if ( $colours[$pdbk] != '' ) {
393 $attribs['class'] = $colours[$pdbk];
395 $type = array( 'known', 'noclasses' );
397 $replacePairs[$searchkey] = Linker::link( $title, $displayText,
398 $attribs, $query, $type );
401 $replacer = new HashtableReplacer( $replacePairs, 1 );
402 wfProfileOut( __METHOD__.'-construct' );
404 # Do the thing
405 wfProfileIn( __METHOD__.'-replace' );
406 $text = preg_replace_callback(
407 '/(<!--LINK .*?-->)/',
408 $replacer->cb(),
409 $text);
411 wfProfileOut( __METHOD__.'-replace' );
412 wfProfileOut( __METHOD__ );
416 * Replace interwiki links
418 protected function replaceInterwiki( &$text ) {
419 if ( empty( $this->interwikis ) ) {
420 return;
423 wfProfileIn( __METHOD__ );
424 # Make interwiki link HTML
425 $output = $this->parent->getOutput();
426 $replacePairs = array();
427 foreach( $this->interwikis as $key => $link ) {
428 $replacePairs[$key] = Linker::link( $link['title'], $link['text'] );
429 $output->addInterwikiLink( $link['title'] );
431 $replacer = new HashtableReplacer( $replacePairs, 1 );
433 $text = preg_replace_callback(
434 '/<!--IWLINK (.*?)-->/',
435 $replacer->cb(),
436 $text );
437 wfProfileOut( __METHOD__ );
441 * Modify $this->internals and $colours according to language variant linking rules
443 protected function doVariants( &$colours ) {
444 global $wgContLang;
445 $linkBatch = new LinkBatch();
446 $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
447 $output = $this->parent->getOutput();
448 $linkCache = LinkCache::singleton();
449 $threshold = $this->parent->getOptions()->getStubThreshold();
450 $titlesToBeConverted = '';
451 $titlesAttrs = array();
453 // Concatenate titles to a single string, thus we only need auto convert the
454 // single string to all variants. This would improve parser's performance
455 // significantly.
456 foreach ( $this->internals as $ns => $entries ) {
457 foreach ( $entries as $index => $entry ) {
458 $pdbk = $entry['pdbk'];
459 // we only deal with new links (in its first query)
460 if ( !isset( $colours[$pdbk] ) || $colours[$pdbk] === 'new' ) {
461 $title = $entry['title'];
462 $titleText = $title->getText();
463 $titlesAttrs[] = array(
464 'ns' => $ns,
465 'key' => "$ns:$index",
466 'titleText' => $titleText,
468 // separate titles with \0 because it would never appears
469 // in a valid title
470 $titlesToBeConverted .= $titleText . "\0";
475 // Now do the conversion and explode string to text of titles
476 $titlesAllVariants = $wgContLang->autoConvertToAllVariants( rtrim( $titlesToBeConverted, "\0" ) );
477 $allVariantsName = array_keys( $titlesAllVariants );
478 foreach ( $titlesAllVariants as &$titlesVariant ) {
479 $titlesVariant = explode( "\0", $titlesVariant );
481 $l = count( $titlesAttrs );
482 // Then add variants of links to link batch
483 for ( $i = 0; $i < $l; $i ++ ) {
484 foreach ( $allVariantsName as $variantName ) {
485 $textVariant = $titlesAllVariants[$variantName][$i];
486 if ( $textVariant != $titlesAttrs[$i]['titleText'] ) {
487 $variantTitle = Title::makeTitle( $titlesAttrs[$i]['ns'], $textVariant );
488 if( is_null( $variantTitle ) ) {
489 continue;
491 $linkBatch->addObj( $variantTitle );
492 $variantMap[$variantTitle->getPrefixedDBkey()][] = $titlesAttrs[$i]['key'];
497 // process categories, check if a category exists in some variant
498 $categoryMap = array(); // maps $category_variant => $category (dbkeys)
499 $varCategories = array(); // category replacements oldDBkey => newDBkey
500 foreach( $output->getCategoryLinks() as $category ){
501 $variants = $wgContLang->autoConvertToAllVariants( $category );
502 foreach($variants as $variant){
503 if($variant != $category){
504 $variantTitle = Title::newFromDBkey( Title::makeName(NS_CATEGORY,$variant) );
505 if(is_null($variantTitle)) continue;
506 $linkBatch->addObj( $variantTitle );
507 $categoryMap[$variant] = $category;
513 if(!$linkBatch->isEmpty()){
514 // construct query
515 $dbr = wfGetDB( DB_SLAVE );
516 $varRes = $dbr->select( 'page',
517 array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect', 'page_len', 'page_latest' ),
518 $linkBatch->constructSet( 'page', $dbr ),
519 __METHOD__
522 $linkcolour_ids = array();
524 // for each found variants, figure out link holders and replace
525 foreach ( $varRes as $s ) {
527 $variantTitle = Title::makeTitle( $s->page_namespace, $s->page_title );
528 $varPdbk = $variantTitle->getPrefixedDBkey();
529 $vardbk = $variantTitle->getDBkey();
531 $holderKeys = array();
532 if( isset( $variantMap[$varPdbk] ) ) {
533 $holderKeys = $variantMap[$varPdbk];
534 $linkCache->addGoodLinkObjFromRow( $variantTitle, $s );
535 $output->addLink( $variantTitle, $s->page_id );
538 // loop over link holders
539 foreach( $holderKeys as $key ) {
540 list( $ns, $index ) = explode( ':', $key, 2 );
541 $entry =& $this->internals[$ns][$index];
542 $pdbk = $entry['pdbk'];
544 if ( !isset( $colours[$pdbk] ) || $colours[$pdbk] === 'new' ) {
545 // found link in some of the variants, replace the link holder data
546 $entry['title'] = $variantTitle;
547 $entry['pdbk'] = $varPdbk;
549 // set pdbk and colour
550 # @todo FIXME: Convoluted data flow
551 # The redirect status and length is passed to getLinkColour via the LinkCache
552 # Use formal parameters instead
553 $colours[$varPdbk] = Linker::getLinkColour( $variantTitle, $threshold );
554 $linkcolour_ids[$s->page_id] = $pdbk;
558 // check if the object is a variant of a category
559 if(isset($categoryMap[$vardbk])){
560 $oldkey = $categoryMap[$vardbk];
561 if($oldkey != $vardbk)
562 $varCategories[$oldkey]=$vardbk;
565 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
567 // rebuild the categories in original order (if there are replacements)
568 if(count($varCategories)>0){
569 $newCats = array();
570 $originalCats = $output->getCategories();
571 foreach($originalCats as $cat => $sortkey){
572 // make the replacement
573 if( array_key_exists($cat,$varCategories) )
574 $newCats[$varCategories[$cat]] = $sortkey;
575 else $newCats[$cat] = $sortkey;
577 $output->setCategoryLinks($newCats);
583 * Replace <!--LINK--> link placeholders with plain text of links
584 * (not HTML-formatted).
586 * @param $text String
587 * @return String
589 function replaceText( $text ) {
590 wfProfileIn( __METHOD__ );
592 $text = preg_replace_callback(
593 '/<!--(LINK|IWLINK) (.*?)-->/',
594 array( &$this, 'replaceTextCallback' ),
595 $text );
597 wfProfileOut( __METHOD__ );
598 return $text;
602 * Callback for replaceText()
604 * @param $matches Array
605 * @return string
606 * @private
608 function replaceTextCallback( $matches ) {
609 $type = $matches[1];
610 $key = $matches[2];
611 if( $type == 'LINK' ) {
612 list( $ns, $index ) = explode( ':', $key, 2 );
613 if( isset( $this->internals[$ns][$index]['text'] ) ) {
614 return $this->internals[$ns][$index]['text'];
616 } elseif( $type == 'IWLINK' ) {
617 if( isset( $this->interwikis[$key]['text'] ) ) {
618 return $this->interwikis[$key]['text'];
621 return $matches[0];