Added versioning to serialized LinkCache objects. Added pref.
[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 $title = $nt->getPrefixedDBkey();
104 if ( $this->isBadLink( $title ) ) { return 0; }
105 $id = $this->getGoodLinkID( $title );
106 if ( 0 != $id ) { return $id; }
107
108 global $wgMemc;
109 $fname = "LinkCache::addLinkObj";
110 wfProfileIn( $fname );
111
112 $ns = $nt->getNamespace();
113 $t = $nt->getDBkey();
114
115 if ( "" == $title ) {
116 wfProfileOut( $fname );
117 return 0;
118 }
119
120 $id = FALSE;
121 if( $wgLinkCacheMemcached )
122 $id = $wgMemc->get( $key = $this->getKey( $title ) );
123 if( $id === FALSE ) {
124 $sql = "SELECT cur_id FROM cur WHERE cur_namespace=" .
125 "{$ns} AND cur_title='" . wfStrencode( $t ) . "'";
126 $res = wfQuery( $sql, DB_READ, "LinkCache::addLink" );
127
128 if ( 0 == wfNumRows( $res ) ) {
129 $id = 0;
130 } else {
131 $s = wfFetchObject( $res );
132 $id = $s->cur_id;
133 }
134 if( $wgLinkCacheMemcached )
135 $wgMemc->add( $key, $id, time()+3600 );
136 }
137
138 if ( 0 == $id ) { $this->addBadLink( $title ); }
139 else { $this->addGoodLink( $id, $title ); }
140 wfProfileOut( $fname );
141 return $id;
142 }
143
144 function preFill( &$fromtitle )
145 {
146 global $wgEnablePersistentLC, $wgCompressedPersistentLC;
147
148 $fname = "LinkCache::preFill";
149 wfProfileIn( $fname );
150 # Note -- $fromtitle is a Title *object*
151 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
152
153 if ( $wgEnablePersistentLC ) {
154 $cc =& $this->getFromLinkscc( $dbkeyfrom );
155 if( $cc != FALSE ){
156 $this->mOldGoodLinks = $this->mGoodLinks = $cc->mGoodLinks;
157 $this->mOldBadLinks = $this->mBadLinks = $cc->mBadLinks;
158 $this->mPreFilled = true;
159 wfProfileOut( $fname );
160 wfDebug( "LinkCache::preFill - got from linkscc\n" );
161 return;
162 }
163 }
164
165
166 $sql = "SELECT cur_id,cur_namespace,cur_title
167 FROM cur,links
168 WHERE cur_id=l_to AND l_from='{$dbkeyfrom}'";
169 $res = wfQuery( $sql, DB_READ, $fname );
170 while( $s = wfFetchObject( $res ) ) {
171 $this->addGoodLink( $s->cur_id,
172 Title::makeName( $s->cur_namespace, $s->cur_title )
173 );
174 }
175
176 $this->suspend();
177 $id = $fromtitle->getArticleID();
178 $this->resume();
179
180 $sql = "SELECT bl_to
181 FROM brokenlinks
182 WHERE bl_from='{$id}'";
183 $res = wfQuery( $sql, DB_READ, "LinkCache::preFill" );
184 while( $s = wfFetchObject( $res ) ) {
185 $this->addBadLink( $s->bl_to );
186 }
187
188 $this->mOldBadLinks = $this->mBadLinks;
189 $this->mOldGoodLinks = $this->mGoodLinks;
190 $this->mPreFilled = true;
191
192 if ( $wgEnablePersistentLC ) {
193 // put fetched link data into cache
194 if( $wgCompressedPersistentLC and function_exists( "gzcompress" ) ) {
195 $ser = wfStrencode( gzcompress( serialize( $this ), 3 ));
196 } else {
197 $ser = wfStrencode( serialize( $this ) );
198 }
199 wfQuery("REPLACE INTO linkscc VALUES({$id}, '{$dbkeyfrom}', '{$ser}')",
200 DB_WRITE);
201 wfDebug( "LinkCache::preFill - saved to linkscc\n" );
202 }
203
204 wfProfileOut( $fname );
205 }
206
207 function getGoodAdditions()
208 {
209 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
210 }
211
212 function getBadAdditions()
213 {
214 #wfDebug( "mOldBadLinks: " . implode( ', ', array_keys( $this->mOldBadLinks ) ) . "\n" );
215 #wfDebug( "mBadLinks: " . implode( ', ', array_keys( $this->mBadLinks ) ) . "\n" );
216 return array_values( array_diff( array_keys( $this->mBadLinks ), array_keys( $this->mOldBadLinks ) ) );
217 }
218
219 function getImageAdditions()
220 {
221 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
222 }
223
224 function getGoodDeletions()
225 {
226 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
227 }
228
229 function getBadDeletions()
230 {
231 return array_values( array_diff( array_keys( $this->mOldBadLinks ), array_keys( $this->mBadLinks ) ));
232 }
233
234 function getImageDeletions()
235 {
236 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
237 }
238
239 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
240 # the incremental update arrays which will be filled. Returns whether or not it's
241 # worth doing the incremental version. For example, if [[List of mathematical topics]]
242 # was blanked, it would take a long, long time to do incrementally.
243 function incrementalSetup( $which, &$del, &$add )
244 {
245 if ( ! $this->mPreFilled ) {
246 return false;
247 }
248
249 switch ( $which ) {
250 case LINKCACHE_GOOD:
251 $old =& $this->mOldGoodLinks;
252 $cur =& $this->mGoodLinks;
253 $del = $this->getGoodDeletions();
254 $add = $this->getGoodAdditions();
255 break;
256 case LINKCACHE_BAD:
257 $old =& $this->mOldBadLinks;
258 $cur =& $this->mBadLinks;
259 $del = $this->getBadDeletions();
260 $add = $this->getBadAdditions();
261 break;
262 default: # LINKCACHE_IMAGE
263 return false;
264 }
265
266 return true;
267 }
268
269 # Clears cache but leaves old preFill copies alone
270 function clear()
271 {
272 $this->mGoodLinks = array();
273 $this->mBadLinks = array();
274 $this->mImageLinks = array();
275 }
276
277
278 function &getFromLinkscc( $dbkeyfrom ){
279 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_title = '{$dbkeyfrom}'",
280 DB_READ);
281 $row = wfFetchObject( $res );
282 if( $row == FALSE)
283 return false;
284
285 $cacheobj = false;
286 if( function_exists( "gzuncompress" ) )
287 $cacheobj = @gzuncompress( $row->lcc_cacheobj );
288
289 if($cacheobj == FALSE){
290 $cacheobj = $row->lcc_cacheobj;
291 }
292 $cc = @unserialize( $cacheobj );
293 if( isset( $cc->mClassVer ) and ($cc->mClassVer == $this->mClassVer ) ){
294 return $cc;
295 } else {
296 return FALSE;
297 }
298 }
299 }
300 ?>