Split files and classes in different packages for phpdocumentor. I probably changed...
[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 */
6
7 /**
8 *
9 */
10 # These are used in incrementalSetup()
11 define ('LINKCACHE_GOOD', 0);
12 define ('LINKCACHE_BAD', 1);
13 define ('LINKCACHE_IMAGE', 2);
14
15 /**
16 *
17 * @package MediaWiki
18 */
19 class LinkCache {
20 // Increment $mClassVer whenever old serialized versions of this class
21 // becomes incompatible with the new version.
22 /* private */ var $mClassVer = 2;
23
24 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
25 /* private */ var $mImageLinks, $mCategoryLinks;
26 /* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
27 /* private */ var $mForUpdate;
28
29 /* private */ function getKey( $title ) {
30 global $wgDBname;
31 return $wgDBname.':lc:title:'.$title;
32 }
33
34 function LinkCache() {
35 $this->mActive = true;
36 $this->mPreFilled = false;
37 $this->mForUpdate = false;
38 $this->mGoodLinks = array();
39 $this->mBadLinks = array();
40 $this->mImageLinks = array();
41 $this->mCategoryLinks = array();
42 $this->mOldGoodLinks = array();
43 $this->mOldBadLinks = array();
44 }
45
46 /**
47 * General accessor to get/set whether SELECT FOR UPDATE should be used
48 */
49 function forUpdate( $update = NULL ) {
50 return wfSetVar( $this->mForUpdate, $update );
51 }
52
53 function getGoodLinkID( $title ) {
54 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
55 return $this->mGoodLinks[$title];
56 } else {
57 return 0;
58 }
59 }
60
61 function isBadLink( $title ) {
62 return array_key_exists( $title, $this->mBadLinks );
63 }
64
65 function addGoodLink( $id, $title ) {
66 if ( $this->mActive ) {
67 $this->mGoodLinks[$title] = $id;
68 }
69 }
70
71 function addBadLink( $title ) {
72 if ( $this->mActive && ( ! $this->isBadLink( $title ) ) ) {
73 $this->mBadLinks[$title] = 1;
74 }
75 }
76
77 function addImageLink( $title ) {
78 if ( $this->mActive ) { $this->mImageLinks[$title] = 1; }
79 }
80
81 function addImageLinkObj( $nt ) {
82 if ( $this->mActive ) { $this->mImageLinks[$nt->getDBkey()] = 1; }
83 }
84
85 function addCategoryLink( $title, $sortkey ) {
86 if ( $this->mActive ) { $this->mCategoryLinks[$title] = $sortkey; }
87 }
88
89 function addCategoryLinkObj( &$nt, $sortkey ) {
90 $this->addCategoryLink( $nt->getDBkey(), $sortkey );
91 }
92
93 function clearBadLink( $title ) {
94 unset( $this->mBadLinks[$title] );
95 $this->clearLink( $title );
96 }
97
98 function clearLink( $title ) {
99 global $wgMemc, $wgLinkCacheMemcached;
100 if( $wgLinkCacheMemcached )
101 $wgMemc->delete( $this->getKey( $title ) );
102 }
103
104 function suspend() { $this->mActive = false; }
105 function resume() { $this->mActive = true; }
106 function getGoodLinks() { return $this->mGoodLinks; }
107 function getBadLinks() { return array_keys( $this->mBadLinks ); }
108 function getImageLinks() { return $this->mImageLinks; }
109 function getCategoryLinks() { return $this->mCategoryLinks; }
110
111 function addLink( $title ) {
112 $nt = Title::newFromDBkey( $title );
113 if( $nt ) {
114 return $this->addLinkObj( $nt );
115 } else {
116 return 0;
117 }
118 }
119
120 function addLinkObj( &$nt ) {
121 global $wgMemc, $wgLinkCacheMemcached;
122 $title = $nt->getPrefixedDBkey();
123 if ( $this->isBadLink( $title ) ) { return 0; }
124 $id = $this->getGoodLinkID( $title );
125 if ( 0 != $id ) { return $id; }
126
127 $fname = 'LinkCache::addLinkObj';
128 wfProfileIn( $fname );
129
130 $ns = $nt->getNamespace();
131 $t = $nt->getDBkey();
132
133 if ( '' == $title ) {
134 wfProfileOut( $fname );
135 return 0;
136 }
137
138 $id = NULL;
139 if( $wgLinkCacheMemcached )
140 $id = $wgMemc->get( $key = $this->getKey( $title ) );
141 if( ! is_integer( $id ) ) {
142 if ( $this->mForUpdate ) {
143 $db =& wfGetDB( DB_MASTER );
144 $options = array( 'FOR UPDATE' );
145 } else {
146 $db =& wfGetDB( DB_SLAVE );
147 $options = array();
148 }
149
150 $id = $db->getField( 'cur', 'cur_id', array( 'cur_namespace' => $ns, 'cur_title' => $t ), $fname, $options );
151 if ( !$id ) {
152 $id = 0;
153 }
154 if( $wgLinkCacheMemcached )
155 $wgMemc->add( $key, $id, 3600*24 );
156 }
157
158 if ( 0 == $id ) { $this->addBadLink( $title ); }
159 else { $this->addGoodLink( $id, $title ); }
160 wfProfileOut( $fname );
161 return $id;
162 }
163
164 function preFill( &$fromtitle ) {
165 global $wgEnablePersistentLC;
166
167 $fname = 'LinkCache::preFill';
168 wfProfileIn( $fname );
169 # Note -- $fromtitle is a Title *object*
170
171 $this->suspend();
172 $id = $fromtitle->getArticleID();
173 $this->resume();
174
175 if( $id == 0 ) {
176 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
177 wfProfileOut( $fname );
178 return;
179 }
180
181 if ( $wgEnablePersistentLC ) {
182 if( $this->fillFromLinkscc( $id ) ){
183 wfProfileOut( $fname );
184 return;
185 }
186 }
187 if ( $this->mForUpdate ) {
188 $db =& wfGetDB( DB_MASTER );
189 $options = 'FOR UPDATE';
190 } else {
191 $db =& wfGetDB( DB_SLAVE );
192 $options = '';
193 }
194
195 $cur = $db->tableName( 'cur' );
196 $links = $db->tableName( 'links' );
197
198 $sql = "SELECT cur_id,cur_namespace,cur_title
199 FROM $cur,$links
200 WHERE cur_id=l_to AND l_from=$id $options";
201 $res = $db->query( $sql, $fname );
202 while( $s = $db->fetchObject( $res ) ) {
203 $this->addGoodLink( $s->cur_id,
204 Title::makeName( $s->cur_namespace, $s->cur_title )
205 );
206 }
207
208 $res = $db->select( 'brokenlinks', array( 'bl_to' ), array( 'bl_from' => $id ), $fname, array( $options ) );
209 while( $s = $db->fetchObject( $res ) ) {
210 $this->addBadLink( $s->bl_to );
211 }
212
213 $this->mOldBadLinks = $this->mBadLinks;
214 $this->mOldGoodLinks = $this->mGoodLinks;
215 $this->mPreFilled = true;
216
217 if ( $wgEnablePersistentLC ) {
218 $this->saveToLinkscc( $id );
219 }
220 wfProfileOut( $fname );
221 }
222
223 function getGoodAdditions() {
224 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
225 }
226
227 function getBadAdditions() {
228 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
229 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
230 return array_values( array_diff( array_keys( $this->mBadLinks ), array_keys( $this->mOldBadLinks ) ) );
231 }
232
233 function getImageAdditions() {
234 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
235 }
236
237 function getGoodDeletions() {
238 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
239 }
240
241 function getBadDeletions() {
242 return array_values( array_diff( array_keys( $this->mOldBadLinks ), array_keys( $this->mBadLinks ) ));
243 }
244
245 function getImageDeletions() {
246 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
247 }
248
249 /**
250 * Parameters:
251 * @param $which is one of the LINKCACHE_xxx constants
252 * @param $del,$add are the incremental update arrays which will be filled.
253 *
254 * @return Returns whether or not it's worth doing the incremental version.
255 *
256 * For example, if [[List of mathematical topics]] was blanked,
257 * it would take a long, long time to do incrementally.
258 */
259 function incrementalSetup( $which, &$del, &$add ) {
260 if ( ! $this->mPreFilled ) {
261 return false;
262 }
263
264 switch ( $which ) {
265 case LINKCACHE_GOOD:
266 $old =& $this->mOldGoodLinks;
267 $cur =& $this->mGoodLinks;
268 $del = $this->getGoodDeletions();
269 $add = $this->getGoodAdditions();
270 break;
271 case LINKCACHE_BAD:
272 $old =& $this->mOldBadLinks;
273 $cur =& $this->mBadLinks;
274 $del = $this->getBadDeletions();
275 $add = $this->getBadAdditions();
276 break;
277 default: # LINKCACHE_IMAGE
278 return false;
279 }
280
281 return true;
282 }
283
284 /**
285 * Clears cache but leaves old preFill copies alone
286 */
287 function clear() {
288 $this->mGoodLinks = array();
289 $this->mBadLinks = array();
290 $this->mImageLinks = array();
291 }
292
293 /**
294 * @access private
295 */
296 function fillFromLinkscc( $id ){
297 $fname = 'LinkCache::fillFromLinkscc';
298
299 $id = IntVal( $id );
300 if ( $this->mForUpdate ) {
301 $db =& wfGetDB( DB_MASTER );
302 $options = 'FOR UPDATE';
303 } else {
304 $db =& wfGetDB( DB_SLAVE );
305 $options = '';
306 }
307 $raw = $db->getField( 'linkscc', 'lcc_cacheobj', array( 'lcc_pageid' => $id ), $fname, $options );
308 if ( $raw === false ) {
309 return false;
310 }
311
312 $cacheobj = false;
313 if( function_exists( 'gzuncompress' ) )
314 $cacheobj = @gzuncompress( $raw );
315
316 if($cacheobj == FALSE){
317 $cacheobj = $raw;
318 }
319 $cc = @unserialize( $cacheobj );
320 if( isset( $cc->mClassVer ) and ($cc->mClassVer == $this->mClassVer ) ){
321 $this->mOldGoodLinks = $this->mGoodLinks = $cc->mGoodLinks;
322 $this->mOldBadLinks = $this->mBadLinks = $cc->mBadLinks;
323 $this->mPreFilled = true;
324 return TRUE;
325 } else {
326 return FALSE;
327 }
328
329 }
330
331 /**
332 * @access private
333 */
334 function saveToLinkscc( $pid ){
335 global $wgCompressedPersistentLC;
336 if( $wgCompressedPersistentLC and function_exists( 'gzcompress' ) ) {
337 $ser = gzcompress( serialize( $this ), 3 );
338 } else {
339 $ser = serialize( $this );
340 }
341 $db =& wfGetDB( DB_MASTER );
342 $db->replace( 'linkscc', array( 'lcc_pageid' ), array( 'lcc_pageid' => $pid, 'lcc_cacheobj' => $ser ) );
343 }
344
345 /**
346 * Delete linkscc rows which link to here
347 * @param $pid is a page id
348 * @static
349 */
350 function linksccClearLinksTo( $pid ){
351 global $wgEnablePersistentLC;
352 if ( $wgEnablePersistentLC ) {
353 $fname = 'LinkCache::linksccClearLinksTo';
354 $pid = intval( $pid );
355 $dbw =& wfGetDB( DB_MASTER );
356 # Delete linkscc rows which link to here
357 $dbw->deleteJoin( 'linkscc', 'links', 'lcc_pageid', 'l_from', array( 'l_to' => $pid ), $fname );
358 # Delete linkscc row representing this page
359 $dbw->delete( 'linkscc', array( 'lcc_pageid' => $pid ), $fname);
360 }
361
362 }
363
364 /**
365 * Delete linkscc rows with broken links to here
366 * @param $title is a prefixed db title for example like Title->getPrefixedDBkey() returns.
367 * @static
368 */
369 function linksccClearBrokenLinksTo( $title ){
370 global $wgEnablePersistentLC;
371 $fname = 'LinkCache::linksccClearBrokenLinksTo';
372
373 if ( $wgEnablePersistentLC ) {
374 $dbw =& wfGetDB( DB_MASTER );
375 $dbw->deleteJoin( 'linkscc', 'brokenlinks', 'lcc_pageid', 'bl_from', array( 'bl_to' => $title ), $fname );
376 }
377 }
378
379 /**
380 * @param $pid is a page id
381 * @static
382 */
383 function linksccClearPage( $pid ){
384 global $wgEnablePersistentLC;
385 if ( $wgEnablePersistentLC ) {
386 $pid = intval( $pid );
387 $dbw =& wfGetDB( DB_MASTER );
388 $dbw->delete( 'linkscc', array( 'lcc_pageid' => $pid ) );
389 }
390 }
391 }
392 ?>