* Rewrote StripState to not use ReplacementArray. The memory usage of FSS was excessi...
[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 protected $tempIdOffset;
16
17 function __construct( $parent ) {
18 $this->parent = $parent;
19 }
20
21 /**
22 * Reduce memory usage to reduce the impact of circular references
23 */
24 function __destruct() {
25 foreach ( $this as $name => $value ) {
26 unset( $this->$name );
27 }
28 }
29
30 /**
31 * Don't serialize the parent object, it is big, and not needed when it is
32 * a parameter to mergeForeign(), which is the only application of
33 * serializing at present.
34 */
35 function __sleep() {
36 return array( 'internals', 'interwikis', 'size' );
37 }
38
39 /**
40 * Merge another LinkHolderArray into this one
41 * @param $other LinkHolderArray
42 */
43 function merge( $other ) {
44 foreach ( $other->internals as $ns => $entries ) {
45 $this->size += count( $entries );
46 if ( !isset( $this->internals[$ns] ) ) {
47 $this->internals[$ns] = $entries;
48 } else {
49 $this->internals[$ns] += $entries;
50 }
51 }
52 $this->interwikis += $other->interwikis;
53 }
54
55 /**
56 * Merge a LinkHolderArray from another parser instance into this one. The
57 * keys will not be preserved. Any text which went with the old
58 * LinkHolderArray and needs to work with the new one should be passed in
59 * the $texts array. The strings in this array will have their link holders
60 * converted for use in the destination link holder. The resulting array of
61 * strings will be returned.
62 *
63 * @param $other LinkHolderArray
64 * @param $text Array of strings
65 * @return Array
66 */
67 function mergeForeign( $other, $texts ) {
68 $this->tempIdOffset = $idOffset = $this->parent->nextLinkID();
69 $maxId = 0;
70
71 # Renumber internal links
72 foreach ( $other->internals as $ns => $nsLinks ) {
73 foreach ( $nsLinks as $key => $entry ) {
74 $newKey = $idOffset + $key;
75 $this->internals[$ns][$newKey] = $entry;
76 $maxId = $newKey > $maxId ? $newKey : $maxId;
77 }
78 }
79 $texts = preg_replace_callback( '(<!--LINK \d+:)(\d+)(-->)',
80 array( $this, 'mergeForeignCallback' ), $texts );
81
82 # Renumber interwiki links
83 foreach ( $links['interwiki'] as $key => $entry ) {
84 $newKey = $idOffset + $key;
85 $this->interwikis[$newKey] = $entry;
86 $maxId = $newKey > $maxId ? $newKey : $maxId;
87
88 }
89 $texts = preg_replace_callback( '(<!--IWLINK )(\d+)(-->)',
90 array( $this, 'mergeForeignCallback' ), $texts );
91
92 # Set the parent link ID to be the highest used ID
93 $this->parent->setLinkID( $maxId );
94 $this->tempIdOffset = null;
95 }
96
97 protected function mergeForeignCallback( $m ) {
98 return $m[1] . ( $m[2] + $this->tempIdOffset ) . $m[3];
99 }
100
101 /**
102 * Get a subset of the current LinkHolderArray which is sufficient to
103 * interpret the given text.
104 */
105 function getSubArray( $text ) {
106 $sub = new LinkHolderArray( $this->parent );
107
108 # Internal links
109 $pos = 0;
110 while ( $pos < strlen( $text ) ) {
111 if ( !preg_match( '/<!--LINK (\d+):(\d+)-->/',
112 $text, $m, PREG_OFFSET_CAPTURE, $pos ) )
113 {
114 break;
115 }
116 $ns = $m[1][0];
117 $key = $m[2][0];
118 $sub->internals[$ns][$key] = $this->internals[$ns][$key];
119 $pos = $m[0][1] + strlen( $m[0][0] );
120 }
121
122 # Interwiki links
123 $pos = 0;
124 while ( $pos < strlen( $text ) ) {
125 if ( !preg_match( '/<!--IWLINK (\d+)-->/', $text, $m, PREG_OFFSET_CAPTURE, $pos ) ) {
126 break;
127 }
128 $key = $m[1][0];
129 $sub->interwikis[$key] = $this->interwikis[$key];
130 $pos = $m[0][1] + strlen( $m[0][0] );
131 }
132 return $sub;
133 }
134
135 /**
136 * Returns true if the memory requirements of this object are getting large
137 */
138 function isBig() {
139 global $wgLinkHolderBatchSize;
140 return $this->size > $wgLinkHolderBatchSize;
141 }
142
143 /**
144 * Clear all stored link holders.
145 * Make sure you don't have any text left using these link holders, before you call this
146 */
147 function clear() {
148 $this->internals = array();
149 $this->interwikis = array();
150 $this->size = 0;
151 }
152
153 /**
154 * Make a link placeholder. The text returned can be later resolved to a real link with
155 * replaceLinkHolders(). This is done for two reasons: firstly to avoid further
156 * parsing of interwiki links, and secondly to allow all existence checks and
157 * article length checks (for stub links) to be bundled into a single query.
158 *
159 * @param $nt Title
160 */
161 function makeHolder( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
162 wfProfileIn( __METHOD__ );
163 if ( ! is_object($nt) ) {
164 # Fail gracefully
165 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
166 } else {
167 # Separate the link trail from the rest of the link
168 list( $inside, $trail ) = Linker::splitTrail( $trail );
169
170 $entry = array(
171 'title' => $nt,
172 'text' => $prefix.$text.$inside,
173 'pdbk' => $nt->getPrefixedDBkey(),
174 );
175 if ( $query !== '' ) {
176 $entry['query'] = $query;
177 }
178
179 if ( $nt->isExternal() ) {
180 // Use a globally unique ID to keep the objects mergable
181 $key = $this->parent->nextLinkID();
182 $this->interwikis[$key] = $entry;
183 $retVal = "<!--IWLINK $key-->{$trail}";
184 } else {
185 $key = $this->parent->nextLinkID();
186 $ns = $nt->getNamespace();
187 $this->internals[$ns][$key] = $entry;
188 $retVal = "<!--LINK $ns:$key-->{$trail}";
189 }
190 $this->size++;
191 }
192 wfProfileOut( __METHOD__ );
193 return $retVal;
194 }
195
196 /**
197 * FIXME: update documentation. makeLinkObj() is deprecated.
198 * Replace <!--LINK--> link placeholders with actual links, in the buffer
199 * Placeholders created in Skin::makeLinkObj()
200 * Returns an array of link CSS classes, indexed by PDBK.
201 */
202 function replace( &$text ) {
203 wfProfileIn( __METHOD__ );
204
205 $colours = $this->replaceInternal( $text );
206 $this->replaceInterwiki( $text );
207
208 wfProfileOut( __METHOD__ );
209 return $colours;
210 }
211
212 /**
213 * Replace internal links
214 */
215 protected function replaceInternal( &$text ) {
216 if ( !$this->internals ) {
217 return;
218 }
219
220 wfProfileIn( __METHOD__ );
221 global $wgContLang;
222
223 $colours = array();
224 $sk = $this->parent->getOptions()->getSkin( $this->parent->mTitle );
225 $linkCache = LinkCache::singleton();
226 $output = $this->parent->getOutput();
227
228 wfProfileIn( __METHOD__.'-check' );
229 $dbr = wfGetDB( DB_SLAVE );
230 $page = $dbr->tableName( 'page' );
231 $threshold = $this->parent->getOptions()->getStubThreshold();
232
233 # Sort by namespace
234 ksort( $this->internals );
235
236 $linkcolour_ids = array();
237
238 # Generate query
239 $query = false;
240 $current = null;
241 foreach ( $this->internals as $ns => $entries ) {
242 foreach ( $entries as $entry ) {
243 $title = $entry['title'];
244 $pdbk = $entry['pdbk'];
245
246 # Skip invalid entries.
247 # Result will be ugly, but prevents crash.
248 if ( is_null( $title ) ) {
249 continue;
250 }
251
252 # Check if it's a static known link, e.g. interwiki
253 if ( $title->isAlwaysKnown() ) {
254 $colours[$pdbk] = '';
255 } elseif ( $ns == NS_SPECIAL ) {
256 $colours[$pdbk] = 'new';
257 } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
258 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
259 $output->addLink( $title, $id );
260 $linkcolour_ids[$id] = $pdbk;
261 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
262 $colours[$pdbk] = 'new';
263 } else {
264 # Not in the link cache, add it to the query
265 if ( !isset( $current ) ) {
266 $current = $ns;
267 $query = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len, page_latest";
268 $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN(";
269 } elseif ( $current != $ns ) {
270 $current = $ns;
271 $query .= ")) OR (page_namespace=$ns AND page_title IN(";
272 } else {
273 $query .= ', ';
274 }
275
276 $query .= $dbr->addQuotes( $title->getDBkey() );
277 }
278 }
279 }
280 if ( $query ) {
281 $query .= '))';
282
283 $res = $dbr->query( $query, __METHOD__ );
284
285 # Fetch data and form into an associative array
286 # non-existent = broken
287 foreach ( $res as $s ) {
288 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
289 $pdbk = $title->getPrefixedDBkey();
290 $linkCache->addGoodLinkObj( $s->page_id, $title, $s->page_len, $s->page_is_redirect, $s->page_latest );
291 $output->addLink( $title, $s->page_id );
292 # FIXME: convoluted data flow
293 # The redirect status and length is passed to getLinkColour via the LinkCache
294 # Use formal parameters instead
295 $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
296 //add id to the extension todolist
297 $linkcolour_ids[$s->page_id] = $pdbk;
298 }
299 unset( $res );
300 }
301 if ( count($linkcolour_ids) ) {
302 //pass an array of page_ids to an extension
303 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
304 }
305 wfProfileOut( __METHOD__.'-check' );
306
307 # Do a second query for different language variants of links and categories
308 if($wgContLang->hasVariants()) {
309 $this->doVariants( $colours );
310 }
311
312 # Construct search and replace arrays
313 wfProfileIn( __METHOD__.'-construct' );
314 $replacePairs = array();
315 foreach ( $this->internals as $ns => $entries ) {
316 foreach ( $entries as $index => $entry ) {
317 $pdbk = $entry['pdbk'];
318 $title = $entry['title'];
319 $query = isset( $entry['query'] ) ? $entry['query'] : '';
320 $key = "$ns:$index";
321 $searchkey = "<!--LINK $key-->";
322 if ( !isset( $colours[$pdbk] ) || $colours[$pdbk] == 'new' ) {
323 $linkCache->addBadLinkObj( $title );
324 $colours[$pdbk] = 'new';
325 $output->addLink( $title, 0 );
326 // FIXME: replace deprecated makeBrokenLinkObj() by link()
327 $replacePairs[$searchkey] = $sk->makeBrokenLinkObj( $title,
328 $entry['text'],
329 $query );
330 } else {
331 // FIXME: replace deprecated makeColouredLinkObj() by link()
332 $replacePairs[$searchkey] = $sk->makeColouredLinkObj( $title, $colours[$pdbk],
333 $entry['text'],
334 $query );
335 }
336 }
337 }
338 $replacer = new HashtableReplacer( $replacePairs, 1 );
339 wfProfileOut( __METHOD__.'-construct' );
340
341 # Do the thing
342 wfProfileIn( __METHOD__.'-replace' );
343 $text = preg_replace_callback(
344 '/(<!--LINK .*?-->)/',
345 $replacer->cb(),
346 $text);
347
348 wfProfileOut( __METHOD__.'-replace' );
349 wfProfileOut( __METHOD__ );
350 }
351
352 /**
353 * Replace interwiki links
354 */
355 protected function replaceInterwiki( &$text ) {
356 if ( empty( $this->interwikis ) ) {
357 return;
358 }
359
360 wfProfileIn( __METHOD__ );
361 # Make interwiki link HTML
362 $sk = $this->parent->getOptions()->getSkin( $this->parent->mTitle );
363 $output = $this->parent->getOutput();
364 $replacePairs = array();
365 foreach( $this->interwikis as $key => $link ) {
366 $replacePairs[$key] = $sk->link( $link['title'], $link['text'] );
367 $output->addInterwikiLink( $link['title'] );
368 }
369 $replacer = new HashtableReplacer( $replacePairs, 1 );
370
371 $text = preg_replace_callback(
372 '/<!--IWLINK (.*?)-->/',
373 $replacer->cb(),
374 $text );
375 wfProfileOut( __METHOD__ );
376 }
377
378 /**
379 * Modify $this->internals and $colours according to language variant linking rules
380 */
381 protected function doVariants( &$colours ) {
382 global $wgContLang;
383 $linkBatch = new LinkBatch();
384 $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
385 $output = $this->parent->getOutput();
386 $linkCache = LinkCache::singleton();
387 $sk = $this->parent->getOptions()->getSkin( $this->parent->mTitle );
388 $threshold = $this->parent->getOptions()->getStubThreshold();
389 $titlesToBeConverted = '';
390 $titlesAttrs = array();
391
392 // Concatenate titles to a single string, thus we only need auto convert the
393 // single string to all variants. This would improve parser's performance
394 // significantly.
395 foreach ( $this->internals as $ns => $entries ) {
396 foreach ( $entries as $index => $entry ) {
397 $pdbk = $entry['pdbk'];
398 // we only deal with new links (in its first query)
399 if ( !isset( $colours[$pdbk] ) ) {
400 $title = $entry['title'];
401 $titleText = $title->getText();
402 $titlesAttrs[] = array(
403 'ns' => $ns,
404 'key' => "$ns:$index",
405 'titleText' => $titleText,
406 );
407 // separate titles with \0 because it would never appears
408 // in a valid title
409 $titlesToBeConverted .= $titleText . "\0";
410 }
411 }
412 }
413
414 // Now do the conversion and explode string to text of titles
415 $titlesAllVariants = $wgContLang->autoConvertToAllVariants( $titlesToBeConverted );
416 $allVariantsName = array_keys( $titlesAllVariants );
417 foreach ( $titlesAllVariants as &$titlesVariant ) {
418 $titlesVariant = explode( "\0", $titlesVariant );
419 }
420 $l = count( $titlesAttrs );
421 // Then add variants of links to link batch
422 for ( $i = 0; $i < $l; $i ++ ) {
423 foreach ( $allVariantsName as $variantName ) {
424 $textVariant = $titlesAllVariants[$variantName][$i];
425 extract( $titlesAttrs[$i] );
426 if($textVariant != $titleText){
427 $variantTitle = Title::makeTitle( $ns, $textVariant );
428 if( is_null( $variantTitle ) ) {
429 continue;
430 }
431 $linkBatch->addObj( $variantTitle );
432 $variantMap[$variantTitle->getPrefixedDBkey()][] = $key;
433 }
434 }
435 }
436
437 // process categories, check if a category exists in some variant
438 $categoryMap = array(); // maps $category_variant => $category (dbkeys)
439 $varCategories = array(); // category replacements oldDBkey => newDBkey
440 foreach( $output->getCategoryLinks() as $category ){
441 $variants = $wgContLang->autoConvertToAllVariants( $category );
442 foreach($variants as $variant){
443 if($variant != $category){
444 $variantTitle = Title::newFromDBkey( Title::makeName(NS_CATEGORY,$variant) );
445 if(is_null($variantTitle)) continue;
446 $linkBatch->addObj( $variantTitle );
447 $categoryMap[$variant] = $category;
448 }
449 }
450 }
451
452
453 if(!$linkBatch->isEmpty()){
454 // construct query
455 $dbr = wfGetDB( DB_SLAVE );
456 $varRes = $dbr->select( 'page',
457 array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect', 'page_len' ),
458 $linkBatch->constructSet( 'page', $dbr ),
459 __METHOD__
460 );
461
462 $linkcolour_ids = array();
463
464 // for each found variants, figure out link holders and replace
465 foreach ( $varRes as $s ) {
466
467 $variantTitle = Title::makeTitle( $s->page_namespace, $s->page_title );
468 $varPdbk = $variantTitle->getPrefixedDBkey();
469 $vardbk = $variantTitle->getDBkey();
470
471 $holderKeys = array();
472 if( isset( $variantMap[$varPdbk] ) ) {
473 $holderKeys = $variantMap[$varPdbk];
474 $linkCache->addGoodLinkObj( $s->page_id, $variantTitle, $s->page_len, $s->page_is_redirect );
475 $output->addLink( $variantTitle, $s->page_id );
476 }
477
478 // loop over link holders
479 foreach( $holderKeys as $key ) {
480 list( $ns, $index ) = explode( ':', $key, 2 );
481 $entry =& $this->internals[$ns][$index];
482 $pdbk = $entry['pdbk'];
483
484 if(!isset($colours[$pdbk])){
485 // found link in some of the variants, replace the link holder data
486 $entry['title'] = $variantTitle;
487 $entry['pdbk'] = $varPdbk;
488
489 // set pdbk and colour
490 # FIXME: convoluted data flow
491 # The redirect status and length is passed to getLinkColour via the LinkCache
492 # Use formal parameters instead
493 $colours[$varPdbk] = $sk->getLinkColour( $variantTitle, $threshold );
494 $linkcolour_ids[$s->page_id] = $pdbk;
495 }
496 }
497
498 // check if the object is a variant of a category
499 if(isset($categoryMap[$vardbk])){
500 $oldkey = $categoryMap[$vardbk];
501 if($oldkey != $vardbk)
502 $varCategories[$oldkey]=$vardbk;
503 }
504 }
505 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
506
507 // rebuild the categories in original order (if there are replacements)
508 if(count($varCategories)>0){
509 $newCats = array();
510 $originalCats = $output->getCategories();
511 foreach($originalCats as $cat => $sortkey){
512 // make the replacement
513 if( array_key_exists($cat,$varCategories) )
514 $newCats[$varCategories[$cat]] = $sortkey;
515 else $newCats[$cat] = $sortkey;
516 }
517 $output->setCategoryLinks($newCats);
518 }
519 }
520 }
521
522 /**
523 * Replace <!--LINK--> link placeholders with plain text of links
524 * (not HTML-formatted).
525 *
526 * @param $text String
527 * @return String
528 */
529 function replaceText( $text ) {
530 wfProfileIn( __METHOD__ );
531
532 $text = preg_replace_callback(
533 '/<!--(LINK|IWLINK) (.*?)-->/',
534 array( &$this, 'replaceTextCallback' ),
535 $text );
536
537 wfProfileOut( __METHOD__ );
538 return $text;
539 }
540
541 /**
542 * Callback for replaceText()
543 *
544 * @param $matches Array
545 * @return string
546 * @private
547 */
548 function replaceTextCallback( $matches ) {
549 $type = $matches[1];
550 $key = $matches[2];
551 if( $type == 'LINK' ) {
552 list( $ns, $index ) = explode( ':', $key, 2 );
553 if( isset( $this->internals[$ns][$index]['text'] ) ) {
554 return $this->internals[$ns][$index]['text'];
555 }
556 } elseif( $type == 'IWLINK' ) {
557 if( isset( $this->interwikis[$key]['text'] ) ) {
558 return $this->interwikis[$key]['text'];
559 }
560 }
561 return $matches[0];
562 }
563 }