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