* Added a TODO note to isASCII()
[mediawiki.git] / includes / LinkCache.php
blob5853f16f228f358dbc0df5570ead96a584d66234
1 <?php
2 /**
3 * Cache for article titles (prefixed DB keys) and ids linked from one source
4 * @package MediaWiki
5 * @subpackage Cache
6 */
8 /**
11 # These are used in incrementalSetup()
12 define ('LINKCACHE_GOOD', 0);
13 define ('LINKCACHE_BAD', 1);
14 define ('LINKCACHE_IMAGE', 2);
16 /**
17 * @package MediaWiki
18 * @subpackage Cache
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;
25 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
26 /* private */ var $mImageLinks, $mCategoryLinks;
27 /* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
28 /* private */ var $mForUpdate;
30 /* private */ function getKey( $title ) {
31 global $wgDBname;
32 return $wgDBname.':lc:title:'.$title;
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();
47 /**
48 * General accessor to get/set whether SELECT FOR UPDATE should be used
50 function forUpdate( $update = NULL ) {
51 return wfSetVar( $this->mForUpdate, $update );
54 function getGoodLinkID( $title ) {
55 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
56 return $this->mGoodLinks[$title];
57 } else {
58 return 0;
62 function isBadLink( $title ) {
63 return array_key_exists( $title, $this->mBadLinks );
66 function addGoodLink( $id, $title ) {
67 if ( $this->mActive ) {
68 $this->mGoodLinks[$title] = $id;
72 function addBadLink( $title ) {
73 if ( $this->mActive && ( ! $this->isBadLink( $title ) ) ) {
74 $this->mBadLinks[$title] = 1;
78 function addImageLink( $title ) {
79 if ( $this->mActive ) { $this->mImageLinks[$title] = 1; }
82 function addImageLinkObj( $nt ) {
83 if ( $this->mActive ) { $this->mImageLinks[$nt->getDBkey()] = 1; }
86 function addCategoryLink( $title, $sortkey ) {
87 if ( $this->mActive ) { $this->mCategoryLinks[$title] = $sortkey; }
90 function addCategoryLinkObj( &$nt, $sortkey ) {
91 $this->addCategoryLink( $nt->getDBkey(), $sortkey );
94 function clearBadLink( $title ) {
95 unset( $this->mBadLinks[$title] );
96 $this->clearLink( $title );
99 function clearLink( $title ) {
100 global $wgMemc, $wgLinkCacheMemcached;
101 if( $wgLinkCacheMemcached )
102 $wgMemc->delete( $this->getKey( $title ) );
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; }
112 function addLink( $title ) {
113 $nt = Title::newFromDBkey( $title );
114 if( $nt ) {
115 return $this->addLinkObj( $nt );
116 } else {
117 return 0;
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; }
128 $fname = 'LinkCache::addLinkObj';
129 wfProfileIn( $fname );
131 $ns = $nt->getNamespace();
132 $t = $nt->getDBkey();
134 if ( '' == $title ) {
135 wfProfileOut( $fname );
136 return 0;
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();
151 $id = $db->selectField( 'page', 'page_id', array( 'page_namespace' => $ns, 'page_title' => $t ), $fname, $options );
152 if ( !$id ) {
153 $id = 0;
155 if( $wgLinkCacheMemcached )
156 $wgMemc->add( $key, $id, 3600*24 );
159 if ( 0 == $id ) { $this->addBadLink( $title ); }
160 else { $this->addGoodLink( $id, $title ); }
161 wfProfileOut( $fname );
162 return $id;
165 function preFill( &$fromtitle ) {
166 global $wgEnablePersistentLC;
168 $fname = 'LinkCache::preFill';
169 wfProfileIn( $fname );
170 # Note -- $fromtitle is a Title *object*
172 $this->suspend();
173 $id = $fromtitle->getArticleID();
174 $this->resume();
176 if( $id == 0 ) {
177 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
178 wfProfileOut( $fname );
179 return;
182 if ( $wgEnablePersistentLC ) {
183 if( $this->fillFromLinkscc( $id ) ){
184 wfProfileOut( $fname );
185 return;
188 if ( $this->mForUpdate ) {
189 $db =& wfGetDB( DB_MASTER );
190 $options = 'FOR UPDATE';
191 } else {
192 $db =& wfGetDB( DB_SLAVE );
193 $options = '';
196 $page = $db->tableName( 'page' );
197 $links = $db->tableName( 'links' );
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 )
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 );
214 $this->mOldBadLinks = $this->mBadLinks;
215 $this->mOldGoodLinks = $this->mGoodLinks;
216 $this->mPreFilled = true;
218 if ( $wgEnablePersistentLC ) {
219 $this->saveToLinkscc( $id );
221 wfProfileOut( $fname );
224 function getGoodAdditions() {
225 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
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 ) ) );
234 function getImageAdditions() {
235 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
238 function getGoodDeletions() {
239 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
242 function getBadDeletions() {
243 return array_values( array_diff( array_keys( $this->mOldBadLinks ), array_keys( $this->mBadLinks ) ));
246 function getImageDeletions() {
247 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
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.
255 * @return Returns whether or not it's worth doing the incremental version.
257 * For example, if [[List of mathematical topics]] was blanked,
258 * it would take a long, long time to do incrementally.
260 function incrementalSetup( $which, &$del, &$add ) {
261 if ( ! $this->mPreFilled ) {
262 return false;
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;
282 return true;
286 * Clears cache but leaves old preFill copies alone
288 function clear() {
289 $this->mGoodLinks = array();
290 $this->mBadLinks = array();
291 $this->mImageLinks = array();
295 * @access private
297 function fillFromLinkscc( $id ){
298 $fname = 'LinkCache::fillFromLinkscc';
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 = '';
308 $raw = $db->selectField( 'linkscc', 'lcc_cacheobj', array( 'lcc_pageid' => $id ), $fname, $options );
309 if ( $raw === false ) {
310 return false;
313 $cacheobj = false;
314 if( function_exists( 'gzuncompress' ) )
315 $cacheobj = @gzuncompress( $raw );
317 if($cacheobj == FALSE){
318 $cacheobj = $raw;
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;
333 * @access private
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 );
342 $db =& wfGetDB( DB_MASTER );
343 $db->replace( 'linkscc', array( 'lcc_pageid' ), array( 'lcc_pageid' => $pid, 'lcc_cacheobj' => $ser ) );
347 * Delete linkscc rows which link to here
348 * @param $pid is a page id
349 * @static
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);
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
370 function linksccClearBrokenLinksTo( $title ){
371 global $wgEnablePersistentLC;
372 $fname = 'LinkCache::linksccClearBrokenLinksTo';
374 if ( $wgEnablePersistentLC ) {
375 $dbw =& wfGetDB( DB_MASTER );
376 $dbw->deleteJoin( 'linkscc', 'brokenlinks', 'lcc_pageid', 'bl_from', array( 'bl_to' => $title ), $fname );
381 * @param $pid is a page id
382 * @static
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 ) );
395 * Class representing a list of titles
396 * The execute() method checks them all for existence and adds them to a LinkCache object
398 * @package MediaWikki
399 * @subpackage Cache
401 class LinkBatch {
402 /**
403 * 2-d array, first index namespace, second index dbkey, value arbitrary
405 var $data = array();
407 function addObj( $title ) {
408 $this->add( $title->getNamespace(), $title->getDBkey() );
411 function add( $ns, $dbkey ) {
412 if ( $ns < 0 ) {
413 return;
415 if ( !array_key_exists( $ns, $this->data ) ) {
416 $this->data[$ns] = array();
419 $this->data[$ns][$dbkey] = 1;
422 function execute( &$cache ) {
423 $fname = 'LinkBatch::execute';
424 $namespaces = array();
426 if ( !count( $this->data ) ) {
427 return;
430 wfProfileIn( $fname );
432 // Construct query
433 // This is very similar to Parser::replaceLinkHolders
434 $dbr = wfGetDB( DB_SLAVE );
435 $page = $dbr->tableName( 'page' );
436 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE ";
437 $first = true;
439 foreach ( $this->data as $ns => $dbkeys ) {
440 if ( !count( $dbkeys ) ) {
441 continue;
444 if ( $first ) {
445 $first = false;
446 } else {
447 $sql .= ' OR ';
449 $sql .= "(page_namespace=$ns AND page_title IN (";
451 $firstTitle = true;
452 foreach( $dbkeys as $dbkey => $nothing ) {
453 if ( $firstTitle ) {
454 $firstTitle = false;
455 } else {
456 $sql .= ',';
458 $sql .= $dbr->addQuotes( $dbkey );
461 $sql .= '))';
464 // Do query
465 $res = $dbr->query( $sql, $fname );
467 // Process results
468 // For each returned entry, add it to the list of good links, and remove it from $remaining
470 $remaining = $this->data;
471 while ( $row = $dbr->fetchObject( $res ) ) {
472 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
473 $cache->addGoodLink( $row->page_id, $title->getPrefixedDBkey() );
474 unset( $remaining[$row->page_namespace][$row->page_title] );
476 $dbr->freeResult( $res );
478 // The remaining links in $data are bad links, register them as such
479 foreach ( $remaining as $ns => $dbkeys ) {
480 foreach ( $dbkeys as $dbkey => $nothing ) {
481 $title = Title::makeTitle( $ns, $dbkey );
482 $cache->addBadLink( $title->getPrefixedText() );
486 wfProfileOut( $fname );