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 $dbw->delete('links',array('l_from'=>$this->mId
, 'l_to'=> $del),$fname);
61 $dbw->delete( 'links', array( 'l_from' => $this->mId
), $fname );
63 # Get the addition list
64 $add = $wgLinkCache->getGoodLinks();
68 if ( 0 != count( $add ) ) {
70 foreach($add as $lt=>$lid)
71 array_push( $arr, array(
72 'l_from' => $this->mId
,
74 # The link cache was constructed without FOR UPDATE, so there may be collisions
75 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
76 # sure it's better than without IGNORE
77 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
80 #------------------------------------------------------------------------------
83 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD
, $del, $add ) ) {
84 # Delete where necessary
85 if ( count( $del ) ) {
86 $dbw->delete('brokenlinks',array('bl_from'=>$this->mId
, 'bl_to'=> $del),$fname);
90 $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId
),$fname );
93 $add = $wgLinkCache->getBadLinks();
98 if ( 0 != count ( $add ) ) {
100 foreach( $add as $blt ) {
101 array_push( $arr, array(
102 'bl_from' => $this->mId
,
105 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
108 #------------------------------------------------------------------------------
110 $dbw->delete('imagelinks',array('il_from'=>$this->mId
),$fname);
113 $add = $wgLinkCache->getImageLinks();
118 if ( 0 != count ( $add ) ) {
120 foreach ($add as $iname => $val ) {
121 $nt = Title
::makeTitle( $image, $iname );
123 $nt->invalidateCache();
124 array_push( $arr, array(
125 'il_from' => $this->mId
,
126 'il_to' => $iname ) );
128 $dbw->insert('imagelinks', $arr, $fname, array('IGNORE'));
131 #------------------------------------------------------------------------------
133 if( $wgUseCategoryMagic ) {
134 global $messageMemc, $wgDBname;
137 $add = $wgLinkCache->getCategoryLinks();
139 # select existing catlinks for this page
140 $res = $dbw->select( $categorylinks, array( 'cl_to' ), array( 'cl_from' => $this->mId
),
141 $fname, 'FOR UPDATE' );
144 if(0 != $dbw->numRows( $res )) {
145 while ( $row = $dbw->fetchObject( $res ) ) {
146 if(!isset($add[$row->cl_to
])) {
147 // in the db, but no longer in the page -> delete
148 $del[] = $row->cl_to
;
150 // remove already existing category memberships
151 // from the add array
152 unset($add[$row->cl_to
]);
156 // delete any removed categorylinks
157 if(count($del) > 0) {
159 $dbw->delete('categorylinks', array('cl_from'=>$this->mId
, 'cl_to'=>$del),$fname);
160 foreach($del as $cname){
161 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
162 $nt->invalidateCache();
163 // update the timestamp which indicates when the last article
164 // was added or removed to/from this article
165 $key = $wgDBname.':Category:'.$nt->getDBkey().':adddeltimestamp';
166 $messageMemc->set( $key , wfTimestamp( TS_MW
), 24*3600 );
167 #wfDebug( "Linksupdate:Cats:del: ".serialize($nt)." $key \n" );
170 // add any new category memberships
171 if (count($add) > 0) {
173 foreach( $add as $cname => $sortkey ) {
174 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
176 $nt->invalidateCache();
177 // update the timestamp which indicates when the last article
178 // was added or removed to/from this article
179 $key = $wgDBname.':Category:'.$nt->getDBkey().':adddeltimestamp';
180 $messageMemc->set( $key , wfTimestamp( TS_MW
), 24*3600 );
181 #wfDebug( "Linksupdate:Cats:add: ".serialize($nt)." $key\n" );
182 #wfDebug( "LU-get: ".$messageMemc->get( $key)."\n");
183 array_push( $arr, array(
184 'cl_from' => $this->mId
,
186 'cl_sortkey' => $sortkey ) );
188 // do the actual sql insertion
189 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
193 $this->fixBrokenLinks();
195 wfProfileOut( $fname );
199 * Link update which clears the previous entries and inserts new ones
200 * May be slower or faster depending on level of lock contention and write speed of DB
201 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
203 function doDumbUpdate() {
204 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
205 $fname = 'LinksUpdate::doDumbUpdate';
206 wfProfileIn( $fname );
209 $dbw =& wfGetDB( DB_MASTER
);
210 $links = $dbw->tableName( 'links' );
211 $brokenlinks = $dbw->tableName( 'brokenlinks' );
212 $imagelinks = $dbw->tableName( 'imagelinks' );
213 $categorylinks = $dbw->tableName( 'categorylinks' );
215 $dbw->delete('links', array('l_from'=>$this->mId
),$fname);
217 $a = $wgLinkCache->getGoodLinks();
218 if ( 0 != count( $a ) ) {
220 foreach( $a as $lt => $lid ) {
221 array_push( $arr, array(
222 'l_from' => $this->mId
,
225 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
228 $dbw->delete('brokenlinks', array('bl_from'=>$this->mId
),$fname);
230 $a = $wgLinkCache->getBadLinks();
231 if ( 0 != count ( $a ) ) {
233 foreach( $a as $blt ) {
234 array_push($arr,array(
235 'bl_from' => $this->mId
,
238 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
241 $dbw->delete('imagelinks', array('il_from'=>$this->mId
),$fname);
243 $a = $wgLinkCache->getImageLinks();
245 if ( 0 != count ( $a ) ) {
247 foreach( $a as $iname => $val )
248 array_push( $arr, array(
249 'il_from' => $this->mId
,
250 'il_to' => $iname ) );
251 $dbw->insert( 'imagelinks', $arr, $fname, array( 'IGNORE' ) );
254 if( $wgUseCategoryMagic ) {
255 $dbw->delete('categorylinks', array('cl_from'=>$this->mId
),$fname);
258 $add = $wgLinkCache->getCategoryLinks();
262 if ( 0 != count ( $add ) ) {
264 foreach( $add as $cname => $sortkey ) {
265 # FIXME: Change all this to avoid unnecessary duplication
266 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
268 $nt->invalidateCache();
269 array_push( $arr, array(
270 'cl_from' => $this->mId
,
272 'cl_sortkey' => $sortkey ) );
274 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
277 $this->fixBrokenLinks();
278 wfProfileOut( $fname );
282 * Update any brokenlinks *to* this page
283 * Call for a newly created page, or just to make sure state is consistent
285 function fixBrokenLinks() {
286 $fname = 'LinksUpdate::fixBrokenLinks';
288 $dbw =& wfGetDB( DB_MASTER
);
289 $page = $dbw->tableName( 'page' );
290 $links = $dbw->tableName( 'links' );
292 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle
),
293 $fname, 'FOR UPDATE' );
294 if ( 0 == $dbw->numRows( $res ) ) { return; }
298 while ( $row = $dbw->fetchObject( $res ) ) {
299 array_push( $arr, array(
300 'l_from' => $row->bl_from
,
301 'l_to' => $this->mId
) );
302 $toucharr[]=$row->bl_from
;
305 # Ignore errors. If a link existed in both the brokenlinks table and the links
306 # table, that's an error which can be fixed at this stage by simply ignoring collisions
307 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
308 $dbw->update( 'page', /* SET */ array( 'page_touched' => $dbw->timestamp() ),
309 /* WHERE */ array( 'page_id' => $toucharr ),$fname);
310 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle
), $fname );