Incremental categorylink updates, mainly to keep the old timestamp.
[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 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}' AND cl_to IN('";
174 $sql .= implode("','", $del) . "')";
175 $dbw->query( $sql, $fname );
176 foreach($del as $cname){
177 $nt = Title::makeTitle( NS_CATEGORY, $cname );
178 $nt->invalidateCache();
179 // update the timestamp which indicates when the last article
180 // was added or removed to/from this article
181 $key = $wgDBname.':Category:'.$nt->getDBkey().':adddeltimestamp';
182 $messageMemc->set( $key , wfTimestamp( TS_MW ), 24*3600 );
183 #wfDebug( "Linksupdate:Cats:del: ".serialize($nt)." $key \n" );
184 }
185 }
186 // add any new category memberships
187 if (count($add) > 0) {
188 $arr = array();
189 foreach( $add as $cname => $sortkey ) {
190 $nt = Title::makeTitle( NS_CATEGORY, $cname );
191 if( !$nt ) continue;
192 $nt->invalidateCache();
193 // update the timestamp which indicates when the last article
194 // was added or removed to/from this article
195 $key = $wgDBname.':Category:'.$nt->getDBkey().':adddeltimestamp';
196 $messageMemc->set( $key , wfTimestamp( TS_MW ), 24*3600 );
197 #wfDebug( "Linksupdate:Cats:add: ".serialize($nt)." $key\n" );
198 #wfDebug( "LU-get: ".$messageMemc->get( $key)."\n");
199 array_push( $arr, array(
200 'cl_from' => $this->mId,
201 'cl_to' => $cname,
202 'cl_sortkey' => $sortkey ) );
203 }
204 // do the actual sql insertion
205 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
206 }
207 }
208
209 $this->fixBrokenLinks();
210
211 wfProfileOut( $fname );
212 }
213
214 /**
215 * Link update which clears the previous entries and inserts new ones
216 * May be slower or faster depending on level of lock contention and write speed of DB
217 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
218 */
219 function doDumbUpdate() {
220 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
221 $fname = 'LinksUpdate::doDumbUpdate';
222 wfProfileIn( $fname );
223
224
225 $dbw =& wfGetDB( DB_MASTER );
226 $links = $dbw->tableName( 'links' );
227 $brokenlinks = $dbw->tableName( 'brokenlinks' );
228 $imagelinks = $dbw->tableName( 'imagelinks' );
229 $categorylinks = $dbw->tableName( 'categorylinks' );
230
231 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
232 $dbw->query( $sql, $fname );
233
234 $a = $wgLinkCache->getGoodLinks();
235 if ( 0 != count( $a ) ) {
236 $arr = array();
237 foreach( $a as $lt => $lid ) {
238 array_push( $arr, array(
239 'l_from' => $this->mId,
240 'l_to' => $lid ) );
241 }
242 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
243 }
244
245 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
246 $dbw->query( $sql, $fname );
247
248 $a = $wgLinkCache->getBadLinks();
249 if ( 0 != count ( $a ) ) {
250 $arr = array();
251 foreach( $a as $blt ) {
252 array_push($arr,array(
253 'bl_from' => $this->mId,
254 'bl_to' => $blt));
255 }
256 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
257 }
258
259 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
260 $dbw->query( $sql, $fname );
261
262 $a = $wgLinkCache->getImageLinks();
263 $sql = '';
264 if ( 0 != count ( $a ) ) {
265 $arr = array();
266 foreach( $a as $iname => $val )
267 array_push( $arr, array(
268 'il_from' => $this->mId,
269 'il_to' => $iname ) );
270 $dbw->insert( 'imagelinks', $arr, $fname, array( 'IGNORE' ) );
271 }
272
273 if( $wgUseCategoryMagic ) {
274 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
275 $dbw->query( $sql, $fname );
276
277 # Get addition list
278 $add = $wgLinkCache->getCategoryLinks();
279
280 # Do the insertion
281 $sql = '';
282 if ( 0 != count ( $add ) ) {
283 $arr = array();
284 foreach( $add as $cname => $sortkey ) {
285 # FIXME: Change all this to avoid unnecessary duplication
286 $nt = Title::makeTitle( NS_CATEGORY, $cname );
287 if( !$nt ) continue;
288 $nt->invalidateCache();
289 array_push( $arr, array(
290 'cl_from' => $this->mId,
291 'cl_to' => $cname,
292 'cl_sortkey' => $sortkey ) );
293 }
294 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
295 }
296 }
297 $this->fixBrokenLinks();
298 wfProfileOut( $fname );
299 }
300
301 /**
302 * Update any brokenlinks *to* this page
303 * Call for a newly created page, or just to make sure state is consistent
304 */
305 function fixBrokenLinks() {
306 $fname = 'LinksUpdate::fixBrokenLinks';
307
308 $dbw =& wfGetDB( DB_MASTER );
309 $page = $dbw->tableName( 'page' );
310 $links = $dbw->tableName( 'links' );
311
312 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle ),
313 $fname, 'FOR UPDATE' );
314 if ( 0 == $dbw->numRows( $res ) ) { return; }
315
316 $arr=array();
317 $now = $dbw->timestamp();
318 $sql2 = "UPDATE $page SET page_touched='{$now}' WHERE page_id IN (";
319 $first = true;
320 while ( $row = $dbw->fetchObject( $res ) ) {
321 if ( ! $first ) { $sql2 .= ","; }
322 $first = false;
323 array_push( $arr, array(
324 'l_from' => $row->bl_from,
325 'l_to' => $this->mId ) );
326 $sql2 .= $row->bl_from;
327 }
328 $sql2 .= ')';
329
330 # Ignore errors. If a link existed in both the brokenlinks table and the links
331 # table, that's an error which can be fixed at this stage by simply ignoring collisions
332 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
333 $dbw->query( $sql2, $fname );
334 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle ), $fname );
335 }
336 }
337 ?>