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 * Replace <!--LINK--> link placeholders with actual links, in the buffer
109 * Placeholders created in Skin::makeLinkObj()
110 * Returns an array of link CSS classes, indexed by PDBK.
112 function replace( &$text ) {
113 wfProfileIn( __METHOD__
);
115 $colours = $this->replaceInternal( $text );
116 $this->replaceInterwiki( $text );
118 wfProfileOut( __METHOD__
);
123 * Replace internal links
125 protected function replaceInternal( &$text ) {
126 if ( !$this->internals
) {
130 wfProfileIn( __METHOD__
);
134 $sk = $this->parent
->getOptions()->getSkin();
135 $linkCache = LinkCache
::singleton();
136 $output = $this->parent
->getOutput();
138 wfProfileIn( __METHOD__
.'-check' );
139 $dbr = wfGetDB( DB_SLAVE
);
140 $page = $dbr->tableName( 'page' );
141 $threshold = $this->getStubThreshold();
144 ksort( $this->internals
);
149 foreach ( $this->internals
as $ns => $entries ) {
150 foreach ( $entries as $index => $entry ) {
152 $title = $entry['title'];
153 $pdbk = $entry['pdbk'];
155 # Skip invalid entries.
156 # Result will be ugly, but prevents crash.
157 if ( is_null( $title ) ) {
161 # Check if it's a static known link, e.g. interwiki
162 if ( $title->isAlwaysKnown() ) {
163 $colours[$pdbk] = '';
164 } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
165 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
166 $output->addLink( $title, $id );
167 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
168 $colours[$pdbk] = 'new';
170 # Not in the link cache, add it to the query
171 if ( !isset( $current ) ) {
173 $query = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len";
174 $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN(";
175 } elseif ( $current != $ns ) {
177 $query .= ")) OR (page_namespace=$ns AND page_title IN(";
182 $query .= $dbr->addQuotes( $title->getDBkey() );
189 $res = $dbr->query( $query, __METHOD__
);
191 # Fetch data and form into an associative array
192 # non-existent = broken
193 $linkcolour_ids = array();
194 while ( $s = $dbr->fetchObject($res) ) {
195 $title = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
196 $pdbk = $title->getPrefixedDBkey();
197 $linkCache->addGoodLinkObj( $s->page_id
, $title, $s->page_len
, $s->page_is_redirect
);
198 $output->addLink( $title, $s->page_id
);
199 # FIXME: convoluted data flow
200 # The redirect status and length is passed to getLinkColour via the LinkCache
201 # Use formal parameters instead
202 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
203 //add id to the extension todolist
204 $linkcolour_ids[$s->page_id
] = $pdbk;
207 //pass an array of page_ids to an extension
208 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
210 wfProfileOut( __METHOD__
.'-check' );
212 # Do a second query for different language variants of links and categories
213 if($wgContLang->hasVariants()) {
214 $this->doVariants( $colours );
217 # Construct search and replace arrays
218 wfProfileIn( __METHOD__
.'-construct' );
219 $replacePairs = array();
220 foreach ( $this->internals
as $ns => $entries ) {
221 foreach ( $entries as $index => $entry ) {
222 $pdbk = $entry['pdbk'];
223 $title = $entry['title'];
224 $query = isset( $entry['query'] ) ?
$entry['query'] : '';
226 $searchkey = "<!--LINK $key-->";
227 if ( !isset( $colours[$pdbk] ) ||
$colours[$pdbk] == 'new' ) {
228 $linkCache->addBadLinkObj( $title );
229 $colours[$pdbk] = 'new';
230 $output->addLink( $title, 0 );
231 $replacePairs[$searchkey] = $sk->makeBrokenLinkObj( $title,
235 $replacePairs[$searchkey] = $sk->makeColouredLinkObj( $title, $colours[$pdbk],
241 $replacer = new HashtableReplacer( $replacePairs, 1 );
242 wfProfileOut( __METHOD__
.'-construct' );
245 wfProfileIn( __METHOD__
.'-replace' );
246 $text = preg_replace_callback(
247 '/(<!--LINK .*?-->)/',
251 wfProfileOut( __METHOD__
.'-replace' );
252 wfProfileOut( __METHOD__
);
256 * Replace interwiki links
258 protected function replaceInterwiki( &$text ) {
259 if ( empty( $this->interwikis
) ) {
263 wfProfileIn( __METHOD__
);
264 # Make interwiki link HTML
265 $sk = $this->parent
->getOptions()->getSkin();
266 $replacePairs = array();
267 foreach( $this->interwikis
as $key => $link ) {
268 $replacePairs[$key] = $sk->link( $link['title'], $link['text'] );
270 $replacer = new HashtableReplacer( $replacePairs, 1 );
272 $text = preg_replace_callback(
273 '/<!--IWLINK (.*?)-->/',
276 wfProfileOut( __METHOD__
);
280 * Modify $this->internals and $colours according to language variant linking rules
282 protected function doVariants( &$colours ) {
284 $linkBatch = new LinkBatch();
285 $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
286 $output = $this->parent
->getOutput();
287 $linkCache = LinkCache
::singleton();
288 $sk = $this->parent
->getOptions()->getSkin();
289 $threshold = $this->getStubThreshold();
291 // Add variants of links to link batch
292 foreach ( $this->internals
as $ns => $entries ) {
293 foreach ( $entries as $index => $entry ) {
295 $pdbk = $entry['pdbk'];
296 $title = $entry['title'];
297 $titleText = $title->getText();
299 // generate all variants of the link title text
300 $allTextVariants = $wgContLang->convertLinkToAllVariants($titleText);
302 // if link was not found (in first query), add all variants to query
303 if ( !isset($colours[$pdbk]) ){
304 foreach($allTextVariants as $textVariant){
305 if($textVariant != $titleText){
306 $variantTitle = Title
::makeTitle( $ns, $textVariant );
307 if(is_null($variantTitle)) continue;
308 $linkBatch->addObj( $variantTitle );
309 $variantMap[$variantTitle->getPrefixedDBkey()][] = $key;
316 // process categories, check if a category exists in some variant
317 $categoryMap = array(); // maps $category_variant => $category (dbkeys)
318 $varCategories = array(); // category replacements oldDBkey => newDBkey
319 foreach( $output->getCategoryLinks() as $category ){
320 $variants = $wgContLang->convertLinkToAllVariants($category);
321 foreach($variants as $variant){
322 if($variant != $category){
323 $variantTitle = Title
::newFromDBkey( Title
::makeName(NS_CATEGORY
,$variant) );
324 if(is_null($variantTitle)) continue;
325 $linkBatch->addObj( $variantTitle );
326 $categoryMap[$variant] = $category;
332 if(!$linkBatch->isEmpty()){
334 $dbr = wfGetDB( DB_SLAVE
);
335 $page = $dbr->tableName( 'page' );
336 $titleClause = $linkBatch->constructSet('page', $dbr);
337 $variantQuery = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len";
338 $variantQuery .= " FROM $page WHERE $titleClause";
339 $varRes = $dbr->query( $variantQuery, __METHOD__
);
340 $linkcolour_ids = array();
342 // for each found variants, figure out link holders and replace
343 while ( $s = $dbr->fetchObject($varRes) ) {
345 $variantTitle = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
346 $varPdbk = $variantTitle->getPrefixedDBkey();
347 $vardbk = $variantTitle->getDBkey();
349 $holderKeys = array();
350 if(isset($variantMap[$varPdbk])){
351 $holderKeys = $variantMap[$varPdbk];
352 $linkCache->addGoodLinkObj( $s->page_id
, $variantTitle, $s->page_len
, $s->page_is_redirect
);
353 $output->addLink( $variantTitle, $s->page_id
);
356 // loop over link holders
357 foreach($holderKeys as $key){
358 list( $ns, $index ) = explode( ':', $key, 2 );
359 $entry =& $this->internals
[$ns][$index];
360 $pdbk = $entry['pdbk'];
362 if(!isset($colours[$pdbk])){
363 // found link in some of the variants, replace the link holder data
364 $entry['title'] = $variantTitle;
365 $entry['pdbk'] = $varPdbk;
367 // set pdbk and colour
368 # FIXME: convoluted data flow
369 # The redirect status and length is passed to getLinkColour via the LinkCache
370 # Use formal parameters instead
371 $colours[$varPdbk] = $sk->getLinkColour( $variantTitle, $threshold );
372 $linkcolour_ids[$s->page_id
] = $pdbk;
376 // check if the object is a variant of a category
377 if(isset($categoryMap[$vardbk])){
378 $oldkey = $categoryMap[$vardbk];
379 if($oldkey != $vardbk)
380 $varCategories[$oldkey]=$vardbk;
383 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
385 // rebuild the categories in original order (if there are replacements)
386 if(count($varCategories)>0){
388 $originalCats = $output->getCategories();
389 foreach($originalCats as $cat => $sortkey){
390 // make the replacement
391 if( array_key_exists($cat,$varCategories) )
392 $newCats[$varCategories[$cat]] = $sortkey;
393 else $newCats[$cat] = $sortkey;
395 $output->setCategoryLinks($newCats);
401 * Replace <!--LINK--> link placeholders with plain text of links
402 * (not HTML-formatted).
403 * @param string $text
406 function replaceText( $text ) {
407 wfProfileIn( __METHOD__
);
409 $text = preg_replace_callback(
410 '/<!--(LINK|IWLINK) (.*?)-->/',
411 array( &$this, 'replaceTextCallback' ),
414 wfProfileOut( __METHOD__
);
419 * @param array $matches
423 function replaceTextCallback( $matches ) {
426 if( $type == 'LINK' ) {
427 list( $ns, $index ) = explode( ':', $key, 2 );
428 if( isset( $this->internals
[$ns][$index]['text'] ) ) {
429 return $this->internals
[$ns][$index]['text'];
431 } elseif( $type == 'IWLINK' ) {
432 if( isset( $this->interwikis
[$key]['text'] ) ) {
433 return $this->interwikis
[$key]['text'];