bug #73: category sortkeys are set to "Special:Upload" instead of the
[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 $dbw->delete('links',array('l_from'=>$this->mId, 'l_to'=> $del),$fname);
58 }
59 } else {
60 # Delete everything
61 $dbw->delete( 'links', array( 'l_from' => $this->mId ), $fname );
62
63 # Get the addition list
64 $add = $wgLinkCache->getGoodLinks();
65 }
66
67 # Do the insertion
68 if ( 0 != count( $add ) ) {
69 $arr=array();
70 foreach($add as $lt=>$lid)
71 array_push( $arr, array(
72 'l_from' => $this->mId,
73 'l_to' => $lid ) );
74 # The link cache was constructed without FOR UPDATE, so there may be collisions
75 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
76 # sure it's better than without IGNORE
77 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
78 }
79
80 #------------------------------------------------------------------------------
81 # Bad links
82
83 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD, $del, $add ) ) {
84 # Delete where necessary
85 if ( count( $del ) ) {
86 $dbw->delete('brokenlinks',array('bl_from'=>$this->mId, 'bl_to'=> $del),$fname);
87 }
88 } else {
89 # Delete all
90 $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId ),$fname );
91
92 # Get addition list
93 $add = $wgLinkCache->getBadLinks();
94 }
95
96 # Do additions
97 $sql = '';
98 if ( 0 != count ( $add ) ) {
99 $arr = array();
100 foreach( $add as $blt ) {
101 array_push( $arr, array(
102 'bl_from' => $this->mId,
103 'bl_to' => $blt ) );
104 }
105 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
106 }
107
108 #------------------------------------------------------------------------------
109 # Image links
110 $dbw->delete('imagelinks',array('il_from'=>$this->mId),$fname);
111
112 # Get addition list
113 $add = $wgLinkCache->getImageLinks();
114
115 # Do the insertion
116 $sql = '';
117 $image = NS_IMAGE;
118 if ( 0 != count ( $add ) ) {
119 $arr = array();
120 foreach ($add as $iname => $val ) {
121 $nt = Title::makeTitle( $image, $iname );
122 if( !$nt ) continue;
123 $nt->invalidateCache();
124 array_push( $arr, array(
125 'il_from' => $this->mId,
126 'il_to' => $iname ) );
127 }
128 $dbw->insert('imagelinks', $arr, $fname, array('IGNORE'));
129 }
130
131 #------------------------------------------------------------------------------
132 # Category links
133 if( $wgUseCategoryMagic ) {
134 global $messageMemc, $wgDBname;
135
136 # Get addition list
137 $add = $wgLinkCache->getCategoryLinks();
138
139 # select existing catlinks for this page
140 $res = $dbw->select( $categorylinks, array( 'cl_to' ), array( 'cl_from' => $this->mId ),
141 $fname, 'FOR UPDATE' );
142
143 $del = array();
144 if(0 != $dbw->numRows( $res )) {
145 while ( $row = $dbw->fetchObject( $res ) ) {
146 if(!isset($add[$row->cl_to])) {
147 // in the db, but no longer in the page -> delete
148 $del[] = $row->cl_to;
149 } else {
150 // remove already existing category memberships
151 // from the add array
152 unset($add[$row->cl_to]);
153 }
154 }
155 }
156 // delete any removed categorylinks
157 if(count($del) > 0) {
158 // delete old ones
159 $dbw->delete('categorylinks', array('cl_from'=>$this->mId, 'cl_to'=>$del),$fname);
160 foreach($del as $cname){
161 $nt = Title::makeTitle( NS_CATEGORY, $cname );
162 $nt->invalidateCache();
163 // update the timestamp which indicates when the last article
164 // was added or removed to/from this article
165 $key = $wgDBname.':Category:'.$nt->getDBkey().':adddeltimestamp';
166 $messageMemc->set( $key , wfTimestamp( TS_MW ), 24*3600 );
167 #wfDebug( "Linksupdate:Cats:del: ".serialize($nt)." $key \n" );
168 }
169 }
170 // add any new category memberships
171 if (count($add) > 0) {
172 $arr = array();
173 foreach( $add as $cname => $sortkey ) {
174 $nt = Title::makeTitle( NS_CATEGORY, $cname );
175 if( !$nt ) continue;
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:add: ".serialize($nt)." $key\n" );
182 #wfDebug( "LU-get: ".$messageMemc->get( $key)."\n");
183 array_push( $arr, array(
184 'cl_from' => $this->mId,
185 'cl_to' => $cname,
186 'cl_sortkey' => $sortkey ) );
187 }
188 // do the actual sql insertion
189 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
190 }
191 }
192
193 $this->fixBrokenLinks();
194
195 wfProfileOut( $fname );
196 }
197
198 /**
199 * Link update which clears the previous entries and inserts new ones
200 * May be slower or faster depending on level of lock contention and write speed of DB
201 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
202 */
203 function doDumbUpdate() {
204 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
205 $fname = 'LinksUpdate::doDumbUpdate';
206 wfProfileIn( $fname );
207
208
209 $dbw =& wfGetDB( DB_MASTER );
210 $links = $dbw->tableName( 'links' );
211 $brokenlinks = $dbw->tableName( 'brokenlinks' );
212 $imagelinks = $dbw->tableName( 'imagelinks' );
213 $categorylinks = $dbw->tableName( 'categorylinks' );
214
215 $dbw->delete('links', array('l_from'=>$this->mId),$fname);
216
217 $a = $wgLinkCache->getGoodLinks();
218 if ( 0 != count( $a ) ) {
219 $arr = array();
220 foreach( $a as $lt => $lid ) {
221 array_push( $arr, array(
222 'l_from' => $this->mId,
223 'l_to' => $lid ) );
224 }
225 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
226 }
227
228 $dbw->delete('brokenlinks', array('bl_from'=>$this->mId),$fname);
229
230 $a = $wgLinkCache->getBadLinks();
231 if ( 0 != count ( $a ) ) {
232 $arr = array();
233 foreach( $a as $blt ) {
234 array_push($arr,array(
235 'bl_from' => $this->mId,
236 'bl_to' => $blt));
237 }
238 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
239 }
240
241 $dbw->delete('imagelinks', array('il_from'=>$this->mId),$fname);
242
243 $a = $wgLinkCache->getImageLinks();
244 $sql = '';
245 if ( 0 != count ( $a ) ) {
246 $arr = array();
247 foreach( $a as $iname => $val )
248 array_push( $arr, array(
249 'il_from' => $this->mId,
250 'il_to' => $iname ) );
251 $dbw->insert( 'imagelinks', $arr, $fname, array( 'IGNORE' ) );
252 }
253
254 if( $wgUseCategoryMagic ) {
255 $dbw->delete('categorylinks', array('cl_from'=>$this->mId),$fname);
256
257 # Get addition list
258 $add = $wgLinkCache->getCategoryLinks();
259
260 # Do the insertion
261 $sql = '';
262 if ( 0 != count ( $add ) ) {
263 $arr = array();
264 foreach( $add as $cname => $sortkey ) {
265 # FIXME: Change all this to avoid unnecessary duplication
266 $nt = Title::makeTitle( NS_CATEGORY, $cname );
267 if( !$nt ) continue;
268 $nt->invalidateCache();
269 array_push( $arr, array(
270 'cl_from' => $this->mId,
271 'cl_to' => $cname,
272 'cl_sortkey' => $sortkey ) );
273 }
274 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
275 }
276 }
277 $this->fixBrokenLinks();
278 wfProfileOut( $fname );
279 }
280
281 /**
282 * Update any brokenlinks *to* this page
283 * Call for a newly created page, or just to make sure state is consistent
284 */
285 function fixBrokenLinks() {
286 $fname = 'LinksUpdate::fixBrokenLinks';
287
288 $dbw =& wfGetDB( DB_MASTER );
289 $page = $dbw->tableName( 'page' );
290 $links = $dbw->tableName( 'links' );
291
292 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle ),
293 $fname, 'FOR UPDATE' );
294 if ( 0 == $dbw->numRows( $res ) ) { return; }
295
296 $arr=array();
297 $toucharr=array();
298 while ( $row = $dbw->fetchObject( $res ) ) {
299 array_push( $arr, array(
300 'l_from' => $row->bl_from,
301 'l_to' => $this->mId ) );
302 $toucharr[]=$row->bl_from;
303 }
304
305 # Ignore errors. If a link existed in both the brokenlinks table and the links
306 # table, that's an error which can be fixed at this stage by simply ignoring collisions
307 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
308 $dbw->update( 'page', /* SET */ array( 'page_touched' => $dbw->timestamp() ),
309 /* WHERE */ array( 'page_id' => $toucharr ),$fname);
310 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle ), $fname );
311 }
312 }
313 ?>