Always use reference assignment when taking the return value of wfGetDB(), or else...
[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 }
157 } else {
158 $db =& wfGetDB( DB_SLAVE );
159 $options = array();
160 }
161
162 $id = $db->selectField( 'page', 'page_id',
163 array( 'page_namespace' => $ns, 'page_title' => $t ),
164 $fname, $options );
165 wfdebug("link cache: id=$id\n");
166 if ( !$id ) {
167 $id = 0;
168 }
169 if( $wgLinkCacheMemcached )
170 $wgMemc->add( $key, $id, 3600*24 );
171 }
172
173 if( 0 == $id ) {
174 $this->addBadLinkObj( $nt );
175 } else {
176 $this->addGoodLinkObj( $id, $nt );
177 }
178 wfProfileOut( $fname );
179 return $id;
180 }
181
182 /**
183 * Bulk-check the pagelinks and page arrays for existence info.
184 * @param Title $fromtitle
185 */
186 function preFill( &$fromtitle ) {
187 global $wgAntiLockFlags;
188 $fname = 'LinkCache::preFill';
189 wfProfileIn( $fname );
190
191 $this->suspend();
192 $id = $fromtitle->getArticleID();
193 $this->resume();
194
195 if( $id == 0 ) {
196 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
197 wfProfileOut( $fname );
198 return;
199 }
200
201 if ( $this->mForUpdate ) {
202 $db =& wfGetDB( DB_MASTER );
203 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
204 $options = 'FOR UPDATE';
205 } else {
206 $options = '';
207 }
208 } else {
209 $db =& wfGetDB( DB_SLAVE );
210 $options = '';
211 }
212
213 $page = $db->tableName( 'page' );
214 $pagelinks = $db->tableName( 'pagelinks' );
215
216 $sql = "SELECT page_id,pl_namespace,pl_title
217 FROM $pagelinks
218 LEFT JOIN $page
219 ON pl_namespace=page_namespace AND pl_title=page_title
220 WHERE pl_from=$id $options";
221 $res = $db->query( $sql, $fname );
222 while( $s = $db->fetchObject( $res ) ) {
223 $title = Title::makeTitle( $s->pl_namespace, $s->pl_title );
224 if( $s->page_id ) {
225 $this->addGoodLinkObj( $s->page_id, $title );
226 } else {
227 $this->addBadLinkObj( $title );
228 }
229 }
230 $this->mPreFilled = true;
231
232 wfProfileOut( $fname );
233 }
234
235 function getGoodAdditions() {
236 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
237 }
238
239 function getBadAdditions() {
240 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
241 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
242 return array_values( array_diff( array_keys( $this->mBadLinks ), array_keys( $this->mOldBadLinks ) ) );
243 }
244
245 function getImageAdditions() {
246 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
247 }
248
249 function getGoodDeletions() {
250 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
251 }
252
253 function getBadDeletions() {
254 return array_values( array_diff( array_keys( $this->mOldBadLinks ), array_keys( $this->mBadLinks ) ));
255 }
256
257 function getImageDeletions() {
258 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
259 }
260
261 function getPageAdditions() {
262 $set = array_diff( array_keys( $this->mPageLinks ), array_keys( $this->mOldPageLinks ) );
263 $out = array();
264 foreach( $set as $key ) {
265 $out[$key] = $this->mPageLinks[$key];
266 }
267 return $out;
268 }
269
270 function getPageDeletions() {
271 $set = array_diff( array_keys( $this->mOldPageLinks ), array_keys( $this->mPageLinks ) );
272 $out = array();
273 foreach( $set as $key ) {
274 $out[$key] = $this->mOldPageLinks[$key];
275 }
276 return $out;
277 }
278
279 /**
280 * Parameters:
281 * @param $which is one of the LINKCACHE_xxx constants
282 * @param $del,$add are the incremental update arrays which will be filled.
283 *
284 * @return Returns whether or not it's worth doing the incremental version.
285 *
286 * For example, if [[List of mathematical topics]] was blanked,
287 * it would take a long, long time to do incrementally.
288 */
289 function incrementalSetup( $which, &$del, &$add ) {
290 if ( ! $this->mPreFilled ) {
291 return false;
292 }
293
294 switch ( $which ) {
295 case LINKCACHE_GOOD:
296 $old =& $this->mOldGoodLinks;
297 $cur =& $this->mGoodLinks;
298 $del = $this->getGoodDeletions();
299 $add = $this->getGoodAdditions();
300 break;
301 case LINKCACHE_BAD:
302 $old =& $this->mOldBadLinks;
303 $cur =& $this->mBadLinks;
304 $del = $this->getBadDeletions();
305 $add = $this->getBadAdditions();
306 break;
307 case LINKCACHE_PAGE:
308 $old =& $this->mOldPageLinks;
309 $cur =& $this->mPageLinks;
310 $del = $this->getPageDeletions();
311 $add = $this->getPageAdditions();
312 break;
313 default: # LINKCACHE_IMAGE
314 return false;
315 }
316
317 return true;
318 }
319
320 /**
321 * Clears cache
322 */
323 function clear() {
324 $this->mPageLinks = array();
325 $this->mGoodLinks = array();
326 $this->mBadLinks = array();
327 $this->mImageLinks = array();
328 $this->mCategoryLinks = array();
329 $this->mOldGoodLinks = array();
330 $this->mOldBadLinks = array();
331 $this->mOldPageLinks = array();
332 }
333
334 /**
335 * Swaps old and current link registers
336 */
337 function swapRegisters() {
338 swap( $this->mGoodLinks, $this->mOldGoodLinks );
339 swap( $this->mBadLinks, $this->mOldBadLinks );
340 swap( $this->mImageLinks, $this->mOldImageLinks );
341 swap( $this->mPageLinks, $this->mOldPageLinks );
342 }
343 }
344
345 /**
346 * Class representing a list of titles
347 * The execute() method checks them all for existence and adds them to a LinkCache object
348 +
349 * @package MediaWiki
350 * @subpackage Cache
351 */
352 class LinkBatch {
353 /**
354 * 2-d array, first index namespace, second index dbkey, value arbitrary
355 */
356 var $data = array();
357
358 function LinkBatch( $arr = array() ) {
359 foreach( $arr as $item ) {
360 $this->addObj( $item );
361 }
362 }
363
364 function addObj( $title ) {
365 $this->add( $title->getNamespace(), $title->getDBkey() );
366 }
367
368 function add( $ns, $dbkey ) {
369 if ( $ns < 0 ) {
370 return;
371 }
372 if ( !array_key_exists( $ns, $this->data ) ) {
373 $this->data[$ns] = array();
374 }
375
376 $this->data[$ns][$dbkey] = 1;
377 }
378
379 function execute( &$cache ) {
380 $fname = 'LinkBatch::execute';
381 $namespaces = array();
382
383 if ( !count( $this->data ) ) {
384 return;
385 }
386
387 wfProfileIn( $fname );
388
389 // Construct query
390 // This is very similar to Parser::replaceLinkHolders
391 $dbr =& wfGetDB( DB_SLAVE );
392 $page = $dbr->tableName( 'page' );
393 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE "
394 . $this->constructSet( 'page', $dbr );
395
396 // Do query
397 $res = $dbr->query( $sql, $fname );
398
399 // Process results
400 // For each returned entry, add it to the list of good links, and remove it from $remaining
401
402 $remaining = $this->data;
403 while ( $row = $dbr->fetchObject( $res ) ) {
404 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
405 $cache->addGoodLinkObj( $row->page_id, $title );
406 unset( $remaining[$row->page_namespace][$row->page_title] );
407 }
408 $dbr->freeResult( $res );
409
410 // The remaining links in $data are bad links, register them as such
411 foreach ( $remaining as $ns => $dbkeys ) {
412 foreach ( $dbkeys as $dbkey => $nothing ) {
413 $title = Title::makeTitle( $ns, $dbkey );
414 $cache->addBadLinkObj( $title );
415 }
416 }
417
418 wfProfileOut( $fname );
419 }
420
421 /**
422 * Construct a WHERE clause which will match all the given titles.
423 * Give the appropriate table's field name prefix ('page', 'pl', etc).
424 *
425 * @param string $prefix
426 * @return string
427 * @access public
428 */
429 function constructSet( $prefix, $db ) {
430 $first = true;
431 $sql = '';
432 foreach ( $this->data as $ns => $dbkeys ) {
433 if ( !count( $dbkeys ) ) {
434 continue;
435 }
436
437 if ( $first ) {
438 $first = false;
439 } else {
440 $sql .= ' OR ';
441 }
442 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
443
444 $firstTitle = true;
445 foreach( $dbkeys as $dbkey => $nothing ) {
446 if ( $firstTitle ) {
447 $firstTitle = false;
448 } else {
449 $sql .= ',';
450 }
451 $sql .= $db->addQuotes( $dbkey );
452 }
453
454 $sql .= '))';
455 }
456 return $sql;
457 }
458 }
459
460 ?>