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