Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / parser / LinkHolderArray.php
blob1c6f40457688f85910682e7be0e4b623357aff0b
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 = [];
29 public $interwikis = [];
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 [ '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 [ $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 [ $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 = [];
214 $this->interwikis = [];
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 = [], $trail = '', $prefix = '' ) {
232 if ( !is_object( $nt ) ) {
233 # Fail gracefully
234 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
235 } else {
236 # Separate the link trail from the rest of the link
237 list( $inside, $trail ) = Linker::splitTrail( $trail );
239 $entry = [
240 'title' => $nt,
241 'text' => $prefix . $text . $inside,
242 'pdbk' => $nt->getPrefixedDBkey(),
244 if ( $query !== [] ) {
245 $entry['query'] = $query;
248 if ( $nt->isExternal() ) {
249 // Use a globally unique ID to keep the objects mergable
250 $key = $this->parent->nextLinkID();
251 $this->interwikis[$key] = $entry;
252 $retVal = "<!--IWLINK $key-->{$trail}";
253 } else {
254 $key = $this->parent->nextLinkID();
255 $ns = $nt->getNamespace();
256 $this->internals[$ns][$key] = $entry;
257 $retVal = "<!--LINK $ns:$key-->{$trail}";
259 $this->size++;
261 return $retVal;
265 * Replace <!--LINK--> link placeholders with actual links, in the buffer
267 * @param string $text
269 public function replace( &$text ) {
271 $this->replaceInternal( $text );
272 $this->replaceInterwiki( $text );
277 * Replace internal links
278 * @param string $text
280 protected function replaceInternal( &$text ) {
281 if ( !$this->internals ) {
282 return;
285 global $wgContLang;
287 $colours = [];
288 $linkCache = LinkCache::singleton();
289 $output = $this->parent->getOutput();
290 $linkRenderer = $this->parent->getLinkRenderer();
291 $threshold = $linkRenderer->getStubThreshold();
293 $dbr = wfGetDB( DB_SLAVE );
295 # Sort by namespace
296 ksort( $this->internals );
298 $linkcolour_ids = [];
300 # Generate query
301 $lb = new LinkBatch();
302 $lb->setCaller( __METHOD__ );
304 foreach ( $this->internals as $ns => $entries ) {
305 foreach ( $entries as $entry ) {
306 /** @var Title $title */
307 $title = $entry['title'];
308 $pdbk = $entry['pdbk'];
310 # Skip invalid entries.
311 # Result will be ugly, but prevents crash.
312 if ( is_null( $title ) ) {
313 continue;
316 # Check if it's a static known link, e.g. interwiki
317 if ( $title->isAlwaysKnown() ) {
318 $colours[$pdbk] = '';
319 } elseif ( $ns == NS_SPECIAL ) {
320 $colours[$pdbk] = 'new';
321 } else {
322 $id = $linkCache->getGoodLinkID( $pdbk );
323 if ( $id != 0 ) {
324 $colours[$pdbk] = Linker::getLinkColour( $title, $threshold );
325 $output->addLink( $title, $id );
326 $linkcolour_ids[$id] = $pdbk;
327 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
328 $colours[$pdbk] = 'new';
329 } else {
330 # Not in the link cache, add it to the query
331 $lb->addObj( $title );
336 if ( !$lb->isEmpty() ) {
337 $fields = array_merge(
338 LinkCache::getSelectFields(),
339 [ 'page_namespace', 'page_title' ]
342 $res = $dbr->select(
343 'page',
344 $fields,
345 $lb->constructSet( 'page', $dbr ),
346 __METHOD__
349 # Fetch data and form into an associative array
350 # non-existent = broken
351 foreach ( $res as $s ) {
352 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
353 $pdbk = $title->getPrefixedDBkey();
354 $linkCache->addGoodLinkObjFromRow( $title, $s );
355 $output->addLink( $title, $s->page_id );
356 $colours[$pdbk] = Linker::getLinkColour( $title, $threshold );
357 // add id to the extension todolist
358 $linkcolour_ids[$s->page_id] = $pdbk;
360 unset( $res );
362 if ( count( $linkcolour_ids ) ) {
363 // pass an array of page_ids to an extension
364 Hooks::run( 'GetLinkColours', [ $linkcolour_ids, &$colours ] );
367 # Do a second query for different language variants of links and categories
368 if ( $wgContLang->hasVariants() ) {
369 $this->doVariants( $colours );
372 # Construct search and replace arrays
373 $replacePairs = [];
374 foreach ( $this->internals as $ns => $entries ) {
375 foreach ( $entries as $index => $entry ) {
376 $pdbk = $entry['pdbk'];
377 $title = $entry['title'];
378 $query = isset( $entry['query'] ) ? $entry['query'] : [];
379 $key = "$ns:$index";
380 $searchkey = "<!--LINK $key-->";
381 $displayText = $entry['text'];
382 if ( isset( $entry['selflink'] ) ) {
383 $replacePairs[$searchkey] = Linker::makeSelfLinkObj( $title, $displayText, $query );
384 continue;
386 if ( $displayText === '' ) {
387 $displayText = null;
388 } else {
389 $displayText = new HtmlArmor( $displayText );
391 if ( !isset( $colours[$pdbk] ) ) {
392 $colours[$pdbk] = 'new';
394 $attribs = [];
395 if ( $colours[$pdbk] == 'new' ) {
396 $linkCache->addBadLinkObj( $title );
397 $output->addLink( $title, 0 );
398 $link = $linkRenderer->makeBrokenLink(
399 $title, $displayText, $attribs, $query
401 } else {
402 $link = $linkRenderer->makePreloadedLink(
403 $title, $displayText, $colours[$pdbk], $attribs, $query
407 $replacePairs[$searchkey] = $link;
410 $replacer = new HashtableReplacer( $replacePairs, 1 );
412 # Do the thing
413 $text = preg_replace_callback(
414 '/(<!--LINK .*?-->)/',
415 $replacer->cb(),
416 $text
422 * Replace interwiki links
423 * @param string $text
425 protected function replaceInterwiki( &$text ) {
426 if ( empty( $this->interwikis ) ) {
427 return;
430 # Make interwiki link HTML
431 $output = $this->parent->getOutput();
432 $replacePairs = [];
433 $linkRenderer = $this->parent->getLinkRenderer();
434 foreach ( $this->interwikis as $key => $link ) {
435 $replacePairs[$key] = $linkRenderer->makeLink(
436 $link['title'],
437 new HtmlArmor( $link['text'] )
439 $output->addInterwikiLink( $link['title'] );
441 $replacer = new HashtableReplacer( $replacePairs, 1 );
443 $text = preg_replace_callback(
444 '/<!--IWLINK (.*?)-->/',
445 $replacer->cb(),
446 $text );
450 * Modify $this->internals and $colours according to language variant linking rules
451 * @param array $colours
453 protected function doVariants( &$colours ) {
454 global $wgContLang;
455 $linkBatch = new LinkBatch();
456 $variantMap = []; // maps $pdbkey_Variant => $keys (of link holders)
457 $output = $this->parent->getOutput();
458 $linkCache = LinkCache::singleton();
459 $threshold = $this->parent->getOptions()->getStubThreshold();
460 $titlesToBeConverted = '';
461 $titlesAttrs = [];
463 // Concatenate titles to a single string, thus we only need auto convert the
464 // single string to all variants. This would improve parser's performance
465 // significantly.
466 foreach ( $this->internals as $ns => $entries ) {
467 if ( $ns == NS_SPECIAL ) {
468 continue;
470 foreach ( $entries as $index => $entry ) {
471 $pdbk = $entry['pdbk'];
472 // we only deal with new links (in its first query)
473 if ( !isset( $colours[$pdbk] ) || $colours[$pdbk] === 'new' ) {
474 $titlesAttrs[] = [ $index, $entry['title'] ];
475 // separate titles with \0 because it would never appears
476 // in a valid title
477 $titlesToBeConverted .= $entry['title']->getText() . "\0";
482 // Now do the conversion and explode string to text of titles
483 $titlesAllVariants = $wgContLang->autoConvertToAllVariants( rtrim( $titlesToBeConverted, "\0" ) );
484 $allVariantsName = array_keys( $titlesAllVariants );
485 foreach ( $titlesAllVariants as &$titlesVariant ) {
486 $titlesVariant = explode( "\0", $titlesVariant );
489 // Then add variants of links to link batch
490 $parentTitle = $this->parent->getTitle();
491 foreach ( $titlesAttrs as $i => $attrs ) {
492 /** @var Title $title */
493 list( $index, $title ) = $attrs;
494 $ns = $title->getNamespace();
495 $text = $title->getText();
497 foreach ( $allVariantsName as $variantName ) {
498 $textVariant = $titlesAllVariants[$variantName][$i];
499 if ( $textVariant === $text ) {
500 continue;
503 $variantTitle = Title::makeTitle( $ns, $textVariant );
505 // Self-link checking for mixed/different variant titles. At this point, we
506 // already know the exact title does not exist, so the link cannot be to a
507 // variant of the current title that exists as a separate page.
508 if ( $variantTitle->equals( $parentTitle ) && !$title->hasFragment() ) {
509 $this->internals[$ns][$index]['selflink'] = true;
510 continue 2;
513 $linkBatch->addObj( $variantTitle );
514 $variantMap[$variantTitle->getPrefixedDBkey()][] = "$ns:$index";
518 // process categories, check if a category exists in some variant
519 $categoryMap = []; // maps $category_variant => $category (dbkeys)
520 $varCategories = []; // category replacements oldDBkey => newDBkey
521 foreach ( $output->getCategoryLinks() as $category ) {
522 $categoryTitle = Title::makeTitleSafe( NS_CATEGORY, $category );
523 $linkBatch->addObj( $categoryTitle );
524 $variants = $wgContLang->autoConvertToAllVariants( $category );
525 foreach ( $variants as $variant ) {
526 if ( $variant !== $category ) {
527 $variantTitle = Title::makeTitleSafe( NS_CATEGORY, $variant );
528 if ( is_null( $variantTitle ) ) {
529 continue;
531 $linkBatch->addObj( $variantTitle );
532 $categoryMap[$variant] = [ $category, $categoryTitle ];
537 if ( !$linkBatch->isEmpty() ) {
538 // construct query
539 $dbr = wfGetDB( DB_SLAVE );
540 $fields = array_merge(
541 LinkCache::getSelectFields(),
542 [ 'page_namespace', 'page_title' ]
545 $varRes = $dbr->select( 'page',
546 $fields,
547 $linkBatch->constructSet( 'page', $dbr ),
548 __METHOD__
551 $linkcolour_ids = [];
553 // for each found variants, figure out link holders and replace
554 foreach ( $varRes as $s ) {
555 $variantTitle = Title::makeTitle( $s->page_namespace, $s->page_title );
556 $varPdbk = $variantTitle->getPrefixedDBkey();
557 $vardbk = $variantTitle->getDBkey();
559 $holderKeys = [];
560 if ( isset( $variantMap[$varPdbk] ) ) {
561 $holderKeys = $variantMap[$varPdbk];
562 $linkCache->addGoodLinkObjFromRow( $variantTitle, $s );
563 $output->addLink( $variantTitle, $s->page_id );
566 // loop over link holders
567 foreach ( $holderKeys as $key ) {
568 list( $ns, $index ) = explode( ':', $key, 2 );
569 $entry =& $this->internals[$ns][$index];
570 $pdbk = $entry['pdbk'];
572 if ( !isset( $colours[$pdbk] ) || $colours[$pdbk] === 'new' ) {
573 // found link in some of the variants, replace the link holder data
574 $entry['title'] = $variantTitle;
575 $entry['pdbk'] = $varPdbk;
577 // set pdbk and colour
578 $colours[$varPdbk] = Linker::getLinkColour( $variantTitle, $threshold );
579 $linkcolour_ids[$s->page_id] = $pdbk;
583 // check if the object is a variant of a category
584 if ( isset( $categoryMap[$vardbk] ) ) {
585 list( $oldkey, $oldtitle ) = $categoryMap[$vardbk];
586 if ( !isset( $varCategories[$oldkey] ) && !$oldtitle->exists() ) {
587 $varCategories[$oldkey] = $vardbk;
591 Hooks::run( 'GetLinkColours', [ $linkcolour_ids, &$colours ] );
593 // rebuild the categories in original order (if there are replacements)
594 if ( count( $varCategories ) > 0 ) {
595 $newCats = [];
596 $originalCats = $output->getCategories();
597 foreach ( $originalCats as $cat => $sortkey ) {
598 // make the replacement
599 if ( array_key_exists( $cat, $varCategories ) ) {
600 $newCats[$varCategories[$cat]] = $sortkey;
601 } else {
602 $newCats[$cat] = $sortkey;
605 $output->setCategoryLinks( $newCats );
611 * Replace <!--LINK--> link placeholders with plain text of links
612 * (not HTML-formatted).
614 * @param string $text
615 * @return string
617 public function replaceText( $text ) {
619 $text = preg_replace_callback(
620 '/<!--(LINK|IWLINK) (.*?)-->/',
621 [ &$this, 'replaceTextCallback' ],
622 $text );
624 return $text;
628 * Callback for replaceText()
630 * @param array $matches
631 * @return string
632 * @private
634 public function replaceTextCallback( $matches ) {
635 $type = $matches[1];
636 $key = $matches[2];
637 if ( $type == 'LINK' ) {
638 list( $ns, $index ) = explode( ':', $key, 2 );
639 if ( isset( $this->internals[$ns][$index]['text'] ) ) {
640 return $this->internals[$ns][$index]['text'];
642 } elseif ( $type == 'IWLINK' ) {
643 if ( isset( $this->interwikis[$key]['text'] ) ) {
644 return $this->interwikis[$key]['text'];
647 return $matches[0];