3 * Holder of replacement pairs for wiki links
11 class LinkHolderArray
{
12 var $internals = array(), $interwikis = array();
15 protected $tempIdOffset;
17 function __construct( $parent ) {
18 $this->parent
= $parent;
22 * Reduce memory usage to reduce the impact of circular references
24 function __destruct() {
25 foreach ( $this as $name => $value ) {
26 unset( $this->$name );
31 * Don't serialize the parent object, it is big, and not needed when it is
32 * a parameter to mergeForeign(), which is the only application of
33 * serializing at present.
35 * Compact the titles, only serialize the text form.
39 foreach ( $this->internals
as &$nsLinks ) {
40 foreach ( $nsLinks as &$entry ) {
41 unset( $entry['title'] );
47 foreach ( $this->interwikis
as &$entry ) {
48 unset( $entry['title'] );
52 return array( 'internals', 'interwikis', 'size' );
56 * Recreate the Title objects
59 foreach ( $this->internals
as &$nsLinks ) {
60 foreach ( $nsLinks as &$entry ) {
61 $entry['title'] = Title
::newFromText( $entry['pdbk'] );
67 foreach ( $this->interwikis
as &$entry ) {
68 $entry['title'] = Title
::newFromText( $entry['pdbk'] );
74 * Merge another LinkHolderArray into this one
75 * @param $other LinkHolderArray
77 function merge( $other ) {
78 foreach ( $other->internals
as $ns => $entries ) {
79 $this->size +
= count( $entries );
80 if ( !isset( $this->internals
[$ns] ) ) {
81 $this->internals
[$ns] = $entries;
83 $this->internals
[$ns] +
= $entries;
86 $this->interwikis +
= $other->interwikis
;
90 * Merge a LinkHolderArray from another parser instance into this one. The
91 * keys will not be preserved. Any text which went with the old
92 * LinkHolderArray and needs to work with the new one should be passed in
93 * the $texts array. The strings in this array will have their link holders
94 * converted for use in the destination link holder. The resulting array of
95 * strings will be returned.
97 * @param $other LinkHolderArray
98 * @param $texts Array of strings
101 function mergeForeign( $other, $texts ) {
102 $this->tempIdOffset
= $idOffset = $this->parent
->nextLinkID();
105 # Renumber internal links
106 foreach ( $other->internals
as $ns => $nsLinks ) {
107 foreach ( $nsLinks as $key => $entry ) {
108 $newKey = $idOffset +
$key;
109 $this->internals
[$ns][$newKey] = $entry;
110 $maxId = $newKey > $maxId ?
$newKey : $maxId;
113 $texts = preg_replace_callback( '/(<!--LINK \d+:)(\d+)(-->)/',
114 array( $this, 'mergeForeignCallback' ), $texts );
116 # Renumber interwiki links
117 foreach ( $other->interwikis
as $key => $entry ) {
118 $newKey = $idOffset +
$key;
119 $this->interwikis
[$newKey] = $entry;
120 $maxId = $newKey > $maxId ?
$newKey : $maxId;
122 $texts = preg_replace_callback( '/(<!--IWLINK )(\d+)(-->)/',
123 array( $this, 'mergeForeignCallback' ), $texts );
125 # Set the parent link ID to be beyond the highest used ID
126 $this->parent
->setLinkID( $maxId +
1 );
127 $this->tempIdOffset
= null;
131 protected function mergeForeignCallback( $m ) {
132 return $m[1] . ( $m[2] +
$this->tempIdOffset
) . $m[3];
136 * Get a subset of the current LinkHolderArray which is sufficient to
137 * interpret the given text.
138 * @return LinkHolderArray
140 function getSubArray( $text ) {
141 $sub = new LinkHolderArray( $this->parent
);
145 while ( $pos < strlen( $text ) ) {
146 if ( !preg_match( '/<!--LINK (\d+):(\d+)-->/',
147 $text, $m, PREG_OFFSET_CAPTURE
, $pos ) )
153 $sub->internals
[$ns][$key] = $this->internals
[$ns][$key];
154 $pos = $m[0][1] +
strlen( $m[0][0] );
159 while ( $pos < strlen( $text ) ) {
160 if ( !preg_match( '/<!--IWLINK (\d+)-->/', $text, $m, PREG_OFFSET_CAPTURE
, $pos ) ) {
164 $sub->interwikis
[$key] = $this->interwikis
[$key];
165 $pos = $m[0][1] +
strlen( $m[0][0] );
171 * Returns true if the memory requirements of this object are getting large
175 global $wgLinkHolderBatchSize;
176 return $this->size
> $wgLinkHolderBatchSize;
180 * Clear all stored link holders.
181 * Make sure you don't have any text left using these link holders, before you call this
184 $this->internals
= array();
185 $this->interwikis
= array();
190 * Make a link placeholder. The text returned can be later resolved to a real link with
191 * replaceLinkHolders(). This is done for two reasons: firstly to avoid further
192 * parsing of interwiki links, and secondly to allow all existence checks and
193 * article length checks (for stub links) to be bundled into a single query.
198 function makeHolder( $nt, $text = '', $query = array(), $trail = '', $prefix = '' ) {
199 wfProfileIn( __METHOD__
);
200 if ( ! is_object($nt) ) {
202 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
204 # Separate the link trail from the rest of the link
205 list( $inside, $trail ) = Linker
::splitTrail( $trail );
209 'text' => $prefix.$text.$inside,
210 'pdbk' => $nt->getPrefixedDBkey(),
212 if ( $query !== array() ) {
213 $entry['query'] = $query;
216 if ( $nt->isExternal() ) {
217 // Use a globally unique ID to keep the objects mergable
218 $key = $this->parent
->nextLinkID();
219 $this->interwikis
[$key] = $entry;
220 $retVal = "<!--IWLINK $key-->{$trail}";
222 $key = $this->parent
->nextLinkID();
223 $ns = $nt->getNamespace();
224 $this->internals
[$ns][$key] = $entry;
225 $retVal = "<!--LINK $ns:$key-->{$trail}";
229 wfProfileOut( __METHOD__
);
234 * @todo FIXME: Update documentation. makeLinkObj() is deprecated.
235 * Replace <!--LINK--> link placeholders with actual links, in the buffer
236 * Placeholders created in Skin::makeLinkObj()
237 * Returns an array of link CSS classes, indexed by PDBK.
239 function replace( &$text ) {
240 wfProfileIn( __METHOD__
);
242 $colours = $this->replaceInternal( $text );
243 $this->replaceInterwiki( $text );
245 wfProfileOut( __METHOD__
);
250 * Replace internal links
252 protected function replaceInternal( &$text ) {
253 if ( !$this->internals
) {
257 wfProfileIn( __METHOD__
);
261 $linkCache = LinkCache
::singleton();
262 $output = $this->parent
->getOutput();
264 wfProfileIn( __METHOD__
.'-check' );
265 $dbr = wfGetDB( DB_SLAVE
);
266 $threshold = $this->parent
->getOptions()->getStubThreshold();
269 ksort( $this->internals
);
271 $linkcolour_ids = array();
275 foreach ( $this->internals
as $ns => $entries ) {
276 foreach ( $entries as $entry ) {
277 $title = $entry['title'];
278 $pdbk = $entry['pdbk'];
280 # Skip invalid entries.
281 # Result will be ugly, but prevents crash.
282 if ( is_null( $title ) ) {
286 # Check if it's a static known link, e.g. interwiki
287 if ( $title->isAlwaysKnown() ) {
288 $colours[$pdbk] = '';
289 } elseif ( $ns == NS_SPECIAL
) {
290 $colours[$pdbk] = 'new';
291 } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
292 $colours[$pdbk] = Linker
::getLinkColour( $title, $threshold );
293 $output->addLink( $title, $id );
294 $linkcolour_ids[$id] = $pdbk;
295 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
296 $colours[$pdbk] = 'new';
298 # Not in the link cache, add it to the query
299 $queries[$ns][] = $title->getDBkey();
305 foreach( $queries as $ns => $pages ){
306 $where[] = $dbr->makeList(
308 'page_namespace' => $ns,
309 'page_title' => $pages,
317 array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect', 'page_len', 'page_latest' ),
318 $dbr->makeList( $where, LIST_OR
),
322 # Fetch data and form into an associative array
323 # non-existent = broken
324 foreach ( $res as $s ) {
325 $title = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
326 $pdbk = $title->getPrefixedDBkey();
327 $linkCache->addGoodLinkObjFromRow( $title, $s );
328 $output->addLink( $title, $s->page_id
);
329 # @todo FIXME: Convoluted data flow
330 # The redirect status and length is passed to getLinkColour via the LinkCache
331 # Use formal parameters instead
332 $colours[$pdbk] = Linker
::getLinkColour( $title, $threshold );
333 //add id to the extension todolist
334 $linkcolour_ids[$s->page_id
] = $pdbk;
338 if ( count($linkcolour_ids) ) {
339 //pass an array of page_ids to an extension
340 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
342 wfProfileOut( __METHOD__
.'-check' );
344 # Do a second query for different language variants of links and categories
345 if($wgContLang->hasVariants()) {
346 $this->doVariants( $colours );
349 # Construct search and replace arrays
350 wfProfileIn( __METHOD__
.'-construct' );
351 $replacePairs = array();
352 foreach ( $this->internals
as $ns => $entries ) {
353 foreach ( $entries as $index => $entry ) {
354 $pdbk = $entry['pdbk'];
355 $title = $entry['title'];
356 $query = isset( $entry['query'] ) ?
$entry['query'] : array();
358 $searchkey = "<!--LINK $key-->";
359 $displayText = $entry['text'];
360 if ( $displayText === '' ) {
363 if ( !isset( $colours[$pdbk] ) ) {
364 $colours[$pdbk] = 'new';
367 if ( $colours[$pdbk] == 'new' ) {
368 $linkCache->addBadLinkObj( $title );
369 $output->addLink( $title, 0 );
370 $type = array( 'broken' );
372 if ( $colours[$pdbk] != '' ) {
373 $attribs['class'] = $colours[$pdbk];
375 $type = array( 'known', 'noclasses' );
377 $replacePairs[$searchkey] = Linker
::link( $title, $displayText,
378 $attribs, $query, $type );
381 $replacer = new HashtableReplacer( $replacePairs, 1 );
382 wfProfileOut( __METHOD__
.'-construct' );
385 wfProfileIn( __METHOD__
.'-replace' );
386 $text = preg_replace_callback(
387 '/(<!--LINK .*?-->)/',
391 wfProfileOut( __METHOD__
.'-replace' );
392 wfProfileOut( __METHOD__
);
396 * Replace interwiki links
398 protected function replaceInterwiki( &$text ) {
399 if ( empty( $this->interwikis
) ) {
403 wfProfileIn( __METHOD__
);
404 # Make interwiki link HTML
405 $output = $this->parent
->getOutput();
406 $replacePairs = array();
407 foreach( $this->interwikis
as $key => $link ) {
408 $replacePairs[$key] = Linker
::link( $link['title'], $link['text'] );
409 $output->addInterwikiLink( $link['title'] );
411 $replacer = new HashtableReplacer( $replacePairs, 1 );
413 $text = preg_replace_callback(
414 '/<!--IWLINK (.*?)-->/',
417 wfProfileOut( __METHOD__
);
421 * Modify $this->internals and $colours according to language variant linking rules
423 protected function doVariants( &$colours ) {
425 $linkBatch = new LinkBatch();
426 $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
427 $output = $this->parent
->getOutput();
428 $linkCache = LinkCache
::singleton();
429 $threshold = $this->parent
->getOptions()->getStubThreshold();
430 $titlesToBeConverted = '';
431 $titlesAttrs = array();
433 // Concatenate titles to a single string, thus we only need auto convert the
434 // single string to all variants. This would improve parser's performance
436 foreach ( $this->internals
as $ns => $entries ) {
437 foreach ( $entries as $index => $entry ) {
438 $pdbk = $entry['pdbk'];
439 // we only deal with new links (in its first query)
440 if ( !isset( $colours[$pdbk] ) ) {
441 $title = $entry['title'];
442 $titleText = $title->getText();
443 $titlesAttrs[] = array(
445 'key' => "$ns:$index",
446 'titleText' => $titleText,
448 // separate titles with \0 because it would never appears
450 $titlesToBeConverted .= $titleText . "\0";
455 // Now do the conversion and explode string to text of titles
456 $titlesAllVariants = $wgContLang->autoConvertToAllVariants( $titlesToBeConverted );
457 $allVariantsName = array_keys( $titlesAllVariants );
458 foreach ( $titlesAllVariants as &$titlesVariant ) {
459 $titlesVariant = explode( "\0", $titlesVariant );
461 $l = count( $titlesAttrs );
462 // Then add variants of links to link batch
463 for ( $i = 0; $i < $l; $i ++
) {
464 foreach ( $allVariantsName as $variantName ) {
465 $textVariant = $titlesAllVariants[$variantName][$i];
466 if ( $textVariant != $titlesAttrs[$i]['titleText'] ) {
467 $variantTitle = Title
::makeTitle( $titlesAttrs[$i]['ns'], $textVariant );
468 if( is_null( $variantTitle ) ) {
471 $linkBatch->addObj( $variantTitle );
472 $variantMap[$variantTitle->getPrefixedDBkey()][] = $titlesAttrs[$i]['key'];
477 // process categories, check if a category exists in some variant
478 $categoryMap = array(); // maps $category_variant => $category (dbkeys)
479 $varCategories = array(); // category replacements oldDBkey => newDBkey
480 foreach( $output->getCategoryLinks() as $category ){
481 $variants = $wgContLang->autoConvertToAllVariants( $category );
482 foreach($variants as $variant){
483 if($variant != $category){
484 $variantTitle = Title
::newFromDBkey( Title
::makeName(NS_CATEGORY
,$variant) );
485 if(is_null($variantTitle)) continue;
486 $linkBatch->addObj( $variantTitle );
487 $categoryMap[$variant] = $category;
493 if(!$linkBatch->isEmpty()){
495 $dbr = wfGetDB( DB_SLAVE
);
496 $varRes = $dbr->select( 'page',
497 array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect', 'page_len', 'page_latest' ),
498 $linkBatch->constructSet( 'page', $dbr ),
502 $linkcolour_ids = array();
504 // for each found variants, figure out link holders and replace
505 foreach ( $varRes as $s ) {
507 $variantTitle = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
508 $varPdbk = $variantTitle->getPrefixedDBkey();
509 $vardbk = $variantTitle->getDBkey();
511 $holderKeys = array();
512 if( isset( $variantMap[$varPdbk] ) ) {
513 $holderKeys = $variantMap[$varPdbk];
514 $linkCache->addGoodLinkObjFromRow( $variantTitle, $s );
515 $output->addLink( $variantTitle, $s->page_id
);
518 // loop over link holders
519 foreach( $holderKeys as $key ) {
520 list( $ns, $index ) = explode( ':', $key, 2 );
521 $entry =& $this->internals
[$ns][$index];
522 $pdbk = $entry['pdbk'];
524 if(!isset($colours[$pdbk])){
525 // found link in some of the variants, replace the link holder data
526 $entry['title'] = $variantTitle;
527 $entry['pdbk'] = $varPdbk;
529 // set pdbk and colour
530 # @todo FIXME: Convoluted data flow
531 # The redirect status and length is passed to getLinkColour via the LinkCache
532 # Use formal parameters instead
533 $colours[$varPdbk] = Linker
::getLinkColour( $variantTitle, $threshold );
534 $linkcolour_ids[$s->page_id
] = $pdbk;
538 // check if the object is a variant of a category
539 if(isset($categoryMap[$vardbk])){
540 $oldkey = $categoryMap[$vardbk];
541 if($oldkey != $vardbk)
542 $varCategories[$oldkey]=$vardbk;
545 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
547 // rebuild the categories in original order (if there are replacements)
548 if(count($varCategories)>0){
550 $originalCats = $output->getCategories();
551 foreach($originalCats as $cat => $sortkey){
552 // make the replacement
553 if( array_key_exists($cat,$varCategories) )
554 $newCats[$varCategories[$cat]] = $sortkey;
555 else $newCats[$cat] = $sortkey;
557 $output->setCategoryLinks($newCats);
563 * Replace <!--LINK--> link placeholders with plain text of links
564 * (not HTML-formatted).
566 * @param $text String
569 function replaceText( $text ) {
570 wfProfileIn( __METHOD__
);
572 $text = preg_replace_callback(
573 '/<!--(LINK|IWLINK) (.*?)-->/',
574 array( &$this, 'replaceTextCallback' ),
577 wfProfileOut( __METHOD__
);
582 * Callback for replaceText()
584 * @param $matches Array
588 function replaceTextCallback( $matches ) {
591 if( $type == 'LINK' ) {
592 list( $ns, $index ) = explode( ':', $key, 2 );
593 if( isset( $this->internals
[$ns][$index]['text'] ) ) {
594 return $this->internals
[$ns][$index]['text'];
596 } elseif( $type == 'IWLINK' ) {
597 if( isset( $this->interwikis
[$key]['text'] ) ) {
598 return $this->interwikis
[$key]['text'];