11 /* private */ var $mId, $mTitle;
13 function LinksUpdate( $id, $title )
16 $this->mTitle
= $title;
22 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
23 global $wgEnablePersistentLC, $wgUseCategoryMagic;
25 /* Update link tables with outgoing links from an updated article */
26 /* Relies on the 'link cache' to be filled out */
28 $fname = 'LinksUpdate::doUpdate';
29 wfProfileIn( $fname );
34 $dbw =& wfGetDB( DB_MASTER
);
35 $links = $dbw->tableName( 'links' );
36 $brokenlinks = $dbw->tableName( 'brokenlinks' );
37 $imagelinks = $dbw->tableName( 'imagelinks' );
38 $categorylinks = $dbw->tableName( 'categorylinks' );
40 #------------------------------------------------------------------------------
43 if ( $wgLinkCache->incrementalSetup( LINKCACHE_GOOD
, $del, $add ) ) {
44 # Delete where necessary
45 if ( count( $del ) ) {
46 $sql = "DELETE FROM $links WHERE l_from={$this->mId} AND l_to IN(".
47 implode( ',', $del ) . ')';
48 $dbw->query( $sql, $fname );
52 $dbw->delete( 'links', array( 'l_from' => $this->mId
), $fname );
54 # Get the addition list
55 $add = $wgLinkCache->getGoodLinks();
59 if ( 0 != count( $add ) ) {
61 foreach($add as $lt=>$lid)
62 array_push($arr,array(
65 # The link cache was constructed without FOR UPDATE, so there may be collisions
66 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
67 # sure it's better than without IGNORE
68 $dbw->insertArray('links', $arr, $fname, array('IGNORE'));
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(";
79 foreach( $del as $badTitle ) {
85 $sql .= $dbw->addQuotes( $badTitle );
88 $dbw->query( $sql, $fname );
92 $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId
) );
95 $add = $wgLinkCache->getBadLinks();
100 if ( 0 != count ( $add ) ) {
102 foreach( $add as $blt ) {
103 $blt = $dbw->strencode( $blt );
104 array_push($arr,array(
105 'bl_from'=>$this->mId
,
108 $dbw->insertArray( 'brokenlinks',$arr,$fname,array('IGNORE'));
111 #------------------------------------------------------------------------------
113 $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
114 $dbw->query( $sql, $fname );
117 $add = $wgLinkCache->getImageLinks();
121 $image = Namespace::getImage();
122 if ( 0 != count ( $add ) ) {
124 foreach ($add as $iname => $val ) {
125 $nt = Title
::makeTitle( $image, $iname );
127 $nt->invalidateCache();
128 $iname = $dbw->strencode( $iname );
129 array_push($arr,array(
130 'il_from'=>$this->mId
,
133 $dbw->insertArray($imagelinks, $arr, $fname, array('IGNORE'));
136 #------------------------------------------------------------------------------
138 if( $wgUseCategoryMagic ) {
139 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
140 $dbw->query( $sql, $fname );
143 $add = $wgLinkCache->getCategoryLinks();
147 if ( 0 != count ( $add ) ) {
149 foreach( $add as $cname => $sortkey ) {
150 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
152 $nt->invalidateCache();
153 array_push($arr,array(
154 'cl_from'=>$this->mId
,
155 'cl_to'=>$dbw->strencode( $cname ),
156 'cl_sortkey'=>$dbw->strencode( $sortkey )));
158 $dbw->insertArray($categorylinks,$arr,$fname,array('IGNORE'));
162 $this->fixBrokenLinks();
164 wfProfileOut( $fname );
167 function doDumbUpdate()
169 # Old inefficient update function
170 # Used for rebuilding the link table
171 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
172 $fname = 'LinksUpdate::doDumbUpdate';
173 wfProfileIn( $fname );
176 $dbw =& wfGetDB( DB_MASTER
);
177 $links = $dbw->tableName( 'links' );
178 $brokenlinks = $dbw->tableName( 'brokenlinks' );
179 $imagelinks = $dbw->tableName( 'imagelinks' );
180 $categorylinks = $dbw->tableName( 'categorylinks' );
182 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
183 $dbw->query( $sql, $fname );
185 $a = $wgLinkCache->getGoodLinks();
186 if ( 0 != count( $a ) ) {
188 foreach( $a as $lt => $lid ) {
189 array_push($arr,array(
190 'l_from'=>$this->mId
,
193 $dbw->insertArray($links,$arr,$fname,array('IGNORE'));
196 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
197 $dbw->query( $sql, $fname );
199 $a = $wgLinkCache->getBadLinks();
200 if ( 0 != count ( $a ) ) {
202 foreach( $a as $blt ) {
203 $blt = $dbw->strencode( $blt );
204 array_push($arr,array(
205 'bl_from'=>$this->mId
,
208 $dbw->insertArray($brokenlinks,$arr,$fname,array('IGNORE'));
211 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
212 $dbw->query( $sql, $fname );
214 $a = $wgLinkCache->getImageLinks();
216 if ( 0 != count ( $a ) ) {
218 foreach( $a as $iname => $val )
219 array_push($arr,array(
220 'il_from'=>$this->mId
,
221 'il_to'=>$dbw->strencode( $iname )));
222 $dbw->insertArray($imagelinks,$arr,$fname,array('IGNORE'));
225 if( $wgUseCategoryMagic ) {
226 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
227 $dbw->query( $sql, $fname );
230 $add = $wgLinkCache->getCategoryLinks();
234 if ( 0 != count ( $add ) ) {
236 foreach( $add as $cname => $sortkey ) {
237 # FIXME: Change all this to avoid unnecessary duplication
238 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
240 $nt->invalidateCache();
241 array_push($arr,array(
242 'cl_from'=>$this->mId
,
243 'cl_to'=>$dbw->strencode( $cname ),
244 'cl_sortkey'=>$dbw->strencode( $sortkey )));
246 $dbw->insertArray($categorylinks,$arr,$fname,array('IGNORE'));
249 $this->fixBrokenLinks();
250 wfProfileOut( $fname );
253 function fixBrokenLinks() {
254 /* Update any brokenlinks *to* this page */
255 /* Call for a newly created page, or just to make sure state is consistent */
256 $fname = 'LinksUpdate::fixBrokenLinks';
258 $dbw =& wfGetDB( DB_MASTER
);
259 $cur = $dbw->tableName( 'cur' );
260 $links = $dbw->tableName( 'links' );
262 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle
),
263 $fname, 'FOR UPDATE' );
264 if ( 0 == $dbw->numRows( $res ) ) { return; }
266 # Ignore errors. If a link existed in both the brokenlinks table and the links
267 # table, that's an error which can be fixed at this stage by simply ignoring collisions
269 $now = wfTimestampNow();
270 $sql2 = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_id IN (";
272 while ( $row = $dbw->fetchObject( $res ) ) {
273 if ( ! $first ) { $sql2 .= ","; }
275 array_push($arr,array('l_from'=>$row->bl_from
,'l_to'=>$this->mId
));
276 $sql2 .= $row->bl_from
;
279 $dbw->insertArray($links,$arr,$fname,array('IGNORE'));
280 $dbw->query( $sql2, $fname );
281 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle
), $fname );