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.
38 foreach ( $this->internals
as $ns => &$nsLinks ) {
39 foreach ( $nsLinks as $key => &$entry ) {
40 unset( $entry['title'] );
46 foreach ( $this->interwikis
as $key => &$entry ) {
47 unset( $entry['title'] );
51 return array( 'internals', 'interwikis', 'size' );
55 * Recreate the Title objects
58 foreach ( $this->internals
as $ns => &$nsLinks ) {
59 foreach ( $nsLinks as $key => &$entry ) {
60 $entry['title'] = Title
::newFromText( $entry['pdbk'] );
66 foreach ( $this->interwikis
as $key => &$entry ) {
67 $entry['title'] = Title
::newFromText( $entry['pdbk'] );
73 * Merge another LinkHolderArray into this one
74 * @param $other LinkHolderArray
76 function merge( $other ) {
77 foreach ( $other->internals
as $ns => $entries ) {
78 $this->size +
= count( $entries );
79 if ( !isset( $this->internals
[$ns] ) ) {
80 $this->internals
[$ns] = $entries;
82 $this->internals
[$ns] +
= $entries;
85 $this->interwikis +
= $other->interwikis
;
89 * Merge a LinkHolderArray from another parser instance into this one. The
90 * keys will not be preserved. Any text which went with the old
91 * LinkHolderArray and needs to work with the new one should be passed in
92 * the $texts array. The strings in this array will have their link holders
93 * converted for use in the destination link holder. The resulting array of
94 * strings will be returned.
96 * @param $other LinkHolderArray
97 * @param $text Array of strings
100 function mergeForeign( $other, $texts ) {
101 $this->tempIdOffset
= $idOffset = $this->parent
->nextLinkID();
104 # Renumber internal links
105 foreach ( $other->internals
as $ns => $nsLinks ) {
106 foreach ( $nsLinks as $key => $entry ) {
107 $newKey = $idOffset +
$key;
108 $this->internals
[$ns][$newKey] = $entry;
109 $maxId = $newKey > $maxId ?
$newKey : $maxId;
112 $texts = preg_replace_callback( '/(<!--LINK \d+:)(\d+)(-->)/',
113 array( $this, 'mergeForeignCallback' ), $texts );
115 # Renumber interwiki links
116 foreach ( $other->interwikis
as $key => $entry ) {
117 $newKey = $idOffset +
$key;
118 $this->interwikis
[$newKey] = $entry;
119 $maxId = $newKey > $maxId ?
$newKey : $maxId;
121 $texts = preg_replace_callback( '/(<!--IWLINK )(\d+)(-->)/',
122 array( $this, 'mergeForeignCallback' ), $texts );
124 # Set the parent link ID to be beyond the highest used ID
125 $this->parent
->setLinkID( $maxId +
1 );
126 $this->tempIdOffset
= null;
130 protected function mergeForeignCallback( $m ) {
131 return $m[1] . ( $m[2] +
$this->tempIdOffset
) . $m[3];
135 * Get a subset of the current LinkHolderArray which is sufficient to
136 * interpret the given text.
138 function getSubArray( $text ) {
139 $sub = new LinkHolderArray( $this->parent
);
143 while ( $pos < strlen( $text ) ) {
144 if ( !preg_match( '/<!--LINK (\d+):(\d+)-->/',
145 $text, $m, PREG_OFFSET_CAPTURE
, $pos ) )
151 $sub->internals
[$ns][$key] = $this->internals
[$ns][$key];
152 $pos = $m[0][1] +
strlen( $m[0][0] );
157 while ( $pos < strlen( $text ) ) {
158 if ( !preg_match( '/<!--IWLINK (\d+)-->/', $text, $m, PREG_OFFSET_CAPTURE
, $pos ) ) {
162 $sub->interwikis
[$key] = $this->interwikis
[$key];
163 $pos = $m[0][1] +
strlen( $m[0][0] );
169 * Returns true if the memory requirements of this object are getting large
172 global $wgLinkHolderBatchSize;
173 return $this->size
> $wgLinkHolderBatchSize;
177 * Clear all stored link holders.
178 * Make sure you don't have any text left using these link holders, before you call this
181 $this->internals
= array();
182 $this->interwikis
= array();
187 * Make a link placeholder. The text returned can be later resolved to a real link with
188 * replaceLinkHolders(). This is done for two reasons: firstly to avoid further
189 * parsing of interwiki links, and secondly to allow all existence checks and
190 * article length checks (for stub links) to be bundled into a single query.
194 function makeHolder( $nt, $text = '', $query = array(), $trail = '', $prefix = '' ) {
195 wfProfileIn( __METHOD__
);
196 if ( ! is_object($nt) ) {
198 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
200 # Separate the link trail from the rest of the link
201 list( $inside, $trail ) = Linker
::splitTrail( $trail );
205 'text' => $prefix.$text.$inside,
206 'pdbk' => $nt->getPrefixedDBkey(),
208 if ( $query !== array() ) {
209 $entry['query'] = $query;
212 if ( $nt->isExternal() ) {
213 // Use a globally unique ID to keep the objects mergable
214 $key = $this->parent
->nextLinkID();
215 $this->interwikis
[$key] = $entry;
216 $retVal = "<!--IWLINK $key-->{$trail}";
218 $key = $this->parent
->nextLinkID();
219 $ns = $nt->getNamespace();
220 $this->internals
[$ns][$key] = $entry;
221 $retVal = "<!--LINK $ns:$key-->{$trail}";
225 wfProfileOut( __METHOD__
);
230 * FIXME: update documentation. makeLinkObj() is deprecated.
231 * Replace <!--LINK--> link placeholders with actual links, in the buffer
232 * Placeholders created in Skin::makeLinkObj()
233 * Returns an array of link CSS classes, indexed by PDBK.
235 function replace( &$text ) {
236 wfProfileIn( __METHOD__
);
238 $colours = $this->replaceInternal( $text );
239 $this->replaceInterwiki( $text );
241 wfProfileOut( __METHOD__
);
246 * Replace internal links
248 protected function replaceInternal( &$text ) {
249 if ( !$this->internals
) {
253 wfProfileIn( __METHOD__
);
257 $linkCache = LinkCache
::singleton();
258 $output = $this->parent
->getOutput();
260 wfProfileIn( __METHOD__
.'-check' );
261 $dbr = wfGetDB( DB_SLAVE
);
262 $page = $dbr->tableName( 'page' );
263 $threshold = $this->parent
->getOptions()->getStubThreshold();
266 ksort( $this->internals
);
268 $linkcolour_ids = array();
273 foreach ( $this->internals
as $ns => $entries ) {
274 foreach ( $entries as $entry ) {
275 $title = $entry['title'];
276 $pdbk = $entry['pdbk'];
278 # Skip invalid entries.
279 # Result will be ugly, but prevents crash.
280 if ( is_null( $title ) ) {
284 # Check if it's a static known link, e.g. interwiki
285 if ( $title->isAlwaysKnown() ) {
286 $colours[$pdbk] = '';
287 } elseif ( $ns == NS_SPECIAL
) {
288 $colours[$pdbk] = 'new';
289 } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
290 $colours[$pdbk] = Linker
::getLinkColour( $title, $threshold );
291 $output->addLink( $title, $id );
292 $linkcolour_ids[$id] = $pdbk;
293 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
294 $colours[$pdbk] = 'new';
296 # Not in the link cache, add it to the query
297 if ( !isset( $current ) ) {
299 $query = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len, page_latest";
300 $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN(";
301 } elseif ( $current != $ns ) {
303 $query .= ")) OR (page_namespace=$ns AND page_title IN(";
308 $query .= $dbr->addQuotes( $title->getDBkey() );
315 $res = $dbr->query( $query, __METHOD__
);
317 # Fetch data and form into an associative array
318 # non-existent = broken
319 foreach ( $res as $s ) {
320 $title = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
321 $pdbk = $title->getPrefixedDBkey();
322 $linkCache->addGoodLinkObj( $s->page_id
, $title, $s->page_len
, $s->page_is_redirect
, $s->page_latest
);
323 $output->addLink( $title, $s->page_id
);
324 # FIXME: convoluted data flow
325 # The redirect status and length is passed to getLinkColour via the LinkCache
326 # Use formal parameters instead
327 $colours[$pdbk] = Linker
::getLinkColour( $title, $threshold );
328 //add id to the extension todolist
329 $linkcolour_ids[$s->page_id
] = $pdbk;
333 if ( count($linkcolour_ids) ) {
334 //pass an array of page_ids to an extension
335 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
337 wfProfileOut( __METHOD__
.'-check' );
339 # Do a second query for different language variants of links and categories
340 if($wgContLang->hasVariants()) {
341 $this->doVariants( $colours );
344 # Construct search and replace arrays
345 wfProfileIn( __METHOD__
.'-construct' );
346 $replacePairs = array();
347 foreach ( $this->internals
as $ns => $entries ) {
348 foreach ( $entries as $index => $entry ) {
349 $pdbk = $entry['pdbk'];
350 $title = $entry['title'];
351 $query = isset( $entry['query'] ) ?
$entry['query'] : array();
353 $searchkey = "<!--LINK $key-->";
354 $displayText = $entry['text'];
355 if ( $displayText === '' ) {
358 if ( !isset( $colours[$pdbk] ) ) {
359 $colours[$pdbk] = 'new';
362 if ( $colours[$pdbk] == 'new' ) {
363 $linkCache->addBadLinkObj( $title );
364 $output->addLink( $title, 0 );
365 $type = array( 'broken' );
367 if ( $colours[$pdbk] != '' ) {
368 $attribs['class'] = $colours[$pdbk];
370 $type = array( 'known', 'noclasses' );
372 $replacePairs[$searchkey] = Linker
::link( $title, $displayText,
373 $attribs, $query, $type );
376 $replacer = new HashtableReplacer( $replacePairs, 1 );
377 wfProfileOut( __METHOD__
.'-construct' );
380 wfProfileIn( __METHOD__
.'-replace' );
381 $text = preg_replace_callback(
382 '/(<!--LINK .*?-->)/',
386 wfProfileOut( __METHOD__
.'-replace' );
387 wfProfileOut( __METHOD__
);
391 * Replace interwiki links
393 protected function replaceInterwiki( &$text ) {
394 if ( empty( $this->interwikis
) ) {
398 wfProfileIn( __METHOD__
);
399 # Make interwiki link HTML
400 $output = $this->parent
->getOutput();
401 $replacePairs = array();
402 foreach( $this->interwikis
as $key => $link ) {
403 $replacePairs[$key] = Linker
::link( $link['title'], $link['text'] );
404 $output->addInterwikiLink( $link['title'] );
406 $replacer = new HashtableReplacer( $replacePairs, 1 );
408 $text = preg_replace_callback(
409 '/<!--IWLINK (.*?)-->/',
412 wfProfileOut( __METHOD__
);
416 * Modify $this->internals and $colours according to language variant linking rules
418 protected function doVariants( &$colours ) {
420 $linkBatch = new LinkBatch();
421 $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
422 $output = $this->parent
->getOutput();
423 $linkCache = LinkCache
::singleton();
424 $threshold = $this->parent
->getOptions()->getStubThreshold();
425 $titlesToBeConverted = '';
426 $titlesAttrs = array();
428 // Concatenate titles to a single string, thus we only need auto convert the
429 // single string to all variants. This would improve parser's performance
431 foreach ( $this->internals
as $ns => $entries ) {
432 foreach ( $entries as $index => $entry ) {
433 $pdbk = $entry['pdbk'];
434 // we only deal with new links (in its first query)
435 if ( !isset( $colours[$pdbk] ) ) {
436 $title = $entry['title'];
437 $titleText = $title->getText();
438 $titlesAttrs[] = array(
440 'key' => "$ns:$index",
441 'titleText' => $titleText,
443 // separate titles with \0 because it would never appears
445 $titlesToBeConverted .= $titleText . "\0";
450 // Now do the conversion and explode string to text of titles
451 $titlesAllVariants = $wgContLang->autoConvertToAllVariants( $titlesToBeConverted );
452 $allVariantsName = array_keys( $titlesAllVariants );
453 foreach ( $titlesAllVariants as &$titlesVariant ) {
454 $titlesVariant = explode( "\0", $titlesVariant );
456 $l = count( $titlesAttrs );
457 // Then add variants of links to link batch
458 for ( $i = 0; $i < $l; $i ++
) {
459 foreach ( $allVariantsName as $variantName ) {
460 $textVariant = $titlesAllVariants[$variantName][$i];
461 if ( $textVariant != $titlesAttrs[$i]['titleText'] ) {
462 $variantTitle = Title
::makeTitle( $titlesAttrs[$i]['ns'], $textVariant );
463 if( is_null( $variantTitle ) ) {
466 $linkBatch->addObj( $variantTitle );
467 $variantMap[$variantTitle->getPrefixedDBkey()][] = $titlesAttrs[$i]['key'];
472 // process categories, check if a category exists in some variant
473 $categoryMap = array(); // maps $category_variant => $category (dbkeys)
474 $varCategories = array(); // category replacements oldDBkey => newDBkey
475 foreach( $output->getCategoryLinks() as $category ){
476 $variants = $wgContLang->autoConvertToAllVariants( $category );
477 foreach($variants as $variant){
478 if($variant != $category){
479 $variantTitle = Title
::newFromDBkey( Title
::makeName(NS_CATEGORY
,$variant) );
480 if(is_null($variantTitle)) continue;
481 $linkBatch->addObj( $variantTitle );
482 $categoryMap[$variant] = $category;
488 if(!$linkBatch->isEmpty()){
490 $dbr = wfGetDB( DB_SLAVE
);
491 $varRes = $dbr->select( 'page',
492 array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect', 'page_len' ),
493 $linkBatch->constructSet( 'page', $dbr ),
497 $linkcolour_ids = array();
499 // for each found variants, figure out link holders and replace
500 foreach ( $varRes as $s ) {
502 $variantTitle = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
503 $varPdbk = $variantTitle->getPrefixedDBkey();
504 $vardbk = $variantTitle->getDBkey();
506 $holderKeys = array();
507 if( isset( $variantMap[$varPdbk] ) ) {
508 $holderKeys = $variantMap[$varPdbk];
509 $linkCache->addGoodLinkObj( $s->page_id
, $variantTitle, $s->page_len
, $s->page_is_redirect
);
510 $output->addLink( $variantTitle, $s->page_id
);
513 // loop over link holders
514 foreach( $holderKeys as $key ) {
515 list( $ns, $index ) = explode( ':', $key, 2 );
516 $entry =& $this->internals
[$ns][$index];
517 $pdbk = $entry['pdbk'];
519 if(!isset($colours[$pdbk])){
520 // found link in some of the variants, replace the link holder data
521 $entry['title'] = $variantTitle;
522 $entry['pdbk'] = $varPdbk;
524 // set pdbk and colour
525 # FIXME: convoluted data flow
526 # The redirect status and length is passed to getLinkColour via the LinkCache
527 # Use formal parameters instead
528 $colours[$varPdbk] = Linker
::getLinkColour( $variantTitle, $threshold );
529 $linkcolour_ids[$s->page_id
] = $pdbk;
533 // check if the object is a variant of a category
534 if(isset($categoryMap[$vardbk])){
535 $oldkey = $categoryMap[$vardbk];
536 if($oldkey != $vardbk)
537 $varCategories[$oldkey]=$vardbk;
540 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
542 // rebuild the categories in original order (if there are replacements)
543 if(count($varCategories)>0){
545 $originalCats = $output->getCategories();
546 foreach($originalCats as $cat => $sortkey){
547 // make the replacement
548 if( array_key_exists($cat,$varCategories) )
549 $newCats[$varCategories[$cat]] = $sortkey;
550 else $newCats[$cat] = $sortkey;
552 $output->setCategoryLinks($newCats);
558 * Replace <!--LINK--> link placeholders with plain text of links
559 * (not HTML-formatted).
561 * @param $text String
564 function replaceText( $text ) {
565 wfProfileIn( __METHOD__
);
567 $text = preg_replace_callback(
568 '/<!--(LINK|IWLINK) (.*?)-->/',
569 array( &$this, 'replaceTextCallback' ),
572 wfProfileOut( __METHOD__
);
577 * Callback for replaceText()
579 * @param $matches Array
583 function replaceTextCallback( $matches ) {
586 if( $type == 'LINK' ) {
587 list( $ns, $index ) = explode( ':', $key, 2 );
588 if( isset( $this->internals
[$ns][$index]['text'] ) ) {
589 return $this->internals
[$ns][$index]['text'];
591 } elseif( $type == 'IWLINK' ) {
592 if( isset( $this->interwikis
[$key]['text'] ) ) {
593 return $this->interwikis
[$key]['text'];