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