Per Platonides; follow-up r78201: add "stubthreshold" to ParserOptions; also provides...
[lhc/web/wiklou.git] / includes / parser / LinkHolderArray.php
1 <?php
2 /**
3 * Holder of replacement pairs for wiki links
4 *
5 * @file
6 */
7
8 /**
9 * @ingroup Parser
10 */
11 class LinkHolderArray {
12 var $internals = array(), $interwikis = array();
13 var $size = 0;
14 var $parent;
15
16 function __construct( $parent ) {
17 $this->parent = $parent;
18 }
19
20 /**
21 * Reduce memory usage to reduce the impact of circular references
22 */
23 function __destruct() {
24 foreach ( $this as $name => $value ) {
25 unset( $this->$name );
26 }
27 }
28
29 /**
30 * Merge another LinkHolderArray into this one
31 * @param $other LinkHolderArray
32 */
33 function merge( $other ) {
34 foreach ( $other->internals as $ns => $entries ) {
35 $this->size += count( $entries );
36 if ( !isset( $this->internals[$ns] ) ) {
37 $this->internals[$ns] = $entries;
38 } else {
39 $this->internals[$ns] += $entries;
40 }
41 }
42 $this->interwikis += $other->interwikis;
43 }
44
45 /**
46 * Returns true if the memory requirements of this object are getting large
47 */
48 function isBig() {
49 global $wgLinkHolderBatchSize;
50 return $this->size > $wgLinkHolderBatchSize;
51 }
52
53 /**
54 * Clear all stored link holders.
55 * Make sure you don't have any text left using these link holders, before you call this
56 */
57 function clear() {
58 $this->internals = array();
59 $this->interwikis = array();
60 $this->size = 0;
61 }
62
63 /**
64 * Make a link placeholder. The text returned can be later resolved to a real link with
65 * replaceLinkHolders(). This is done for two reasons: firstly to avoid further
66 * parsing of interwiki links, and secondly to allow all existence checks and
67 * article length checks (for stub links) to be bundled into a single query.
68 *
69 * @param $nt Title
70 */
71 function makeHolder( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
72 wfProfileIn( __METHOD__ );
73 if ( ! is_object($nt) ) {
74 # Fail gracefully
75 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
76 } else {
77 # Separate the link trail from the rest of the link
78 list( $inside, $trail ) = Linker::splitTrail( $trail );
79
80 $entry = array(
81 'title' => $nt,
82 'text' => $prefix.$text.$inside,
83 'pdbk' => $nt->getPrefixedDBkey(),
84 );
85 if ( $query !== '' ) {
86 $entry['query'] = $query;
87 }
88
89 if ( $nt->isExternal() ) {
90 // Use a globally unique ID to keep the objects mergable
91 $key = $this->parent->nextLinkID();
92 $this->interwikis[$key] = $entry;
93 $retVal = "<!--IWLINK $key-->{$trail}";
94 } else {
95 $key = $this->parent->nextLinkID();
96 $ns = $nt->getNamespace();
97 $this->internals[$ns][$key] = $entry;
98 $retVal = "<!--LINK $ns:$key-->{$trail}";
99 }
100 $this->size++;
101 }
102 wfProfileOut( __METHOD__ );
103 return $retVal;
104 }
105
106 /**
107 * FIXME: update documentation. makeLinkObj() is deprecated.
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.
111 */
112 function replace( &$text ) {
113 wfProfileIn( __METHOD__ );
114
115 $colours = $this->replaceInternal( $text );
116 $this->replaceInterwiki( $text );
117
118 wfProfileOut( __METHOD__ );
119 return $colours;
120 }
121
122 /**
123 * Replace internal links
124 */
125 protected function replaceInternal( &$text ) {
126 if ( !$this->internals ) {
127 return;
128 }
129
130 wfProfileIn( __METHOD__ );
131 global $wgContLang;
132
133 $colours = array();
134 $sk = $this->parent->getOptions()->getSkin( $this->parent->mTitle );
135 $linkCache = LinkCache::singleton();
136 $output = $this->parent->getOutput();
137
138 wfProfileIn( __METHOD__.'-check' );
139 $dbr = wfGetDB( DB_SLAVE );
140 $page = $dbr->tableName( 'page' );
141 $threshold = $this->parent->getOptions()->getStubThreshold();
142
143 # Sort by namespace
144 ksort( $this->internals );
145
146 $linkcolour_ids = array();
147
148 # Generate query
149 $query = false;
150 $current = null;
151 foreach ( $this->internals as $ns => $entries ) {
152 foreach ( $entries as $entry ) {
153 $title = $entry['title'];
154 $pdbk = $entry['pdbk'];
155
156 # Skip invalid entries.
157 # Result will be ugly, but prevents crash.
158 if ( is_null( $title ) ) {
159 continue;
160 }
161
162 # Check if it's a static known link, e.g. interwiki
163 if ( $title->isAlwaysKnown() ) {
164 $colours[$pdbk] = '';
165 } elseif ( $ns == NS_SPECIAL ) {
166 $colours[$pdbk] = 'new';
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';
173 } else {
174 # Not in the link cache, add it to the query
175 if ( !isset( $current ) ) {
176 $current = $ns;
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 ) {
180 $current = $ns;
181 $query .= ")) OR (page_namespace=$ns AND page_title IN(";
182 } else {
183 $query .= ', ';
184 }
185
186 $query .= $dbr->addQuotes( $title->getDBkey() );
187 }
188 }
189 }
190 if ( $query ) {
191 $query .= '))';
192
193 $res = $dbr->query( $query, __METHOD__ );
194
195 # Fetch data and form into an associative array
196 # non-existent = broken
197 foreach ( $res as $s ) {
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;
208 }
209 unset( $res );
210 }
211 if ( count($linkcolour_ids) ) {
212 //pass an array of page_ids to an extension
213 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
214 }
215 wfProfileOut( __METHOD__.'-check' );
216
217 # Do a second query for different language variants of links and categories
218 if($wgContLang->hasVariants()) {
219 $this->doVariants( $colours );
220 }
221
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'] : '';
230 $key = "$ns:$index";
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,
238 $entry['text'],
239 $query );
240 } else {
241 // FIXME: replace deprecated makeColouredLinkObj() by link()
242 $replacePairs[$searchkey] = $sk->makeColouredLinkObj( $title, $colours[$pdbk],
243 $entry['text'],
244 $query );
245 }
246 }
247 }
248 $replacer = new HashtableReplacer( $replacePairs, 1 );
249 wfProfileOut( __METHOD__.'-construct' );
250
251 # Do the thing
252 wfProfileIn( __METHOD__.'-replace' );
253 $text = preg_replace_callback(
254 '/(<!--LINK .*?-->)/',
255 $replacer->cb(),
256 $text);
257
258 wfProfileOut( __METHOD__.'-replace' );
259 wfProfileOut( __METHOD__ );
260 }
261
262 /**
263 * Replace interwiki links
264 */
265 protected function replaceInterwiki( &$text ) {
266 if ( empty( $this->interwikis ) ) {
267 return;
268 }
269
270 wfProfileIn( __METHOD__ );
271 # Make interwiki link HTML
272 $sk = $this->parent->getOptions()->getSkin( $this->parent->mTitle );
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'] );
278 }
279 $replacer = new HashtableReplacer( $replacePairs, 1 );
280
281 $text = preg_replace_callback(
282 '/<!--IWLINK (.*?)-->/',
283 $replacer->cb(),
284 $text );
285 wfProfileOut( __METHOD__ );
286 }
287
288 /**
289 * Modify $this->internals and $colours according to language variant linking rules
290 */
291 protected function doVariants( &$colours ) {
292 global $wgContLang;
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( $this->parent->mTitle );
298 $threshold = $this->parent->getOptions()->getStubThreshold();
299 $titlesToBeConverted = '';
300 $titlesAttrs = array();
301
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
304 // significantly.
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(
313 'ns' => $ns,
314 'key' => "$ns:$index",
315 'titleText' => $titleText,
316 );
317 // separate titles with \0 because it would never appears
318 // in a valid title
319 $titlesToBeConverted .= $titleText . "\0";
320 }
321 }
322 }
323
324 // Now do the conversion and explode string to text of titles
325 $titlesAllVariants = $wgContLang->autoConvertToAllVariants( $titlesToBeConverted );
326 $allVariantsName = array_keys( $titlesAllVariants );
327 foreach ( $titlesAllVariants as &$titlesVariant ) {
328 $titlesVariant = explode( "\0", $titlesVariant );
329 }
330 $l = count( $titlesAttrs );
331 // Then add variants of links to link batch
332 for ( $i = 0; $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 ) ) {
339 continue;
340 }
341 $linkBatch->addObj( $variantTitle );
342 $variantMap[$variantTitle->getPrefixedDBkey()][] = $key;
343 }
344 }
345 }
346
347 // process categories, check if a category exists in some variant
348 $categoryMap = array(); // maps $category_variant => $category (dbkeys)
349 $varCategories = array(); // category replacements oldDBkey => newDBkey
350 foreach( $output->getCategoryLinks() as $category ){
351 $variants = $wgContLang->autoConvertToAllVariants( $category );
352 foreach($variants as $variant){
353 if($variant != $category){
354 $variantTitle = Title::newFromDBkey( Title::makeName(NS_CATEGORY,$variant) );
355 if(is_null($variantTitle)) continue;
356 $linkBatch->addObj( $variantTitle );
357 $categoryMap[$variant] = $category;
358 }
359 }
360 }
361
362
363 if(!$linkBatch->isEmpty()){
364 // construct query
365 $dbr = wfGetDB( DB_SLAVE );
366 $varRes = $dbr->select( 'page',
367 array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect', 'page_len' ),
368 $linkBatch->constructSet( 'page', $dbr ),
369 __METHOD__
370 );
371
372 $linkcolour_ids = array();
373
374 // for each found variants, figure out link holders and replace
375 foreach ( $varRes as $s ) {
376
377 $variantTitle = Title::makeTitle( $s->page_namespace, $s->page_title );
378 $varPdbk = $variantTitle->getPrefixedDBkey();
379 $vardbk = $variantTitle->getDBkey();
380
381 $holderKeys = array();
382 if( isset( $variantMap[$varPdbk] ) ) {
383 $holderKeys = $variantMap[$varPdbk];
384 $linkCache->addGoodLinkObj( $s->page_id, $variantTitle, $s->page_len, $s->page_is_redirect );
385 $output->addLink( $variantTitle, $s->page_id );
386 }
387
388 // loop over link holders
389 foreach( $holderKeys as $key ) {
390 list( $ns, $index ) = explode( ':', $key, 2 );
391 $entry =& $this->internals[$ns][$index];
392 $pdbk = $entry['pdbk'];
393
394 if(!isset($colours[$pdbk])){
395 // found link in some of the variants, replace the link holder data
396 $entry['title'] = $variantTitle;
397 $entry['pdbk'] = $varPdbk;
398
399 // set pdbk and colour
400 # FIXME: convoluted data flow
401 # The redirect status and length is passed to getLinkColour via the LinkCache
402 # Use formal parameters instead
403 $colours[$varPdbk] = $sk->getLinkColour( $variantTitle, $threshold );
404 $linkcolour_ids[$s->page_id] = $pdbk;
405 }
406 }
407
408 // check if the object is a variant of a category
409 if(isset($categoryMap[$vardbk])){
410 $oldkey = $categoryMap[$vardbk];
411 if($oldkey != $vardbk)
412 $varCategories[$oldkey]=$vardbk;
413 }
414 }
415 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
416
417 // rebuild the categories in original order (if there are replacements)
418 if(count($varCategories)>0){
419 $newCats = array();
420 $originalCats = $output->getCategories();
421 foreach($originalCats as $cat => $sortkey){
422 // make the replacement
423 if( array_key_exists($cat,$varCategories) )
424 $newCats[$varCategories[$cat]] = $sortkey;
425 else $newCats[$cat] = $sortkey;
426 }
427 $output->setCategoryLinks($newCats);
428 }
429 }
430 }
431
432 /**
433 * Replace <!--LINK--> link placeholders with plain text of links
434 * (not HTML-formatted).
435 *
436 * @param $text String
437 * @return String
438 */
439 function replaceText( $text ) {
440 wfProfileIn( __METHOD__ );
441
442 $text = preg_replace_callback(
443 '/<!--(LINK|IWLINK) (.*?)-->/',
444 array( &$this, 'replaceTextCallback' ),
445 $text );
446
447 wfProfileOut( __METHOD__ );
448 return $text;
449 }
450
451 /**
452 * Callback for replaceText()
453 *
454 * @param $matches Array
455 * @return string
456 * @private
457 */
458 function replaceTextCallback( $matches ) {
459 $type = $matches[1];
460 $key = $matches[2];
461 if( $type == 'LINK' ) {
462 list( $ns, $index ) = explode( ':', $key, 2 );
463 if( isset( $this->internals[$ns][$index]['text'] ) ) {
464 return $this->internals[$ns][$index]['text'];
465 }
466 } elseif( $type == 'IWLINK' ) {
467 if( isset( $this->interwikis[$key]['text'] ) ) {
468 return $this->interwikis[$key]['text'];
469 }
470 }
471 return $matches[0];
472 }
473 }