block/unblock log; split off profiling into Profiling.php and broke it (turn it off...
[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 if ( $this->isBadLink( $title ) ) { return 0; }
78 $id = $this->getGoodLinkID( $title );
79 if ( 0 != $id ) { return $id; }
80
81 global $wgMemc, $wgDBname;
82 $fname = "LinkCache::addLink-checkdatabase";
83 wfProfileIn( $fname );
84
85 $nt = Title::newFromDBkey( $title );
86 $ns = $nt->getNamespace();
87 $t = $nt->getDBkey();
88
89 if ( "" == $t ) {
90 wfProfileOut( $fname);
91 return 0;
92 }
93
94 $id = $wgMemc->get( $key = "$wgDBname:lc:title:$title" );
95 if( $id === FALSE ) {
96 $sql = "SELECT HIGH_PRIORITY cur_id FROM cur WHERE cur_namespace=" .
97 "{$ns} AND cur_title='" . wfStrencode( $t ) . "'";
98 $res = wfQuery( $sql, DB_READ, "LinkCache::addLink" );
99
100 if ( 0 == wfNumRows( $res ) ) {
101 $id = 0;
102 } else {
103 $s = wfFetchObject( $res );
104 $id = $s->cur_id;
105 }
106 $wgMemc->add( $key, $id, time()+3600 );
107 }
108 if ( 0 == $id ) { $this->addBadLink( $title ); }
109 else { $this->addGoodLink( $id, $title ); }
110 wfProfileOut( $fname );
111 return $id;
112 }
113
114 function preFill( $fromtitle )
115 {
116 $fname = "LinkCache::preFill";
117 wfProfileIn( $fname );
118 # Note -- $fromtitle is a Title *object*
119 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
120 $sql = "SELECT HIGH_PRIORITY cur_id,cur_namespace,cur_title
121 FROM cur,links
122 WHERE cur_id=l_to AND l_from='{$dbkeyfrom}'";
123 $res = wfQuery( $sql, DB_READ, "LinkCache::preFill" );
124 while( $s = wfFetchObject( $res ) ) {
125 $this->addGoodLink( $s->cur_id,
126 Title::makeName( $s->cur_namespace, $s->cur_title )
127 );
128 }
129
130 $this->suspend();
131 $id = $fromtitle->getArticleID();
132 $this->resume();
133
134 $sql = "SELECT HIGH_PRIORITY bl_to
135 FROM brokenlinks
136 WHERE bl_from='{$id}'";
137 $res = wfQuery( $sql, DB_READ, "LinkCache::preFill" );
138 while( $s = wfFetchObject( $res ) ) {
139 $this->addBadLink( $s->bl_to );
140 }
141
142 $this->mOldBadLinks = $this->mBadLinks;
143 $this->mOldGoodLinks = $this->mGoodLinks;
144 $this->mPreFilled = true;
145
146 wfProfileOut( $fname );
147 }
148
149 function getGoodAdditions()
150 {
151 return array_diff( $this->mGoodLinks, $this->mOldGoodLinks );
152 }
153
154 function getBadAdditions()
155 {
156 return array_values( array_diff( $this->mBadLinks, $this->mOldBadLinks ) );
157 }
158
159 function getImageAdditions()
160 {
161 return array_diff_assoc( $this->mImageLinks, $this->mOldImageLinks );
162 }
163
164 function getGoodDeletions()
165 {
166 return array_diff( $this->mOldGoodLinks, $this->mGoodLinks );
167 }
168
169 function getBadDeletions()
170 {
171 return array_values( array_diff( $this->mOldBadLinks, $this->mBadLinks ) );
172 }
173
174 function getImageDeletions()
175 {
176 return array_diff_assoc( $this->mOldImageLinks, $this->mImageLinks );
177 }
178
179 # Parameters: $which is one of the LINKCACHE_xxx constants, $del and $add are
180 # the incremental update arrays which will be filled. Returns whether or not it's
181 # worth doing the incremental version. For example, if [[List of mathematical topics]]
182 # was blanked, it would take a long, long time to do incrementally.
183 function incrementalSetup( $which, &$del, &$add )
184 {
185 if ( ! $this->mPreFilled ) {
186 return false;
187 }
188
189 switch ( $which ) {
190 case LINKCACHE_GOOD:
191 $old =& $this->mOldGoodLinks;
192 $cur =& $this->mGoodLinks;
193 $del = $this->getGoodDeletions();
194 $add = $this->getGoodAdditions();
195 break;
196 case LINKCACHE_BAD:
197 $old =& $this->mOldBadLinks;
198 $cur =& $this->mBadLinks;
199 $del = $this->getBadDeletions();
200 $add = $this->getBadAdditions();
201 break;
202 default: # LINKCACHE_IMAGE
203 return false;
204 }
205
206 return true;
207 }
208
209 # Clears cache but leaves old preFill copies alone
210 function clear()
211 {
212 $this->mGoodLinks = array();
213 $this->mBadLinks = array();
214 $this->mImageLinks = array();
215 }
216
217 }
218 ?>