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