accidentially removed the test for
[lhc/web/wiklou.git] / includes / LinksUpdate.php
1 <?php
2 # See deferred.doc
3
4 class LinksUpdate {
5
6 /* private */ var $mId, $mTitle;
7
8 function LinksUpdate( $id, $title )
9 {
10 $this->mId = $id;
11 $this->mTitle = $title;
12 $this->mTitleEnc = wfStrencode( $title );
13 }
14
15
16 function doUpdate()
17 {
18 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
19 global $wgEnablePersistentLC, $wgUseCategoryMagic;
20
21 /* Update link tables with outgoing links from an updated article */
22 /* Relies on the 'link cache' to be filled out */
23
24 $fname = "LinksUpdate::doUpdate";
25 wfProfileIn( $fname );
26
27 $del = array();
28 $add = array();
29
30 if( $wgDBtransactions ) {
31 $sql = "BEGIN";
32 wfQuery( $sql, DB_WRITE, $fname );
33 }
34
35 #------------------------------------------------------------------------------
36 # Good links
37
38 if ( $wgLinkCache->incrementalSetup( LINKCACHE_GOOD, $del, $add ) ) {
39 # Delete where necessary
40 if ( count( $del ) ) {
41 $sql = "DELETE FROM links WHERE l_from={$this->mId} AND l_to IN(".
42 implode( ",", $del ) . ")";
43 wfQuery( $sql, DB_WRITE, $fname );
44 }
45 } else {
46 # Delete everything
47 $sql = "DELETE FROM links WHERE l_from={$this->mId}";
48 wfQuery( $sql, DB_WRITE, $fname );
49
50 # Get the addition list
51 $add = $wgLinkCache->getGoodLinks();
52 }
53
54 # Do the insertion
55 $sql = "";
56 if ( 0 != count( $add ) ) {
57 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
58 $first = true;
59 foreach( $add as $lt => $lid ) {
60
61 if ( ! $first ) { $sql .= ","; }
62 $first = false;
63
64 $sql .= "({$this->mId},{$lid})";
65 }
66 }
67 if ( "" != $sql ) {
68 wfQuery( $sql, DB_WRITE, $fname );
69 }
70
71 #------------------------------------------------------------------------------
72 # Bad links
73
74 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD, $del, $add ) ) {
75 # Delete where necessary
76 if ( count( $del ) ) {
77 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId} AND bl_to IN('" .
78 implode( "','", array_map( "wfStrencode", $del ) ) . "')";
79 wfQuery( $sql, DB_WRITE, $fname );
80 }
81 } else {
82 # Delete all
83 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
84 wfQuery( $sql, DB_WRITE, $fname );
85
86 # Get addition list
87 $add = $wgLinkCache->getBadLinks();
88 }
89
90 # Do additions
91 $sql = "";
92 if ( 0 != count ( $add ) ) {
93 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
94 $first = true;
95 foreach( $add as $blt ) {
96 $blt = wfStrencode( $blt );
97 if ( ! $first ) { $sql .= ","; }
98 $first = false;
99
100 $sql .= "({$this->mId},'{$blt}')";
101 }
102 }
103 if ( "" != $sql ) {
104 wfQuery( $sql, DB_WRITE, $fname );
105 }
106
107 #------------------------------------------------------------------------------
108 # Image links
109 $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mId}'";
110 wfQuery( $sql, DB_WRITE, $fname );
111
112 # Get addition list
113 $add = $wgLinkCache->getImageLinks();
114
115 # Do the insertion
116 $sql = "";
117 $image = Namespace::getImage();
118 if ( 0 != count ( $add ) ) {
119 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
120 $first = true;
121 foreach( $add as $iname => $val ) {
122 # FIXME: Change all this to avoid unnecessary duplication
123 $nt = Title::makeTitle( $image, $iname );
124 if( !$nt ) continue;
125 $nt->invalidateCache();
126
127 $iname = wfStrencode( $iname );
128 if ( ! $first ) { $sql .= ","; }
129 $first = false;
130
131 $sql .= "({$this->mId},'{$iname}')";
132 }
133 }
134 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
135
136 #------------------------------------------------------------------------------
137 # Category links
138 if( $wgUseCategoryMagic ) {
139 $sql = "DELETE FROM categorylinks WHERE cl_from='{$this->mId}'";
140 wfQuery( $sql, DB_WRITE, $fname );
141
142 # Get addition list
143 $add = $wgLinkCache->getCategoryLinks();
144
145 # Do the insertion
146 $sql = "";
147 if ( 0 != count ( $add ) ) {
148 $sql = "INSERT INTO categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
149 $first = true;
150 foreach( $add as $cname => $sortkey ) {
151 # FIXME: Change all this to avoid unnecessary duplication
152 $nt = Title::makeTitle( NS_CATEGORY, $cname );
153 if( !$nt ) continue;
154 $nt->invalidateCache();
155
156 if ( ! $first ) { $sql .= ","; }
157 $first = false;
158
159 $sql .= "({$this->mId},'" . wfStrencode( $cname ) .
160 "','" . wfStrencode( $sortkey ) . "')";
161 }
162 }
163 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
164 }
165
166 $this->fixBrokenLinks();
167
168 if( $wgDBtransactions ) {
169 $sql = "COMMIT";
170 wfQuery( $sql, DB_WRITE, $fname );
171 }
172 wfProfileOut( $fname );
173 }
174
175 function doDumbUpdate()
176 {
177 # Old inefficient update function
178 # Used for rebuilding the link table
179
180 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
181 $fname = "LinksUpdate::doDumbUpdate";
182 wfProfileIn( $fname );
183
184 if( $wgDBtransactions ) {
185 $sql = "BEGIN";
186 wfQuery( $sql, DB_WRITE, $fname );
187 }
188
189 $sql = "DELETE FROM links WHERE l_from={$this->mId}";
190 wfQuery( $sql, DB_WRITE, $fname );
191
192 $a = $wgLinkCache->getGoodLinks();
193 $sql = "";
194 if ( 0 != count( $a ) ) {
195 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
196 $first = true;
197 foreach( $a as $lt => $lid ) {
198 if ( ! $first ) { $sql .= ","; }
199 $first = false;
200
201 $sql .= "({$this->mId},{$lid})";
202 }
203 }
204 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
205
206 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
207 wfQuery( $sql, DB_WRITE, $fname );
208
209 $a = $wgLinkCache->getBadLinks();
210 $sql = "";
211 if ( 0 != count ( $a ) ) {
212 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
213 $first = true;
214 foreach( $a as $blt ) {
215 $blt = wfStrencode( $blt );
216 if ( ! $first ) { $sql .= ","; }
217 $first = false;
218
219 $sql .= "({$this->mId},'{$blt}')";
220 }
221 }
222 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
223
224 $sql = "DELETE FROM imagelinks WHERE il_from={$this->mId}";
225 wfQuery( $sql, DB_WRITE, $fname );
226
227 $a = $wgLinkCache->getImageLinks();
228 $sql = "";
229 if ( 0 != count ( $a ) ) {
230 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
231 $first = true;
232 foreach( $a as $iname => $val ) {
233 $iname = wfStrencode( $iname );
234 if ( ! $first ) { $sql .= ","; }
235 $first = false;
236
237 $sql .= "({$this->mId},'{$iname}')";
238 }
239 }
240 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
241
242 if( $wgUseCategoryMagic ) {
243 $sql = "DELETE FROM categorylinks WHERE cl_from='{$this->mId}'";
244 wfQuery( $sql, DB_WRITE, $fname );
245
246 # Get addition list
247 $add = $wgLinkCache->getCategoryLinks();
248
249 # Do the insertion
250 $sql = "";
251 if ( 0 != count ( $add ) ) {
252 $sql = "INSERT INTO categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
253 $first = true;
254 foreach( $add as $cname => $sortkey ) {
255 # FIXME: Change all this to avoid unnecessary duplication
256 $nt = Title::makeTitle( NS_CATEGORY, $cname );
257 if( !$nt ) continue;
258 $nt->invalidateCache();
259
260 if ( ! $first ) { $sql .= ","; }
261 $first = false;
262
263 $sql .= "({$this->mId},'" . wfStrencode( $cname ) .
264 "','" . wfStrencode( $sortkey ) . "')";
265 }
266 }
267 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
268 }
269 $this->fixBrokenLinks();
270
271 if( $wgDBtransactions ) {
272 $sql = "COMMIT";
273 wfQuery( $sql, DB_WRITE, $fname );
274 }
275 wfProfileOut( $fname );
276 }
277
278 function fixBrokenLinks() {
279 /* Update any brokenlinks *to* this page */
280 /* Call for a newly created page, or just to make sure state is consistent */
281 $fname = "LinksUpdate::fixBrokenLinks";
282
283 $sql = "SELECT bl_from FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
284 $res = wfQuery( $sql, DB_READ, $fname );
285 if ( 0 == wfNumRows( $res ) ) { return; }
286
287 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
288 $now = wfTimestampNow();
289 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
290 $first = true;
291 while ( $row = wfFetchObject( $res ) ) {
292 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
293 $first = false;
294
295 $sql .= "({$row->bl_from},{$this->mId})";
296 $sql2 .= $row->bl_from;
297 }
298 $sql2 .= ")";
299 wfQuery( $sql, DB_WRITE, $fname );
300 wfQuery( $sql2, DB_WRITE, $fname );
301
302 $sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
303 wfQuery( $sql, DB_WRITE, $fname );
304 }
305
306 }
307
308 ?>