6 /* private */ var $mId, $mTitle;
8 function LinksUpdate( $id, $title )
11 $this->mTitle
= $title;
12 $this->mTitleEnc
= wfStrencode( $title );
18 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
19 global $wgEnablePersistentLC, $wgUseCategoryMagic;
21 /* Update link tables with outgoing links from an updated article */
22 /* Relies on the 'link cache' to be filled out */
24 $fname = "LinksUpdate::doUpdate";
25 wfProfileIn( $fname );
30 if( $wgDBtransactions ) {
32 wfQuery( $sql, DB_WRITE
, $fname );
35 #------------------------------------------------------------------------------
38 if ( $wgLinkCache->incrementalSetup( LINKCACHE_GOOD
, $del, $add ) ) {
39 # Delete where necessary
40 if ( count( $del ) ) {
41 $sql = "DELETE FROM links WHERE l_from={$this->mId} AND l_to IN(".
42 implode( ",", $del ) . ")";
43 wfQuery( $sql, DB_WRITE
, $fname );
47 $sql = "DELETE FROM links WHERE l_from={$this->mId}";
48 wfQuery( $sql, DB_WRITE
, $fname );
50 # Get the addition list
51 $add = $wgLinkCache->getGoodLinks();
56 if ( 0 != count( $add ) ) {
57 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
59 foreach( $add as $lt => $lid ) {
61 if ( ! $first ) { $sql .= ","; }
64 $sql .= "({$this->mId},{$lid})";
68 wfQuery( $sql, DB_WRITE
, $fname );
71 #------------------------------------------------------------------------------
74 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD
, $del, $add ) ) {
75 # Delete where necessary
76 if ( count( $del ) ) {
77 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId} AND bl_to IN('" .
78 implode( "','", array_map( "wfStrencode", $del ) ) . "')";
79 wfQuery( $sql, DB_WRITE
, $fname );
83 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
84 wfQuery( $sql, DB_WRITE
, $fname );
87 $add = $wgLinkCache->getBadLinks();
92 if ( 0 != count ( $add ) ) {
93 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
95 foreach( $add as $blt ) {
96 $blt = wfStrencode( $blt );
97 if ( ! $first ) { $sql .= ","; }
100 $sql .= "({$this->mId},'{$blt}')";
104 wfQuery( $sql, DB_WRITE
, $fname );
107 #------------------------------------------------------------------------------
109 $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mId}'";
110 wfQuery( $sql, DB_WRITE
, $fname );
113 $add = $wgLinkCache->getImageLinks();
117 $image = Namespace::getImage();
118 if ( 0 != count ( $add ) ) {
119 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
121 foreach( $add as $iname => $val ) {
122 # FIXME: Change all this to avoid unnecessary duplication
123 $nt = Title
::makeTitle( $image, $iname );
125 $nt->invalidateCache();
127 $iname = wfStrencode( $iname );
128 if ( ! $first ) { $sql .= ","; }
131 $sql .= "({$this->mId},'{$iname}')";
134 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE
, $fname ); }
136 #------------------------------------------------------------------------------
138 if( $wgUseCategoryMagic ) {
139 $sql = "DELETE FROM categorylinks WHERE cl_from='{$this->mId}'";
140 wfQuery( $sql, DB_WRITE
, $fname );
143 $add = $wgLinkCache->getCategoryLinks();
147 if ( 0 != count ( $add ) ) {
148 $sql = "INSERT INTO categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
150 foreach( $add as $cname => $sortkey ) {
151 # FIXME: Change all this to avoid unnecessary duplication
152 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
154 $nt->invalidateCache();
156 if ( ! $first ) { $sql .= ","; }
159 $sql .= "({$this->mId},'" . wfStrencode( $cname ) .
160 "','" . wfStrencode( $sortkey ) . "')";
163 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE
, $fname ); }
166 $this->fixBrokenLinks();
168 if( $wgDBtransactions ) {
170 wfQuery( $sql, DB_WRITE
, $fname );
172 wfProfileOut( $fname );
175 function doDumbUpdate()
177 # Old inefficient update function
178 # Used for rebuilding the link table
180 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
181 $fname = "LinksUpdate::doDumbUpdate";
182 wfProfileIn( $fname );
184 if( $wgDBtransactions ) {
186 wfQuery( $sql, DB_WRITE
, $fname );
189 $sql = "DELETE FROM links WHERE l_from={$this->mId}";
190 wfQuery( $sql, DB_WRITE
, $fname );
192 $a = $wgLinkCache->getGoodLinks();
194 if ( 0 != count( $a ) ) {
195 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
197 foreach( $a as $lt => $lid ) {
198 if ( ! $first ) { $sql .= ","; }
201 $sql .= "({$this->mId},{$lid})";
204 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE
, $fname ); }
206 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
207 wfQuery( $sql, DB_WRITE
, $fname );
209 $a = $wgLinkCache->getBadLinks();
211 if ( 0 != count ( $a ) ) {
212 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
214 foreach( $a as $blt ) {
215 $blt = wfStrencode( $blt );
216 if ( ! $first ) { $sql .= ","; }
219 $sql .= "({$this->mId},'{$blt}')";
222 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE
, $fname ); }
224 $sql = "DELETE FROM imagelinks WHERE il_from={$this->mId}";
225 wfQuery( $sql, DB_WRITE
, $fname );
227 $a = $wgLinkCache->getImageLinks();
229 if ( 0 != count ( $a ) ) {
230 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
232 foreach( $a as $iname => $val ) {
233 $iname = wfStrencode( $iname );
234 if ( ! $first ) { $sql .= ","; }
237 $sql .= "({$this->mId},'{$iname}')";
240 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE
, $fname ); }
242 if( $wgUseCategoryMagic ) {
243 $sql = "DELETE FROM categorylinks WHERE cl_from='{$this->mId}'";
244 wfQuery( $sql, DB_WRITE
, $fname );
247 $add = $wgLinkCache->getCategoryLinks();
251 if ( 0 != count ( $add ) ) {
252 $sql = "INSERT INTO categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
254 foreach( $add as $cname => $sortkey ) {
255 # FIXME: Change all this to avoid unnecessary duplication
256 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
258 $nt->invalidateCache();
260 if ( ! $first ) { $sql .= ","; }
263 $sql .= "({$this->mId},'" . wfStrencode( $cname ) .
264 "','" . wfStrencode( $sortkey ) . "')";
267 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE
, $fname ); }
269 $this->fixBrokenLinks();
271 if( $wgDBtransactions ) {
273 wfQuery( $sql, DB_WRITE
, $fname );
275 wfProfileOut( $fname );
278 function fixBrokenLinks() {
279 /* Update any brokenlinks *to* this page */
280 /* Call for a newly created page, or just to make sure state is consistent */
281 $fname = "LinksUpdate::fixBrokenLinks";
283 $sql = "SELECT bl_from FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
284 $res = wfQuery( $sql, DB_READ
, $fname );
285 if ( 0 == wfNumRows( $res ) ) { return; }
287 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
288 $now = wfTimestampNow();
289 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
291 while ( $row = wfFetchObject( $res ) ) {
292 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
295 $sql .= "({$row->bl_from},{$this->mId})";
296 $sql2 .= $row->bl_from
;
299 wfQuery( $sql, DB_WRITE
, $fname );
300 wfQuery( $sql2, DB_WRITE
, $fname );
302 $sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
303 wfQuery( $sql, DB_WRITE
, $fname );