3 class LinkHolderArray
{
6 var $internals = array(), $interwikis = array();
10 function __construct( $parent ) {
11 $this->parent
= $parent;
15 * Reduce memory usage to reduce the impact of circular references
17 function __destruct() {
18 foreach ( $this as $name => $value ) {
19 unset( $this->$name );
24 * Merge another LinkHolderArray into this one
26 function merge( $other ) {
27 foreach ( $other->internals
as $ns => $entries ) {
28 $this->size +
= count( $entries );
29 if ( !isset( $this->internals
[$ns] ) ) {
30 $this->internals
[$ns] = $entries;
32 $this->internals
[$ns] +
= $entries;
35 $this->interwikis +
= $other->interwikis
;
39 * Returns true if the memory requirements of this object are getting large
42 return $this->size
> $this->batchSize
;
46 * Clear all stored link holders.
47 * Make sure you don't have any text left using these link holders, before you call this
50 $this->internals
= array();
51 $this->interwikis
= array();
56 * Make a link placeholder. The text returned can be later resolved to a real link with
57 * replaceLinkHolders(). This is done for two reasons: firstly to avoid further
58 * parsing of interwiki links, and secondly to allow all existence checks and
59 * article length checks (for stub links) to be bundled into a single query.
62 function makeHolder( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
63 wfProfileIn( __METHOD__
);
64 if ( ! is_object($nt) ) {
66 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
68 # Separate the link trail from the rest of the link
69 list( $inside, $trail ) = Linker
::splitTrail( $trail );
73 'text' => $prefix.$text.$inside,
74 'pdbk' => $nt->getPrefixedDBkey(),
76 if ( $query !== '' ) {
77 $entry['query'] = $query;
80 if ( $nt->isExternal() ) {
81 // Use a globally unique ID to keep the objects mergable
82 $key = $this->parent
->nextLinkID();
83 $this->interwikis
[$key] = $entry;
84 $retVal = "<!--IWLINK $key-->{$trail}";
86 $key = $this->parent
->nextLinkID();
87 $ns = $nt->getNamespace();
88 $this->internals
[$ns][$key] = $entry;
89 $retVal = "<!--LINK $ns:$key-->{$trail}";
93 wfProfileOut( __METHOD__
);
98 * Replace <!--LINK--> link placeholders with actual links, in the buffer
99 * Placeholders created in Skin::makeLinkObj()
100 * Returns an array of link CSS classes, indexed by PDBK.
102 function replace( &$text ) {
103 wfProfileIn( __METHOD__
);
105 $colours = $this->replaceInternal( $text );
106 $this->replaceInterwiki( $text );
108 wfProfileOut( __METHOD__
);
113 * Replace internal links
115 protected function replaceInternal( &$text ) {
116 if ( !$this->internals
) {
120 wfProfileIn( __METHOD__
);
121 global $wgUser, $wgContLang;
125 $linkcolour_ids = array();
126 $sk = $this->parent
->getOptions()->getSkin();
127 $linkCache = LinkCache
::singleton();
128 $output = $this->parent
->getOutput();
130 wfProfileIn( __METHOD__
.'-check' );
131 $dbr = wfGetDB( DB_SLAVE
);
132 $page = $dbr->tableName( 'page' );
133 $threshold = $wgUser->getOption('stubthreshold');
136 ksort( $this->internals
);
141 foreach ( $this->internals
as $ns => $entries ) {
142 foreach ( $entries as $index => $entry ) {
144 $title = $entry['title'];
145 $pdbk = $entry['pdbk'];
147 # Skip invalid entries.
148 # Result will be ugly, but prevents crash.
149 if ( is_null( $title ) ) {
153 # Check if it's a static known link, e.g. interwiki
154 if ( $title->isAlwaysKnown() ) {
155 $colours[$pdbk] = '';
156 } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
157 $colours[$pdbk] = '';
158 $output->addLink( $title, $id );
159 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
160 $colours[$pdbk] = 'new';
161 } elseif ( $title->getNamespace() == NS_SPECIAL
&& !SpecialPage
::exists( $pdbk ) ) {
162 $colours[$pdbk] = 'new';
164 # Not in the link cache, add it to the query
165 if ( !isset( $current ) ) {
167 $query = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len";
168 $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN(";
169 } elseif ( $current != $ns ) {
171 $query .= ")) OR (page_namespace=$ns AND page_title IN(";
176 $query .= $dbr->addQuotes( $title->getDBkey() );
183 $res = $dbr->query( $query, __METHOD__
);
185 # Fetch data and form into an associative array
186 # non-existent = broken
187 while ( $s = $dbr->fetchObject($res) ) {
188 $title = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
189 $pdbk = $title->getPrefixedDBkey();
190 $linkCache->addGoodLinkObj( $s->page_id
, $title, $s->page_len
, $s->page_is_redirect
);
191 $output->addLink( $title, $s->page_id
);
192 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
193 //add id to the extension todolist
194 $linkcolour_ids[$s->page_id
] = $pdbk;
197 //pass an array of page_ids to an extension
198 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
200 wfProfileOut( __METHOD__
.'-check' );
202 # Do a second query for different language variants of links and categories
203 if($wgContLang->hasVariants()){
204 $linkBatch = new LinkBatch();
205 $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
206 $categoryMap = array(); // maps $category_variant => $category (dbkeys)
207 $varCategories = array(); // category replacements oldDBkey => newDBkey
209 $categories = $output->getCategoryLinks();
211 // Add variants of links to link batch
212 foreach ( $this->internals
as $ns => $entries ) {
213 foreach ( $entries as $index => $entry ) {
215 $pdbk = $entry['pdbk'];
216 $title = $entry['title'];
217 $titleText = $title->getText();
219 // generate all variants of the link title text
220 $allTextVariants = $wgContLang->convertLinkToAllVariants($titleText);
222 // if link was not found (in first query), add all variants to query
223 if ( !isset($colours[$pdbk]) ){
224 foreach($allTextVariants as $textVariant){
225 if($textVariant != $titleText){
226 $variantTitle = Title
::makeTitle( $ns, $textVariant );
227 if(is_null($variantTitle)) continue;
228 $linkBatch->addObj( $variantTitle );
229 $variantMap[$variantTitle->getPrefixedDBkey()][] = $key;
236 // process categories, check if a category exists in some variant
237 foreach( $categories as $category ){
238 $variants = $wgContLang->convertLinkToAllVariants($category);
239 foreach($variants as $variant){
240 if($variant != $category){
241 $variantTitle = Title
::newFromDBkey( Title
::makeName(NS_CATEGORY
,$variant) );
242 if(is_null($variantTitle)) continue;
243 $linkBatch->addObj( $variantTitle );
244 $categoryMap[$variant] = $category;
250 if(!$linkBatch->isEmpty()){
252 $titleClause = $linkBatch->constructSet('page', $dbr);
254 $variantQuery = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len";
256 $variantQuery .= " FROM $page WHERE $titleClause";
258 $varRes = $dbr->query( $variantQuery, __METHOD__
);
260 // for each found variants, figure out link holders and replace
261 while ( $s = $dbr->fetchObject($varRes) ) {
263 $variantTitle = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
264 $varPdbk = $variantTitle->getPrefixedDBkey();
265 $vardbk = $variantTitle->getDBkey();
267 $holderKeys = array();
268 if(isset($variantMap[$varPdbk])){
269 $holderKeys = $variantMap[$varPdbk];
270 $linkCache->addGoodLinkObj( $s->page_id
, $variantTitle, $s->page_len
, $s->page_is_redirect
);
271 $output->addLink( $variantTitle, $s->page_id
);
274 // loop over link holders
275 foreach($holderKeys as $key){
276 list( $ns, $index ) = explode( ':', $key, 2 );
277 $entry =& $this->internals
[$ns][$index];
278 $pdbk = $entry['pdbk'];
280 if(!isset($colours[$pdbk])){
281 // found link in some of the variants, replace the link holder data
282 $entry['title'] = $variantTitle;
283 $entry['pdbk'] = $varPdbk;
285 // set pdbk and colour
286 $colours[$varPdbk] = $sk->getLinkColour( $variantTitle, $threshold );
287 $linkcolour_ids[$s->page_id
] = $pdbk;
289 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
292 // check if the object is a variant of a category
293 if(isset($categoryMap[$vardbk])){
294 $oldkey = $categoryMap[$vardbk];
295 if($oldkey != $vardbk)
296 $varCategories[$oldkey]=$vardbk;
300 // rebuild the categories in original order (if there are replacements)
301 if(count($varCategories)>0){
303 $originalCats = $output->getCategories();
304 foreach($originalCats as $cat => $sortkey){
305 // make the replacement
306 if( array_key_exists($cat,$varCategories) )
307 $newCats[$varCategories[$cat]] = $sortkey;
308 else $newCats[$cat] = $sortkey;
310 $this->mOutput
->parent
->setCategoryLinks($newCats);
315 # Construct search and replace arrays
316 wfProfileIn( __METHOD__
.'-construct' );
317 $replacePairs = array();
318 foreach ( $this->internals
as $ns => $entries ) {
319 foreach ( $entries as $index => $entry ) {
320 $pdbk = $entry['pdbk'];
321 $title = $entry['title'];
322 $query = isset( $entry['query'] ) ?
$entry['query'] : '';
324 $searchkey = "<!--LINK $key-->";
325 if ( !isset( $colours[$pdbk] ) ||
$colours[$pdbk] == 'new' ) {
326 $linkCache->addBadLinkObj( $title );
327 $colours[$pdbk] = 'new';
328 $output->addLink( $title, 0 );
329 $replacePairs[$searchkey] = $sk->makeBrokenLinkObj( $title,
333 $replacePairs[$searchkey] = $sk->makeColouredLinkObj( $title, $colours[$pdbk],
339 $replacer = new HashtableReplacer( $replacePairs, 1 );
340 wfProfileOut( __METHOD__
.'-construct' );
343 wfProfileIn( __METHOD__
.'-replace' );
344 $text = preg_replace_callback(
345 '/(<!--LINK .*?-->)/',
349 wfProfileOut( __METHOD__
.'-replace' );
350 wfProfileOut( __METHOD__
);
354 * Replace interwiki links
356 protected function replaceInterwiki( &$text ) {
357 if ( empty( $this->interwikis
) ) {
361 wfProfileIn( __METHOD__
);
362 # Make interwiki link HTML
363 $sk = $this->parent
->getOptions()->getSkin();
364 $replacePairs = array();
365 foreach( $this->interwikis
as $key => $link ) {
366 $replacePairs[$key] = $sk->link( $link['title'], $link['text'] );
368 $replacer = new HashtableReplacer( $replacePairs, 1 );
370 $text = preg_replace_callback(
371 '/<!--IWLINK (.*?)-->/',
374 wfProfileOut( __METHOD__
);
378 * Replace <!--LINK--> link placeholders with plain text of links
379 * (not HTML-formatted).
380 * @param string $text
383 function replaceText( $text ) {
384 wfProfileIn( __METHOD__
);
386 $text = preg_replace_callback(
387 '/<!--(LINK|IWLINK) (.*?)-->/',
388 array( &$this, 'replaceTextCallback' ),
391 wfProfileOut( __METHOD__
);
396 * @param array $matches
400 function replaceTextCallback( $matches ) {
403 if( $type == 'LINK' ) {
404 list( $ns, $index ) = explode( ':', $key, 2 );
405 if( isset( $this->internals
[$ns][$index]['text'] ) ) {
406 return $this->internals
[$ns][$index]['text'];
408 } elseif( $type == 'IWLINK' ) {
409 if( isset( $this->interwikis
[$key]['text'] ) ) {
410 return $this->interwikis
[$key]['text'];