25b84ac4bc5abf73c407fb493c5ba4221252a7a4
[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 function LinkCache()
16 {
17 $this->mActive = true;
18 $this->mPreFilled = false;
19 $this->mGoodLinks = array();
20 $this->mBadLinks = array();
21 $this->mImageLinks = array();
22 $this->mOldGoodLinks = array();
23 $this->mOldBadLinks = array();
24 }
25
26 function getGoodLinkID( $title )
27 {
28 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
29 return $this->mGoodLinks[$title];
30 } else {
31 return 0;
32 }
33 }
34
35 function isBadLink( $title )
36 {
37 return in_array( $title, $this->mBadLinks );
38 }
39
40 function addGoodLink( $id, $title )
41 {
42 if ( $this->mActive ) {
43 $this->mGoodLinks[$title] = $id;
44 }
45 }
46
47 function addBadLink( $title )
48 {
49 if ( $this->mActive && ( ! $this->isBadLink( $title ) ) ) {
50 array_push( $this->mBadLinks, $title );
51 }
52 }
53
54 function addImageLink( $title )
55 {
56 if ( $this->mActive ) { $this->mImageLinks[$title] = 1; }
57 }
58
59 function clearBadLink( $title )
60 {
61 $index = array_search( $title, $this->mBadLinks );
62 if ( isset( $index ) ) {
63 unset( $this->mBadLinks[$index] );
64 }
65 global $wgMemc, $wgDBname;
66 $wgMemc->delete( "$wgDBname:lc:title:$title" );
67 }
68
69 function suspend() { $this->mActive = false; }
70 function resume() { $this->mActive = true; }
71 function getGoodLinks() { return $this->mGoodLinks; }
72 function getBadLinks() { return $this->mBadLinks; }
73 function getImageLinks() { return $this->mImageLinks; }
74
75 function addLink( $title )
76 {
77 return $this->addLinkObj( Title::newFromDBkey( $title ) );
78 }
79
80 function addLinkObj( &$nt )
81 {
82 $title = $nt->getDBkey();
83 if ( $this->isBadLink( $title ) ) { return 0; }
84 $id = $this->getGoodLinkID( $title );
85 if ( 0 != $id ) { return $id; }
86
87 global $wgMemc, $wgDBname;
88 $fname = "LinkCache::addLink-checkdatabase";
89 wfProfileIn( $fname );
90
91 $ns = $nt->getNamespace();
92
93 if ( "" == $title ) {
94 wfProfileOut( $fname );
95 return 0;
96 }
97
98 $id = $wgMemc->get( $key = "$wgDBname:lc:title:$title" );
99 if( $id === FALSE ) {
100 $sql = "SELECT HIGH_PRIORITY cur_id FROM cur WHERE cur_namespace=" .
101 "{$ns} AND cur_title='" . wfStrencode( $title ) . "'";
102 $res = wfQuery( $sql, DB_READ, "LinkCache::addLink" );
103
104 if ( 0 == wfNumRows( $res ) ) {
105 $id = 0;
106 } else {
107 $s = wfFetchObject( $res );
108 $id = $s->cur_id;
109 }
110 $wgMemc->add( $key, $id, time()+3600 );
111 }
112 if ( 0 == $id ) { $this->addBadLink( $title ); }
113 else { $this->addGoodLink( $id, $title ); }
114 wfProfileOut( $fname );
115 return $id;
116 }
117
118 function preFill( &$fromtitle )
119 {
120 $fname = "LinkCache::preFill";
121 wfProfileIn( $fname );
122 # Note -- $fromtitle is a Title *object*
123 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
124 $sql = "SELECT HIGH_PRIORITY cur_id,cur_namespace,cur_title
125 FROM cur,links
126 WHERE cur_id=l_to AND l_from='{$dbkeyfrom}'";
127 $res = wfQuery( $sql, DB_READ, "LinkCache::preFill" );
128 while( $s = wfFetchObject( $res ) ) {
129 $this->addGoodLink( $s->cur_id,
130 Title::makeName( $s->cur_namespace, $s->cur_title )
131 );
132 }
133
134 $this->suspend();
135 $id = $fromtitle->getArticleID();
136 $this->resume();
137
138 $sql = "SELECT HIGH_PRIORITY bl_to
139 FROM brokenlinks
140 WHERE bl_from='{$id}'";
141 $res = wfQuery( $sql, DB_READ, "LinkCache::preFill" );
142 while( $s = wfFetchObject( $res ) ) {
143 $this->addBadLink( $s->bl_to );
144 }
145
146 $this->mOldBadLinks = $this->mBadLinks;
147 $this->mOldGoodLinks = $this->mGoodLinks;
148 $this->mPreFilled = true;
149
150 wfProfileOut( $fname );
151 }
152
153 function getGoodAdditions()
154 {
155 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
156 }
157
158 function getBadAdditions()
159 {
160 return array_values( array_diff( $this->mBadLinks, $this->mOldBadLinks ) );
161 }
162
163 function getImageAdditions()
164 {
165 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
166 }
167
168 function getGoodDeletions()
169 {
170 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
171 }
172
173 function getBadDeletions()
174 {
175 return array_values( array_diff( $this->mOldBadLinks, $this->mBadLinks ) );
176 }
177
178 function getImageDeletions()
179 {
180 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
181 }
182
183 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
184 # the incremental update arrays which will be filled. Returns whether or not it's
185 # worth doing the incremental version. For example, if [[List of mathematical topics]]
186 # was blanked, it would take a long, long time to do incrementally.
187 function incrementalSetup( $which, &$del, &$add )
188 {
189 if ( ! $this->mPreFilled ) {
190 return false;
191 }
192
193 switch ( $which ) {
194 case LINKCACHE_GOOD:
195 $old =& $this->mOldGoodLinks;
196 $cur =& $this->mGoodLinks;
197 $del = $this->getGoodDeletions();
198 $add = $this->getGoodAdditions();
199 break;
200 case LINKCACHE_BAD:
201 $old =& $this->mOldBadLinks;
202 $cur =& $this->mBadLinks;
203 $del = $this->getBadDeletions();
204 $add = $this->getBadAdditions();
205 break;
206 default: # LINKCACHE_IMAGE
207 return false;
208 }
209
210 return true;
211 }
212
213 # Clears cache but leaves old preFill copies alone
214 function clear()
215 {
216 $this->mGoodLinks = array();
217 $this->mBadLinks = array();
218 $this->mImageLinks = array();
219 }
220
221 }
222 ?>