yay, use high level Database:: methods! :)
[lhc/web/wiklou.git] / includes / LinksUpdate.php
1 <?php
2 /**
3 * See deferred.txt
4 * @package MediaWiki
5 */
6
7 /**
8 * @todo document
9 * @package MediaWiki
10 */
11 class LinksUpdate {
12
13 /**#@+
14 * @access private
15 */
16 var $mId, $mTitle;
17 /**#@-*/
18
19 /**
20 * Constructor
21 * Initialize private variables
22 * @param integer $id
23 * @param string $title
24 */
25 function LinksUpdate( $id, $title ) {
26 $this->mId = $id;
27 $this->mTitle = $title;
28 }
29
30 /**
31 * Update link tables with outgoing links from an updated article
32 * Relies on the 'link cache' to be filled out.
33 */
34
35 function doUpdate() {
36 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
37 global $wgEnablePersistentLC, $wgUseCategoryMagic;
38
39 $fname = 'LinksUpdate::doUpdate';
40 wfProfileIn( $fname );
41
42 $del = array();
43 $add = array();
44
45 $dbw =& wfGetDB( DB_MASTER );
46 $links = $dbw->tableName( 'links' );
47 $brokenlinks = $dbw->tableName( 'brokenlinks' );
48 $imagelinks = $dbw->tableName( 'imagelinks' );
49 $categorylinks = $dbw->tableName( 'categorylinks' );
50
51 #------------------------------------------------------------------------------
52 # Good links
53
54 if ( $wgLinkCache->incrementalSetup( LINKCACHE_GOOD, $del, $add ) ) {
55 # Delete where necessary
56 if ( count( $del ) ) {
57 $sql = "DELETE FROM $links WHERE l_from={$this->mId} AND l_to IN(".
58 implode( ',', $del ) . ')';
59 $dbw->query( $sql, $fname );
60 }
61 } else {
62 # Delete everything
63 $dbw->delete( 'links', array( 'l_from' => $this->mId ), $fname );
64
65 # Get the addition list
66 $add = $wgLinkCache->getGoodLinks();
67 }
68
69 # Do the insertion
70 if ( 0 != count( $add ) ) {
71 $arr=array();
72 foreach($add as $lt=>$lid)
73 array_push( $arr, array(
74 'l_from' => $this->mId,
75 'l_to' => $lid ) );
76 # The link cache was constructed without FOR UPDATE, so there may be collisions
77 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
78 # sure it's better than without IGNORE
79 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
80 }
81
82 #------------------------------------------------------------------------------
83 # Bad links
84
85 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD, $del, $add ) ) {
86 # Delete where necessary
87 if ( count( $del ) ) {
88 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId} AND bl_to IN(";
89 $first = true;
90 foreach( $del as $badTitle ) {
91 if ( $first ) {
92 $first = false;
93 } else {
94 $sql .= ',';
95 }
96 $sql .= $dbw->addQuotes( $badTitle );
97 }
98 $sql .= ')';
99 $dbw->query( $sql, $fname );
100 }
101 } else {
102 # Delete all
103 $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId ) );
104
105 # Get addition list
106 $add = $wgLinkCache->getBadLinks();
107 }
108
109 # Do additions
110 $sql = '';
111 if ( 0 != count ( $add ) ) {
112 $arr = array();
113 foreach( $add as $blt ) {
114 array_push( $arr, array(
115 'bl_from' => $this->mId,
116 'bl_to' => $blt ) );
117 }
118 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
119 }
120
121 #------------------------------------------------------------------------------
122 # Image links
123 $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
124 $dbw->query( $sql, $fname );
125
126 # Get addition list
127 $add = $wgLinkCache->getImageLinks();
128
129 # Do the insertion
130 $sql = '';
131 $image = NS_IMAGE;
132 if ( 0 != count ( $add ) ) {
133 $arr = array();
134 foreach ($add as $iname => $val ) {
135 $nt = Title::makeTitle( $image, $iname );
136 if( !$nt ) continue;
137 $nt->invalidateCache();
138 array_push( $arr, array(
139 'il_from' => $this->mId,
140 'il_to' => $iname ) );
141 }
142 $dbw->insert('imagelinks', $arr, $fname, array('IGNORE'));
143 }
144
145 #------------------------------------------------------------------------------
146 # Category links
147 if( $wgUseCategoryMagic ) {
148 global $messageMemc, $wgDBname;
149
150 # Get addition list
151 $add = $wgLinkCache->getCategoryLinks();
152
153 # select existing catlinks for this page
154 $res = $dbw->select( $categorylinks, array( 'cl_to' ), array( 'cl_from' => $this->mId ),
155 $fname, 'FOR UPDATE' );
156
157 $del = array();
158 if(0 != $dbw->numRows( $res )) {
159 while ( $row = $dbw->fetchObject( $res ) ) {
160 if(!isset($add[$row->cl_to])) {
161 // in the db, but no longer in the page -> delete
162 $del[] = $row->cl_to;
163 } else {
164 // remove already existing category memberships
165 // from the add array
166 unset($add[$row->cl_to]);
167 }
168 }
169 }
170 // delete any removed categorylinks
171 if(count($del) > 0) {
172 // delete old ones
173 $dbw->delete('categorylinks', array('cl_from'=>$this->mId, 'cl_to'=>$del));
174 foreach($del as $cname){
175 $nt = Title::makeTitle( NS_CATEGORY, $cname );
176 $nt->invalidateCache();
177 // update the timestamp which indicates when the last article
178 // was added or removed to/from this article
179 $key = $wgDBname.':Category:'.$nt->getDBkey().':adddeltimestamp';
180 $messageMemc->set( $key , wfTimestamp( TS_MW ), 24*3600 );
181 #wfDebug( "Linksupdate:Cats:del: ".serialize($nt)." $key \n" );
182 }
183 }
184 // add any new category memberships
185 if (count($add) > 0) {
186 $arr = array();
187 foreach( $add as $cname => $sortkey ) {
188 $nt = Title::makeTitle( NS_CATEGORY, $cname );
189 if( !$nt ) continue;
190 $nt->invalidateCache();
191 // update the timestamp which indicates when the last article
192 // was added or removed to/from this article
193 $key = $wgDBname.':Category:'.$nt->getDBkey().':adddeltimestamp';
194 $messageMemc->set( $key , wfTimestamp( TS_MW ), 24*3600 );
195 #wfDebug( "Linksupdate:Cats:add: ".serialize($nt)." $key\n" );
196 #wfDebug( "LU-get: ".$messageMemc->get( $key)."\n");
197 array_push( $arr, array(
198 'cl_from' => $this->mId,
199 'cl_to' => $cname,
200 'cl_sortkey' => $sortkey ) );
201 }
202 // do the actual sql insertion
203 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
204 }
205 }
206
207 $this->fixBrokenLinks();
208
209 wfProfileOut( $fname );
210 }
211
212 /**
213 * Link update which clears the previous entries and inserts new ones
214 * May be slower or faster depending on level of lock contention and write speed of DB
215 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
216 */
217 function doDumbUpdate() {
218 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
219 $fname = 'LinksUpdate::doDumbUpdate';
220 wfProfileIn( $fname );
221
222
223 $dbw =& wfGetDB( DB_MASTER );
224 $links = $dbw->tableName( 'links' );
225 $brokenlinks = $dbw->tableName( 'brokenlinks' );
226 $imagelinks = $dbw->tableName( 'imagelinks' );
227 $categorylinks = $dbw->tableName( 'categorylinks' );
228
229 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
230 $dbw->query( $sql, $fname );
231
232 $a = $wgLinkCache->getGoodLinks();
233 if ( 0 != count( $a ) ) {
234 $arr = array();
235 foreach( $a as $lt => $lid ) {
236 array_push( $arr, array(
237 'l_from' => $this->mId,
238 'l_to' => $lid ) );
239 }
240 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
241 }
242
243 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
244 $dbw->query( $sql, $fname );
245
246 $a = $wgLinkCache->getBadLinks();
247 if ( 0 != count ( $a ) ) {
248 $arr = array();
249 foreach( $a as $blt ) {
250 array_push($arr,array(
251 'bl_from' => $this->mId,
252 'bl_to' => $blt));
253 }
254 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
255 }
256
257 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
258 $dbw->query( $sql, $fname );
259
260 $a = $wgLinkCache->getImageLinks();
261 $sql = '';
262 if ( 0 != count ( $a ) ) {
263 $arr = array();
264 foreach( $a as $iname => $val )
265 array_push( $arr, array(
266 'il_from' => $this->mId,
267 'il_to' => $iname ) );
268 $dbw->insert( 'imagelinks', $arr, $fname, array( 'IGNORE' ) );
269 }
270
271 if( $wgUseCategoryMagic ) {
272 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
273 $dbw->query( $sql, $fname );
274
275 # Get addition list
276 $add = $wgLinkCache->getCategoryLinks();
277
278 # Do the insertion
279 $sql = '';
280 if ( 0 != count ( $add ) ) {
281 $arr = array();
282 foreach( $add as $cname => $sortkey ) {
283 # FIXME: Change all this to avoid unnecessary duplication
284 $nt = Title::makeTitle( NS_CATEGORY, $cname );
285 if( !$nt ) continue;
286 $nt->invalidateCache();
287 array_push( $arr, array(
288 'cl_from' => $this->mId,
289 'cl_to' => $cname,
290 'cl_sortkey' => $sortkey ) );
291 }
292 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
293 }
294 }
295 $this->fixBrokenLinks();
296 wfProfileOut( $fname );
297 }
298
299 /**
300 * Update any brokenlinks *to* this page
301 * Call for a newly created page, or just to make sure state is consistent
302 */
303 function fixBrokenLinks() {
304 $fname = 'LinksUpdate::fixBrokenLinks';
305
306 $dbw =& wfGetDB( DB_MASTER );
307 $page = $dbw->tableName( 'page' );
308 $links = $dbw->tableName( 'links' );
309
310 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle ),
311 $fname, 'FOR UPDATE' );
312 if ( 0 == $dbw->numRows( $res ) ) { return; }
313
314 $arr=array();
315 $now = $dbw->timestamp();
316 $sql2 = "UPDATE $page SET page_touched='{$now}' WHERE page_id IN (";
317 $first = true;
318 while ( $row = $dbw->fetchObject( $res ) ) {
319 if ( ! $first ) { $sql2 .= ","; }
320 $first = false;
321 array_push( $arr, array(
322 'l_from' => $row->bl_from,
323 'l_to' => $this->mId ) );
324 $sql2 .= $row->bl_from;
325 }
326 $sql2 .= ')';
327
328 # Ignore errors. If a link existed in both the brokenlinks table and the links
329 # table, that's an error which can be fixed at this stage by simply ignoring collisions
330 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
331 $dbw->query( $sql2, $fname );
332 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle ), $fname );
333 }
334 }
335 ?>