3 * Holder of replacement pairs for wiki links
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
27 class LinkHolderArray
{
28 var $internals = array(), $interwikis = array();
31 protected $tempIdOffset;
33 function __construct( $parent ) {
34 $this->parent
= $parent;
38 * Reduce memory usage to reduce the impact of circular references
40 function __destruct() {
41 foreach ( $this as $name => $value ) {
42 unset( $this->$name );
47 * Don't serialize the parent object, it is big, and not needed when it is
48 * a parameter to mergeForeign(), which is the only application of
49 * serializing at present.
51 * Compact the titles, only serialize the text form.
55 foreach ( $this->internals
as &$nsLinks ) {
56 foreach ( $nsLinks as &$entry ) {
57 unset( $entry['title'] );
63 foreach ( $this->interwikis
as &$entry ) {
64 unset( $entry['title'] );
68 return array( 'internals', 'interwikis', 'size' );
72 * Recreate the Title objects
75 foreach ( $this->internals
as &$nsLinks ) {
76 foreach ( $nsLinks as &$entry ) {
77 $entry['title'] = Title
::newFromText( $entry['pdbk'] );
83 foreach ( $this->interwikis
as &$entry ) {
84 $entry['title'] = Title
::newFromText( $entry['pdbk'] );
90 * Merge another LinkHolderArray into this one
91 * @param $other LinkHolderArray
93 function merge( $other ) {
94 foreach ( $other->internals
as $ns => $entries ) {
95 $this->size +
= count( $entries );
96 if ( !isset( $this->internals
[$ns] ) ) {
97 $this->internals
[$ns] = $entries;
99 $this->internals
[$ns] +
= $entries;
102 $this->interwikis +
= $other->interwikis
;
106 * Merge a LinkHolderArray from another parser instance into this one. The
107 * keys will not be preserved. Any text which went with the old
108 * LinkHolderArray and needs to work with the new one should be passed in
109 * the $texts array. The strings in this array will have their link holders
110 * converted for use in the destination link holder. The resulting array of
111 * strings will be returned.
113 * @param $other LinkHolderArray
114 * @param $texts Array of strings
117 function mergeForeign( $other, $texts ) {
118 $this->tempIdOffset
= $idOffset = $this->parent
->nextLinkID();
121 # Renumber internal links
122 foreach ( $other->internals
as $ns => $nsLinks ) {
123 foreach ( $nsLinks as $key => $entry ) {
124 $newKey = $idOffset +
$key;
125 $this->internals
[$ns][$newKey] = $entry;
126 $maxId = $newKey > $maxId ?
$newKey : $maxId;
129 $texts = preg_replace_callback( '/(<!--LINK \d+:)(\d+)(-->)/',
130 array( $this, 'mergeForeignCallback' ), $texts );
132 # Renumber interwiki links
133 foreach ( $other->interwikis
as $key => $entry ) {
134 $newKey = $idOffset +
$key;
135 $this->interwikis
[$newKey] = $entry;
136 $maxId = $newKey > $maxId ?
$newKey : $maxId;
138 $texts = preg_replace_callback( '/(<!--IWLINK )(\d+)(-->)/',
139 array( $this, 'mergeForeignCallback' ), $texts );
141 # Set the parent link ID to be beyond the highest used ID
142 $this->parent
->setLinkID( $maxId +
1 );
143 $this->tempIdOffset
= null;
147 protected function mergeForeignCallback( $m ) {
148 return $m[1] . ( $m[2] +
$this->tempIdOffset
) . $m[3];
152 * Get a subset of the current LinkHolderArray which is sufficient to
153 * interpret the given text.
154 * @return LinkHolderArray
156 function getSubArray( $text ) {
157 $sub = new LinkHolderArray( $this->parent
);
161 while ( $pos < strlen( $text ) ) {
162 if ( !preg_match( '/<!--LINK (\d+):(\d+)-->/',
163 $text, $m, PREG_OFFSET_CAPTURE
, $pos ) )
169 $sub->internals
[$ns][$key] = $this->internals
[$ns][$key];
170 $pos = $m[0][1] +
strlen( $m[0][0] );
175 while ( $pos < strlen( $text ) ) {
176 if ( !preg_match( '/<!--IWLINK (\d+)-->/', $text, $m, PREG_OFFSET_CAPTURE
, $pos ) ) {
180 $sub->interwikis
[$key] = $this->interwikis
[$key];
181 $pos = $m[0][1] +
strlen( $m[0][0] );
187 * Returns true if the memory requirements of this object are getting large
191 global $wgLinkHolderBatchSize;
192 return $this->size
> $wgLinkHolderBatchSize;
196 * Clear all stored link holders.
197 * Make sure you don't have any text left using these link holders, before you call this
200 $this->internals
= array();
201 $this->interwikis
= array();
206 * Make a link placeholder. The text returned can be later resolved to a real link with
207 * replaceLinkHolders(). This is done for two reasons: firstly to avoid further
208 * parsing of interwiki links, and secondly to allow all existence checks and
209 * article length checks (for stub links) to be bundled into a single query.
214 function makeHolder( $nt, $text = '', $query = array(), $trail = '', $prefix = '' ) {
215 wfProfileIn( __METHOD__
);
216 if ( ! is_object($nt) ) {
218 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
220 # Separate the link trail from the rest of the link
221 list( $inside, $trail ) = Linker
::splitTrail( $trail );
225 'text' => $prefix.$text.$inside,
226 'pdbk' => $nt->getPrefixedDBkey(),
228 if ( $query !== array() ) {
229 $entry['query'] = $query;
232 if ( $nt->isExternal() ) {
233 // Use a globally unique ID to keep the objects mergable
234 $key = $this->parent
->nextLinkID();
235 $this->interwikis
[$key] = $entry;
236 $retVal = "<!--IWLINK $key-->{$trail}";
238 $key = $this->parent
->nextLinkID();
239 $ns = $nt->getNamespace();
240 $this->internals
[$ns][$key] = $entry;
241 $retVal = "<!--LINK $ns:$key-->{$trail}";
245 wfProfileOut( __METHOD__
);
250 * @todo FIXME: Update documentation. makeLinkObj() is deprecated.
251 * Replace <!--LINK--> link placeholders with actual links, in the buffer
252 * Placeholders created in Skin::makeLinkObj()
253 * Returns an array of link CSS classes, indexed by PDBK.
255 function replace( &$text ) {
256 wfProfileIn( __METHOD__
);
258 $colours = $this->replaceInternal( $text );
259 $this->replaceInterwiki( $text );
261 wfProfileOut( __METHOD__
);
266 * Replace internal links
268 protected function replaceInternal( &$text ) {
269 if ( !$this->internals
) {
273 wfProfileIn( __METHOD__
);
277 $linkCache = LinkCache
::singleton();
278 $output = $this->parent
->getOutput();
280 wfProfileIn( __METHOD__
.'-check' );
281 $dbr = wfGetDB( DB_SLAVE
);
282 $threshold = $this->parent
->getOptions()->getStubThreshold();
285 ksort( $this->internals
);
287 $linkcolour_ids = array();
291 foreach ( $this->internals
as $ns => $entries ) {
292 foreach ( $entries as $entry ) {
293 $title = $entry['title'];
294 $pdbk = $entry['pdbk'];
296 # Skip invalid entries.
297 # Result will be ugly, but prevents crash.
298 if ( is_null( $title ) ) {
302 # Check if it's a static known link, e.g. interwiki
303 if ( $title->isAlwaysKnown() ) {
304 $colours[$pdbk] = '';
305 } elseif ( $ns == NS_SPECIAL
) {
306 $colours[$pdbk] = 'new';
307 } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
308 $colours[$pdbk] = Linker
::getLinkColour( $title, $threshold );
309 $output->addLink( $title, $id );
310 $linkcolour_ids[$id] = $pdbk;
311 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
312 $colours[$pdbk] = 'new';
314 # Not in the link cache, add it to the query
315 $queries[$ns][] = $title->getDBkey();
321 foreach( $queries as $ns => $pages ){
322 $where[] = $dbr->makeList(
324 'page_namespace' => $ns,
325 'page_title' => $pages,
333 array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect', 'page_len', 'page_latest' ),
334 $dbr->makeList( $where, LIST_OR
),
338 # Fetch data and form into an associative array
339 # non-existent = broken
340 foreach ( $res as $s ) {
341 $title = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
342 $pdbk = $title->getPrefixedDBkey();
343 $linkCache->addGoodLinkObjFromRow( $title, $s );
344 $output->addLink( $title, $s->page_id
);
345 # @todo FIXME: Convoluted data flow
346 # The redirect status and length is passed to getLinkColour via the LinkCache
347 # Use formal parameters instead
348 $colours[$pdbk] = Linker
::getLinkColour( $title, $threshold );
349 //add id to the extension todolist
350 $linkcolour_ids[$s->page_id
] = $pdbk;
354 if ( count($linkcolour_ids) ) {
355 //pass an array of page_ids to an extension
356 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
358 wfProfileOut( __METHOD__
.'-check' );
360 # Do a second query for different language variants of links and categories
361 if($wgContLang->hasVariants()) {
362 $this->doVariants( $colours );
365 # Construct search and replace arrays
366 wfProfileIn( __METHOD__
.'-construct' );
367 $replacePairs = array();
368 foreach ( $this->internals
as $ns => $entries ) {
369 foreach ( $entries as $index => $entry ) {
370 $pdbk = $entry['pdbk'];
371 $title = $entry['title'];
372 $query = isset( $entry['query'] ) ?
$entry['query'] : array();
374 $searchkey = "<!--LINK $key-->";
375 $displayText = $entry['text'];
376 if ( $displayText === '' ) {
379 if ( !isset( $colours[$pdbk] ) ) {
380 $colours[$pdbk] = 'new';
383 if ( $colours[$pdbk] == 'new' ) {
384 $linkCache->addBadLinkObj( $title );
385 $output->addLink( $title, 0 );
386 $type = array( 'broken' );
388 if ( $colours[$pdbk] != '' ) {
389 $attribs['class'] = $colours[$pdbk];
391 $type = array( 'known', 'noclasses' );
393 $replacePairs[$searchkey] = Linker
::link( $title, $displayText,
394 $attribs, $query, $type );
397 $replacer = new HashtableReplacer( $replacePairs, 1 );
398 wfProfileOut( __METHOD__
.'-construct' );
401 wfProfileIn( __METHOD__
.'-replace' );
402 $text = preg_replace_callback(
403 '/(<!--LINK .*?-->)/',
407 wfProfileOut( __METHOD__
.'-replace' );
408 wfProfileOut( __METHOD__
);
412 * Replace interwiki links
414 protected function replaceInterwiki( &$text ) {
415 if ( empty( $this->interwikis
) ) {
419 wfProfileIn( __METHOD__
);
420 # Make interwiki link HTML
421 $output = $this->parent
->getOutput();
422 $replacePairs = array();
423 foreach( $this->interwikis
as $key => $link ) {
424 $replacePairs[$key] = Linker
::link( $link['title'], $link['text'] );
425 $output->addInterwikiLink( $link['title'] );
427 $replacer = new HashtableReplacer( $replacePairs, 1 );
429 $text = preg_replace_callback(
430 '/<!--IWLINK (.*?)-->/',
433 wfProfileOut( __METHOD__
);
437 * Modify $this->internals and $colours according to language variant linking rules
439 protected function doVariants( &$colours ) {
441 $linkBatch = new LinkBatch();
442 $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
443 $output = $this->parent
->getOutput();
444 $linkCache = LinkCache
::singleton();
445 $threshold = $this->parent
->getOptions()->getStubThreshold();
446 $titlesToBeConverted = '';
447 $titlesAttrs = array();
449 // Concatenate titles to a single string, thus we only need auto convert the
450 // single string to all variants. This would improve parser's performance
452 foreach ( $this->internals
as $ns => $entries ) {
453 foreach ( $entries as $index => $entry ) {
454 $pdbk = $entry['pdbk'];
455 // we only deal with new links (in its first query)
456 if ( !isset( $colours[$pdbk] ) ) {
457 $title = $entry['title'];
458 $titleText = $title->getText();
459 $titlesAttrs[] = array(
461 'key' => "$ns:$index",
462 'titleText' => $titleText,
464 // separate titles with \0 because it would never appears
466 $titlesToBeConverted .= $titleText . "\0";
471 // Now do the conversion and explode string to text of titles
472 $titlesAllVariants = $wgContLang->autoConvertToAllVariants( $titlesToBeConverted );
473 $allVariantsName = array_keys( $titlesAllVariants );
474 foreach ( $titlesAllVariants as &$titlesVariant ) {
475 $titlesVariant = explode( "\0", $titlesVariant );
477 $l = count( $titlesAttrs );
478 // Then add variants of links to link batch
479 for ( $i = 0; $i < $l; $i ++
) {
480 foreach ( $allVariantsName as $variantName ) {
481 $textVariant = $titlesAllVariants[$variantName][$i];
482 if ( $textVariant != $titlesAttrs[$i]['titleText'] ) {
483 $variantTitle = Title
::makeTitle( $titlesAttrs[$i]['ns'], $textVariant );
484 if( is_null( $variantTitle ) ) {
487 $linkBatch->addObj( $variantTitle );
488 $variantMap[$variantTitle->getPrefixedDBkey()][] = $titlesAttrs[$i]['key'];
493 // process categories, check if a category exists in some variant
494 $categoryMap = array(); // maps $category_variant => $category (dbkeys)
495 $varCategories = array(); // category replacements oldDBkey => newDBkey
496 foreach( $output->getCategoryLinks() as $category ){
497 $variants = $wgContLang->autoConvertToAllVariants( $category );
498 foreach($variants as $variant){
499 if($variant != $category){
500 $variantTitle = Title
::newFromDBkey( Title
::makeName(NS_CATEGORY
,$variant) );
501 if(is_null($variantTitle)) continue;
502 $linkBatch->addObj( $variantTitle );
503 $categoryMap[$variant] = $category;
509 if(!$linkBatch->isEmpty()){
511 $dbr = wfGetDB( DB_SLAVE
);
512 $varRes = $dbr->select( 'page',
513 array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect', 'page_len', 'page_latest' ),
514 $linkBatch->constructSet( 'page', $dbr ),
518 $linkcolour_ids = array();
520 // for each found variants, figure out link holders and replace
521 foreach ( $varRes as $s ) {
523 $variantTitle = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
524 $varPdbk = $variantTitle->getPrefixedDBkey();
525 $vardbk = $variantTitle->getDBkey();
527 $holderKeys = array();
528 if( isset( $variantMap[$varPdbk] ) ) {
529 $holderKeys = $variantMap[$varPdbk];
530 $linkCache->addGoodLinkObjFromRow( $variantTitle, $s );
531 $output->addLink( $variantTitle, $s->page_id
);
534 // loop over link holders
535 foreach( $holderKeys as $key ) {
536 list( $ns, $index ) = explode( ':', $key, 2 );
537 $entry =& $this->internals
[$ns][$index];
538 $pdbk = $entry['pdbk'];
540 if(!isset($colours[$pdbk])){
541 // found link in some of the variants, replace the link holder data
542 $entry['title'] = $variantTitle;
543 $entry['pdbk'] = $varPdbk;
545 // set pdbk and colour
546 # @todo FIXME: Convoluted data flow
547 # The redirect status and length is passed to getLinkColour via the LinkCache
548 # Use formal parameters instead
549 $colours[$varPdbk] = Linker
::getLinkColour( $variantTitle, $threshold );
550 $linkcolour_ids[$s->page_id
] = $pdbk;
554 // check if the object is a variant of a category
555 if(isset($categoryMap[$vardbk])){
556 $oldkey = $categoryMap[$vardbk];
557 if($oldkey != $vardbk)
558 $varCategories[$oldkey]=$vardbk;
561 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
563 // rebuild the categories in original order (if there are replacements)
564 if(count($varCategories)>0){
566 $originalCats = $output->getCategories();
567 foreach($originalCats as $cat => $sortkey){
568 // make the replacement
569 if( array_key_exists($cat,$varCategories) )
570 $newCats[$varCategories[$cat]] = $sortkey;
571 else $newCats[$cat] = $sortkey;
573 $output->setCategoryLinks($newCats);
579 * Replace <!--LINK--> link placeholders with plain text of links
580 * (not HTML-formatted).
582 * @param $text String
585 function replaceText( $text ) {
586 wfProfileIn( __METHOD__
);
588 $text = preg_replace_callback(
589 '/<!--(LINK|IWLINK) (.*?)-->/',
590 array( &$this, 'replaceTextCallback' ),
593 wfProfileOut( __METHOD__
);
598 * Callback for replaceText()
600 * @param $matches Array
604 function replaceTextCallback( $matches ) {
607 if( $type == 'LINK' ) {
608 list( $ns, $index ) = explode( ':', $key, 2 );
609 if( isset( $this->internals
[$ns][$index]['text'] ) ) {
610 return $this->internals
[$ns][$index]['text'];
612 } elseif( $type == 'IWLINK' ) {
613 if( isset( $this->interwikis
[$key]['text'] ) ) {
614 return $this->interwikis
[$key]['text'];