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