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