* Added templatelinks table. The table currently represents a literal list of templat...
[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 /** @deprecated */
114 function suspend() { $this->mActive = false; }
115 /** @deprecated */
116 function resume() { $this->mActive = true; }
117
118 function getPageLinks() { return $this->mPageLinks; }
119 function getGoodLinks() { return $this->mGoodLinks; }
120 function getBadLinks() { return array_keys( $this->mBadLinks ); }
121 function getImageLinks() { return $this->mImageLinks; }
122 function getCategoryLinks() { return $this->mCategoryLinks; }
123
124 /**
125 * Add a title to the link cache, return the page_id or zero if non-existent
126 * @param string $title Title to add
127 * @return integer
128 */
129 function addLink( $title ) {
130 $nt = Title::newFromDBkey( $title );
131 if( $nt ) {
132 return $this->addLinkObj( $nt );
133 } else {
134 return 0;
135 }
136 }
137
138 /**
139 * Add a title to the link cache, return the page_id or zero if non-existent
140 * @param Title $nt Title to add
141 * @return integer
142 */
143 function addLinkObj( &$nt ) {
144 global $wgMemc, $wgLinkCacheMemcached, $wgAntiLockFlags;
145 $title = $nt->getPrefixedDBkey();
146 if ( $this->isBadLink( $title ) ) { return 0; }
147 $id = $this->getGoodLinkID( $title );
148 if ( 0 != $id ) { return $id; }
149
150 $fname = 'LinkCache::addLinkObj';
151 global $wgProfiling, $wgProfiler;
152 if ( $wgProfiling && isset( $wgProfiler ) ) {
153 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
154 }
155
156 wfProfileIn( $fname );
157
158 $ns = $nt->getNamespace();
159 $t = $nt->getDBkey();
160
161 if ( '' == $title ) {
162 wfProfileOut( $fname );
163 return 0;
164 }
165
166 $id = NULL;
167 if( $wgLinkCacheMemcached )
168 $id = $wgMemc->get( $key = $this->getKey( $title ) );
169 if( ! is_integer( $id ) ) {
170 if ( $this->mForUpdate ) {
171 $db =& wfGetDB( DB_MASTER );
172 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
173 $options = array( 'FOR UPDATE' );
174 } else {
175 $options = array();
176 }
177 } else {
178 $db =& wfGetDB( DB_SLAVE );
179 $options = array();
180 }
181
182 $id = $db->selectField( 'page', 'page_id',
183 array( 'page_namespace' => $ns, 'page_title' => $t ),
184 $fname, $options );
185 if ( !$id ) {
186 $id = 0;
187 }
188 if( $wgLinkCacheMemcached )
189 $wgMemc->add( $key, $id, 3600*24 );
190 }
191
192 if( 0 == $id ) {
193 $this->addBadLinkObj( $nt );
194 } else {
195 $this->addGoodLinkObj( $id, $nt );
196 }
197 wfProfileOut( $fname );
198 return $id;
199 }
200
201 /**
202 * Bulk-check the pagelinks and page arrays for existence info.
203 * @param Title $fromtitle
204 * @deprecated
205 */
206 function preFill( &$fromtitle ) {
207 global $wgAntiLockFlags;
208 $fname = 'LinkCache::preFill';
209 wfProfileIn( $fname );
210
211 $this->suspend();
212 $id = $fromtitle->getArticleID();
213 $this->resume();
214
215 if( $id == 0 ) {
216 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
217 wfProfileOut( $fname );
218 return;
219 }
220
221 if ( $this->mForUpdate ) {
222 $db =& wfGetDB( DB_MASTER );
223 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
224 $options = 'FOR UPDATE';
225 } else {
226 $options = '';
227 }
228 } else {
229 $db =& wfGetDB( DB_SLAVE );
230 $options = '';
231 }
232
233 $page = $db->tableName( 'page' );
234 $pagelinks = $db->tableName( 'pagelinks' );
235
236 $sql = "SELECT page_id,pl_namespace,pl_title
237 FROM $pagelinks
238 LEFT JOIN $page
239 ON pl_namespace=page_namespace AND pl_title=page_title
240 WHERE pl_from=$id $options";
241 $res = $db->query( $sql, $fname );
242 while( $s = $db->fetchObject( $res ) ) {
243 $title = Title::makeTitle( $s->pl_namespace, $s->pl_title );
244 if( $s->page_id ) {
245 $this->addGoodLinkObj( $s->page_id, $title );
246 } else {
247 $this->addBadLinkObj( $title );
248 }
249 }
250 $this->mPreFilled = true;
251
252 wfProfileOut( $fname );
253 }
254
255 /**
256 * Clears cache
257 */
258 function clear() {
259 $this->mPageLinks = array();
260 $this->mGoodLinks = array();
261 $this->mBadLinks = array();
262 $this->mImageLinks = array();
263 $this->mCategoryLinks = array();
264 $this->mOldGoodLinks = array();
265 $this->mOldBadLinks = array();
266 $this->mOldPageLinks = array();
267 }
268
269 /**
270 * Swaps old and current link registers
271 * @deprecated
272 */
273 function swapRegisters() {
274 swap( $this->mGoodLinks, $this->mOldGoodLinks );
275 swap( $this->mBadLinks, $this->mOldBadLinks );
276 swap( $this->mImageLinks, $this->mOldImageLinks );
277 swap( $this->mPageLinks, $this->mOldPageLinks );
278 }
279 }
280
281 /**
282 * Class representing a list of titles
283 * The execute() method checks them all for existence and adds them to a LinkCache object
284 +
285 * @package MediaWiki
286 * @subpackage Cache
287 */
288 class LinkBatch {
289 /**
290 * 2-d array, first index namespace, second index dbkey, value arbitrary
291 */
292 var $data = array();
293
294 function LinkBatch( $arr = array() ) {
295 foreach( $arr as $item ) {
296 $this->addObj( $item );
297 }
298 }
299
300 function addObj( $title ) {
301 if ( is_object( $title ) ) {
302 $this->add( $title->getNamespace(), $title->getDBkey() );
303 } else {
304 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
305 }
306 }
307
308 function add( $ns, $dbkey ) {
309 if ( $ns < 0 ) {
310 return;
311 }
312 if ( !array_key_exists( $ns, $this->data ) ) {
313 $this->data[$ns] = array();
314 }
315
316 $this->data[$ns][$dbkey] = 1;
317 }
318
319 /**
320 * Set the link list to a given 2-d array
321 * First key is the namespace, second is the DB key, value arbitrary
322 */
323 function setArray( $array ) {
324 $this->data = $array;
325 }
326
327 /**
328 * Do the query and add the results to a LinkCache object
329 * Return an array mapping PDBK to ID
330 */
331 function execute( &$cache ) {
332 $fname = 'LinkBatch::execute';
333 wfProfileIn( $fname );
334 // Do query
335 $res = $this->doQuery();
336 if ( !$res ) {
337 wfProfileOut( $fname );
338 return array();
339 }
340
341 // For each returned entry, add it to the list of good links, and remove it from $remaining
342
343 $ids = array();
344 $remaining = $this->data;
345 while ( $row = $res->fetchObject() ) {
346 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
347 $cache->addGoodLinkObj( $row->page_id, $title );
348 $ids[$title->getPrefixedDBkey()] = $row->page_id;
349 unset( $remaining[$row->page_namespace][$row->page_title] );
350 }
351 $res->free();
352
353 // The remaining links in $data are bad links, register them as such
354 foreach ( $remaining as $ns => $dbkeys ) {
355 foreach ( $dbkeys as $dbkey => $nothing ) {
356 $title = Title::makeTitle( $ns, $dbkey );
357 $cache->addBadLinkObj( $title );
358 $ids[$title->getPrefixedDBkey()] = 0;
359 }
360 }
361 wfProfileOut( $fname );
362 return $ids;
363 }
364
365 /**
366 * Perform the existence test query, return a ResultWrapper with page_id fields
367 */
368 function doQuery() {
369 $fname = 'LinkBatch::execute';
370 $namespaces = array();
371
372 if ( !count( $this->data ) ) {
373 return false;
374 }
375 wfProfileIn( $fname );
376
377 // Construct query
378 // This is very similar to Parser::replaceLinkHolders
379 $dbr =& wfGetDB( DB_SLAVE );
380 $page = $dbr->tableName( 'page' );
381 $set = $this->constructSet( 'page', $dbr );
382 if ( $set === false ) {
383 return false;
384 }
385 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set";
386
387 // Do query
388 $res = new ResultWrapper( $dbr, $dbr->query( $sql, $fname ) );
389 wfProfileOut( $fname );
390 return $res;
391 }
392
393 /**
394 * Construct a WHERE clause which will match all the given titles.
395 * Give the appropriate table's field name prefix ('page', 'pl', etc).
396 *
397 * @param string $prefix
398 * @return string
399 * @access public
400 */
401 function constructSet( $prefix, &$db ) {
402 $first = true;
403 $firstTitle = true;
404 $sql = '';
405 foreach ( $this->data as $ns => $dbkeys ) {
406 if ( !count( $dbkeys ) ) {
407 continue;
408 }
409
410 if ( $first ) {
411 $first = false;
412 } else {
413 $sql .= ' OR ';
414 }
415 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
416
417 $firstTitle = true;
418 foreach( $dbkeys as $dbkey => $nothing ) {
419 if ( $firstTitle ) {
420 $firstTitle = false;
421 } else {
422 $sql .= ',';
423 }
424 $sql .= $db->addQuotes( $dbkey );
425 }
426
427 $sql .= '))';
428 }
429 if ( $first && $firstTitle ) {
430 # No titles added
431 return false;
432 } else {
433 return $sql;
434 }
435 }
436 }
437
438 ?>