f5ff7f9423fd144874b12178d0cdc31bf25ce1f5
[lhc/web/wiklou.git] / includes / LinkCache.php
1 <?php
2 /**
3 * Cache for article titles (prefixed DB keys) and ids linked from one source
4 * @package MediaWiki
5 * @subpackage Cache
6 */
7
8 /**
9 *
10 */
11 # These are used in incrementalSetup()
12 define ('LINKCACHE_GOOD', 0);
13 define ('LINKCACHE_BAD', 1);
14 define ('LINKCACHE_IMAGE', 2);
15 define ('LINKCACHE_PAGE', 3);
16
17 /**
18 * @package MediaWiki
19 * @subpackage Cache
20 */
21 class LinkCache {
22 // Increment $mClassVer whenever old serialized versions of this class
23 // becomes incompatible with the new version.
24 /* private */ var $mClassVer = 3;
25
26 /* private */ var $mPageLinks;
27 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
28 /* private */ var $mImageLinks, $mCategoryLinks;
29 /* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
30 /* private */ var $mForUpdate;
31
32 /* private */ function getKey( $title ) {
33 global $wgDBname;
34 return $wgDBname.':lc:title:'.$title;
35 }
36
37 function LinkCache() {
38 $this->mActive = true;
39 $this->mPreFilled = false;
40 $this->mForUpdate = false;
41 $this->mPageLinks = array();
42 $this->mGoodLinks = array();
43 $this->mBadLinks = array();
44 $this->mImageLinks = array();
45 $this->mCategoryLinks = array();
46 $this->mOldGoodLinks = array();
47 $this->mOldBadLinks = array();
48 $this->mOldPageLinks = array();
49 }
50
51 /**
52 * General accessor to get/set whether SELECT FOR UPDATE should be used
53 */
54 function forUpdate( $update = NULL ) {
55 return wfSetVar( $this->mForUpdate, $update );
56 }
57
58 function getGoodLinkID( $title ) {
59 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
60 return $this->mGoodLinks[$title];
61 } else {
62 return 0;
63 }
64 }
65
66 function isBadLink( $title ) {
67 return array_key_exists( $title, $this->mBadLinks );
68 }
69
70 function addGoodLinkObj( $id, $title ) {
71 if ( $this->mActive ) {
72 $dbkey = $title->getPrefixedDbKey();
73 $this->mGoodLinks[$dbkey] = $id;
74 $this->mPageLinks[$dbkey] = $title;
75 }
76 }
77
78 function addBadLinkObj( $title ) {
79 $dbkey = $title->getPrefixedDbKey();
80 if ( $this->mActive && ( ! $this->isBadLink( $dbkey ) ) ) {
81 $this->mBadLinks[$dbkey] = 1;
82 $this->mPageLinks[$dbkey] = $title;
83 }
84 }
85
86 function addImageLink( $title ) {
87 if ( $this->mActive ) { $this->mImageLinks[$title] = 1; }
88 }
89
90 function addImageLinkObj( $nt ) {
91 if ( $this->mActive ) { $this->mImageLinks[$nt->getDBkey()] = 1; }
92 }
93
94 function addCategoryLink( $title, $sortkey ) {
95 if ( $this->mActive ) { $this->mCategoryLinks[$title] = $sortkey; }
96 }
97
98 function addCategoryLinkObj( &$nt, $sortkey ) {
99 $this->addCategoryLink( $nt->getDBkey(), $sortkey );
100 }
101
102 function clearBadLink( $title ) {
103 unset( $this->mBadLinks[$title] );
104 $this->clearLink( $title );
105 }
106
107 function clearLink( $title ) {
108 global $wgMemc, $wgLinkCacheMemcached;
109 if( $wgLinkCacheMemcached )
110 $wgMemc->delete( $this->getKey( $title ) );
111 }
112
113 function suspend() { $this->mActive = false; }
114 function resume() { $this->mActive = true; }
115 function getPageLinks() { return $this->mPageLinks; }
116 function getGoodLinks() { return $this->mGoodLinks; }
117 function getBadLinks() { return array_keys( $this->mBadLinks ); }
118 function getImageLinks() { return $this->mImageLinks; }
119 function getCategoryLinks() { return $this->mCategoryLinks; }
120
121 function addLink( $title ) {
122 $nt = Title::newFromDBkey( $title );
123 if( $nt ) {
124 return $this->addLinkObj( $nt );
125 } else {
126 return 0;
127 }
128 }
129
130 function addLinkObj( &$nt ) {
131 global $wgMemc, $wgLinkCacheMemcached, $wgAntiLockFlags;
132 $title = $nt->getPrefixedDBkey();
133 if ( $this->isBadLink( $title ) ) { return 0; }
134 $id = $this->getGoodLinkID( $title );
135 if ( 0 != $id ) { return $id; }
136
137 $fname = 'LinkCache::addLinkObj';
138 wfProfileIn( $fname );
139
140 $ns = $nt->getNamespace();
141 $t = $nt->getDBkey();
142
143 if ( '' == $title ) {
144 wfProfileOut( $fname );
145 return 0;
146 }
147
148 $id = NULL;
149 if( $wgLinkCacheMemcached )
150 $id = $wgMemc->get( $key = $this->getKey( $title ) );
151 if( ! is_integer( $id ) ) {
152 if ( $this->mForUpdate ) {
153 $db =& wfGetDB( DB_MASTER );
154 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
155 $options = array( 'FOR UPDATE' );
156 } else {
157 $options = array();
158 }
159 } else {
160 $db =& wfGetDB( DB_SLAVE );
161 $options = array();
162 }
163
164 $id = $db->selectField( 'page', 'page_id',
165 array( 'page_namespace' => $ns, 'page_title' => $t ),
166 $fname, $options );
167 wfdebug("link cache: id=$id\n");
168 if ( !$id ) {
169 $id = 0;
170 }
171 if( $wgLinkCacheMemcached )
172 $wgMemc->add( $key, $id, 3600*24 );
173 }
174
175 if( 0 == $id ) {
176 $this->addBadLinkObj( $nt );
177 } else {
178 $this->addGoodLinkObj( $id, $nt );
179 }
180 wfProfileOut( $fname );
181 return $id;
182 }
183
184 /**
185 * Bulk-check the pagelinks and page arrays for existence info.
186 * @param Title $fromtitle
187 */
188 function preFill( &$fromtitle ) {
189 global $wgAntiLockFlags;
190 $fname = 'LinkCache::preFill';
191 wfProfileIn( $fname );
192
193 $this->suspend();
194 $id = $fromtitle->getArticleID();
195 $this->resume();
196
197 if( $id == 0 ) {
198 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
199 wfProfileOut( $fname );
200 return;
201 }
202
203 if ( $this->mForUpdate ) {
204 $db =& wfGetDB( DB_MASTER );
205 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
206 $options = 'FOR UPDATE';
207 } else {
208 $options = '';
209 }
210 } else {
211 $db =& wfGetDB( DB_SLAVE );
212 $options = '';
213 }
214
215 $page = $db->tableName( 'page' );
216 $pagelinks = $db->tableName( 'pagelinks' );
217
218 $sql = "SELECT page_id,pl_namespace,pl_title
219 FROM $pagelinks
220 LEFT JOIN $page
221 ON pl_namespace=page_namespace AND pl_title=page_title
222 WHERE pl_from=$id $options";
223 $res = $db->query( $sql, $fname );
224 while( $s = $db->fetchObject( $res ) ) {
225 $title = Title::makeTitle( $s->pl_namespace, $s->pl_title );
226 if( $s->page_id ) {
227 $this->addGoodLinkObj( $s->page_id, $title );
228 } else {
229 $this->addBadLinkObj( $title );
230 }
231 }
232 $this->mPreFilled = true;
233
234 wfProfileOut( $fname );
235 }
236
237 function getGoodAdditions() {
238 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
239 }
240
241 function getBadAdditions() {
242 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
243 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
244 return array_values( array_diff( array_keys( $this->mBadLinks ), array_keys( $this->mOldBadLinks ) ) );
245 }
246
247 function getImageAdditions() {
248 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
249 }
250
251 function getGoodDeletions() {
252 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
253 }
254
255 function getBadDeletions() {
256 return array_values( array_diff( array_keys( $this->mOldBadLinks ), array_keys( $this->mBadLinks ) ));
257 }
258
259 function getImageDeletions() {
260 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
261 }
262
263 function getPageAdditions() {
264 $set = array_diff( array_keys( $this->mPageLinks ), array_keys( $this->mOldPageLinks ) );
265 $out = array();
266 foreach( $set as $key ) {
267 $out[$key] = $this->mPageLinks[$key];
268 }
269 return $out;
270 }
271
272 function getPageDeletions() {
273 $set = array_diff( array_keys( $this->mOldPageLinks ), array_keys( $this->mPageLinks ) );
274 $out = array();
275 foreach( $set as $key ) {
276 $out[$key] = $this->mOldPageLinks[$key];
277 }
278 return $out;
279 }
280
281 /**
282 * Parameters:
283 * @param $which is one of the LINKCACHE_xxx constants
284 * @param $del,$add are the incremental update arrays which will be filled.
285 *
286 * @return Returns whether or not it's worth doing the incremental version.
287 *
288 * For example, if [[List of mathematical topics]] was blanked,
289 * it would take a long, long time to do incrementally.
290 */
291 function incrementalSetup( $which, &$del, &$add ) {
292 if ( ! $this->mPreFilled ) {
293 return false;
294 }
295
296 switch ( $which ) {
297 case LINKCACHE_GOOD:
298 $old =& $this->mOldGoodLinks;
299 $cur =& $this->mGoodLinks;
300 $del = $this->getGoodDeletions();
301 $add = $this->getGoodAdditions();
302 break;
303 case LINKCACHE_BAD:
304 $old =& $this->mOldBadLinks;
305 $cur =& $this->mBadLinks;
306 $del = $this->getBadDeletions();
307 $add = $this->getBadAdditions();
308 break;
309 case LINKCACHE_PAGE:
310 $old =& $this->mOldPageLinks;
311 $cur =& $this->mPageLinks;
312 $del = $this->getPageDeletions();
313 $add = $this->getPageAdditions();
314 break;
315 default: # LINKCACHE_IMAGE
316 return false;
317 }
318
319 return true;
320 }
321
322 /**
323 * Clears cache
324 */
325 function clear() {
326 $this->mPageLinks = array();
327 $this->mGoodLinks = array();
328 $this->mBadLinks = array();
329 $this->mImageLinks = array();
330 $this->mCategoryLinks = array();
331 $this->mOldGoodLinks = array();
332 $this->mOldBadLinks = array();
333 $this->mOldPageLinks = array();
334 }
335
336 /**
337 * Swaps old and current link registers
338 */
339 function swapRegisters() {
340 swap( $this->mGoodLinks, $this->mOldGoodLinks );
341 swap( $this->mBadLinks, $this->mOldBadLinks );
342 swap( $this->mImageLinks, $this->mOldImageLinks );
343 swap( $this->mPageLinks, $this->mOldPageLinks );
344 }
345 }
346
347 /**
348 * Class representing a list of titles
349 * The execute() method checks them all for existence and adds them to a LinkCache object
350 +
351 * @package MediaWiki
352 * @subpackage Cache
353 */
354 class LinkBatch {
355 /**
356 * 2-d array, first index namespace, second index dbkey, value arbitrary
357 */
358 var $data = array();
359
360 function LinkBatch( $arr = array() ) {
361 foreach( $arr as $item ) {
362 $this->addObj( $item );
363 }
364 }
365
366 function addObj( $title ) {
367 $this->add( $title->getNamespace(), $title->getDBkey() );
368 }
369
370 function add( $ns, $dbkey ) {
371 if ( $ns < 0 ) {
372 return;
373 }
374 if ( !array_key_exists( $ns, $this->data ) ) {
375 $this->data[$ns] = array();
376 }
377
378 $this->data[$ns][$dbkey] = 1;
379 }
380
381 function execute( &$cache ) {
382 $fname = 'LinkBatch::execute';
383 $namespaces = array();
384
385 if ( !count( $this->data ) ) {
386 return;
387 }
388
389 wfProfileIn( $fname );
390
391 // Construct query
392 // This is very similar to Parser::replaceLinkHolders
393 $dbr =& wfGetDB( DB_SLAVE );
394 $page = $dbr->tableName( 'page' );
395 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE "
396 . $this->constructSet( 'page', $dbr );
397
398 // Do query
399 $res = $dbr->query( $sql, $fname );
400
401 // Process results
402 // For each returned entry, add it to the list of good links, and remove it from $remaining
403
404 $remaining = $this->data;
405 while ( $row = $dbr->fetchObject( $res ) ) {
406 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
407 $cache->addGoodLinkObj( $row->page_id, $title );
408 unset( $remaining[$row->page_namespace][$row->page_title] );
409 }
410 $dbr->freeResult( $res );
411
412 // The remaining links in $data are bad links, register them as such
413 foreach ( $remaining as $ns => $dbkeys ) {
414 foreach ( $dbkeys as $dbkey => $nothing ) {
415 $title = Title::makeTitle( $ns, $dbkey );
416 $cache->addBadLinkObj( $title );
417 }
418 }
419
420 wfProfileOut( $fname );
421 }
422
423 /**
424 * Construct a WHERE clause which will match all the given titles.
425 * Give the appropriate table's field name prefix ('page', 'pl', etc).
426 *
427 * @param string $prefix
428 * @return string
429 * @access public
430 */
431 function constructSet( $prefix, $db ) {
432 $first = true;
433 $sql = '';
434 foreach ( $this->data as $ns => $dbkeys ) {
435 if ( !count( $dbkeys ) ) {
436 continue;
437 }
438
439 if ( $first ) {
440 $first = false;
441 } else {
442 $sql .= ' OR ';
443 }
444 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
445
446 $firstTitle = true;
447 foreach( $dbkeys as $dbkey => $nothing ) {
448 if ( $firstTitle ) {
449 $firstTitle = false;
450 } else {
451 $sql .= ',';
452 }
453 $sql .= $db->addQuotes( $dbkey );
454 }
455
456 $sql .= '))';
457 }
458 return $sql;
459 }
460 }
461
462 ?>