Fix move page / linkcache / memcached problem
[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 $fname = "LinkCache::preFill";
141 wfProfileIn( $fname );
142 # Note -- $fromtitle is a Title *object*
143 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
144 $sql = "SELECT cur_id,cur_namespace,cur_title
145 FROM cur,links
146 WHERE cur_id=l_to AND l_from='{$dbkeyfrom}'";
147 $res = wfQuery( $sql, DB_READ, $fname );
148 while( $s = wfFetchObject( $res ) ) {
149 $this->addGoodLink( $s->cur_id,
150 Title::makeName( $s->cur_namespace, $s->cur_title )
151 );
152 }
153
154 $this->suspend();
155 $id = $fromtitle->getArticleID();
156 $this->resume();
157
158 $sql = "SELECT bl_to
159 FROM brokenlinks
160 WHERE bl_from='{$id}'";
161 $res = wfQuery( $sql, DB_READ, "LinkCache::preFill" );
162 while( $s = wfFetchObject( $res ) ) {
163 $this->addBadLink( $s->bl_to );
164 }
165
166 $this->mOldBadLinks = $this->mBadLinks;
167 $this->mOldGoodLinks = $this->mGoodLinks;
168 $this->mPreFilled = true;
169
170 wfProfileOut( $fname );
171 }
172
173 function getGoodAdditions()
174 {
175 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
176 }
177
178 function getBadAdditions()
179 {
180 return array_values( array_diff( $this->mBadLinks, $this->mOldBadLinks ) );
181 }
182
183 function getImageAdditions()
184 {
185 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
186 }
187
188 function getGoodDeletions()
189 {
190 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
191 }
192
193 function getBadDeletions()
194 {
195 return array_values( array_diff( $this->mOldBadLinks, $this->mBadLinks ) );
196 }
197
198 function getImageDeletions()
199 {
200 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
201 }
202
203 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
204 # the incremental update arrays which will be filled. Returns whether or not it's
205 # worth doing the incremental version. For example, if [[List of mathematical topics]]
206 # was blanked, it would take a long, long time to do incrementally.
207 function incrementalSetup( $which, &$del, &$add )
208 {
209 if ( ! $this->mPreFilled ) {
210 return false;
211 }
212
213 switch ( $which ) {
214 case LINKCACHE_GOOD:
215 $old =& $this->mOldGoodLinks;
216 $cur =& $this->mGoodLinks;
217 $del = $this->getGoodDeletions();
218 $add = $this->getGoodAdditions();
219 break;
220 case LINKCACHE_BAD:
221 $old =& $this->mOldBadLinks;
222 $cur =& $this->mBadLinks;
223 $del = $this->getBadDeletions();
224 $add = $this->getBadAdditions();
225 break;
226 default: # LINKCACHE_IMAGE
227 return false;
228 }
229
230 return true;
231 }
232
233 # Clears cache but leaves old preFill copies alone
234 function clear()
235 {
236 $this->mGoodLinks = array();
237 $this->mBadLinks = array();
238 $this->mImageLinks = array();
239 }
240
241 }
242 ?>