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