Avoid saving to linkscc if the article doesn't exist; this leaves
[lhc/web/wiklou.git] / includes / LinkCache.php
1 <?
2 # Cache for article titles 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 = 1;
13
14 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
15 /* private */ var $mImageLinks;
16 /* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
17
18 /* private */ function getKey( $title ) {
19 global $wgDBname;
20 return "$wgDBname:lc:title:$title";
21 }
22
23 function LinkCache()
24 {
25 $this->mActive = true;
26 $this->mPreFilled = false;
27 $this->mGoodLinks = array();
28 $this->mBadLinks = array();
29 $this->mImageLinks = array();
30 $this->mOldGoodLinks = array();
31 $this->mOldBadLinks = array();
32 }
33
34 function getGoodLinkID( $title )
35 {
36 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
37 return $this->mGoodLinks[$title];
38 } else {
39 return 0;
40 }
41 }
42
43 function isBadLink( $title )
44 {
45 return array_key_exists( $title, $this->mBadLinks );
46 }
47
48 function addGoodLink( $id, $title )
49 {
50 if ( $this->mActive ) {
51 $this->mGoodLinks[$title] = $id;
52 }
53 }
54
55 function addBadLink( $title )
56 {
57 if ( $this->mActive && ( ! $this->isBadLink( $title ) ) ) {
58 $this->mBadLinks[$title] = 1;
59 }
60 }
61
62 function addImageLink( $title )
63 {
64 if ( $this->mActive ) { $this->mImageLinks[$title] = 1; }
65 }
66
67 function addImageLinkObj( $nt )
68 {
69 if ( $this->mActive ) { $this->mImageLinks[$nt->getDBkey()] = 1; }
70 }
71
72 function clearBadLink( $title )
73 {
74 unset( $this->mBadLinks[$title] );
75 $this->clearLink( $title );
76 }
77
78 function clearLink( $title )
79 {
80 global $wgMemc, $wgLinkCacheMemcached;
81 if( $wgLinkCacheMemcached )
82 $wgMemc->delete( $this->getKey( $title ) );
83 }
84
85 function suspend() { $this->mActive = false; }
86 function resume() { $this->mActive = true; }
87 function getGoodLinks() { return $this->mGoodLinks; }
88 function getBadLinks() { return array_keys( $this->mBadLinks ); }
89 function getImageLinks() { return $this->mImageLinks; }
90
91 function addLink( $title )
92 {
93 $nt = Title::newFromDBkey( $title );
94 if( $nt ) {
95 return $this->addLinkObj( $nt );
96 } else {
97 return 0;
98 }
99 }
100
101 function addLinkObj( &$nt )
102 {
103 global $wgMemc, $wgLinkCacheMemcached;
104
105 $title = $nt->getPrefixedDBkey();
106 if ( $this->isBadLink( $title ) ) { return 0; }
107 $id = $this->getGoodLinkID( $title );
108 if ( 0 != $id ) { return $id; }
109
110 $fname = "LinkCache::addLinkObj";
111 wfProfileIn( $fname );
112
113 $ns = $nt->getNamespace();
114 $t = $nt->getDBkey();
115
116 if ( "" == $title ) {
117 wfProfileOut( $fname );
118 return 0;
119 }
120
121 $id = FALSE;
122 if( $wgLinkCacheMemcached )
123 $id = $wgMemc->get( $key = $this->getKey( $title ) );
124 if( $id === FALSE ) {
125 $sql = "SELECT cur_id FROM cur WHERE cur_namespace=" .
126 "{$ns} AND cur_title='" . wfStrencode( $t ) . "'";
127 $res = wfQuery( $sql, DB_READ, "LinkCache::addLink" );
128
129 if ( 0 == wfNumRows( $res ) ) {
130 $id = 0;
131 } else {
132 $s = wfFetchObject( $res );
133 $id = $s->cur_id;
134 }
135 if( $wgLinkCacheMemcached )
136 $wgMemc->add( $key, $id, time()+3600 );
137 }
138
139 if ( 0 == $id ) { $this->addBadLink( $title ); }
140 else { $this->addGoodLink( $id, $title ); }
141 wfProfileOut( $fname );
142 return $id;
143 }
144
145 function preFill( &$fromtitle )
146 {
147 global $wgEnablePersistentLC, $wgCompressedPersistentLC;
148
149 $fname = "LinkCache::preFill";
150 wfProfileIn( $fname );
151 # Note -- $fromtitle is a Title *object*
152 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
153
154 if ( $wgEnablePersistentLC ) {
155 $cc =& $this->getFromLinkscc( $dbkeyfrom );
156 if( $cc != FALSE ){
157 $this->mOldGoodLinks = $this->mGoodLinks = $cc->mGoodLinks;
158 $this->mOldBadLinks = $this->mBadLinks = $cc->mBadLinks;
159 $this->mPreFilled = true;
160 wfProfileOut( $fname );
161 wfDebug( "LinkCache::preFill - got from linkscc\n" );
162 return;
163 }
164 }
165
166
167 $sql = "SELECT cur_id,cur_namespace,cur_title
168 FROM cur,links
169 WHERE cur_id=l_to AND l_from='{$dbkeyfrom}'";
170 $res = wfQuery( $sql, DB_READ, $fname );
171 while( $s = wfFetchObject( $res ) ) {
172 $this->addGoodLink( $s->cur_id,
173 Title::makeName( $s->cur_namespace, $s->cur_title )
174 );
175 }
176
177 $this->suspend();
178 $id = $fromtitle->getArticleID();
179 $this->resume();
180
181 if( $id == 0 ) {
182 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
183 wfProfileOut( $fname );
184 return;
185 }
186
187 $sql = "SELECT bl_to
188 FROM brokenlinks
189 WHERE bl_from='{$id}'";
190 $res = wfQuery( $sql, DB_READ, "LinkCache::preFill" );
191 while( $s = wfFetchObject( $res ) ) {
192 $this->addBadLink( $s->bl_to );
193 }
194
195 $this->mOldBadLinks = $this->mBadLinks;
196 $this->mOldGoodLinks = $this->mGoodLinks;
197 $this->mPreFilled = true;
198
199 if ( $wgEnablePersistentLC ) {
200 // put fetched link data into cache
201 if( $wgCompressedPersistentLC and function_exists( "gzcompress" ) ) {
202 $ser = wfStrencode( gzcompress( serialize( $this ), 3 ));
203 } else {
204 $ser = wfStrencode( serialize( $this ) );
205 }
206 wfQuery("REPLACE INTO linkscc(lcc_pageid,lcc_title,lcc_cacheobj) VALUES({$id}, '{$dbkeyfrom}', '{$ser}')",
207 DB_WRITE);
208 wfDebug( "$fname - saved to linkscc\n" );
209 }
210
211 wfProfileOut( $fname );
212 }
213
214 function getGoodAdditions()
215 {
216 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
217 }
218
219 function getBadAdditions()
220 {
221 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
222 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
223 return array_values( array_diff( array_keys( $this->mBadLinks ), array_keys( $this->mOldBadLinks ) ) );
224 }
225
226 function getImageAdditions()
227 {
228 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
229 }
230
231 function getGoodDeletions()
232 {
233 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
234 }
235
236 function getBadDeletions()
237 {
238 return array_values( array_diff( array_keys( $this->mOldBadLinks ), array_keys( $this->mBadLinks ) ));
239 }
240
241 function getImageDeletions()
242 {
243 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
244 }
245
246 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
247 # the incremental update arrays which will be filled. Returns whether or not it's
248 # worth doing the incremental version. For example, if [[List of mathematical topics]]
249 # was blanked, it would take a long, long time to do incrementally.
250 function incrementalSetup( $which, &$del, &$add )
251 {
252 if ( ! $this->mPreFilled ) {
253 return false;
254 }
255
256 switch ( $which ) {
257 case LINKCACHE_GOOD:
258 $old =& $this->mOldGoodLinks;
259 $cur =& $this->mGoodLinks;
260 $del = $this->getGoodDeletions();
261 $add = $this->getGoodAdditions();
262 break;
263 case LINKCACHE_BAD:
264 $old =& $this->mOldBadLinks;
265 $cur =& $this->mBadLinks;
266 $del = $this->getBadDeletions();
267 $add = $this->getBadAdditions();
268 break;
269 default: # LINKCACHE_IMAGE
270 return false;
271 }
272
273 return true;
274 }
275
276 # Clears cache but leaves old preFill copies alone
277 function clear()
278 {
279 $this->mGoodLinks = array();
280 $this->mBadLinks = array();
281 $this->mImageLinks = array();
282 }
283
284
285 function &getFromLinkscc( $dbkeyfrom ){
286 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_title = '{$dbkeyfrom}'",
287 DB_READ);
288 $row = wfFetchObject( $res );
289 if( $row == FALSE)
290 return false;
291
292 $cacheobj = false;
293 if( function_exists( "gzuncompress" ) )
294 $cacheobj = @gzuncompress( $row->lcc_cacheobj );
295
296 if($cacheobj == FALSE){
297 $cacheobj = $row->lcc_cacheobj;
298 }
299 $cc = @unserialize( $cacheobj );
300 if( isset( $cc->mClassVer ) and ($cc->mClassVer == $this->mClassVer ) ){
301 return $cc;
302 } else {
303 return FALSE;
304 }
305 }
306 }
307 ?>