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