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