23d715340a9c5f134168c9a64683851db6ac50cb
3 * Holder of replacement pairs for wiki links
11 class LinkHolderArray
{
12 var $internals = array(), $interwikis = array();
16 function __construct( $parent ) {
17 $this->parent
= $parent;
21 * Reduce memory usage to reduce the impact of circular references
23 function __destruct() {
24 foreach ( $this as $name => $value ) {
25 unset( $this->$name );
30 * Merge another LinkHolderArray into this one
32 function merge( $other ) {
33 foreach ( $other->internals
as $ns => $entries ) {
34 $this->size +
= count( $entries );
35 if ( !isset( $this->internals
[$ns] ) ) {
36 $this->internals
[$ns] = $entries;
38 $this->internals
[$ns] +
= $entries;
41 $this->interwikis +
= $other->interwikis
;
45 * Returns true if the memory requirements of this object are getting large
48 global $wgLinkHolderBatchSize;
49 return $this->size
> $wgLinkHolderBatchSize;
53 * Clear all stored link holders.
54 * Make sure you don't have any text left using these link holders, before you call this
57 $this->internals
= array();
58 $this->interwikis
= array();
63 * Make a link placeholder. The text returned can be later resolved to a real link with
64 * replaceLinkHolders(). This is done for two reasons: firstly to avoid further
65 * parsing of interwiki links, and secondly to allow all existence checks and
66 * article length checks (for stub links) to be bundled into a single query.
69 function makeHolder( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
70 wfProfileIn( __METHOD__
);
71 if ( ! is_object($nt) ) {
73 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
75 # Separate the link trail from the rest of the link
76 list( $inside, $trail ) = Linker
::splitTrail( $trail );
80 'text' => $prefix.$text.$inside,
81 'pdbk' => $nt->getPrefixedDBkey(),
83 if ( $query !== '' ) {
84 $entry['query'] = $query;
87 if ( $nt->isExternal() ) {
88 // Use a globally unique ID to keep the objects mergable
89 $key = $this->parent
->nextLinkID();
90 $this->interwikis
[$key] = $entry;
91 $retVal = "<!--IWLINK $key-->{$trail}";
93 $key = $this->parent
->nextLinkID();
94 $ns = $nt->getNamespace();
95 $this->internals
[$ns][$key] = $entry;
96 $retVal = "<!--LINK $ns:$key-->{$trail}";
100 wfProfileOut( __METHOD__
);
105 * Get the stub threshold
107 function getStubThreshold() {
108 if ( !isset( $this->stubThreshold
) ) {
109 $this->stubThreshold
= $this->parent
->getUser()->getStubThreshold();
111 return $this->stubThreshold
;
115 * FIXME: update documentation. makeLinkObj() is deprecated.
116 * Replace <!--LINK--> link placeholders with actual links, in the buffer
117 * Placeholders created in Skin::makeLinkObj()
118 * Returns an array of link CSS classes, indexed by PDBK.
120 function replace( &$text ) {
121 wfProfileIn( __METHOD__
);
123 $colours = $this->replaceInternal( $text );
124 $this->replaceInterwiki( $text );
126 wfProfileOut( __METHOD__
);
131 * Replace internal links
133 protected function replaceInternal( &$text ) {
134 if ( !$this->internals
) {
138 wfProfileIn( __METHOD__
);
142 $sk = $this->parent
->getOptions()->getSkin( $this->parent
->mTitle
);
143 $linkCache = LinkCache
::singleton();
144 $output = $this->parent
->getOutput();
146 wfProfileIn( __METHOD__
.'-check' );
147 $dbr = wfGetDB( DB_SLAVE
);
148 $page = $dbr->tableName( 'page' );
149 $threshold = $this->getStubThreshold();
152 ksort( $this->internals
);
154 $linkcolour_ids = array();
159 foreach ( $this->internals
as $ns => $entries ) {
160 foreach ( $entries as $entry ) {
161 $title = $entry['title'];
162 $pdbk = $entry['pdbk'];
164 # Skip invalid entries.
165 # Result will be ugly, but prevents crash.
166 if ( is_null( $title ) ) {
170 # Check if it's a static known link, e.g. interwiki
171 if ( $title->isAlwaysKnown() ) {
172 $colours[$pdbk] = '';
173 } elseif ( $ns == NS_SPECIAL
) {
174 $colours[$pdbk] = 'new';
175 } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
176 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
177 $output->addLink( $title, $id );
178 $linkcolour_ids[$id] = $pdbk;
179 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
180 $colours[$pdbk] = 'new';
182 # Not in the link cache, add it to the query
183 if ( !isset( $current ) ) {
185 $query = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len, page_latest";
186 $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN(";
187 } elseif ( $current != $ns ) {
189 $query .= ")) OR (page_namespace=$ns AND page_title IN(";
194 $query .= $dbr->addQuotes( $title->getDBkey() );
201 $res = $dbr->query( $query, __METHOD__
);
203 # Fetch data and form into an associative array
204 # non-existent = broken
205 foreach ( $res as $s ) {
206 $title = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
207 $pdbk = $title->getPrefixedDBkey();
208 $linkCache->addGoodLinkObj( $s->page_id
, $title, $s->page_len
, $s->page_is_redirect
, $s->page_latest
);
209 $output->addLink( $title, $s->page_id
);
210 # FIXME: convoluted data flow
211 # The redirect status and length is passed to getLinkColour via the LinkCache
212 # Use formal parameters instead
213 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
214 //add id to the extension todolist
215 $linkcolour_ids[$s->page_id
] = $pdbk;
219 if ( count($linkcolour_ids) ) {
220 //pass an array of page_ids to an extension
221 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
223 wfProfileOut( __METHOD__
.'-check' );
225 # Do a second query for different language variants of links and categories
226 if($wgContLang->hasVariants()) {
227 $this->doVariants( $colours );
230 # Construct search and replace arrays
231 wfProfileIn( __METHOD__
.'-construct' );
232 $replacePairs = array();
233 foreach ( $this->internals
as $ns => $entries ) {
234 foreach ( $entries as $index => $entry ) {
235 $pdbk = $entry['pdbk'];
236 $title = $entry['title'];
237 $query = isset( $entry['query'] ) ?
$entry['query'] : '';
239 $searchkey = "<!--LINK $key-->";
240 if ( !isset( $colours[$pdbk] ) ||
$colours[$pdbk] == 'new' ) {
241 $linkCache->addBadLinkObj( $title );
242 $colours[$pdbk] = 'new';
243 $output->addLink( $title, 0 );
244 // FIXME: replace deprecated makeBrokenLinkObj() by link()
245 $replacePairs[$searchkey] = $sk->makeBrokenLinkObj( $title,
249 // FIXME: replace deprecated makeColouredLinkObj() by link()
250 $replacePairs[$searchkey] = $sk->makeColouredLinkObj( $title, $colours[$pdbk],
256 $replacer = new HashtableReplacer( $replacePairs, 1 );
257 wfProfileOut( __METHOD__
.'-construct' );
260 wfProfileIn( __METHOD__
.'-replace' );
261 $text = preg_replace_callback(
262 '/(<!--LINK .*?-->)/',
266 wfProfileOut( __METHOD__
.'-replace' );
267 wfProfileOut( __METHOD__
);
271 * Replace interwiki links
273 protected function replaceInterwiki( &$text ) {
274 if ( empty( $this->interwikis
) ) {
278 wfProfileIn( __METHOD__
);
279 # Make interwiki link HTML
280 $sk = $this->parent
->getOptions()->getSkin( $this->parent
->mTitle
);
281 $output = $this->parent
->getOutput();
282 $replacePairs = array();
283 foreach( $this->interwikis
as $key => $link ) {
284 $replacePairs[$key] = $sk->link( $link['title'], $link['text'] );
285 $output->addInterwikiLink( $link['title'] );
287 $replacer = new HashtableReplacer( $replacePairs, 1 );
289 $text = preg_replace_callback(
290 '/<!--IWLINK (.*?)-->/',
293 wfProfileOut( __METHOD__
);
297 * Modify $this->internals and $colours according to language variant linking rules
299 protected function doVariants( &$colours ) {
301 $linkBatch = new LinkBatch();
302 $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
303 $output = $this->parent
->getOutput();
304 $linkCache = LinkCache
::singleton();
305 $sk = $this->parent
->getOptions()->getSkin( $this->parent
->mTitle
);
306 $threshold = $this->getStubThreshold();
307 $titlesToBeConverted = '';
308 $titlesAttrs = array();
310 // Concatenate titles to a single string, thus we only need auto convert the
311 // single string to all variants. This would improve parser's performance
313 foreach ( $this->internals
as $ns => $entries ) {
314 foreach ( $entries as $index => $entry ) {
315 $pdbk = $entry['pdbk'];
316 // we only deal with new links (in its first query)
317 if ( !isset( $colours[$pdbk] ) ) {
318 $title = $entry['title'];
319 $titleText = $title->getText();
320 $titlesAttrs[] = array(
322 'key' => "$ns:$index",
323 'titleText' => $titleText,
325 // separate titles with \0 because it would never appears
327 $titlesToBeConverted .= $titleText . "\0";
332 // Now do the conversion and explode string to text of titles
333 $titlesAllVariants = $wgContLang->autoConvertToAllVariants( $titlesToBeConverted );
334 $allVariantsName = array_keys( $titlesAllVariants );
335 foreach ( $titlesAllVariants as &$titlesVariant ) {
336 $titlesVariant = explode( "\0", $titlesVariant );
338 $l = count( $titlesAttrs );
339 // Then add variants of links to link batch
340 for ( $i = 0; $i < $l; $i ++
) {
341 foreach ( $allVariantsName as $variantName ) {
342 $textVariant = $titlesAllVariants[$variantName][$i];
343 extract( $titlesAttrs[$i] );
344 if($textVariant != $titleText){
345 $variantTitle = Title
::makeTitle( $ns, $textVariant );
346 if( is_null( $variantTitle ) ) {
349 $linkBatch->addObj( $variantTitle );
350 $variantMap[$variantTitle->getPrefixedDBkey()][] = $key;
355 // process categories, check if a category exists in some variant
356 $categoryMap = array(); // maps $category_variant => $category (dbkeys)
357 $varCategories = array(); // category replacements oldDBkey => newDBkey
358 foreach( $output->getCategoryLinks() as $category ){
359 $variants = $wgContLang->autoConvertToAllVariants( $category );
360 foreach($variants as $variant){
361 if($variant != $category){
362 $variantTitle = Title
::newFromDBkey( Title
::makeName(NS_CATEGORY
,$variant) );
363 if(is_null($variantTitle)) continue;
364 $linkBatch->addObj( $variantTitle );
365 $categoryMap[$variant] = $category;
371 if(!$linkBatch->isEmpty()){
373 $dbr = wfGetDB( DB_SLAVE
);
374 $page = $dbr->tableName( 'page' );
375 $titleClause = $linkBatch->constructSet('page', $dbr);
376 $variantQuery = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len";
377 $variantQuery .= " FROM $page WHERE $titleClause";
378 $varRes = $dbr->query( $variantQuery, __METHOD__
);
379 $linkcolour_ids = array();
381 // for each found variants, figure out link holders and replace
382 foreach ( $varRes as $s ) {
384 $variantTitle = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
385 $varPdbk = $variantTitle->getPrefixedDBkey();
386 $vardbk = $variantTitle->getDBkey();
388 $holderKeys = array();
389 if(isset($variantMap[$varPdbk])){
390 $holderKeys = $variantMap[$varPdbk];
391 $linkCache->addGoodLinkObj( $s->page_id
, $variantTitle, $s->page_len
, $s->page_is_redirect
);
392 $output->addLink( $variantTitle, $s->page_id
);
395 // loop over link holders
396 foreach($holderKeys as $key){
397 list( $ns, $index ) = explode( ':', $key, 2 );
398 $entry =& $this->internals
[$ns][$index];
399 $pdbk = $entry['pdbk'];
401 if(!isset($colours[$pdbk])){
402 // found link in some of the variants, replace the link holder data
403 $entry['title'] = $variantTitle;
404 $entry['pdbk'] = $varPdbk;
406 // set pdbk and colour
407 # FIXME: convoluted data flow
408 # The redirect status and length is passed to getLinkColour via the LinkCache
409 # Use formal parameters instead
410 $colours[$varPdbk] = $sk->getLinkColour( $variantTitle, $threshold );
411 $linkcolour_ids[$s->page_id
] = $pdbk;
415 // check if the object is a variant of a category
416 if(isset($categoryMap[$vardbk])){
417 $oldkey = $categoryMap[$vardbk];
418 if($oldkey != $vardbk)
419 $varCategories[$oldkey]=$vardbk;
422 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
424 // rebuild the categories in original order (if there are replacements)
425 if(count($varCategories)>0){
427 $originalCats = $output->getCategories();
428 foreach($originalCats as $cat => $sortkey){
429 // make the replacement
430 if( array_key_exists($cat,$varCategories) )
431 $newCats[$varCategories[$cat]] = $sortkey;
432 else $newCats[$cat] = $sortkey;
434 $output->setCategoryLinks($newCats);
440 * Replace <!--LINK--> link placeholders with plain text of links
441 * (not HTML-formatted).
443 * @param $text String
446 function replaceText( $text ) {
447 wfProfileIn( __METHOD__
);
449 $text = preg_replace_callback(
450 '/<!--(LINK|IWLINK) (.*?)-->/',
451 array( &$this, 'replaceTextCallback' ),
454 wfProfileOut( __METHOD__
);
459 * Callback for replaceText()
461 * @param $matches Array
465 function replaceTextCallback( $matches ) {
468 if( $type == 'LINK' ) {
469 list( $ns, $index ) = explode( ':', $key, 2 );
470 if( isset( $this->internals
[$ns][$index]['text'] ) ) {
471 return $this->internals
[$ns][$index]['text'];
473 } elseif( $type == 'IWLINK' ) {
474 if( isset( $this->interwikis
[$key]['text'] ) ) {
475 return $this->interwikis
[$key]['text'];