21 * Initialize private variables
23 * @param string $title
25 function LinksUpdate( $id, $title ) {
27 $this->mTitle
= $title;
31 * Update link tables with outgoing links from an updated article
32 * Relies on the 'link cache' to be filled out.
36 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
37 global $wgEnablePersistentLC, $wgUseCategoryMagic;
39 $fname = 'LinksUpdate::doUpdate';
40 wfProfileIn( $fname );
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' );
51 #------------------------------------------------------------------------------
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 );
63 $dbw->delete( 'links', array( 'l_from' => $this->mId
), $fname );
65 # Get the addition list
66 $add = $wgLinkCache->getGoodLinks();
70 if ( 0 != count( $add ) ) {
72 foreach($add as $lt=>$lid)
73 array_push( $arr, array(
74 'l_from' => $this->mId
,
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' ) );
82 #------------------------------------------------------------------------------
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(";
90 foreach( $del as $badTitle ) {
96 $sql .= $dbw->addQuotes( $badTitle );
99 $dbw->query( $sql, $fname );
103 $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId
) );
106 $add = $wgLinkCache->getBadLinks();
111 if ( 0 != count ( $add ) ) {
113 foreach( $add as $blt ) {
114 array_push( $arr, array(
115 'bl_from' => $this->mId
,
118 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
121 #------------------------------------------------------------------------------
123 $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
124 $dbw->query( $sql, $fname );
127 $add = $wgLinkCache->getImageLinks();
132 if ( 0 != count ( $add ) ) {
134 foreach ($add as $iname => $val ) {
135 $nt = Title
::makeTitle( $image, $iname );
137 $nt->invalidateCache();
138 array_push( $arr, array(
139 'il_from' => $this->mId
,
140 'il_to' => $iname ) );
142 $dbw->insert('imagelinks', $arr, $fname, array('IGNORE'));
145 #------------------------------------------------------------------------------
147 if( $wgUseCategoryMagic ) {
148 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
149 $dbw->query( $sql, $fname );
152 $add = $wgLinkCache->getCategoryLinks();
156 if ( 0 != count ( $add ) ) {
158 foreach( $add as $cname => $sortkey ) {
159 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
161 $nt->invalidateCache();
162 array_push( $arr, array(
163 'cl_from' => $this->mId
,
165 'cl_sortkey' => $sortkey ) );
167 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
171 $this->fixBrokenLinks();
173 wfProfileOut( $fname );
177 * Old inefficient update function
178 * Used for rebuilding the link table
179 * @todo Only used by ./maintenance/refreshLinks.inc probably need to be replaced
182 function doDumbUpdate() {
183 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
184 $fname = 'LinksUpdate::doDumbUpdate';
185 wfProfileIn( $fname );
188 $dbw =& wfGetDB( DB_MASTER
);
189 $links = $dbw->tableName( 'links' );
190 $brokenlinks = $dbw->tableName( 'brokenlinks' );
191 $imagelinks = $dbw->tableName( 'imagelinks' );
192 $categorylinks = $dbw->tableName( 'categorylinks' );
194 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
195 $dbw->query( $sql, $fname );
197 $a = $wgLinkCache->getGoodLinks();
198 if ( 0 != count( $a ) ) {
200 foreach( $a as $lt => $lid ) {
201 array_push( $arr, array(
202 'l_from' => $this->mId
,
205 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
208 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
209 $dbw->query( $sql, $fname );
211 $a = $wgLinkCache->getBadLinks();
212 if ( 0 != count ( $a ) ) {
214 foreach( $a as $blt ) {
215 array_push($arr,array(
216 'bl_from' => $this->mId
,
219 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
222 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
223 $dbw->query( $sql, $fname );
225 $a = $wgLinkCache->getImageLinks();
227 if ( 0 != count ( $a ) ) {
229 foreach( $a as $iname => $val )
230 array_push( $arr, array(
231 'il_from' => $this->mId
,
232 'il_to' => $iname ) );
233 $dbw->insert( 'imagelinks', $arr, $fname, array( 'IGNORE' ) );
236 if( $wgUseCategoryMagic ) {
237 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
238 $dbw->query( $sql, $fname );
241 $add = $wgLinkCache->getCategoryLinks();
245 if ( 0 != count ( $add ) ) {
247 foreach( $add as $cname => $sortkey ) {
248 # FIXME: Change all this to avoid unnecessary duplication
249 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
251 $nt->invalidateCache();
252 array_push( $arr, array(
253 'cl_from' => $this->mId
,
255 'cl_sortkey' => $sortkey ) );
257 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
260 $this->fixBrokenLinks();
261 wfProfileOut( $fname );
265 * Update any brokenlinks *to* this page
266 * Call for a newly created page, or just to make sure state is consistent
268 function fixBrokenLinks() {
269 $fname = 'LinksUpdate::fixBrokenLinks';
271 $dbw =& wfGetDB( DB_MASTER
);
272 $page = $dbw->tableName( 'page' );
273 $links = $dbw->tableName( 'links' );
275 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle
),
276 $fname, 'FOR UPDATE' );
277 if ( 0 == $dbw->numRows( $res ) ) { return; }
280 $now = $dbw->timestamp();
281 $sql2 = "UPDATE $page SET page_touched='{$now}' WHERE page_id IN (";
283 while ( $row = $dbw->fetchObject( $res ) ) {
284 if ( ! $first ) { $sql2 .= ","; }
286 array_push( $arr, array(
287 'l_from' => $row->bl_from
,
288 'l_to' => $this->mId
) );
289 $sql2 .= $row->bl_from
;
293 # Ignore errors. If a link existed in both the brokenlinks table and the links
294 # table, that's an error which can be fixed at this stage by simply ignoring collisions
295 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
296 $dbw->query( $sql2, $fname );
297 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle
), $fname );