5dece6db70da1b62b169dd49c97c832d802ed71f
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 * Replace <!--LINK--> link placeholders with actual links, in the buffer
98 * Placeholders created in Skin::makeLinkObj()
99 * Returns an array of link CSS classes, indexed by PDBK.
101 function replace( &$text ) {
102 wfProfileIn( __METHOD__
);
104 $colours = $this->replaceInternal( $text );
105 $this->replaceInterwiki( $text );
107 wfProfileOut( __METHOD__
);
112 * Replace internal links
114 protected function replaceInternal( &$text ) {
115 if ( !$this->internals
) {
119 wfProfileIn( __METHOD__
);
120 global $wgUser, $wgContLang;
124 $linkcolour_ids = array();
125 $sk = $this->parent
->getOptions()->getSkin();
126 $linkCache = LinkCache
::singleton();
127 $output = $this->parent
->getOutput();
129 wfProfileIn( __METHOD__
.'-check' );
130 $dbr = wfGetDB( DB_SLAVE
);
131 $page = $dbr->tableName( 'page' );
132 $threshold = $wgUser->getOption('stubthreshold');
135 ksort( $this->internals
);
140 foreach ( $this->internals
as $ns => $entries ) {
141 foreach ( $entries as $index => $entry ) {
143 $title = $entry['title'];
144 $pdbk = $entry['pdbk'];
146 # Skip invalid entries.
147 # Result will be ugly, but prevents crash.
148 if ( is_null( $title ) ) {
152 # Check if it's a static known link, e.g. interwiki
153 if ( $title->isAlwaysKnown() ) {
154 $colours[$pdbk] = '';
155 } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
156 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
157 $output->addLink( $title, $id );
158 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
159 $colours[$pdbk] = 'new';
160 } elseif ( $title->getNamespace() == NS_SPECIAL
&& !SpecialPage
::exists( $pdbk ) ) {
161 $colours[$pdbk] = 'new';
163 # Not in the link cache, add it to the query
164 if ( !isset( $current ) ) {
166 $query = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len";
167 $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN(";
168 } elseif ( $current != $ns ) {
170 $query .= ")) OR (page_namespace=$ns AND page_title IN(";
175 $query .= $dbr->addQuotes( $title->getDBkey() );
182 $res = $dbr->query( $query, __METHOD__
);
184 # Fetch data and form into an associative array
185 # non-existent = broken
186 while ( $s = $dbr->fetchObject($res) ) {
187 $title = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
188 $pdbk = $title->getPrefixedDBkey();
189 $linkCache->addGoodLinkObj( $s->page_id
, $title, $s->page_len
, $s->page_is_redirect
);
190 $output->addLink( $title, $s->page_id
);
191 # FIXME: convoluted data flow
192 # The redirect status and length is passed to getLinkColour via the LinkCache
193 # Use formal parameters instead
194 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
195 //add id to the extension todolist
196 $linkcolour_ids[$s->page_id
] = $pdbk;
199 //pass an array of page_ids to an extension
200 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
202 wfProfileOut( __METHOD__
.'-check' );
204 # Do a second query for different language variants of links and categories
205 if($wgContLang->hasVariants()){
206 $linkBatch = new LinkBatch();
207 $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
208 $categoryMap = array(); // maps $category_variant => $category (dbkeys)
209 $varCategories = array(); // category replacements oldDBkey => newDBkey
211 $categories = $output->getCategoryLinks();
213 // Add variants of links to link batch
214 foreach ( $this->internals
as $ns => $entries ) {
215 foreach ( $entries as $index => $entry ) {
217 $pdbk = $entry['pdbk'];
218 $title = $entry['title'];
219 $titleText = $title->getText();
221 // generate all variants of the link title text
222 $allTextVariants = $wgContLang->convertLinkToAllVariants($titleText);
224 // if link was not found (in first query), add all variants to query
225 if ( !isset($colours[$pdbk]) ){
226 foreach($allTextVariants as $textVariant){
227 if($textVariant != $titleText){
228 $variantTitle = Title
::makeTitle( $ns, $textVariant );
229 if(is_null($variantTitle)) continue;
230 $linkBatch->addObj( $variantTitle );
231 $variantMap[$variantTitle->getPrefixedDBkey()][] = $key;
238 // process categories, check if a category exists in some variant
239 foreach( $categories as $category ){
240 $variants = $wgContLang->convertLinkToAllVariants($category);
241 foreach($variants as $variant){
242 if($variant != $category){
243 $variantTitle = Title
::newFromDBkey( Title
::makeName(NS_CATEGORY
,$variant) );
244 if(is_null($variantTitle)) continue;
245 $linkBatch->addObj( $variantTitle );
246 $categoryMap[$variant] = $category;
252 if(!$linkBatch->isEmpty()){
254 $titleClause = $linkBatch->constructSet('page', $dbr);
256 $variantQuery = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len";
258 $variantQuery .= " FROM $page WHERE $titleClause";
260 $varRes = $dbr->query( $variantQuery, __METHOD__
);
262 // for each found variants, figure out link holders and replace
263 while ( $s = $dbr->fetchObject($varRes) ) {
265 $variantTitle = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
266 $varPdbk = $variantTitle->getPrefixedDBkey();
267 $vardbk = $variantTitle->getDBkey();
269 $holderKeys = array();
270 if(isset($variantMap[$varPdbk])){
271 $holderKeys = $variantMap[$varPdbk];
272 $linkCache->addGoodLinkObj( $s->page_id
, $variantTitle, $s->page_len
, $s->page_is_redirect
);
273 $output->addLink( $variantTitle, $s->page_id
);
276 // loop over link holders
277 foreach($holderKeys as $key){
278 list( $ns, $index ) = explode( ':', $key, 2 );
279 $entry =& $this->internals
[$ns][$index];
280 $pdbk = $entry['pdbk'];
282 if(!isset($colours[$pdbk])){
283 // found link in some of the variants, replace the link holder data
284 $entry['title'] = $variantTitle;
285 $entry['pdbk'] = $varPdbk;
287 // set pdbk and colour
288 # FIXME: convoluted data flow
289 # The redirect status and length is passed to getLinkColour via the LinkCache
290 # Use formal parameters instead
291 $colours[$varPdbk] = $sk->getLinkColour( $variantTitle, $threshold );
292 $linkcolour_ids[$s->page_id
] = $pdbk;
294 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
297 // check if the object is a variant of a category
298 if(isset($categoryMap[$vardbk])){
299 $oldkey = $categoryMap[$vardbk];
300 if($oldkey != $vardbk)
301 $varCategories[$oldkey]=$vardbk;
305 // rebuild the categories in original order (if there are replacements)
306 if(count($varCategories)>0){
308 $originalCats = $output->getCategories();
309 foreach($originalCats as $cat => $sortkey){
310 // make the replacement
311 if( array_key_exists($cat,$varCategories) )
312 $newCats[$varCategories[$cat]] = $sortkey;
313 else $newCats[$cat] = $sortkey;
315 $this->parent
->mOutput
->setCategoryLinks($newCats);
320 # Construct search and replace arrays
321 wfProfileIn( __METHOD__
.'-construct' );
322 $replacePairs = array();
323 foreach ( $this->internals
as $ns => $entries ) {
324 foreach ( $entries as $index => $entry ) {
325 $pdbk = $entry['pdbk'];
326 $title = $entry['title'];
327 $query = isset( $entry['query'] ) ?
$entry['query'] : '';
329 $searchkey = "<!--LINK $key-->";
330 if ( !isset( $colours[$pdbk] ) ||
$colours[$pdbk] == 'new' ) {
331 $linkCache->addBadLinkObj( $title );
332 $colours[$pdbk] = 'new';
333 $output->addLink( $title, 0 );
334 $replacePairs[$searchkey] = $sk->makeBrokenLinkObj( $title,
338 $replacePairs[$searchkey] = $sk->makeColouredLinkObj( $title, $colours[$pdbk],
344 $replacer = new HashtableReplacer( $replacePairs, 1 );
345 wfProfileOut( __METHOD__
.'-construct' );
348 wfProfileIn( __METHOD__
.'-replace' );
349 $text = preg_replace_callback(
350 '/(<!--LINK .*?-->)/',
354 wfProfileOut( __METHOD__
.'-replace' );
355 wfProfileOut( __METHOD__
);
359 * Replace interwiki links
361 protected function replaceInterwiki( &$text ) {
362 if ( empty( $this->interwikis
) ) {
366 wfProfileIn( __METHOD__
);
367 # Make interwiki link HTML
368 $sk = $this->parent
->getOptions()->getSkin();
369 $replacePairs = array();
370 foreach( $this->interwikis
as $key => $link ) {
371 $replacePairs[$key] = $sk->link( $link['title'], $link['text'] );
373 $replacer = new HashtableReplacer( $replacePairs, 1 );
375 $text = preg_replace_callback(
376 '/<!--IWLINK (.*?)-->/',
379 wfProfileOut( __METHOD__
);
383 * Replace <!--LINK--> link placeholders with plain text of links
384 * (not HTML-formatted).
385 * @param string $text
388 function replaceText( $text ) {
389 wfProfileIn( __METHOD__
);
391 $text = preg_replace_callback(
392 '/<!--(LINK|IWLINK) (.*?)-->/',
393 array( &$this, 'replaceTextCallback' ),
396 wfProfileOut( __METHOD__
);
401 * @param array $matches
405 function replaceTextCallback( $matches ) {
408 if( $type == 'LINK' ) {
409 list( $ns, $index ) = explode( ':', $key, 2 );
410 if( isset( $this->internals
[$ns][$index]['text'] ) ) {
411 return $this->internals
[$ns][$index]['text'];
413 } elseif( $type == 'IWLINK' ) {
414 if( isset( $this->interwikis
[$key]['text'] ) ) {
415 return $this->interwikis
[$key]['text'];