3 * Cache for article titles (prefixed DB keys) and ids linked from one source
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);
22 // Increment $mClassVer whenever old serialized versions of this class
23 // becomes incompatible with the new version.
24 /* private */ var $mClassVer = 3;
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;
32 /* private */ function getKey( $title ) {
34 return $wgDBname.':lc:title:'.$title;
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();
52 * General accessor to get/set whether SELECT FOR UPDATE should be used
54 function forUpdate( $update = NULL ) {
55 return wfSetVar( $this->mForUpdate
, $update );
58 function getGoodLinkID( $title ) {
59 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
60 return $this->mGoodLinks
[$title];
66 function isBadLink( $title ) {
67 return array_key_exists( $title, $this->mBadLinks
);
70 function addGoodLinkObj( $id, $title ) {
71 if ( $this->mActive
) {
72 $dbkey = $title->getPrefixedDbKey();
73 $this->mGoodLinks
[$dbkey] = $id;
74 $this->mPageLinks
[$dbkey] = $title;
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;
86 function addImageLink( $title ) {
87 if ( $this->mActive
) { $this->mImageLinks
[$title] = 1; }
90 function addImageLinkObj( $nt ) {
91 if ( $this->mActive
) { $this->mImageLinks
[$nt->getDBkey()] = 1; }
94 function addCategoryLink( $title, $sortkey ) {
95 if ( $this->mActive
) { $this->mCategoryLinks
[$title] = $sortkey; }
98 function addCategoryLinkObj( &$nt, $sortkey ) {
99 $this->addCategoryLink( $nt->getDBkey(), $sortkey );
102 function clearBadLink( $title ) {
103 unset( $this->mBadLinks
[$title] );
104 $this->clearLink( $title );
107 function clearLink( $title ) {
108 global $wgMemc, $wgLinkCacheMemcached;
109 if( $wgLinkCacheMemcached )
110 $wgMemc->delete( $this->getKey( $title ) );
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
; }
121 function addLink( $title ) {
122 $nt = Title
::newFromDBkey( $title );
124 return $this->addLinkObj( $nt );
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; }
137 $fname = 'LinkCache::addLinkObj';
138 wfProfileIn( $fname );
140 $ns = $nt->getNamespace();
141 $t = $nt->getDBkey();
143 if ( '' == $title ) {
144 wfProfileOut( $fname );
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' );
160 $db =& wfGetDB( DB_SLAVE
);
164 $id = $db->selectField( 'page', 'page_id',
165 array( 'page_namespace' => $ns, 'page_title' => $t ),
170 if( $wgLinkCacheMemcached )
171 $wgMemc->add( $key, $id, 3600*24 );
175 $this->addBadLinkObj( $nt );
177 $this->addGoodLinkObj( $id, $nt );
179 wfProfileOut( $fname );
184 * Bulk-check the pagelinks and page arrays for existence info.
185 * @param Title $fromtitle
187 function preFill( &$fromtitle ) {
188 global $wgAntiLockFlags;
189 $fname = 'LinkCache::preFill';
190 wfProfileIn( $fname );
193 $id = $fromtitle->getArticleID();
197 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
198 wfProfileOut( $fname );
202 if ( $this->mForUpdate
) {
203 $db =& wfGetDB( DB_MASTER
);
204 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
205 $options = 'FOR UPDATE';
210 $db =& wfGetDB( DB_SLAVE
);
214 $page = $db->tableName( 'page' );
215 $pagelinks = $db->tableName( 'pagelinks' );
217 $sql = "SELECT page_id,pl_namespace,pl_title
220 ON pl_namespace=page_namespace AND pl_title=page_title
221 WHERE pl_from=$id $options";
222 $res = $db->query( $sql, $fname );
223 while( $s = $db->fetchObject( $res ) ) {
224 $title = Title
::makeTitle( $s->pl_namespace
, $s->pl_title
);
226 $this->addGoodLinkObj( $s->page_id
, $title );
228 $this->addBadLinkObj( $title );
231 $this->mPreFilled
= true;
233 wfProfileOut( $fname );
236 function getGoodAdditions() {
237 return array_diff( $this->mGoodLinks
, $this->mOldGoodLinks
);
240 function getBadAdditions() {
241 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
242 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
243 return array_values( array_diff( array_keys( $this->mBadLinks
), array_keys( $this->mOldBadLinks
) ) );
246 function getImageAdditions() {
247 return array_diff_assoc( $this->mImageLinks
, $this->mOldImageLinks
);
250 function getGoodDeletions() {
251 return array_diff( $this->mOldGoodLinks
, $this->mGoodLinks
);
254 function getBadDeletions() {
255 return array_values( array_diff( array_keys( $this->mOldBadLinks
), array_keys( $this->mBadLinks
) ));
258 function getImageDeletions() {
259 return array_diff_assoc( $this->mOldImageLinks
, $this->mImageLinks
);
262 function getPageAdditions() {
263 $set = array_diff( array_keys( $this->mPageLinks
), array_keys( $this->mOldPageLinks
) );
265 foreach( $set as $key ) {
266 $out[$key] = $this->mPageLinks
[$key];
271 function getPageDeletions() {
272 $set = array_diff( array_keys( $this->mOldPageLinks
), array_keys( $this->mPageLinks
) );
274 foreach( $set as $key ) {
275 $out[$key] = $this->mOldPageLinks
[$key];
282 * @param $which is one of the LINKCACHE_xxx constants
283 * @param $del,$add are the incremental update arrays which will be filled.
285 * @return Returns whether or not it's worth doing the incremental version.
287 * For example, if [[List of mathematical topics]] was blanked,
288 * it would take a long, long time to do incrementally.
290 function incrementalSetup( $which, &$del, &$add ) {
291 if ( ! $this->mPreFilled
) {
297 $old =& $this->mOldGoodLinks
;
298 $cur =& $this->mGoodLinks
;
299 $del = $this->getGoodDeletions();
300 $add = $this->getGoodAdditions();
303 $old =& $this->mOldBadLinks
;
304 $cur =& $this->mBadLinks
;
305 $del = $this->getBadDeletions();
306 $add = $this->getBadAdditions();
309 $old =& $this->mOldPageLinks
;
310 $cur =& $this->mPageLinks
;
311 $del = $this->getPageDeletions();
312 $add = $this->getPageAdditions();
314 default: # LINKCACHE_IMAGE
325 $this->mPageLinks
= array();
326 $this->mGoodLinks
= array();
327 $this->mBadLinks
= array();
328 $this->mImageLinks
= array();
329 $this->mCategoryLinks
= array();
330 $this->mOldGoodLinks
= array();
331 $this->mOldBadLinks
= array();
332 $this->mOldPageLinks
= array();
336 * Swaps old and current link registers
338 function swapRegisters() {
339 swap( $this->mGoodLinks
, $this->mOldGoodLinks
);
340 swap( $this->mBadLinks
, $this->mOldBadLinks
);
341 swap( $this->mImageLinks
, $this->mOldImageLinks
);
342 swap( $this->mPageLinks
, $this->mOldPageLinks
);
347 * Class representing a list of titles
348 * The execute() method checks them all for existence and adds them to a LinkCache object
355 * 2-d array, first index namespace, second index dbkey, value arbitrary
359 function LinkBatch( $arr = array() ) {
360 foreach( $arr as $item ) {
361 $this->addObj( $item );
365 function addObj( $title ) {
366 if ( is_object( $title ) ) {
367 $this->add( $title->getNamespace(), $title->getDBkey() );
369 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
373 function add( $ns, $dbkey ) {
377 if ( !array_key_exists( $ns, $this->data
) ) {
378 $this->data
[$ns] = array();
381 $this->data
[$ns][$dbkey] = 1;
384 function execute( &$cache ) {
385 $fname = 'LinkBatch::execute';
386 $namespaces = array();
388 if ( !count( $this->data
) ) {
392 wfProfileIn( $fname );
395 // This is very similar to Parser::replaceLinkHolders
396 $dbr =& wfGetDB( DB_SLAVE
);
397 $page = $dbr->tableName( 'page' );
398 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE "
399 . $this->constructSet( 'page', $dbr );
402 $res = $dbr->query( $sql, $fname );
405 // For each returned entry, add it to the list of good links, and remove it from $remaining
407 $remaining = $this->data
;
408 while ( $row = $dbr->fetchObject( $res ) ) {
409 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
410 $cache->addGoodLinkObj( $row->page_id
, $title );
411 unset( $remaining[$row->page_namespace
][$row->page_title
] );
413 $dbr->freeResult( $res );
415 // The remaining links in $data are bad links, register them as such
416 foreach ( $remaining as $ns => $dbkeys ) {
417 foreach ( $dbkeys as $dbkey => $nothing ) {
418 $title = Title
::makeTitle( $ns, $dbkey );
419 $cache->addBadLinkObj( $title );
423 wfProfileOut( $fname );
427 * Construct a WHERE clause which will match all the given titles.
428 * Give the appropriate table's field name prefix ('page', 'pl', etc).
430 * @param string $prefix
434 function constructSet( $prefix, $db ) {
437 foreach ( $this->data
as $ns => $dbkeys ) {
438 if ( !count( $dbkeys ) ) {
447 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
450 foreach( $dbkeys as $dbkey => $nothing ) {
456 $sql .= $db->addQuotes( $dbkey );