fix a problem by not inserting __MWTEMPLATESECTION for section titles that don't...
[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 /**#@+
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->insertArray('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 $blt = $dbw->strencode( $blt );
115 array_push($arr,array(
116 'bl_from'=>$this->mId,
117 'bl_to'=>$blt));
118 }
119 $dbw->insertArray( 'brokenlinks',$arr,$fname,array('IGNORE'));
120 }
121
122 #------------------------------------------------------------------------------
123 # Image links
124 $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
125 $dbw->query( $sql, $fname );
126
127 # Get addition list
128 $add = $wgLinkCache->getImageLinks();
129
130 # Do the insertion
131 $sql = '';
132 $image = Namespace::getImage();
133 if ( 0 != count ( $add ) ) {
134 $arr = array();
135 foreach ($add as $iname => $val ) {
136 $nt = Title::makeTitle( $image, $iname );
137 if( !$nt ) continue;
138 $nt->invalidateCache();
139 $iname = $dbw->strencode( $iname );
140 array_push($arr,array(
141 'il_from'=>$this->mId,
142 'il_to'=>$iname));
143 }
144 $dbw->insertArray($imagelinks, $arr, $fname, array('IGNORE'));
145 }
146
147 #------------------------------------------------------------------------------
148 # Category links
149 if( $wgUseCategoryMagic ) {
150 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
151 $dbw->query( $sql, $fname );
152
153 # Get addition list
154 $add = $wgLinkCache->getCategoryLinks();
155
156 # Do the insertion
157 $sql = '';
158 if ( 0 != count ( $add ) ) {
159 $arr=array();
160 foreach( $add as $cname => $sortkey ) {
161 $nt = Title::makeTitle( NS_CATEGORY, $cname );
162 if( !$nt ) continue;
163 $nt->invalidateCache();
164 array_push($arr,array(
165 'cl_from'=>$this->mId,
166 'cl_to'=>$dbw->strencode( $cname ),
167 'cl_sortkey'=>$dbw->strencode( $sortkey )));
168 }
169 $dbw->insertArray($categorylinks,$arr,$fname,array('IGNORE'));
170 }
171 }
172
173 $this->fixBrokenLinks();
174
175 wfProfileOut( $fname );
176 }
177
178 /**
179 * Old inefficient update function
180 * Used for rebuilding the link table
181 * @todo Only used by ./maintenance/refreshLinks.inc probably need to be replaced
182 * @deprecated
183 */
184 function doDumbUpdate() {
185 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
186 $fname = 'LinksUpdate::doDumbUpdate';
187 wfProfileIn( $fname );
188
189
190 $dbw =& wfGetDB( DB_MASTER );
191 $links = $dbw->tableName( 'links' );
192 $brokenlinks = $dbw->tableName( 'brokenlinks' );
193 $imagelinks = $dbw->tableName( 'imagelinks' );
194 $categorylinks = $dbw->tableName( 'categorylinks' );
195
196 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
197 $dbw->query( $sql, $fname );
198
199 $a = $wgLinkCache->getGoodLinks();
200 if ( 0 != count( $a ) ) {
201 $arr=array();
202 foreach( $a as $lt => $lid ) {
203 array_push($arr,array(
204 'l_from'=>$this->mId,
205 'l_to'=>$lid));
206 }
207 $dbw->insertArray($links,$arr,$fname,array('IGNORE'));
208 }
209
210 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
211 $dbw->query( $sql, $fname );
212
213 $a = $wgLinkCache->getBadLinks();
214 if ( 0 != count ( $a ) ) {
215 $arr=array();
216 foreach( $a as $blt ) {
217 $blt = $dbw->strencode( $blt );
218 array_push($arr,array(
219 'bl_from'=>$this->mId,
220 'bl_to'=>$blt));
221 }
222 $dbw->insertArray($brokenlinks,$arr,$fname,array('IGNORE'));
223 }
224
225 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
226 $dbw->query( $sql, $fname );
227
228 $a = $wgLinkCache->getImageLinks();
229 $sql = '';
230 if ( 0 != count ( $a ) ) {
231 $arr=array();
232 foreach( $a as $iname => $val )
233 array_push($arr,array(
234 'il_from'=>$this->mId,
235 'il_to'=>$dbw->strencode( $iname )));
236 $dbw->insertArray($imagelinks,$arr,$fname,array('IGNORE'));
237 }
238
239 if( $wgUseCategoryMagic ) {
240 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
241 $dbw->query( $sql, $fname );
242
243 # Get addition list
244 $add = $wgLinkCache->getCategoryLinks();
245
246 # Do the insertion
247 $sql = '';
248 if ( 0 != count ( $add ) ) {
249 $arr=array();
250 foreach( $add as $cname => $sortkey ) {
251 # FIXME: Change all this to avoid unnecessary duplication
252 $nt = Title::makeTitle( NS_CATEGORY, $cname );
253 if( !$nt ) continue;
254 $nt->invalidateCache();
255 array_push($arr,array(
256 'cl_from'=>$this->mId,
257 'cl_to'=>$dbw->strencode( $cname ),
258 'cl_sortkey'=>$dbw->strencode( $sortkey )));
259 }
260 $dbw->insertArray($categorylinks,$arr,$fname,array('IGNORE'));
261 }
262 }
263 $this->fixBrokenLinks();
264 wfProfileOut( $fname );
265 }
266
267 /**
268 * Update any brokenlinks *to* this page
269 * Call for a newly created page, or just to make sure state is consistent
270 */
271 function fixBrokenLinks() {
272 $fname = 'LinksUpdate::fixBrokenLinks';
273
274 $dbw =& wfGetDB( DB_MASTER );
275 $cur = $dbw->tableName( 'cur' );
276 $links = $dbw->tableName( 'links' );
277
278 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle ),
279 $fname, 'FOR UPDATE' );
280 if ( 0 == $dbw->numRows( $res ) ) { return; }
281
282 # Ignore errors. If a link existed in both the brokenlinks table and the links
283 # table, that's an error which can be fixed at this stage by simply ignoring collisions
284 $arr=array();
285 $now = $dbw->timestamp();
286 $sql2 = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_id IN (";
287 $first = true;
288 while ( $row = $dbw->fetchObject( $res ) ) {
289 if ( ! $first ) { $sql2 .= ","; }
290 $first = false;
291 array_push($arr,array('l_from'=>$row->bl_from,'l_to'=>$this->mId));
292 $sql2 .= $row->bl_from;
293 }
294 $sql2 .= ')';
295 $dbw->insertArray($links,$arr,$fname,array('IGNORE'));
296 $dbw->query( $sql2, $fname );
297 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle ), $fname );
298 }
299 }
300
301 ?>