Merge "Localisation updates from https://translatewiki.net."
[mediawiki.git] / includes / parser / LinkHolderArray.php
blob7794fae4e7e08cd51528e25c4cec65d751387aac
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 public $internals = array();
29 public $interwikis = array();
30 public $size = 0;
32 /**
33 * @var Parser
35 public $parent;
36 protected $tempIdOffset;
38 /**
39 * @param Parser $parent
41 public function __construct( $parent ) {
42 $this->parent = $parent;
45 /**
46 * Reduce memory usage to reduce the impact of circular references
48 public function __destruct() {
49 foreach ( $this as $name => $value ) {
50 unset( $this->$name );
54 /**
55 * Don't serialize the parent object, it is big, and not needed when it is
56 * a parameter to mergeForeign(), which is the only application of
57 * serializing at present.
59 * Compact the titles, only serialize the text form.
60 * @return array
62 public function __sleep() {
63 foreach ( $this->internals as &$nsLinks ) {
64 foreach ( $nsLinks as &$entry ) {
65 unset( $entry['title'] );
68 unset( $nsLinks );
69 unset( $entry );
71 foreach ( $this->interwikis as &$entry ) {
72 unset( $entry['title'] );
74 unset( $entry );
76 return array( 'internals', 'interwikis', 'size' );
79 /**
80 * Recreate the Title objects
82 public function __wakeup() {
83 foreach ( $this->internals as &$nsLinks ) {
84 foreach ( $nsLinks as &$entry ) {
85 $entry['title'] = Title::newFromText( $entry['pdbk'] );
88 unset( $nsLinks );
89 unset( $entry );
91 foreach ( $this->interwikis as &$entry ) {
92 $entry['title'] = Title::newFromText( $entry['pdbk'] );
94 unset( $entry );
97 /**
98 * Merge another LinkHolderArray into this one
99 * @param LinkHolderArray $other
101 public function merge( $other ) {
102 foreach ( $other->internals as $ns => $entries ) {
103 $this->size += count( $entries );
104 if ( !isset( $this->internals[$ns] ) ) {
105 $this->internals[$ns] = $entries;
106 } else {
107 $this->internals[$ns] += $entries;
110 $this->interwikis += $other->interwikis;
114 * Merge a LinkHolderArray from another parser instance into this one. The
115 * keys will not be preserved. Any text which went with the old
116 * LinkHolderArray and needs to work with the new one should be passed in
117 * the $texts array. The strings in this array will have their link holders
118 * converted for use in the destination link holder. The resulting array of
119 * strings will be returned.
121 * @param LinkHolderArray $other
122 * @param array $texts Array of strings
123 * @return array
125 public function mergeForeign( $other, $texts ) {
126 $this->tempIdOffset = $idOffset = $this->parent->nextLinkID();
127 $maxId = 0;
129 # Renumber internal links
130 foreach ( $other->internals as $ns => $nsLinks ) {
131 foreach ( $nsLinks as $key => $entry ) {
132 $newKey = $idOffset + $key;
133 $this->internals[$ns][$newKey] = $entry;
134 $maxId = $newKey > $maxId ? $newKey : $maxId;
137 $texts = preg_replace_callback( '/(<!--LINK \d+:)(\d+)(-->)/',
138 array( $this, 'mergeForeignCallback' ), $texts );
140 # Renumber interwiki links
141 foreach ( $other->interwikis as $key => $entry ) {
142 $newKey = $idOffset + $key;
143 $this->interwikis[$newKey] = $entry;
144 $maxId = $newKey > $maxId ? $newKey : $maxId;
146 $texts = preg_replace_callback( '/(<!--IWLINK )(\d+)(-->)/',
147 array( $this, 'mergeForeignCallback' ), $texts );
149 # Set the parent link ID to be beyond the highest used ID
150 $this->parent->setLinkID( $maxId + 1 );
151 $this->tempIdOffset = null;
152 return $texts;
156 * @param array $m
157 * @return string
159 protected function mergeForeignCallback( $m ) {
160 return $m[1] . ( $m[2] + $this->tempIdOffset ) . $m[3];
164 * Get a subset of the current LinkHolderArray which is sufficient to
165 * interpret the given text.
166 * @param string $text
167 * @return LinkHolderArray
169 public function getSubArray( $text ) {
170 $sub = new LinkHolderArray( $this->parent );
172 # Internal links
173 $pos = 0;
174 while ( $pos < strlen( $text ) ) {
175 if ( !preg_match( '/<!--LINK (\d+):(\d+)-->/',
176 $text, $m, PREG_OFFSET_CAPTURE, $pos )
178 break;
180 $ns = $m[1][0];
181 $key = $m[2][0];
182 $sub->internals[$ns][$key] = $this->internals[$ns][$key];
183 $pos = $m[0][1] + strlen( $m[0][0] );
186 # Interwiki links
187 $pos = 0;
188 while ( $pos < strlen( $text ) ) {
189 if ( !preg_match( '/<!--IWLINK (\d+)-->/', $text, $m, PREG_OFFSET_CAPTURE, $pos ) ) {
190 break;
192 $key = $m[1][0];
193 $sub->interwikis[$key] = $this->interwikis[$key];
194 $pos = $m[0][1] + strlen( $m[0][0] );
196 return $sub;
200 * Returns true if the memory requirements of this object are getting large
201 * @return bool
203 public function isBig() {
204 global $wgLinkHolderBatchSize;
205 return $this->size > $wgLinkHolderBatchSize;
209 * Clear all stored link holders.
210 * Make sure you don't have any text left using these link holders, before you call this
212 public function clear() {
213 $this->internals = array();
214 $this->interwikis = array();
215 $this->size = 0;
219 * Make a link placeholder. The text returned can be later resolved to a real link with
220 * replaceLinkHolders(). This is done for two reasons: firstly to avoid further
221 * parsing of interwiki links, and secondly to allow all existence checks and
222 * article length checks (for stub links) to be bundled into a single query.
224 * @param Title $nt
225 * @param string $text
226 * @param array $query [optional]
227 * @param string $trail [optional]
228 * @param string $prefix [optional]
229 * @return string
231 public function makeHolder( $nt, $text = '', $query = array(), $trail = '', $prefix = '' ) {
232 wfProfileIn( __METHOD__ );
233 if ( !is_object( $nt ) ) {
234 # Fail gracefully
235 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
236 } else {
237 # Separate the link trail from the rest of the link
238 list( $inside, $trail ) = Linker::splitTrail( $trail );
240 $entry = array(
241 'title' => $nt,
242 'text' => $prefix . $text . $inside,
243 'pdbk' => $nt->getPrefixedDBkey(),
245 if ( $query !== array() ) {
246 $entry['query'] = $query;
249 if ( $nt->isExternal() ) {
250 // Use a globally unique ID to keep the objects mergable
251 $key = $this->parent->nextLinkID();
252 $this->interwikis[$key] = $entry;
253 $retVal = "<!--IWLINK $key-->{$trail}";
254 } else {
255 $key = $this->parent->nextLinkID();
256 $ns = $nt->getNamespace();
257 $this->internals[$ns][$key] = $entry;
258 $retVal = "<!--LINK $ns:$key-->{$trail}";
260 $this->size++;
262 wfProfileOut( __METHOD__ );
263 return $retVal;
267 * Replace <!--LINK--> link placeholders with actual links, in the buffer
269 * @param string $text
270 * @return array Array of link CSS classes, indexed by PDBK.
272 public function replace( &$text ) {
273 wfProfileIn( __METHOD__ );
275 /** @todo FIXME: replaceInternal doesn't return a value */
276 $colours = $this->replaceInternal( $text );
277 $this->replaceInterwiki( $text );
279 wfProfileOut( __METHOD__ );
280 return $colours;
284 * Replace internal links
285 * @param string $text
287 protected function replaceInternal( &$text ) {
288 if ( !$this->internals ) {
289 return;
292 wfProfileIn( __METHOD__ );
293 global $wgContLang, $wgContentHandlerUseDB;
295 $colours = array();
296 $linkCache = LinkCache::singleton();
297 $output = $this->parent->getOutput();
299 wfProfileIn( __METHOD__ . '-check' );
300 $dbr = wfGetDB( DB_SLAVE );
301 $threshold = $this->parent->getOptions()->getStubThreshold();
303 # Sort by namespace
304 ksort( $this->internals );
306 $linkcolour_ids = array();
308 # Generate query
309 $queries = array();
310 foreach ( $this->internals as $ns => $entries ) {
311 foreach ( $entries as $entry ) {
312 /** @var Title $title */
313 $title = $entry['title'];
314 $pdbk = $entry['pdbk'];
316 # Skip invalid entries.
317 # Result will be ugly, but prevents crash.
318 if ( is_null( $title ) ) {
319 continue;
322 # Check if it's a static known link, e.g. interwiki
323 if ( $title->isAlwaysKnown() ) {
324 $colours[$pdbk] = '';
325 } elseif ( $ns == NS_SPECIAL ) {
326 $colours[$pdbk] = 'new';
327 } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
328 $colours[$pdbk] = Linker::getLinkColour( $title, $threshold );
329 $output->addLink( $title, $id );
330 $linkcolour_ids[$id] = $pdbk;
331 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
332 $colours[$pdbk] = 'new';
333 } else {
334 # Not in the link cache, add it to the query
335 $queries[$ns][] = $title->getDBkey();
339 if ( $queries ) {
340 $where = array();
341 foreach ( $queries as $ns => $pages ) {
342 $where[] = $dbr->makeList(
343 array(
344 'page_namespace' => $ns,
345 'page_title' => array_unique( $pages ),
347 LIST_AND
351 $fields = array( 'page_id', 'page_namespace', 'page_title',
352 'page_is_redirect', 'page_len', 'page_latest' );
354 if ( $wgContentHandlerUseDB ) {
355 $fields[] = 'page_content_model';
358 $res = $dbr->select(
359 'page',
360 $fields,
361 $dbr->makeList( $where, LIST_OR ),
362 __METHOD__
365 # Fetch data and form into an associative array
366 # non-existent = broken
367 foreach ( $res as $s ) {
368 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
369 $pdbk = $title->getPrefixedDBkey();
370 $linkCache->addGoodLinkObjFromRow( $title, $s );
371 $output->addLink( $title, $s->page_id );
372 # @todo FIXME: Convoluted data flow
373 # The redirect status and length is passed to getLinkColour via the LinkCache
374 # Use formal parameters instead
375 $colours[$pdbk] = Linker::getLinkColour( $title, $threshold );
376 //add id to the extension todolist
377 $linkcolour_ids[$s->page_id] = $pdbk;
379 unset( $res );
381 if ( count( $linkcolour_ids ) ) {
382 //pass an array of page_ids to an extension
383 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
385 wfProfileOut( __METHOD__ . '-check' );
387 # Do a second query for different language variants of links and categories
388 if ( $wgContLang->hasVariants() ) {
389 $this->doVariants( $colours );
392 # Construct search and replace arrays
393 wfProfileIn( __METHOD__ . '-construct' );
394 $replacePairs = array();
395 foreach ( $this->internals as $ns => $entries ) {
396 foreach ( $entries as $index => $entry ) {
397 $pdbk = $entry['pdbk'];
398 $title = $entry['title'];
399 $query = isset( $entry['query'] ) ? $entry['query'] : array();
400 $key = "$ns:$index";
401 $searchkey = "<!--LINK $key-->";
402 $displayText = $entry['text'];
403 if ( isset( $entry['selflink'] ) ) {
404 $replacePairs[$searchkey] = Linker::makeSelfLinkObj( $title, $displayText, $query );
405 continue;
407 if ( $displayText === '' ) {
408 $displayText = null;
410 if ( !isset( $colours[$pdbk] ) ) {
411 $colours[$pdbk] = 'new';
413 $attribs = array();
414 if ( $colours[$pdbk] == 'new' ) {
415 $linkCache->addBadLinkObj( $title );
416 $output->addLink( $title, 0 );
417 $type = array( 'broken' );
418 } else {
419 if ( $colours[$pdbk] != '' ) {
420 $attribs['class'] = $colours[$pdbk];
422 $type = array( 'known', 'noclasses' );
424 $replacePairs[$searchkey] = Linker::link( $title, $displayText,
425 $attribs, $query, $type );
428 $replacer = new HashtableReplacer( $replacePairs, 1 );
429 wfProfileOut( __METHOD__ . '-construct' );
431 # Do the thing
432 wfProfileIn( __METHOD__ . '-replace' );
433 $text = preg_replace_callback(
434 '/(<!--LINK .*?-->)/',
435 $replacer->cb(),
436 $text
439 wfProfileOut( __METHOD__ . '-replace' );
440 wfProfileOut( __METHOD__ );
444 * Replace interwiki links
445 * @param string $text
447 protected function replaceInterwiki( &$text ) {
448 if ( empty( $this->interwikis ) ) {
449 return;
452 wfProfileIn( __METHOD__ );
453 # Make interwiki link HTML
454 $output = $this->parent->getOutput();
455 $replacePairs = array();
456 foreach ( $this->interwikis as $key => $link ) {
457 $replacePairs[$key] = Linker::link( $link['title'], $link['text'] );
458 $output->addInterwikiLink( $link['title'] );
460 $replacer = new HashtableReplacer( $replacePairs, 1 );
462 $text = preg_replace_callback(
463 '/<!--IWLINK (.*?)-->/',
464 $replacer->cb(),
465 $text );
466 wfProfileOut( __METHOD__ );
470 * Modify $this->internals and $colours according to language variant linking rules
471 * @param array $colours
473 protected function doVariants( &$colours ) {
474 global $wgContLang, $wgContentHandlerUseDB;
475 $linkBatch = new LinkBatch();
476 $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
477 $output = $this->parent->getOutput();
478 $linkCache = LinkCache::singleton();
479 $threshold = $this->parent->getOptions()->getStubThreshold();
480 $titlesToBeConverted = '';
481 $titlesAttrs = array();
483 // Concatenate titles to a single string, thus we only need auto convert the
484 // single string to all variants. This would improve parser's performance
485 // significantly.
486 foreach ( $this->internals as $ns => $entries ) {
487 if ( $ns == NS_SPECIAL ) {
488 continue;
490 foreach ( $entries as $index => $entry ) {
491 $pdbk = $entry['pdbk'];
492 // we only deal with new links (in its first query)
493 if ( !isset( $colours[$pdbk] ) || $colours[$pdbk] === 'new' ) {
494 $titlesAttrs[] = array( $index, $entry['title'] );
495 // separate titles with \0 because it would never appears
496 // in a valid title
497 $titlesToBeConverted .= $entry['title']->getText() . "\0";
502 // Now do the conversion and explode string to text of titles
503 $titlesAllVariants = $wgContLang->autoConvertToAllVariants( rtrim( $titlesToBeConverted, "\0" ) );
504 $allVariantsName = array_keys( $titlesAllVariants );
505 foreach ( $titlesAllVariants as &$titlesVariant ) {
506 $titlesVariant = explode( "\0", $titlesVariant );
509 // Then add variants of links to link batch
510 $parentTitle = $this->parent->getTitle();
511 foreach ( $titlesAttrs as $i => $attrs ) {
512 /** @var Title $title */
513 list( $index, $title ) = $attrs;
514 $ns = $title->getNamespace();
515 $text = $title->getText();
517 foreach ( $allVariantsName as $variantName ) {
518 $textVariant = $titlesAllVariants[$variantName][$i];
519 if ( $textVariant === $text ) {
520 continue;
523 $variantTitle = Title::makeTitle( $ns, $textVariant );
524 if ( is_null( $variantTitle ) ) {
525 continue;
528 // Self-link checking for mixed/different variant titles. At this point, we
529 // already know the exact title does not exist, so the link cannot be to a
530 // variant of the current title that exists as a separate page.
531 if ( $variantTitle->equals( $parentTitle ) && !$title->hasFragment() ) {
532 $this->internals[$ns][$index]['selflink'] = true;
533 continue 2;
536 $linkBatch->addObj( $variantTitle );
537 $variantMap[$variantTitle->getPrefixedDBkey()][] = "$ns:$index";
541 // process categories, check if a category exists in some variant
542 $categoryMap = array(); // maps $category_variant => $category (dbkeys)
543 $varCategories = array(); // category replacements oldDBkey => newDBkey
544 foreach ( $output->getCategoryLinks() as $category ) {
545 $categoryTitle = Title::makeTitleSafe( NS_CATEGORY, $category );
546 $linkBatch->addObj( $categoryTitle );
547 $variants = $wgContLang->autoConvertToAllVariants( $category );
548 foreach ( $variants as $variant ) {
549 if ( $variant !== $category ) {
550 $variantTitle = Title::makeTitleSafe( NS_CATEGORY, $variant );
551 if ( is_null( $variantTitle ) ) {
552 continue;
554 $linkBatch->addObj( $variantTitle );
555 $categoryMap[$variant] = array( $category, $categoryTitle );
560 if ( !$linkBatch->isEmpty() ) {
561 // construct query
562 $dbr = wfGetDB( DB_SLAVE );
563 $fields = array( 'page_id', 'page_namespace', 'page_title',
564 'page_is_redirect', 'page_len', 'page_latest' );
566 if ( $wgContentHandlerUseDB ) {
567 $fields[] = 'page_content_model';
570 $varRes = $dbr->select( 'page',
571 $fields,
572 $linkBatch->constructSet( 'page', $dbr ),
573 __METHOD__
576 $linkcolour_ids = array();
578 // for each found variants, figure out link holders and replace
579 foreach ( $varRes as $s ) {
581 $variantTitle = Title::makeTitle( $s->page_namespace, $s->page_title );
582 $varPdbk = $variantTitle->getPrefixedDBkey();
583 $vardbk = $variantTitle->getDBkey();
585 $holderKeys = array();
586 if ( isset( $variantMap[$varPdbk] ) ) {
587 $holderKeys = $variantMap[$varPdbk];
588 $linkCache->addGoodLinkObjFromRow( $variantTitle, $s );
589 $output->addLink( $variantTitle, $s->page_id );
592 // loop over link holders
593 foreach ( $holderKeys as $key ) {
594 list( $ns, $index ) = explode( ':', $key, 2 );
595 $entry =& $this->internals[$ns][$index];
596 $pdbk = $entry['pdbk'];
598 if ( !isset( $colours[$pdbk] ) || $colours[$pdbk] === 'new' ) {
599 // found link in some of the variants, replace the link holder data
600 $entry['title'] = $variantTitle;
601 $entry['pdbk'] = $varPdbk;
603 // set pdbk and colour
604 # @todo FIXME: Convoluted data flow
605 # The redirect status and length is passed to getLinkColour via the LinkCache
606 # Use formal parameters instead
607 $colours[$varPdbk] = Linker::getLinkColour( $variantTitle, $threshold );
608 $linkcolour_ids[$s->page_id] = $pdbk;
612 // check if the object is a variant of a category
613 if ( isset( $categoryMap[$vardbk] ) ) {
614 list( $oldkey, $oldtitle ) = $categoryMap[$vardbk];
615 if ( !isset( $varCategories[$oldkey] ) && !$oldtitle->exists() ) {
616 $varCategories[$oldkey] = $vardbk;
620 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
622 // rebuild the categories in original order (if there are replacements)
623 if ( count( $varCategories ) > 0 ) {
624 $newCats = array();
625 $originalCats = $output->getCategories();
626 foreach ( $originalCats as $cat => $sortkey ) {
627 // make the replacement
628 if ( array_key_exists( $cat, $varCategories ) ) {
629 $newCats[$varCategories[$cat]] = $sortkey;
630 } else {
631 $newCats[$cat] = $sortkey;
634 $output->setCategoryLinks( $newCats );
640 * Replace <!--LINK--> link placeholders with plain text of links
641 * (not HTML-formatted).
643 * @param string $text
644 * @return string
646 public function replaceText( $text ) {
647 wfProfileIn( __METHOD__ );
649 $text = preg_replace_callback(
650 '/<!--(LINK|IWLINK) (.*?)-->/',
651 array( &$this, 'replaceTextCallback' ),
652 $text );
654 wfProfileOut( __METHOD__ );
655 return $text;
659 * Callback for replaceText()
661 * @param array $matches
662 * @return string
663 * @private
665 public function replaceTextCallback( $matches ) {
666 $type = $matches[1];
667 $key = $matches[2];
668 if ( $type == 'LINK' ) {
669 list( $ns, $index ) = explode( ':', $key, 2 );
670 if ( isset( $this->internals[$ns][$index]['text'] ) ) {
671 return $this->internals[$ns][$index]['text'];
673 } elseif ( $type == 'IWLINK' ) {
674 if ( isset( $this->interwikis[$key]['text'] ) ) {
675 return $this->interwikis[$key]['text'];
678 return $matches[0];