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