* Added a test for a link with multiple pipes
[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 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
149 $dbw->query( $sql, $fname );
150
151 # Get addition list
152 $add = $wgLinkCache->getCategoryLinks();
153
154 # Do the insertion
155 $sql = '';
156 if ( 0 != count ( $add ) ) {
157 $arr = array();
158 foreach( $add as $cname => $sortkey ) {
159 $nt = Title::makeTitle( NS_CATEGORY, $cname );
160 if( !$nt ) continue;
161 $nt->invalidateCache();
162 array_push( $arr, array(
163 'cl_from' => $this->mId,
164 'cl_to' => $cname,
165 'cl_sortkey' => $sortkey ) );
166 }
167 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
168 }
169 }
170
171 $this->fixBrokenLinks();
172
173 wfProfileOut( $fname );
174 }
175
176 /**
177 * Link update which clears the previous entries and inserts new ones
178 * May be slower or faster depending on level of lock contention and write speed of DB
179 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
180 */
181 function doDumbUpdate() {
182 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
183 $fname = 'LinksUpdate::doDumbUpdate';
184 wfProfileIn( $fname );
185
186
187 $dbw =& wfGetDB( DB_MASTER );
188 $links = $dbw->tableName( 'links' );
189 $brokenlinks = $dbw->tableName( 'brokenlinks' );
190 $imagelinks = $dbw->tableName( 'imagelinks' );
191 $categorylinks = $dbw->tableName( 'categorylinks' );
192
193 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
194 $dbw->query( $sql, $fname );
195
196 $a = $wgLinkCache->getGoodLinks();
197 if ( 0 != count( $a ) ) {
198 $arr = array();
199 foreach( $a as $lt => $lid ) {
200 array_push( $arr, array(
201 'l_from' => $this->mId,
202 'l_to' => $lid ) );
203 }
204 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
205 }
206
207 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
208 $dbw->query( $sql, $fname );
209
210 $a = $wgLinkCache->getBadLinks();
211 if ( 0 != count ( $a ) ) {
212 $arr = array();
213 foreach( $a as $blt ) {
214 array_push($arr,array(
215 'bl_from' => $this->mId,
216 'bl_to' => $blt));
217 }
218 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
219 }
220
221 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
222 $dbw->query( $sql, $fname );
223
224 $a = $wgLinkCache->getImageLinks();
225 $sql = '';
226 if ( 0 != count ( $a ) ) {
227 $arr = array();
228 foreach( $a as $iname => $val )
229 array_push( $arr, array(
230 'il_from' => $this->mId,
231 'il_to' => $iname ) );
232 $dbw->insert( 'imagelinks', $arr, $fname, array( 'IGNORE' ) );
233 }
234
235 if( $wgUseCategoryMagic ) {
236 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
237 $dbw->query( $sql, $fname );
238
239 # Get addition list
240 $add = $wgLinkCache->getCategoryLinks();
241
242 # Do the insertion
243 $sql = '';
244 if ( 0 != count ( $add ) ) {
245 $arr = array();
246 foreach( $add as $cname => $sortkey ) {
247 # FIXME: Change all this to avoid unnecessary duplication
248 $nt = Title::makeTitle( NS_CATEGORY, $cname );
249 if( !$nt ) continue;
250 $nt->invalidateCache();
251 array_push( $arr, array(
252 'cl_from' => $this->mId,
253 'cl_to' => $cname,
254 'cl_sortkey' => $sortkey ) );
255 }
256 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
257 }
258 }
259 $this->fixBrokenLinks();
260 wfProfileOut( $fname );
261 }
262
263 /**
264 * Update any brokenlinks *to* this page
265 * Call for a newly created page, or just to make sure state is consistent
266 */
267 function fixBrokenLinks() {
268 $fname = 'LinksUpdate::fixBrokenLinks';
269
270 $dbw =& wfGetDB( DB_MASTER );
271 $page = $dbw->tableName( 'page' );
272 $links = $dbw->tableName( 'links' );
273
274 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle ),
275 $fname, 'FOR UPDATE' );
276 if ( 0 == $dbw->numRows( $res ) ) { return; }
277
278 $arr=array();
279 $now = $dbw->timestamp();
280 $sql2 = "UPDATE $page SET page_touched='{$now}' WHERE page_id IN (";
281 $first = true;
282 while ( $row = $dbw->fetchObject( $res ) ) {
283 if ( ! $first ) { $sql2 .= ","; }
284 $first = false;
285 array_push( $arr, array(
286 'l_from' => $row->bl_from,
287 'l_to' => $this->mId ) );
288 $sql2 .= $row->bl_from;
289 }
290 $sql2 .= ')';
291
292 # Ignore errors. If a link existed in both the brokenlinks table and the links
293 # table, that's an error which can be fixed at this stage by simply ignoring collisions
294 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
295 $dbw->query( $sql2, $fname );
296 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle ), $fname );
297 }
298 }
299 ?>