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