3 class LinkHolderArray
{
4 var $internals = array(), $interwikis = array();
8 function __construct( $parent ) {
9 $this->parent
= $parent;
13 * Reduce memory usage to reduce the impact of circular references
15 function __destruct() {
16 foreach ( $this as $name => $value ) {
17 unset( $this->$name );
22 * Merge another LinkHolderArray into this one
24 function merge( $other ) {
25 foreach ( $other->internals
as $ns => $entries ) {
26 $this->size +
= count( $entries );
27 if ( !isset( $this->internals
[$ns] ) ) {
28 $this->internals
[$ns] = $entries;
30 $this->internals
[$ns] +
= $entries;
33 $this->interwikis +
= $other->interwikis
;
37 * Returns true if the memory requirements of this object are getting large
40 global $wgLinkHolderBatchSize;
41 return $this->size
> $wgLinkHolderBatchSize;
45 * Clear all stored link holders.
46 * Make sure you don't have any text left using these link holders, before you call this
49 $this->internals
= array();
50 $this->interwikis
= array();
55 * Make a link placeholder. The text returned can be later resolved to a real link with
56 * replaceLinkHolders(). This is done for two reasons: firstly to avoid further
57 * parsing of interwiki links, and secondly to allow all existence checks and
58 * article length checks (for stub links) to be bundled into a single query.
61 function makeHolder( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
62 wfProfileIn( __METHOD__
);
63 if ( ! is_object($nt) ) {
65 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
67 # Separate the link trail from the rest of the link
68 list( $inside, $trail ) = Linker
::splitTrail( $trail );
72 'text' => $prefix.$text.$inside,
73 'pdbk' => $nt->getPrefixedDBkey(),
75 if ( $query !== '' ) {
76 $entry['query'] = $query;
79 if ( $nt->isExternal() ) {
80 // Use a globally unique ID to keep the objects mergable
81 $key = $this->parent
->nextLinkID();
82 $this->interwikis
[$key] = $entry;
83 $retVal = "<!--IWLINK $key-->{$trail}";
85 $key = $this->parent
->nextLinkID();
86 $ns = $nt->getNamespace();
87 $this->internals
[$ns][$key] = $entry;
88 $retVal = "<!--LINK $ns:$key-->{$trail}";
92 wfProfileOut( __METHOD__
);
97 * Get the stub threshold
99 function getStubThreshold() {
101 if ( !isset( $this->stubThreshold
) ) {
102 $this->stubThreshold
= $wgUser->getOption('stubthreshold');
104 return $this->stubThreshold
;
108 * FIXME: update documentation. makeLinkObj() is deprecated.
109 * Replace <!--LINK--> link placeholders with actual links, in the buffer
110 * Placeholders created in Skin::makeLinkObj()
111 * Returns an array of link CSS classes, indexed by PDBK.
113 function replace( &$text ) {
114 wfProfileIn( __METHOD__
);
116 $colours = $this->replaceInternal( $text );
117 $this->replaceInterwiki( $text );
119 wfProfileOut( __METHOD__
);
124 * Replace internal links
126 protected function replaceInternal( &$text ) {
127 if ( !$this->internals
) {
131 wfProfileIn( __METHOD__
);
135 $sk = $this->parent
->getOptions()->getSkin();
136 $linkCache = LinkCache
::singleton();
137 $output = $this->parent
->getOutput();
139 wfProfileIn( __METHOD__
.'-check' );
140 $dbr = wfGetDB( DB_SLAVE
);
141 $page = $dbr->tableName( 'page' );
142 $threshold = $this->getStubThreshold();
145 ksort( $this->internals
);
147 $linkcolour_ids = array();
152 foreach ( $this->internals
as $ns => $entries ) {
153 foreach ( $entries as $index => $entry ) {
155 $title = $entry['title'];
156 $pdbk = $entry['pdbk'];
158 # Skip invalid entries.
159 # Result will be ugly, but prevents crash.
160 if ( is_null( $title ) ) {
164 # Check if it's a static known link, e.g. interwiki
165 if ( $title->isAlwaysKnown() ) {
166 $colours[$pdbk] = '';
167 } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
168 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
169 $output->addLink( $title, $id );
170 $linkcolour_ids[$id] = $pdbk;
171 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
172 $colours[$pdbk] = 'new';
174 # Not in the link cache, add it to the query
175 if ( !isset( $current ) ) {
177 $query = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len, page_latest";
178 $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN(";
179 } elseif ( $current != $ns ) {
181 $query .= ")) OR (page_namespace=$ns AND page_title IN(";
186 $query .= $dbr->addQuotes( $title->getDBkey() );
193 $res = $dbr->query( $query, __METHOD__
);
195 # Fetch data and form into an associative array
196 # non-existent = broken
197 while ( $s = $dbr->fetchObject($res) ) {
198 $title = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
199 $pdbk = $title->getPrefixedDBkey();
200 $linkCache->addGoodLinkObj( $s->page_id
, $title, $s->page_len
, $s->page_is_redirect
, $s->page_latest
);
201 $output->addLink( $title, $s->page_id
);
202 # FIXME: convoluted data flow
203 # The redirect status and length is passed to getLinkColour via the LinkCache
204 # Use formal parameters instead
205 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
206 //add id to the extension todolist
207 $linkcolour_ids[$s->page_id
] = $pdbk;
211 if ( count($linkcolour_ids) ) {
212 //pass an array of page_ids to an extension
213 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
215 wfProfileOut( __METHOD__
.'-check' );
217 # Do a second query for different language variants of links and categories
218 if($wgContLang->hasVariants()) {
219 $this->doVariants( $colours );
222 # Construct search and replace arrays
223 wfProfileIn( __METHOD__
.'-construct' );
224 $replacePairs = array();
225 foreach ( $this->internals
as $ns => $entries ) {
226 foreach ( $entries as $index => $entry ) {
227 $pdbk = $entry['pdbk'];
228 $title = $entry['title'];
229 $query = isset( $entry['query'] ) ?
$entry['query'] : '';
231 $searchkey = "<!--LINK $key-->";
232 if ( !isset( $colours[$pdbk] ) ||
$colours[$pdbk] == 'new' ) {
233 $linkCache->addBadLinkObj( $title );
234 $colours[$pdbk] = 'new';
235 $output->addLink( $title, 0 );
236 // FIXME: replace deprecated makeBrokenLinkObj() by link()
237 $replacePairs[$searchkey] = $sk->makeBrokenLinkObj( $title,
241 // FIXME: replace deprecated makeColouredLinkObj() by link()
242 $replacePairs[$searchkey] = $sk->makeColouredLinkObj( $title, $colours[$pdbk],
248 $replacer = new HashtableReplacer( $replacePairs, 1 );
249 wfProfileOut( __METHOD__
.'-construct' );
252 wfProfileIn( __METHOD__
.'-replace' );
253 $text = preg_replace_callback(
254 '/(<!--LINK .*?-->)/',
258 wfProfileOut( __METHOD__
.'-replace' );
259 wfProfileOut( __METHOD__
);
263 * Replace interwiki links
265 protected function replaceInterwiki( &$text ) {
266 if ( empty( $this->interwikis
) ) {
270 wfProfileIn( __METHOD__
);
271 # Make interwiki link HTML
272 $sk = $this->parent
->getOptions()->getSkin();
273 $output = $this->parent
->getOutput();
274 $replacePairs = array();
275 foreach( $this->interwikis
as $key => $link ) {
276 $replacePairs[$key] = $sk->link( $link['title'], $link['text'] );
277 $output->addInterwikiLink( $link['title'] );
279 $replacer = new HashtableReplacer( $replacePairs, 1 );
281 $text = preg_replace_callback(
282 '/<!--IWLINK (.*?)-->/',
285 wfProfileOut( __METHOD__
);
289 * Modify $this->internals and $colours according to language variant linking rules
291 protected function doVariants( &$colours ) {
293 $linkBatch = new LinkBatch();
294 $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
295 $output = $this->parent
->getOutput();
296 $linkCache = LinkCache
::singleton();
297 $sk = $this->parent
->getOptions()->getSkin();
298 $threshold = $this->getStubThreshold();
299 $titlesToBeConverted = '';
300 $titlesAttrs = array();
302 // Concatenate titles to a single string, thus we only need auto convert the
303 // single string to all variants. This would improve parser's performance
305 foreach ( $this->internals
as $ns => $entries ) {
306 foreach ( $entries as $index => $entry ) {
307 $pdbk = $entry['pdbk'];
308 // we only deal with new links (in its first query)
309 if ( !isset( $colours[$pdbk] ) ) {
310 $title = $entry['title'];
311 $titleText = $title->getText();
312 $titlesAttrs[] = array(
314 'key' => "$ns:$index",
315 'titleText' => $titleText,
317 // separate titles with \0 because it would never appears
319 $titlesToBeConverted .= $titleText . "\0";
324 // Now do the conversion and explode string to text of titles
325 $titlesAllVariants = $wgContLang->convertLinkToAllVariants( $titlesToBeConverted );
326 $allVariantsName = array_keys( $titlesAllVariants );
327 foreach ( $titlesAllVariants as &$titlesVariant ) {
328 $titlesVariant = explode( "\0", $titlesVariant );
331 // Then add variants of links to link batch
332 for ( $i = 0; $l = count( $titlesAttrs ), $i < $l; $i ++
) {
333 foreach ( $allVariantsName as $variantName ) {
334 $textVariant = $titlesAllVariants[$variantName][$i];
335 extract( $titlesAttrs[$i] );
336 if($textVariant != $titleText){
337 $variantTitle = Title
::makeTitle( $ns, $textVariant );
338 if( is_null( $variantTitle ) ) { continue; }
339 $linkBatch->addObj( $variantTitle );
340 $variantMap[$variantTitle->getPrefixedDBkey()][] = $key;
345 // process categories, check if a category exists in some variant
346 $categoryMap = array(); // maps $category_variant => $category (dbkeys)
347 $varCategories = array(); // category replacements oldDBkey => newDBkey
348 foreach( $output->getCategoryLinks() as $category ){
349 $variants = $wgContLang->convertLinkToAllVariants( $category );
350 foreach($variants as $variant){
351 if($variant != $category){
352 $variantTitle = Title
::newFromDBkey( Title
::makeName(NS_CATEGORY
,$variant) );
353 if(is_null($variantTitle)) continue;
354 $linkBatch->addObj( $variantTitle );
355 $categoryMap[$variant] = $category;
361 if(!$linkBatch->isEmpty()){
363 $dbr = wfGetDB( DB_SLAVE
);
364 $page = $dbr->tableName( 'page' );
365 $titleClause = $linkBatch->constructSet('page', $dbr);
366 $variantQuery = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len";
367 $variantQuery .= " FROM $page WHERE $titleClause";
368 $varRes = $dbr->query( $variantQuery, __METHOD__
);
369 $linkcolour_ids = array();
371 // for each found variants, figure out link holders and replace
372 while ( $s = $dbr->fetchObject($varRes) ) {
374 $variantTitle = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
375 $varPdbk = $variantTitle->getPrefixedDBkey();
376 $vardbk = $variantTitle->getDBkey();
378 $holderKeys = array();
379 if(isset($variantMap[$varPdbk])){
380 $holderKeys = $variantMap[$varPdbk];
381 $linkCache->addGoodLinkObj( $s->page_id
, $variantTitle, $s->page_len
, $s->page_is_redirect
);
382 $output->addLink( $variantTitle, $s->page_id
);
385 // loop over link holders
386 foreach($holderKeys as $key){
387 list( $ns, $index ) = explode( ':', $key, 2 );
388 $entry =& $this->internals
[$ns][$index];
389 $pdbk = $entry['pdbk'];
391 if(!isset($colours[$pdbk])){
392 // found link in some of the variants, replace the link holder data
393 $entry['title'] = $variantTitle;
394 $entry['pdbk'] = $varPdbk;
396 // set pdbk and colour
397 # FIXME: convoluted data flow
398 # The redirect status and length is passed to getLinkColour via the LinkCache
399 # Use formal parameters instead
400 $colours[$varPdbk] = $sk->getLinkColour( $variantTitle, $threshold );
401 $linkcolour_ids[$s->page_id
] = $pdbk;
405 // check if the object is a variant of a category
406 if(isset($categoryMap[$vardbk])){
407 $oldkey = $categoryMap[$vardbk];
408 if($oldkey != $vardbk)
409 $varCategories[$oldkey]=$vardbk;
412 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
414 // rebuild the categories in original order (if there are replacements)
415 if(count($varCategories)>0){
417 $originalCats = $output->getCategories();
418 foreach($originalCats as $cat => $sortkey){
419 // make the replacement
420 if( array_key_exists($cat,$varCategories) )
421 $newCats[$varCategories[$cat]] = $sortkey;
422 else $newCats[$cat] = $sortkey;
424 $output->setCategoryLinks($newCats);
430 * Replace <!--LINK--> link placeholders with plain text of links
431 * (not HTML-formatted).
433 * @param $text String
436 function replaceText( $text ) {
437 wfProfileIn( __METHOD__
);
439 $text = preg_replace_callback(
440 '/<!--(LINK|IWLINK) (.*?)-->/',
441 array( &$this, 'replaceTextCallback' ),
444 wfProfileOut( __METHOD__
);
449 * Callback for replaceText()
451 * @param $matches Array
455 function replaceTextCallback( $matches ) {
458 if( $type == 'LINK' ) {
459 list( $ns, $index ) = explode( ':', $key, 2 );
460 if( isset( $this->internals
[$ns][$index]['text'] ) ) {
461 return $this->internals
[$ns][$index]['text'];
463 } elseif( $type == 'IWLINK' ) {
464 if( isset( $this->interwikis
[$key]['text'] ) ) {
465 return $this->interwikis
[$key]['text'];