OOP calling convention for database functions. DBMS abstraction implemented by means...
[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 }
13
14
15 function doUpdate()
16 {
17 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
18 global $wgEnablePersistentLC, $wgUseCategoryMagic;
19
20 /* Update link tables with outgoing links from an updated article */
21 /* Relies on the 'link cache' to be filled out */
22
23 $fname = "LinksUpdate::doUpdate";
24 wfProfileIn( $fname );
25
26 $del = array();
27 $add = array();
28
29 $dbw =& wfGetDB( DB_WRITE );
30 $links = $dbw->tableName( 'links' );
31 $brokenlinks = $dbw->tableName( 'brokenlinks' );
32 $imagelinks = $dbw->tableName( 'imagelinks' );
33 $categorylinks = $dbw->tableName( 'categorylinks' );
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 $dbw->query( $sql, $fname );
44 }
45 } else {
46 # Delete everything
47 $dbw->delete( 'links', array( 'l_from' => $this->mId ), $fname );
48
49 # Get the addition list
50 $add = $wgLinkCache->getGoodLinks();
51 }
52
53 # Do the insertion
54 $sql = "";
55 if ( 0 != count( $add ) ) {
56 # The link cache was constructed without FOR UPDATE, so there may be collisions
57 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
58 # sure it's better than without IGNORE
59 $sql = "INSERT IGNORE INTO $links (l_from,l_to) VALUES ";
60 $first = true;
61 foreach( $add as $lt => $lid ) {
62 if ( ! $first ) { $sql .= ","; }
63 $first = false;
64
65 $sql .= "({$this->mId},{$lid})";
66 }
67 }
68 if ( "" != $sql ) {
69 $dbw->query( $sql, $fname );
70 }
71
72 #------------------------------------------------------------------------------
73 # Bad links
74
75 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD, $del, $add ) ) {
76 # Delete where necessary
77 if ( count( $del ) ) {
78 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId} AND bl_to IN(";
79 $first = true;
80 foreach( $del as $badTitle ) {
81 if ( $first ) {
82 $first = false;
83 } else {
84 $sql .= ",";
85 }
86 $sql .= $dbw->addQuotes( $badTitle );
87 }
88 $dbw->query( $sql, $fname );
89 }
90 } else {
91 # Delete all
92 $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId ) );
93
94 # Get addition list
95 $add = $wgLinkCache->getBadLinks();
96 }
97
98 # Do additions
99 $sql = "";
100 if ( 0 != count ( $add ) ) {
101 $sql = "INSERT IGNORE INTO $brokenlinks (bl_from,bl_to) VALUES ";
102 $first = true;
103 foreach( $add as $blt ) {
104 $blt = $dbw->strencode( $blt );
105 if ( ! $first ) { $sql .= ","; }
106 $first = false;
107
108 $sql .= "({$this->mId},'{$blt}')";
109 }
110 }
111 if ( "" != $sql ) {
112 $dbw->query( $sql, $fname );
113 }
114
115 #------------------------------------------------------------------------------
116 # Image links
117 $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
118 $dbw->query( $sql, $fname );
119
120 # Get addition list
121 $add = $wgLinkCache->getImageLinks();
122
123 # Do the insertion
124 $sql = "";
125 $image = Namespace::getImage();
126 if ( 0 != count ( $add ) ) {
127 $sql = "INSERT IGNORE INTO $imagelinks (il_from,il_to) VALUES ";
128 $first = true;
129 foreach( $add as $iname => $val ) {
130 # FIXME: Change all this to avoid unnecessary duplication
131 $nt = Title::makeTitle( $image, $iname );
132 if( !$nt ) continue;
133 $nt->invalidateCache();
134
135 $iname = $dbw->strencode( $iname );
136 if ( ! $first ) { $sql .= ","; }
137 $first = false;
138
139 $sql .= "({$this->mId},'{$iname}')";
140 }
141 }
142 if ( "" != $sql ) {
143 $dbw->query( $sql, $fname );
144 }
145
146 #------------------------------------------------------------------------------
147 # Category links
148 if( $wgUseCategoryMagic ) {
149 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
150 $dbw->query( $sql, $fname );
151
152 # Get addition list
153 $add = $wgLinkCache->getCategoryLinks();
154
155 # Do the insertion
156 $sql = "";
157 if ( 0 != count ( $add ) ) {
158 $sql = "INSERT IGNORE INTO $categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
159 $first = true;
160 foreach( $add as $cname => $sortkey ) {
161 # FIXME: Change all this to avoid unnecessary duplication
162 $nt = Title::makeTitle( NS_CATEGORY, $cname );
163 if( !$nt ) continue;
164 $nt->invalidateCache();
165
166 if ( ! $first ) { $sql .= ","; }
167 $first = false;
168
169 $sql .= "({$this->mId},'" . $dbw->strencode( $cname ) .
170 "','" . $dbw->strencode( $sortkey ) . "')";
171 }
172 }
173 if ( "" != $sql ) {
174 $dbw->query( $sql, $fname );
175 }
176 }
177
178 $this->fixBrokenLinks();
179
180 wfProfileOut( $fname );
181 }
182
183 function doDumbUpdate()
184 {
185 # Old inefficient update function
186 # Used for rebuilding the link table
187 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
188 $fname = "LinksUpdate::doDumbUpdate";
189 wfProfileIn( $fname );
190
191
192 $dbw =& wfGetDB( DB_WRITE );
193 $links = $dbw->tableName( 'links' );
194 $brokenlinks = $dbw->tableName( 'brokenlinks' );
195 $imagelinks = $dbw->tableName( 'imagelinks' );
196 $categorylinks = $dbw->tableName( 'categorylinks' );
197
198 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
199 $dbw->query( $sql, $fname );
200
201 $a = $wgLinkCache->getGoodLinks();
202 $sql = "";
203 if ( 0 != count( $a ) ) {
204 $sql = "INSERT IGNORE INTO $links (l_from,l_to) VALUES ";
205 $first = true;
206 foreach( $a as $lt => $lid ) {
207 if ( ! $first ) { $sql .= ","; }
208 $first = false;
209
210 $sql .= "({$this->mId},{$lid})";
211 }
212 }
213 if ( "" != $sql ) {
214 $dbw->query( $sql, $fname );
215 }
216
217 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
218 $dbw->query( $sql, $fname );
219
220 $a = $wgLinkCache->getBadLinks();
221 $sql = "";
222 if ( 0 != count ( $a ) ) {
223 $sql = "INSERT IGNORE INTO $brokenlinks (bl_from,bl_to) VALUES ";
224 $first = true;
225 foreach( $a as $blt ) {
226 $blt = $dbw->strencode( $blt );
227 if ( ! $first ) { $sql .= ","; }
228 $first = false;
229
230 $sql .= "({$this->mId},'{$blt}')";
231 }
232 }
233 if ( "" != $sql ) {
234 $dbw->query( $sql, $fname );
235 }
236
237 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
238 $dbw->query( $sql, $fname );
239
240 $a = $wgLinkCache->getImageLinks();
241 $sql = "";
242 if ( 0 != count ( $a ) ) {
243 $sql = "INSERT IGNORE INTO $imagelinks (il_from,il_to) VALUES ";
244 $first = true;
245 foreach( $a as $iname => $val ) {
246 $iname = $dbw->strencode( $iname );
247 if ( ! $first ) { $sql .= ","; }
248 $first = false;
249
250 $sql .= "({$this->mId},'{$iname}')";
251 }
252 }
253 if ( "" != $sql ) {
254 $dbw->query( $sql, $fname );
255 }
256
257 if( $wgUseCategoryMagic ) {
258 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
259 $dbw->query( $sql, $fname );
260
261 # Get addition list
262 $add = $wgLinkCache->getCategoryLinks();
263
264 # Do the insertion
265 $sql = "";
266 if ( 0 != count ( $add ) ) {
267 $sql = "INSERT IGNORE INTO $categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
268 $first = true;
269 foreach( $add as $cname => $sortkey ) {
270 # FIXME: Change all this to avoid unnecessary duplication
271 $nt = Title::makeTitle( NS_CATEGORY, $cname );
272 if( !$nt ) continue;
273 $nt->invalidateCache();
274
275 if ( ! $first ) { $sql .= ","; }
276 $first = false;
277
278 $sql .= "({$this->mId},'" . $dbw->strencode( $cname ) .
279 "','" . $dbw->strencode( $sortkey ) . "')";
280 }
281 }
282 if ( "" != $sql ) {
283 $dbw->query( $sql, $fname );
284 }
285 }
286 $this->fixBrokenLinks();
287 wfProfileOut( $fname );
288 }
289
290 function fixBrokenLinks() {
291 /* Update any brokenlinks *to* this page */
292 /* Call for a newly created page, or just to make sure state is consistent */
293 $fname = "LinksUpdate::fixBrokenLinks";
294
295 $dbw =& wfGetDB( DB_WRITE );
296 $cur = $dbw->tableName( 'cur' );
297 $links = $dbw->tableName( 'links' );
298
299 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle ),
300 $fname, 'FOR UPDATE' );
301 if ( 0 == $dbw->numRows( $res ) ) { return; }
302
303 # Ignore errors. If a link existed in both the brokenlinks table and the links
304 # table, that's an error which can be fixed at this stage by simply ignoring collisions
305 $sql = "INSERT IGNORE INTO $links (l_from,l_to) VALUES ";
306 $now = wfTimestampNow();
307 $sql2 = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_id IN (";
308 $first = true;
309 while ( $row = $dbw->fetchObject( $res ) ) {
310 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
311 $first = false;
312
313 $sql .= "({$row->bl_from},{$this->mId})";
314 $sql2 .= $row->bl_from;
315 }
316 $sql2 .= ")";
317 $dbw->query( $sql, $fname );
318 $dbw->query( $sql2, $fname );
319
320 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle ), $fname );
321 }
322 }
323
324 ?>