MediaWiki REL1_33
LinkHolderArray.php
Go to the documentation of this file.
1<?php
25
30 public $internals = [];
31 public $interwikis = [];
32 public $size = 0;
33
37 public $parent;
38 protected $tempIdOffset;
39
43 public function __construct( $parent ) {
44 $this->parent = $parent;
45 }
46
50 public function __destruct() {
51 foreach ( $this as $name => $value ) {
52 unset( $this->$name );
53 }
54 }
55
64 public function __sleep() {
65 foreach ( $this->internals as &$nsLinks ) {
66 foreach ( $nsLinks as &$entry ) {
67 unset( $entry['title'] );
68 }
69 }
70 unset( $nsLinks );
71 unset( $entry );
72
73 foreach ( $this->interwikis as &$entry ) {
74 unset( $entry['title'] );
75 }
76 unset( $entry );
77
78 return [ 'internals', 'interwikis', 'size' ];
79 }
80
84 public function __wakeup() {
85 foreach ( $this->internals as &$nsLinks ) {
86 foreach ( $nsLinks as &$entry ) {
87 $entry['title'] = Title::newFromText( $entry['pdbk'] );
88 }
89 }
90 unset( $nsLinks );
91 unset( $entry );
92
93 foreach ( $this->interwikis as &$entry ) {
94 $entry['title'] = Title::newFromText( $entry['pdbk'] );
95 }
96 unset( $entry );
97 }
98
103 public function merge( $other ) {
104 foreach ( $other->internals as $ns => $entries ) {
105 $this->size += count( $entries );
106 if ( !isset( $this->internals[$ns] ) ) {
107 $this->internals[$ns] = $entries;
108 } else {
109 $this->internals[$ns] += $entries;
110 }
111 }
112 $this->interwikis += $other->interwikis;
113 }
114
127 public function mergeForeign( $other, $texts ) {
128 $this->tempIdOffset = $idOffset = $this->parent->nextLinkID();
129 $maxId = 0;
130
131 # Renumber internal links
132 foreach ( $other->internals as $ns => $nsLinks ) {
133 foreach ( $nsLinks as $key => $entry ) {
134 $newKey = $idOffset + $key;
135 $this->internals[$ns][$newKey] = $entry;
136 $maxId = $newKey > $maxId ? $newKey : $maxId;
137 }
138 }
139 $texts = preg_replace_callback( '/(<!--LINK\'" \d+:)(\d+)(-->)/',
140 [ $this, 'mergeForeignCallback' ], $texts );
141
142 # Renumber interwiki links
143 foreach ( $other->interwikis as $key => $entry ) {
144 $newKey = $idOffset + $key;
145 $this->interwikis[$newKey] = $entry;
146 $maxId = $newKey > $maxId ? $newKey : $maxId;
147 }
148 $texts = preg_replace_callback( '/(<!--IWLINK\'" )(\d+)(-->)/',
149 [ $this, 'mergeForeignCallback' ], $texts );
150
151 # Set the parent link ID to be beyond the highest used ID
152 $this->parent->setLinkID( $maxId + 1 );
153 $this->tempIdOffset = null;
154 return $texts;
155 }
156
161 protected function mergeForeignCallback( $m ) {
162 return $m[1] . ( $m[2] + $this->tempIdOffset ) . $m[3];
163 }
164
171 public function getSubArray( $text ) {
172 $sub = new LinkHolderArray( $this->parent );
173
174 # Internal links
175 $pos = 0;
176 while ( $pos < strlen( $text ) ) {
177 if ( !preg_match( '/<!--LINK\'" (\d+):(\d+)-->/',
178 $text, $m, PREG_OFFSET_CAPTURE, $pos )
179 ) {
180 break;
181 }
182 $ns = $m[1][0];
183 $key = $m[2][0];
184 $sub->internals[$ns][$key] = $this->internals[$ns][$key];
185 $pos = $m[0][1] + strlen( $m[0][0] );
186 }
187
188 # Interwiki links
189 $pos = 0;
190 while ( $pos < strlen( $text ) ) {
191 if ( !preg_match( '/<!--IWLINK\'" (\d+)-->/', $text, $m, PREG_OFFSET_CAPTURE, $pos ) ) {
192 break;
193 }
194 $key = $m[1][0];
195 $sub->interwikis[$key] = $this->interwikis[$key];
196 $pos = $m[0][1] + strlen( $m[0][0] );
197 }
198 return $sub;
199 }
200
205 public function isBig() {
207 return $this->size > $wgLinkHolderBatchSize;
208 }
209
214 public function clear() {
215 $this->internals = [];
216 $this->interwikis = [];
217 $this->size = 0;
218 }
219
233 public function makeHolder( $nt, $text = '', $query = [], $trail = '', $prefix = '' ) {
234 if ( !is_object( $nt ) ) {
235 # Fail gracefully
236 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
237 } else {
238 # Separate the link trail from the rest of the link
239 list( $inside, $trail ) = Linker::splitTrail( $trail );
240
241 $entry = [
242 'title' => $nt,
243 'text' => $prefix . $text . $inside,
244 'pdbk' => $nt->getPrefixedDBkey(),
245 ];
246 if ( $query !== [] ) {
247 $entry['query'] = $query;
248 }
249
250 if ( $nt->isExternal() ) {
251 // Use a globally unique ID to keep the objects mergable
252 $key = $this->parent->nextLinkID();
253 $this->interwikis[$key] = $entry;
254 $retVal = "<!--IWLINK'\" $key-->{$trail}";
255 } else {
256 $key = $this->parent->nextLinkID();
257 $ns = $nt->getNamespace();
258 $this->internals[$ns][$key] = $entry;
259 $retVal = "<!--LINK'\" $ns:$key-->{$trail}";
260 }
261 $this->size++;
262 }
263 return $retVal;
264 }
265
271 public function replace( &$text ) {
272 $this->replaceInternal( $text );
273 $this->replaceInterwiki( $text );
274 }
275
281 protected function replaceInternal( &$text ) {
282 if ( !$this->internals ) {
283 return;
284 }
285
286 $colours = [];
287 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
288 $output = $this->parent->getOutput();
289 $linkRenderer = $this->parent->getLinkRenderer();
290
292
293 # Sort by namespace
294 ksort( $this->internals );
295
296 $linkcolour_ids = [];
297
298 # Generate query
299 $lb = new LinkBatch();
300 $lb->setCaller( __METHOD__ );
301
302 foreach ( $this->internals as $ns => $entries ) {
303 foreach ( $entries as $entry ) {
305 $title = $entry['title'];
306 $pdbk = $entry['pdbk'];
307
308 # Skip invalid entries.
309 # Result will be ugly, but prevents crash.
310 if ( is_null( $title ) ) {
311 continue;
312 }
313
314 # Check if it's a static known link, e.g. interwiki
315 if ( $title->isAlwaysKnown() ) {
316 $colours[$pdbk] = '';
317 } elseif ( $ns == NS_SPECIAL ) {
318 $colours[$pdbk] = 'new';
319 } else {
320 $id = $linkCache->getGoodLinkID( $pdbk );
321 if ( $id != 0 ) {
322 $colours[$pdbk] = $linkRenderer->getLinkClasses( $title );
323 $output->addLink( $title, $id );
324 $linkcolour_ids[$id] = $pdbk;
325 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
326 $colours[$pdbk] = 'new';
327 } else {
328 # Not in the link cache, add it to the query
329 $lb->addObj( $title );
330 }
331 }
332 }
333 }
334 if ( !$lb->isEmpty() ) {
335 $fields = array_merge(
336 LinkCache::getSelectFields(),
337 [ 'page_namespace', 'page_title' ]
338 );
339
340 $res = $dbr->select(
341 'page',
342 $fields,
343 $lb->constructSet( 'page', $dbr ),
344 __METHOD__
345 );
346
347 # Fetch data and form into an associative array
348 # non-existent = broken
349 foreach ( $res as $s ) {
350 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
351 $pdbk = $title->getPrefixedDBkey();
352 $linkCache->addGoodLinkObjFromRow( $title, $s );
353 $output->addLink( $title, $s->page_id );
354 $colours[$pdbk] = $linkRenderer->getLinkClasses( $title );
355 // add id to the extension todolist
356 $linkcolour_ids[$s->page_id] = $pdbk;
357 }
358 unset( $res );
359 }
360 if ( count( $linkcolour_ids ) ) {
361 // pass an array of page_ids to an extension
362 Hooks::run( 'GetLinkColours', [ $linkcolour_ids, &$colours, $this->parent->getTitle() ] );
363 }
364
365 # Do a second query for different language variants of links and categories
366 if ( $this->parent->getContentLanguage()->hasVariants() ) {
367 $this->doVariants( $colours );
368 }
369
370 # Construct search and replace arrays
371 $replacePairs = [];
372 foreach ( $this->internals as $ns => $entries ) {
373 foreach ( $entries as $index => $entry ) {
374 $pdbk = $entry['pdbk'];
375 $title = $entry['title'];
376 $query = $entry['query'] ?? [];
377 $key = "$ns:$index";
378 $searchkey = "<!--LINK'\" $key-->";
379 $displayText = $entry['text'];
380 if ( isset( $entry['selflink'] ) ) {
381 $replacePairs[$searchkey] = Linker::makeSelfLinkObj( $title, $displayText, $query );
382 continue;
383 }
384 if ( $displayText === '' ) {
385 $displayText = null;
386 } else {
387 $displayText = new HtmlArmor( $displayText );
388 }
389 if ( !isset( $colours[$pdbk] ) ) {
390 $colours[$pdbk] = 'new';
391 }
392 $attribs = [];
393 if ( $colours[$pdbk] == 'new' ) {
394 $linkCache->addBadLinkObj( $title );
395 $output->addLink( $title, 0 );
396 $link = $linkRenderer->makeBrokenLink(
397 $title, $displayText, $attribs, $query
398 );
399 } else {
400 $link = $linkRenderer->makePreloadedLink(
401 $title, $displayText, $colours[$pdbk], $attribs, $query
402 );
403 }
404
405 $replacePairs[$searchkey] = $link;
406 }
407 }
408
409 # Do the thing
410 $text = preg_replace_callback(
411 '/(<!--LINK\'" .*?-->)/',
412 function ( array $matches ) use ( $replacePairs ) {
413 return $replacePairs[$matches[1]];
414 },
415 $text
416 );
417 }
418
424 protected function replaceInterwiki( &$text ) {
425 if ( empty( $this->interwikis ) ) {
426 return;
427 }
428
429 # Make interwiki link HTML
430 $output = $this->parent->getOutput();
431 $replacePairs = [];
432 $linkRenderer = $this->parent->getLinkRenderer();
433 foreach ( $this->interwikis as $key => $link ) {
434 $replacePairs[$key] = $linkRenderer->makeLink(
435 $link['title'],
436 new HtmlArmor( $link['text'] )
437 );
438 $output->addInterwikiLink( $link['title'] );
439 }
440
441 $text = preg_replace_callback(
442 '/<!--IWLINK\'" (.*?)-->/',
443 function ( array $matches ) use ( $replacePairs ) {
444 return $replacePairs[$matches[1]];
445 },
446 $text
447 );
448 }
449
454 protected function doVariants( &$colours ) {
455 $linkBatch = new LinkBatch();
456 $variantMap = []; // maps $pdbkey_Variant => $keys (of link holders)
457 $output = $this->parent->getOutput();
458 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
459 $titlesToBeConverted = '';
460 $titlesAttrs = [];
461
462 // Concatenate titles to a single string, thus we only need auto convert the
463 // single string to all variants. This would improve parser's performance
464 // significantly.
465 foreach ( $this->internals as $ns => $entries ) {
466 if ( $ns == NS_SPECIAL ) {
467 continue;
468 }
469 foreach ( $entries as $index => $entry ) {
470 $pdbk = $entry['pdbk'];
471 // we only deal with new links (in its first query)
472 if ( !isset( $colours[$pdbk] ) || $colours[$pdbk] === 'new' ) {
473 $titlesAttrs[] = [ $index, $entry['title'] ];
474 // separate titles with \0 because it would never appears
475 // in a valid title
476 $titlesToBeConverted .= $entry['title']->getText() . "\0";
477 }
478 }
479 }
480
481 // Now do the conversion and explode string to text of titles
482 $titlesAllVariants = $this->parent->getContentLanguage()->
483 autoConvertToAllVariants( rtrim( $titlesToBeConverted, "\0" ) );
484 $allVariantsName = array_keys( $titlesAllVariants );
485 foreach ( $titlesAllVariants as &$titlesVariant ) {
486 $titlesVariant = explode( "\0", $titlesVariant );
487 }
488
489 // Then add variants of links to link batch
490 $parentTitle = $this->parent->getTitle();
491 foreach ( $titlesAttrs as $i => $attrs ) {
493 list( $index, $title ) = $attrs;
494 $ns = $title->getNamespace();
495 $text = $title->getText();
496
497 foreach ( $allVariantsName as $variantName ) {
498 $textVariant = $titlesAllVariants[$variantName][$i];
499 if ( $textVariant === $text ) {
500 continue;
501 }
502
503 $variantTitle = Title::makeTitle( $ns, $textVariant );
504
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;
511 }
512
513 $linkBatch->addObj( $variantTitle );
514 $variantMap[$variantTitle->getPrefixedDBkey()][] = "$ns:$index";
515 }
516 }
517
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 = $this->parent->getContentLanguage()->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;
530 }
531 $linkBatch->addObj( $variantTitle );
532 $categoryMap[$variant] = [ $category, $categoryTitle ];
533 }
534 }
535 }
536
537 if ( !$linkBatch->isEmpty() ) {
538 // construct query
540 $fields = array_merge(
541 LinkCache::getSelectFields(),
542 [ 'page_namespace', 'page_title' ]
543 );
544
545 $varRes = $dbr->select( 'page',
546 $fields,
547 $linkBatch->constructSet( 'page', $dbr ),
548 __METHOD__
549 );
550
551 $linkcolour_ids = [];
552 $linkRenderer = $this->parent->getLinkRenderer();
553
554 // for each found variants, figure out link holders and replace
555 foreach ( $varRes as $s ) {
556 $variantTitle = Title::makeTitle( $s->page_namespace, $s->page_title );
557 $varPdbk = $variantTitle->getPrefixedDBkey();
558 $vardbk = $variantTitle->getDBkey();
559
560 $holderKeys = [];
561 if ( isset( $variantMap[$varPdbk] ) ) {
562 $holderKeys = $variantMap[$varPdbk];
563 $linkCache->addGoodLinkObjFromRow( $variantTitle, $s );
564 $output->addLink( $variantTitle, $s->page_id );
565 }
566
567 // loop over link holders
568 foreach ( $holderKeys as $key ) {
569 list( $ns, $index ) = explode( ':', $key, 2 );
570 $entry =& $this->internals[$ns][$index];
571 $pdbk = $entry['pdbk'];
572
573 if ( !isset( $colours[$pdbk] ) || $colours[$pdbk] === 'new' ) {
574 // found link in some of the variants, replace the link holder data
575 $entry['title'] = $variantTitle;
576 $entry['pdbk'] = $varPdbk;
577
578 // set pdbk and colour
579 $colours[$varPdbk] = $linkRenderer->getLinkClasses( $variantTitle );
580 $linkcolour_ids[$s->page_id] = $pdbk;
581 }
582 }
583
584 // check if the object is a variant of a category
585 if ( isset( $categoryMap[$vardbk] ) ) {
586 list( $oldkey, $oldtitle ) = $categoryMap[$vardbk];
587 if ( !isset( $varCategories[$oldkey] ) && !$oldtitle->exists() ) {
588 $varCategories[$oldkey] = $vardbk;
589 }
590 }
591 }
592 Hooks::run( 'GetLinkColours', [ $linkcolour_ids, &$colours, $this->parent->getTitle() ] );
593
594 // rebuild the categories in original order (if there are replacements)
595 if ( count( $varCategories ) > 0 ) {
596 $newCats = [];
597 $originalCats = $output->getCategories();
598 foreach ( $originalCats as $cat => $sortkey ) {
599 // make the replacement
600 if ( array_key_exists( $cat, $varCategories ) ) {
601 $newCats[$varCategories[$cat]] = $sortkey;
602 } else {
603 $newCats[$cat] = $sortkey;
604 }
605 }
606 $output->setCategoryLinks( $newCats );
607 }
608 }
609 }
610
618 public function replaceText( $text ) {
619 $text = preg_replace_callback(
620 '/<!--(LINK|IWLINK)\'" (.*?)-->/',
621 [ $this, 'replaceTextCallback' ],
622 $text );
623
624 return $text;
625 }
626
634 public function replaceTextCallback( $matches ) {
635 list( , $type, $key ) = $matches;
636 if ( $type == 'LINK' ) {
637 list( $ns, $index ) = explode( ':', $key, 2 );
638 if ( isset( $this->internals[$ns][$index]['text'] ) ) {
639 return $this->internals[$ns][$index]['text'];
640 }
641 } elseif ( $type == 'IWLINK' ) {
642 if ( isset( $this->interwikis[$key]['text'] ) ) {
643 return $this->interwikis[$key]['text'];
644 }
645 }
646 return $matches[0];
647 }
648}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
$wgLinkHolderBatchSize
LinkHolderArray batch size For debugging.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:28
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition LinkBatch.php:34
__wakeup()
Recreate the Title objects.
getSubArray( $text)
Get a subset of the current LinkHolderArray which is sufficient to interpret the given text.
replaceInterwiki(&$text)
Replace interwiki links.
merge( $other)
Merge another LinkHolderArray into this one.
replaceText( $text)
Replace link placeholders with plain text of links (not HTML-formatted).
replaceTextCallback( $matches)
Callback for replaceText()
mergeForeign( $other, $texts)
Merge a LinkHolderArray from another parser instance into this one.
replaceInternal(&$text)
Replace internal links @suppress SecurityCheck-XSS Gets confused with $entry['pdbk'].
__destruct()
Reduce memory usage to reduce the impact of circular references.
clear()
Clear all stored link holders.
isBig()
Returns true if the memory requirements of this object are getting large.
makeHolder( $nt, $text='', $query=[], $trail='', $prefix='')
Make a link placeholder.
doVariants(&$colours)
Modify $this->internals and $colours according to language variant linking rules.
__sleep()
Don't serialize the parent object, it is big, and not needed when it is a parameter to mergeForeign()...
replace(&$text)
Replace link placeholders with actual links, in the buffer.
static makeSelfLinkObj( $nt, $html='', $query='', $trail='', $prefix='')
Make appropriate markup for a link to the current article.
Definition Linker.php:168
static splitTrail( $trail)
Split a link trail, return the "inside" portion and the remainder of the trail as a two-element array...
Definition Linker.php:1714
MediaWikiServices is the service locator for the application scope of MediaWiki.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:69
$res
Definition database.txt:21
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
the value of this variable comes from LanguageConverter indexed by page_id & $colours
Definition hooks.txt:1681
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition hooks.txt:3069
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition hooks.txt:2012
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition hooks.txt:1617
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form before processing starts Return false to skip default processing and return $ret $linkRenderer
Definition hooks.txt:2054
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place $output
Definition hooks.txt:2272
const NS_SPECIAL
Definition Defines.php:62
const NS_CATEGORY
Definition Defines.php:87
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
const DB_REPLICA
Definition defines.php:25