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