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.
36 return array( 'internals', 'interwikis', 'size' );
40 * Merge another LinkHolderArray into this one
41 * @param $other LinkHolderArray
43 function merge( $other ) {
44 foreach ( $other->internals
as $ns => $entries ) {
45 $this->size +
= count( $entries );
46 if ( !isset( $this->internals
[$ns] ) ) {
47 $this->internals
[$ns] = $entries;
49 $this->internals
[$ns] +
= $entries;
52 $this->interwikis +
= $other->interwikis
;
56 * Merge a LinkHolderArray from another parser instance into this one. The
57 * keys will not be preserved. Any text which went with the old
58 * LinkHolderArray and needs to work with the new one should be passed in
59 * the $texts array. The strings in this array will have their link holders
60 * converted for use in the destination link holder. The resulting array of
61 * strings will be returned.
63 * @param $other LinkHolderArray
64 * @param $text Array of strings
67 function mergeForeign( $other, $texts ) {
68 $this->tempIdOffset
= $idOffset = $this->parent
->nextLinkID();
71 # Renumber internal links
72 foreach ( $other->internals
as $ns => $nsLinks ) {
73 foreach ( $nsLinks as $key => $entry ) {
74 $newKey = $idOffset +
$key;
75 $this->internals
[$ns][$newKey] = $entry;
76 $maxId = $newKey > $maxId ?
$newKey : $maxId;
79 $texts = preg_replace_callback( '(<!--LINK \d+:)(\d+)(-->)',
80 array( $this, 'mergeForeignCallback' ), $texts );
82 # Renumber interwiki links
83 foreach ( $links['interwiki'] as $key => $entry ) {
84 $newKey = $idOffset +
$key;
85 $this->interwikis
[$newKey] = $entry;
86 $maxId = $newKey > $maxId ?
$newKey : $maxId;
89 $texts = preg_replace_callback( '(<!--IWLINK )(\d+)(-->)',
90 array( $this, 'mergeForeignCallback' ), $texts );
92 # Set the parent link ID to be the highest used ID
93 $this->parent
->setLinkID( $maxId );
94 $this->tempIdOffset
= null;
97 protected function mergeForeignCallback( $m ) {
98 return $m[1] . ( $m[2] +
$this->tempIdOffset
) . $m[3];
102 * Get a subset of the current LinkHolderArray which is sufficient to
103 * interpret the given text.
105 function getSubArray( $text ) {
106 $sub = new LinkHolderArray( $this->parent
);
110 while ( $pos < strlen( $text ) ) {
111 if ( !preg_match( '/<!--LINK (\d+):(\d+)-->/',
112 $text, $m, PREG_OFFSET_CAPTURE
, $pos ) )
118 $sub->internals
[$ns][$key] = $this->internals
[$ns][$key];
119 $pos = $m[0][1] +
strlen( $m[0][0] );
124 while ( $pos < strlen( $text ) ) {
125 if ( !preg_match( '/<!--IWLINK (\d+)-->/', $text, $m, PREG_OFFSET_CAPTURE
, $pos ) ) {
129 $sub->interwikis
[$key] = $this->interwikis
[$key];
130 $pos = $m[0][1] +
strlen( $m[0][0] );
136 * Returns true if the memory requirements of this object are getting large
139 global $wgLinkHolderBatchSize;
140 return $this->size
> $wgLinkHolderBatchSize;
144 * Clear all stored link holders.
145 * Make sure you don't have any text left using these link holders, before you call this
148 $this->internals
= array();
149 $this->interwikis
= array();
154 * Make a link placeholder. The text returned can be later resolved to a real link with
155 * replaceLinkHolders(). This is done for two reasons: firstly to avoid further
156 * parsing of interwiki links, and secondly to allow all existence checks and
157 * article length checks (for stub links) to be bundled into a single query.
161 function makeHolder( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
162 wfProfileIn( __METHOD__
);
163 if ( ! is_object($nt) ) {
165 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
167 # Separate the link trail from the rest of the link
168 list( $inside, $trail ) = Linker
::splitTrail( $trail );
172 'text' => $prefix.$text.$inside,
173 'pdbk' => $nt->getPrefixedDBkey(),
175 if ( $query !== '' ) {
176 $entry['query'] = $query;
179 if ( $nt->isExternal() ) {
180 // Use a globally unique ID to keep the objects mergable
181 $key = $this->parent
->nextLinkID();
182 $this->interwikis
[$key] = $entry;
183 $retVal = "<!--IWLINK $key-->{$trail}";
185 $key = $this->parent
->nextLinkID();
186 $ns = $nt->getNamespace();
187 $this->internals
[$ns][$key] = $entry;
188 $retVal = "<!--LINK $ns:$key-->{$trail}";
192 wfProfileOut( __METHOD__
);
197 * FIXME: update documentation. makeLinkObj() is deprecated.
198 * Replace <!--LINK--> link placeholders with actual links, in the buffer
199 * Placeholders created in Skin::makeLinkObj()
200 * Returns an array of link CSS classes, indexed by PDBK.
202 function replace( &$text ) {
203 wfProfileIn( __METHOD__
);
205 $colours = $this->replaceInternal( $text );
206 $this->replaceInterwiki( $text );
208 wfProfileOut( __METHOD__
);
213 * Replace internal links
215 protected function replaceInternal( &$text ) {
216 if ( !$this->internals
) {
220 wfProfileIn( __METHOD__
);
224 $sk = $this->parent
->getOptions()->getSkin( $this->parent
->mTitle
);
225 $linkCache = LinkCache
::singleton();
226 $output = $this->parent
->getOutput();
228 wfProfileIn( __METHOD__
.'-check' );
229 $dbr = wfGetDB( DB_SLAVE
);
230 $page = $dbr->tableName( 'page' );
231 $threshold = $this->parent
->getOptions()->getStubThreshold();
234 ksort( $this->internals
);
236 $linkcolour_ids = array();
241 foreach ( $this->internals
as $ns => $entries ) {
242 foreach ( $entries as $entry ) {
243 $title = $entry['title'];
244 $pdbk = $entry['pdbk'];
246 # Skip invalid entries.
247 # Result will be ugly, but prevents crash.
248 if ( is_null( $title ) ) {
252 # Check if it's a static known link, e.g. interwiki
253 if ( $title->isAlwaysKnown() ) {
254 $colours[$pdbk] = '';
255 } elseif ( $ns == NS_SPECIAL
) {
256 $colours[$pdbk] = 'new';
257 } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
258 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
259 $output->addLink( $title, $id );
260 $linkcolour_ids[$id] = $pdbk;
261 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
262 $colours[$pdbk] = 'new';
264 # Not in the link cache, add it to the query
265 if ( !isset( $current ) ) {
267 $query = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len, page_latest";
268 $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN(";
269 } elseif ( $current != $ns ) {
271 $query .= ")) OR (page_namespace=$ns AND page_title IN(";
276 $query .= $dbr->addQuotes( $title->getDBkey() );
283 $res = $dbr->query( $query, __METHOD__
);
285 # Fetch data and form into an associative array
286 # non-existent = broken
287 foreach ( $res as $s ) {
288 $title = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
289 $pdbk = $title->getPrefixedDBkey();
290 $linkCache->addGoodLinkObj( $s->page_id
, $title, $s->page_len
, $s->page_is_redirect
, $s->page_latest
);
291 $output->addLink( $title, $s->page_id
);
292 # FIXME: convoluted data flow
293 # The redirect status and length is passed to getLinkColour via the LinkCache
294 # Use formal parameters instead
295 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
296 //add id to the extension todolist
297 $linkcolour_ids[$s->page_id
] = $pdbk;
301 if ( count($linkcolour_ids) ) {
302 //pass an array of page_ids to an extension
303 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
305 wfProfileOut( __METHOD__
.'-check' );
307 # Do a second query for different language variants of links and categories
308 if($wgContLang->hasVariants()) {
309 $this->doVariants( $colours );
312 # Construct search and replace arrays
313 wfProfileIn( __METHOD__
.'-construct' );
314 $replacePairs = array();
315 foreach ( $this->internals
as $ns => $entries ) {
316 foreach ( $entries as $index => $entry ) {
317 $pdbk = $entry['pdbk'];
318 $title = $entry['title'];
319 $query = isset( $entry['query'] ) ?
$entry['query'] : '';
321 $searchkey = "<!--LINK $key-->";
322 if ( !isset( $colours[$pdbk] ) ||
$colours[$pdbk] == 'new' ) {
323 $linkCache->addBadLinkObj( $title );
324 $colours[$pdbk] = 'new';
325 $output->addLink( $title, 0 );
326 // FIXME: replace deprecated makeBrokenLinkObj() by link()
327 $replacePairs[$searchkey] = $sk->makeBrokenLinkObj( $title,
331 // FIXME: replace deprecated makeColouredLinkObj() by link()
332 $replacePairs[$searchkey] = $sk->makeColouredLinkObj( $title, $colours[$pdbk],
338 $replacer = new HashtableReplacer( $replacePairs, 1 );
339 wfProfileOut( __METHOD__
.'-construct' );
342 wfProfileIn( __METHOD__
.'-replace' );
343 $text = preg_replace_callback(
344 '/(<!--LINK .*?-->)/',
348 wfProfileOut( __METHOD__
.'-replace' );
349 wfProfileOut( __METHOD__
);
353 * Replace interwiki links
355 protected function replaceInterwiki( &$text ) {
356 if ( empty( $this->interwikis
) ) {
360 wfProfileIn( __METHOD__
);
361 # Make interwiki link HTML
362 $sk = $this->parent
->getOptions()->getSkin( $this->parent
->mTitle
);
363 $output = $this->parent
->getOutput();
364 $replacePairs = array();
365 foreach( $this->interwikis
as $key => $link ) {
366 $replacePairs[$key] = $sk->link( $link['title'], $link['text'] );
367 $output->addInterwikiLink( $link['title'] );
369 $replacer = new HashtableReplacer( $replacePairs, 1 );
371 $text = preg_replace_callback(
372 '/<!--IWLINK (.*?)-->/',
375 wfProfileOut( __METHOD__
);
379 * Modify $this->internals and $colours according to language variant linking rules
381 protected function doVariants( &$colours ) {
383 $linkBatch = new LinkBatch();
384 $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
385 $output = $this->parent
->getOutput();
386 $linkCache = LinkCache
::singleton();
387 $sk = $this->parent
->getOptions()->getSkin( $this->parent
->mTitle
);
388 $threshold = $this->parent
->getOptions()->getStubThreshold();
389 $titlesToBeConverted = '';
390 $titlesAttrs = array();
392 // Concatenate titles to a single string, thus we only need auto convert the
393 // single string to all variants. This would improve parser's performance
395 foreach ( $this->internals
as $ns => $entries ) {
396 foreach ( $entries as $index => $entry ) {
397 $pdbk = $entry['pdbk'];
398 // we only deal with new links (in its first query)
399 if ( !isset( $colours[$pdbk] ) ) {
400 $title = $entry['title'];
401 $titleText = $title->getText();
402 $titlesAttrs[] = array(
404 'key' => "$ns:$index",
405 'titleText' => $titleText,
407 // separate titles with \0 because it would never appears
409 $titlesToBeConverted .= $titleText . "\0";
414 // Now do the conversion and explode string to text of titles
415 $titlesAllVariants = $wgContLang->autoConvertToAllVariants( $titlesToBeConverted );
416 $allVariantsName = array_keys( $titlesAllVariants );
417 foreach ( $titlesAllVariants as &$titlesVariant ) {
418 $titlesVariant = explode( "\0", $titlesVariant );
420 $l = count( $titlesAttrs );
421 // Then add variants of links to link batch
422 for ( $i = 0; $i < $l; $i ++
) {
423 foreach ( $allVariantsName as $variantName ) {
424 $textVariant = $titlesAllVariants[$variantName][$i];
425 extract( $titlesAttrs[$i] );
426 if($textVariant != $titleText){
427 $variantTitle = Title
::makeTitle( $ns, $textVariant );
428 if( is_null( $variantTitle ) ) {
431 $linkBatch->addObj( $variantTitle );
432 $variantMap[$variantTitle->getPrefixedDBkey()][] = $key;
437 // process categories, check if a category exists in some variant
438 $categoryMap = array(); // maps $category_variant => $category (dbkeys)
439 $varCategories = array(); // category replacements oldDBkey => newDBkey
440 foreach( $output->getCategoryLinks() as $category ){
441 $variants = $wgContLang->autoConvertToAllVariants( $category );
442 foreach($variants as $variant){
443 if($variant != $category){
444 $variantTitle = Title
::newFromDBkey( Title
::makeName(NS_CATEGORY
,$variant) );
445 if(is_null($variantTitle)) continue;
446 $linkBatch->addObj( $variantTitle );
447 $categoryMap[$variant] = $category;
453 if(!$linkBatch->isEmpty()){
455 $dbr = wfGetDB( DB_SLAVE
);
456 $varRes = $dbr->select( 'page',
457 array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect', 'page_len' ),
458 $linkBatch->constructSet( 'page', $dbr ),
462 $linkcolour_ids = array();
464 // for each found variants, figure out link holders and replace
465 foreach ( $varRes as $s ) {
467 $variantTitle = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
468 $varPdbk = $variantTitle->getPrefixedDBkey();
469 $vardbk = $variantTitle->getDBkey();
471 $holderKeys = array();
472 if( isset( $variantMap[$varPdbk] ) ) {
473 $holderKeys = $variantMap[$varPdbk];
474 $linkCache->addGoodLinkObj( $s->page_id
, $variantTitle, $s->page_len
, $s->page_is_redirect
);
475 $output->addLink( $variantTitle, $s->page_id
);
478 // loop over link holders
479 foreach( $holderKeys as $key ) {
480 list( $ns, $index ) = explode( ':', $key, 2 );
481 $entry =& $this->internals
[$ns][$index];
482 $pdbk = $entry['pdbk'];
484 if(!isset($colours[$pdbk])){
485 // found link in some of the variants, replace the link holder data
486 $entry['title'] = $variantTitle;
487 $entry['pdbk'] = $varPdbk;
489 // set pdbk and colour
490 # FIXME: convoluted data flow
491 # The redirect status and length is passed to getLinkColour via the LinkCache
492 # Use formal parameters instead
493 $colours[$varPdbk] = $sk->getLinkColour( $variantTitle, $threshold );
494 $linkcolour_ids[$s->page_id
] = $pdbk;
498 // check if the object is a variant of a category
499 if(isset($categoryMap[$vardbk])){
500 $oldkey = $categoryMap[$vardbk];
501 if($oldkey != $vardbk)
502 $varCategories[$oldkey]=$vardbk;
505 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
507 // rebuild the categories in original order (if there are replacements)
508 if(count($varCategories)>0){
510 $originalCats = $output->getCategories();
511 foreach($originalCats as $cat => $sortkey){
512 // make the replacement
513 if( array_key_exists($cat,$varCategories) )
514 $newCats[$varCategories[$cat]] = $sortkey;
515 else $newCats[$cat] = $sortkey;
517 $output->setCategoryLinks($newCats);
523 * Replace <!--LINK--> link placeholders with plain text of links
524 * (not HTML-formatted).
526 * @param $text String
529 function replaceText( $text ) {
530 wfProfileIn( __METHOD__
);
532 $text = preg_replace_callback(
533 '/<!--(LINK|IWLINK) (.*?)-->/',
534 array( &$this, 'replaceTextCallback' ),
537 wfProfileOut( __METHOD__
);
542 * Callback for replaceText()
544 * @param $matches Array
548 function replaceTextCallback( $matches ) {
551 if( $type == 'LINK' ) {
552 list( $ns, $index ) = explode( ':', $key, 2 );
553 if( isset( $this->internals
[$ns][$index]['text'] ) ) {
554 return $this->internals
[$ns][$index]['text'];
556 } elseif( $type == 'IWLINK' ) {
557 if( isset( $this->interwikis
[$key]['text'] ) ) {
558 return $this->interwikis
[$key]['text'];