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