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