212b214aefb29da7a53ed8d289ae8faaa7ffb8a9
[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 in_array( $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 array_push( $this->mBadLinks, $title );
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 $index = array_search( $title, $this->mBadLinks );
72 if ( isset( $index ) ) {
73 unset( $this->mBadLinks[$index] );
74 }
75 $this->clearLink( $title );
76 }
77
78 function clearLink( $title ) {
79 global $wgMemc;
80 $wgMemc->delete( $this->getKey( $title ) );
81 }
82
83 function suspend() { $this->mActive = false; }
84 function resume() { $this->mActive = true; }
85 function getGoodLinks() { return $this->mGoodLinks; }
86 function getBadLinks() { return $this->mBadLinks; }
87 function getImageLinks() { return $this->mImageLinks; }
88
89 function addLink( $title )
90 {
91 $nt = Title::newFromDBkey( $title );
92 if( $nt ) {
93 return $this->addLinkObj( $nt );
94 } else {
95 return 0;
96 }
97 }
98
99 function addLinkObj( &$nt )
100 {
101 $title = $nt->getPrefixedDBkey();
102 if ( $this->isBadLink( $title ) ) { return 0; }
103 $id = $this->getGoodLinkID( $title );
104 if ( 0 != $id ) { return $id; }
105
106 global $wgMemc;
107 $fname = "LinkCache::addLinkObj";
108 wfProfileIn( $fname );
109
110 $ns = $nt->getNamespace();
111 $t = $nt->getDBkey();
112
113 if ( "" == $title ) {
114 wfProfileOut( $fname );
115 return 0;
116 }
117
118 $id = $wgMemc->get( $key = $this->getKey( $title ) );
119 if( $id === FALSE ) {
120 $sql = "SELECT cur_id FROM cur WHERE cur_namespace=" .
121 "{$ns} AND cur_title='" . wfStrencode( $t ) . "'";
122 $res = wfQuery( $sql, DB_READ, "LinkCache::addLink" );
123
124 if ( 0 == wfNumRows( $res ) ) {
125 $id = 0;
126 } else {
127 $s = wfFetchObject( $res );
128 $id = $s->cur_id;
129 }
130 $wgMemc->add( $key, $id, time()+3600 );
131 }
132 if ( 0 == $id ) { $this->addBadLink( $title ); }
133 else { $this->addGoodLink( $id, $title ); }
134 wfProfileOut( $fname );
135 return $id;
136 }
137
138 function preFill( &$fromtitle )
139 {
140 global $wgEnablePersistentLC;
141
142 $fname = "LinkCache::preFill";
143 wfProfileIn( $fname );
144 # Note -- $fromtitle is a Title *object*
145 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
146
147 if ( $wgEnablePersistentLC ) {
148 $res = wfQuery("SELECT lcc_cacheobj FROM linkscc WHERE lcc_title = '{$dbkeyfrom}'",
149 DB_READ);
150 $row = wfFetchObject( $res );
151 if( $row != FALSE){
152 $cacheobj = gzuncompress( $row->lcc_cacheobj );
153 $cc = unserialize( $cacheobj );
154 $this->mGoodLinks = $cc->mGoodLinks;
155 $this->mBadLinks = $cc->mBadLinks;
156 $this->mPreFilled = true;
157 wfProfileOut( $fname );
158 return;
159 }
160 }
161
162
163 $sql = "SELECT cur_id,cur_namespace,cur_title
164 FROM cur,links
165 WHERE cur_id=l_to AND l_from='{$dbkeyfrom}'";
166 $res = wfQuery( $sql, DB_READ, $fname );
167 while( $s = wfFetchObject( $res ) ) {
168 $this->addGoodLink( $s->cur_id,
169 Title::makeName( $s->cur_namespace, $s->cur_title )
170 );
171 }
172
173 $this->suspend();
174 $id = $fromtitle->getArticleID();
175 $this->resume();
176
177 $sql = "SELECT bl_to
178 FROM brokenlinks
179 WHERE bl_from='{$id}'";
180 $res = wfQuery( $sql, DB_READ, "LinkCache::preFill" );
181 while( $s = wfFetchObject( $res ) ) {
182 $this->addBadLink( $s->bl_to );
183 }
184
185 $this->mOldBadLinks = $this->mBadLinks;
186 $this->mOldGoodLinks = $this->mGoodLinks;
187 $this->mPreFilled = true;
188
189 if ( $wgEnablePersistentLC ) {
190 // put fetched link data into cache
191 $serCachegz = wfStrencode( gzcompress( serialize( $this ), 3) );
192 wfQuery("REPLACE INTO linkscc VALUES({$id}, '{$dbkeyfrom}', '{$serCachegz}')",
193 DB_WRITE);
194 }
195
196 wfProfileOut( $fname );
197 }
198
199 function getGoodAdditions()
200 {
201 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
202 }
203
204 function getBadAdditions()
205 {
206 return array_values( array_diff( $this->mBadLinks, $this->mOldBadLinks ) );
207 }
208
209 function getImageAdditions()
210 {
211 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
212 }
213
214 function getGoodDeletions()
215 {
216 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
217 }
218
219 function getBadDeletions()
220 {
221 return array_values( array_diff( $this->mOldBadLinks, $this->mBadLinks ) );
222 }
223
224 function getImageDeletions()
225 {
226 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
227 }
228
229 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
230 # the incremental update arrays which will be filled. Returns whether or not it's
231 # worth doing the incremental version. For example, if [[List of mathematical topics]]
232 # was blanked, it would take a long, long time to do incrementally.
233 function incrementalSetup( $which, &$del, &$add )
234 {
235 if ( ! $this->mPreFilled ) {
236 return false;
237 }
238
239 switch ( $which ) {
240 case LINKCACHE_GOOD:
241 $old =& $this->mOldGoodLinks;
242 $cur =& $this->mGoodLinks;
243 $del = $this->getGoodDeletions();
244 $add = $this->getGoodAdditions();
245 break;
246 case LINKCACHE_BAD:
247 $old =& $this->mOldBadLinks;
248 $cur =& $this->mBadLinks;
249 $del = $this->getBadDeletions();
250 $add = $this->getBadAdditions();
251 break;
252 default: # LINKCACHE_IMAGE
253 return false;
254 }
255
256 return true;
257 }
258
259 # Clears cache but leaves old preFill copies alone
260 function clear()
261 {
262 $this->mGoodLinks = array();
263 $this->mBadLinks = array();
264 $this->mImageLinks = array();
265 }
266
267 }
268 ?>