MediaWiki  master
LinkHolderArray.php
Go to the documentation of this file.
1 <?php
27 
35  public $internals = [];
37  public $interwikis = [];
39  public $size = 0;
40 
44  public $parent;
45 
51 
55  private $hookRunner;
56 
63  HookContainer $hookContainer = null
64  ) {
65  $this->parent = $parent;
66 
67  if ( !$languageConverter ) {
68  wfDeprecated( __METHOD__ . ' without $languageConverter parameter', '1.35' );
69  $languageConverter = MediaWikiServices::getInstance()
70  ->getLanguageConverterFactory()
71  ->getLanguageConverter( $parent->getTargetLanguage() );
72  }
73  $this->languageConverter = $languageConverter;
74  if ( !$hookContainer ) {
75  wfDeprecated( __METHOD__ . ' without $hookContainer parameter', '1.35' );
76  $hookContainer = MediaWikiServices::getInstance()->getHookContainer();
77  }
78  $this->hookRunner = new HookRunner( $hookContainer );
79  }
80 
84  public function __destruct() {
85  // @phan-suppress-next-line PhanTypeSuspiciousNonTraversableForeach
86  foreach ( $this as $name => $_ ) {
87  unset( $this->$name );
88  }
89  }
90 
95  public function merge( $other ) {
96  foreach ( $other->internals as $ns => $entries ) {
97  $this->size += count( $entries );
98  if ( !isset( $this->internals[$ns] ) ) {
99  $this->internals[$ns] = $entries;
100  } else {
101  $this->internals[$ns] += $entries;
102  }
103  }
104  $this->interwikis += $other->interwikis;
105  }
106 
111  public function isBig() {
112  global $wgLinkHolderBatchSize;
113  return $this->size > $wgLinkHolderBatchSize;
114  }
115 
120  public function clear() {
121  $this->internals = [];
122  $this->interwikis = [];
123  $this->size = 0;
124  }
125 
138  public function makeHolder( Title $nt, $text = '', $trail = '', $prefix = '' ) {
139  # Separate the link trail from the rest of the link
140  list( $inside, $trail ) = Linker::splitTrail( $trail );
141 
142  $key = $this->parent->nextLinkID();
143  $entry = [
144  'title' => $nt,
145  'text' => $prefix . $text . $inside,
146  'pdbk' => $nt->getPrefixedDBkey(),
147  ];
148 
149  $this->size++;
150  if ( $nt->isExternal() ) {
151  // Use a globally unique ID to keep the objects mergable
152  $this->interwikis[$key] = $entry;
153  return "<!--IWLINK'\" $key-->{$trail}";
154  } else {
155  $ns = $nt->getNamespace();
156  $this->internals[$ns][$key] = $entry;
157  return "<!--LINK'\" $ns:$key-->{$trail}";
158  }
159  }
160 
166  public function replace( &$text ) {
167  $this->replaceInternal( $text );
168  $this->replaceInterwiki( $text );
169  }
170 
176  protected function replaceInternal( &$text ) {
177  if ( !$this->internals ) {
178  return;
179  }
180 
181  $classes = [];
182  $services = MediaWikiServices::getInstance();
183  $linkCache = $services->getLinkCache();
184  $output = $this->parent->getOutput();
185  $linkRenderer = $this->parent->getLinkRenderer();
186 
187  $dbr = wfGetDB( DB_REPLICA );
188 
189  # Sort by namespace
190  ksort( $this->internals );
191 
192  $pagemap = [];
193 
194  # Generate query
195  $linkBatchFactory = $services->getLinkBatchFactory();
196  $lb = $linkBatchFactory->newLinkBatch();
197  $lb->setCaller( __METHOD__ );
198 
199  foreach ( $this->internals as $ns => $entries ) {
200  foreach ( $entries as [ 'title' => $title, 'pdbk' => $pdbk ] ) {
203  # Skip invalid entries.
204  # Result will be ugly, but prevents crash.
205  if ( $title === null ) {
206  continue;
207  }
208 
209  # Check if it's a static known link, e.g. interwiki
210  if ( $title->isAlwaysKnown() ) {
211  $classes[$pdbk] = '';
212  } elseif ( $ns == NS_SPECIAL ) {
213  $classes[$pdbk] = 'new';
214  } else {
215  $id = $linkCache->getGoodLinkID( $pdbk );
216  if ( $id != 0 ) {
217  $classes[$pdbk] = $linkRenderer->getLinkClasses( $title );
218  $output->addLink( $title, $id );
219  $pagemap[$id] = $pdbk;
220  } elseif ( $linkCache->isBadLink( $pdbk ) ) {
221  $classes[$pdbk] = 'new';
222  } else {
223  # Not in the link cache, add it to the query
224  $lb->addObj( $title );
225  }
226  }
227  }
228  }
229  if ( !$lb->isEmpty() ) {
230  $fields = array_merge(
232  [ 'page_namespace', 'page_title' ]
233  );
234 
235  $res = $dbr->select(
236  'page',
237  $fields,
238  $lb->constructSet( 'page', $dbr ),
239  __METHOD__
240  );
241 
242  # Fetch data and form into an associative array
243  # non-existent = broken
244  foreach ( $res as $s ) {
245  $title = Title::makeTitle( $s->page_namespace, $s->page_title );
246  $pdbk = $title->getPrefixedDBkey();
247  $linkCache->addGoodLinkObjFromRow( $title, $s );
248  $output->addLink( $title, $s->page_id );
249  $classes[$pdbk] = $linkRenderer->getLinkClasses( $title );
250  // add id to the extension todolist
251  $pagemap[$s->page_id] = $pdbk;
252  }
253  unset( $res );
254  }
255  if ( $pagemap !== [] ) {
256  // pass an array of page_ids to an extension
257  $this->hookRunner->onGetLinkColours( $pagemap, $classes, $this->parent->getTitle() );
258  }
259 
260  # Do a second query for different language variants of links and categories
261  if ( $this->languageConverter->hasVariants() ) {
262  $this->doVariants( $classes );
263  }
264 
265  # Construct search and replace arrays
266  $replacePairs = [];
267  foreach ( $this->internals as $ns => $entries ) {
268  foreach ( $entries as $index => $entry ) {
269  $pdbk = $entry['pdbk'];
270  $title = $entry['title'];
271  $query = $entry['query'] ?? [];
272  $searchkey = "<!--LINK'\" $ns:$index-->";
273  $displayTextHtml = $entry['text'];
274  if ( isset( $entry['selflink'] ) ) {
275  $replacePairs[$searchkey] = Linker::makeSelfLinkObj( $title, $displayTextHtml, $query );
276  continue;
277  }
278  if ( $displayTextHtml === '' ) {
279  $displayText = null;
280  } else {
281  $displayText = new HtmlArmor( $displayTextHtml );
282  }
283  if ( !isset( $classes[$pdbk] ) ) {
284  $classes[$pdbk] = 'new';
285  }
286  if ( $classes[$pdbk] == 'new' ) {
287  $linkCache->addBadLinkObj( $title );
288  $output->addLink( $title, 0 );
289  $link = $linkRenderer->makeBrokenLink(
290  $title, $displayText, [], $query
291  );
292  } else {
293  $link = $linkRenderer->makePreloadedLink(
294  $title, $displayText, $classes[$pdbk], [], $query
295  );
296  }
297 
298  $replacePairs[$searchkey] = $link;
299  }
300  }
301 
302  # Do the thing
303  $text = preg_replace_callback(
304  '/(<!--LINK\'" .*?-->)/',
305  static function ( array $matches ) use ( $replacePairs ) {
306  return $replacePairs[$matches[1]];
307  },
308  $text
309  );
310  }
311 
317  protected function replaceInterwiki( &$text ) {
318  if ( empty( $this->interwikis ) ) {
319  return;
320  }
321 
322  # Make interwiki link HTML
323  $output = $this->parent->getOutput();
324  $replacePairs = [];
325  $linkRenderer = $this->parent->getLinkRenderer();
326  foreach ( $this->interwikis as $key => $link ) {
327  $replacePairs[$key] = $linkRenderer->makeLink(
328  $link['title'],
329  new HtmlArmor( $link['text'] )
330  );
331  $output->addInterwikiLink( $link['title'] );
332  }
333 
334  $text = preg_replace_callback(
335  '/<!--IWLINK\'" (.*?)-->/',
336  static function ( array $matches ) use ( $replacePairs ) {
337  return $replacePairs[$matches[1]];
338  },
339  $text
340  );
341  }
342 
347  protected function doVariants( &$classes ) {
348  $linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
349  $linkBatch = $linkBatchFactory->newLinkBatch();
350  $variantMap = []; // maps $pdbkey_Variant => $keys (of link holders)
351  $output = $this->parent->getOutput();
352  $linkCache = MediaWikiServices::getInstance()->getLinkCache();
353  $titlesToBeConverted = '';
354  $titlesAttrs = [];
355 
356  // Concatenate titles to a single string, thus we only need auto convert the
357  // single string to all variants. This would improve parser's performance
358  // significantly.
359  foreach ( $this->internals as $ns => $entries ) {
360  if ( $ns == NS_SPECIAL ) {
361  continue;
362  }
363  foreach ( $entries as $index => [ 'title' => $title, 'pdbk' => $pdbk ] ) {
364  // we only deal with new links (in its first query)
365  if ( !isset( $classes[$pdbk] ) || $classes[$pdbk] === 'new' ) {
366  $titlesAttrs[] = [ $index, $title ];
367  // separate titles with \0 because it would never appears
368  // in a valid title
369  $titlesToBeConverted .= $title->getText() . "\0";
370  }
371  }
372  }
373 
374  // Now do the conversion and explode string to text of titles
375  $titlesAllVariants = $this->languageConverter->
376  autoConvertToAllVariants( rtrim( $titlesToBeConverted, "\0" ) );
377  foreach ( $titlesAllVariants as &$titlesVariant ) {
378  $titlesVariant = explode( "\0", $titlesVariant );
379  }
380 
381  // Then add variants of links to link batch
382  $parentTitle = $this->parent->getTitle();
383  foreach ( $titlesAttrs as $i => [ $index, $title ] ) {
385  $ns = $title->getNamespace();
386  $text = $title->getText();
387 
388  foreach ( $titlesAllVariants as $variantName => $textVariants ) {
389  $textVariant = $textVariants[$i];
390  if ( $textVariant === $text ) {
391  continue;
392  }
393 
394  $variantTitle = Title::makeTitle( $ns, $textVariant );
395 
396  // Self-link checking for mixed/different variant titles. At this point, we
397  // already know the exact title does not exist, so the link cannot be to a
398  // variant of the current title that exists as a separate page.
399  if ( $variantTitle->equals( $parentTitle ) && !$title->hasFragment() ) {
400  $this->internals[$ns][$index]['selflink'] = true;
401  continue 2;
402  }
403 
404  $linkBatch->addObj( $variantTitle );
405  $variantMap[$variantTitle->getPrefixedDBkey()][] = "$ns:$index";
406  }
407  }
408 
409  // process categories, check if a category exists in some variant
410  $categoryMap = []; // maps $category_variant => $category (dbkeys)
411  $varCategories = []; // category replacements oldDBkey => newDBkey
412  foreach ( $output->getCategoryLinks() as $category ) {
413  $categoryTitle = Title::makeTitleSafe( NS_CATEGORY, $category );
414  $linkBatch->addObj( $categoryTitle );
415  $variants = $this->languageConverter->autoConvertToAllVariants( $category );
416  foreach ( $variants as $variant ) {
417  if ( $variant !== $category ) {
418  $variantTitle = Title::makeTitleSafe( NS_CATEGORY, $variant );
419  if ( $variantTitle === null ) {
420  continue;
421  }
422  $linkBatch->addObj( $variantTitle );
423  $categoryMap[$variant] = [ $category, $categoryTitle ];
424  }
425  }
426  }
427 
428  if ( !$linkBatch->isEmpty() ) {
429  // construct query
430  $dbr = wfGetDB( DB_REPLICA );
431  $fields = array_merge(
433  [ 'page_namespace', 'page_title' ]
434  );
435 
436  $varRes = $dbr->select( 'page',
437  $fields,
438  $linkBatch->constructSet( 'page', $dbr ),
439  __METHOD__
440  );
441 
442  $pagemap = [];
443  $linkRenderer = $this->parent->getLinkRenderer();
444 
445  // for each found variants, figure out link holders and replace
446  foreach ( $varRes as $s ) {
447  $variantTitle = Title::makeTitle( $s->page_namespace, $s->page_title );
448  $varPdbk = $variantTitle->getPrefixedDBkey();
449  $vardbk = $variantTitle->getDBkey();
450 
451  $holderKeys = [];
452  if ( isset( $variantMap[$varPdbk] ) ) {
453  $holderKeys = $variantMap[$varPdbk];
454  $linkCache->addGoodLinkObjFromRow( $variantTitle, $s );
455  $output->addLink( $variantTitle, $s->page_id );
456  }
457 
458  // loop over link holders
459  foreach ( $holderKeys as $key ) {
460  list( $ns, $index ) = explode( ':', $key, 2 );
461  $entry =& $this->internals[$ns][$index];
462  $pdbk = $entry['pdbk'];
463 
464  if ( !isset( $classes[$pdbk] ) || $classes[$pdbk] === 'new' ) {
465  // found link in some of the variants, replace the link holder data
466  $entry['title'] = $variantTitle;
467  $entry['pdbk'] = $varPdbk;
468 
469  // set pdbk and colour
470  $classes[$varPdbk] = $linkRenderer->getLinkClasses( $variantTitle );
471  $pagemap[$s->page_id] = $pdbk;
472  }
473  }
474 
475  // check if the object is a variant of a category
476  if ( isset( $categoryMap[$vardbk] ) ) {
477  list( $oldkey, $oldtitle ) = $categoryMap[$vardbk];
478  if ( !isset( $varCategories[$oldkey] ) && !$oldtitle->exists() ) {
479  $varCategories[$oldkey] = $vardbk;
480  }
481  }
482  }
483  $this->hookRunner->onGetLinkColours( $pagemap, $classes, $this->parent->getTitle() );
484 
485  // rebuild the categories in original order (if there are replacements)
486  if ( $varCategories !== [] ) {
487  $newCats = [];
488  $originalCats = $output->getCategories();
489  foreach ( $originalCats as $cat => $sortkey ) {
490  // make the replacement
491  $newCats[$varCategories[$cat] ?? $cat] = $sortkey;
492  }
493  $output->setCategoryLinks( $newCats );
494  }
495  }
496  }
497 
505  public function replaceText( $text ) {
506  return preg_replace_callback(
507  '/<!--(IW)?LINK\'" (.*?)-->/',
508  function ( $matches ) {
509  list( $unchanged, $isInterwiki, $key ) = $matches;
510 
511  if ( !$isInterwiki ) {
512  list( $ns, $index ) = explode( ':', $key, 2 );
513  return $this->internals[$ns][$index]['text'] ?? $unchanged;
514  } else {
515  return $this->interwikis[$key]['text'] ?? $unchanged;
516  }
517  },
518  $text
519  );
520  }
521 }
LinkHolderArray\replaceInterwiki
replaceInterwiki(&$text)
Replace interwiki links.
Definition: LinkHolderArray.php:317
LinkHolderArray\isBig
isBig()
Returns true if the memory requirements of this object are getting large.
Definition: LinkHolderArray.php:111
HtmlArmor
Marks HTML that shouldn't be escaped.
Definition: HtmlArmor.php:30
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:184
Linker\makeSelfLinkObj
static makeSelfLinkObj( $nt, $html='', $query='', $trail='', $prefix='')
Make appropriate markup for a link to the current article.
Definition: Linker.php:165
LinkHolderArray\doVariants
doVariants(&$classes)
Modify $this->internals and $classes according to language variant linking rules.
Definition: LinkHolderArray.php:347
Title\getPrefixedDBkey
getPrefixedDBkey()
Get the prefixed database key form.
Definition: Title.php:1926
LinkHolderArray\clear
clear()
Clear all stored link holders.
Definition: LinkHolderArray.php:120
LinkHolderArray\$languageConverter
ILanguageConverter $languageConverter
Current language converter.
Definition: LinkHolderArray.php:50
LinkHolderArray\$size
int $size
Definition: LinkHolderArray.php:39
LinkHolderArray\merge
merge( $other)
Merge another LinkHolderArray into this one.
Definition: LinkHolderArray.php:95
Parser\getTargetLanguage
getTargetLanguage()
Get the target language for the content being parsed.
Definition: Parser.php:1145
Title\isExternal
isExternal()
Is this Title interwiki?
Definition: Title.php:987
LinkHolderArray\replaceText
replaceText( $text)
Replace and link placeholders with plain text of links (not HTML-formatted).
Definition: LinkHolderArray.php:505
$res
$res
Definition: testCompression.php:57
LinkHolderArray\replace
replace(&$text)
Replace link placeholders with actual links, in the buffer.
Definition: LinkHolderArray.php:166
LinkCache\getSelectFields
static getSelectFields()
Fields that LinkCache needs to select.
Definition: LinkCache.php:353
$dbr
$dbr
Definition: testCompression.php:54
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:53
LinkHolderArray\__destruct
__destruct()
Reduce memory usage to reduce the impact of circular references.
Definition: LinkHolderArray.php:84
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:996
$wgLinkHolderBatchSize
$wgLinkHolderBatchSize
LinkHolderArray batch size For debugging.
Definition: DefaultSettings.php:2720
Title\getNamespace
getNamespace()
Get the namespace index, i.e.
Definition: Title.php:1097
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2226
$matches
$matches
Definition: NoLocalSettings.php:24
LinkHolderArray
Definition: LinkHolderArray.php:33
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:680
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
LinkHolderArray\replaceInternal
replaceInternal(&$text)
Replace internal links.
Definition: LinkHolderArray.php:176
Linker\splitTrail
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:1995
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:706
$s
foreach( $mmfl['setupFiles'] as $fileName) if( $queue) if(empty( $mmfl['quiet'])) $s
Definition: mergeMessageFileList.php:206
ILanguageConverter
The shared interface for all language converters.
Definition: ILanguageConverter.php:29
LinkHolderArray\$hookRunner
HookRunner $hookRunner
Definition: LinkHolderArray.php:55
LinkHolderArray\__construct
__construct(Parser $parent, ILanguageConverter $languageConverter=null, HookContainer $hookContainer=null)
Definition: LinkHolderArray.php:62
Parser
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition: Parser.php:89
Title
Represents a title within MediaWiki.
Definition: Title.php:49
LinkHolderArray\$parent
Parser $parent
Definition: LinkHolderArray.php:44
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:78
LinkHolderArray\$interwikis
array[] $interwikis
Definition: LinkHolderArray.php:37
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:45
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:556
LinkHolderArray\makeHolder
makeHolder(Title $nt, $text='', $trail='', $prefix='')
Make a link placeholder.
Definition: LinkHolderArray.php:138
LinkHolderArray\$internals
array[][] $internals
Definition: LinkHolderArray.php:35