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 $pagelinks = $dbw->tableName( 'pagelinks' );
47 $imagelinks = $dbw->tableName( 'imagelinks' );
48 $categorylinks = $dbw->tableName( 'categorylinks' );
50 #------------------------------------------------------------------------------
53 if ( $wgLinkCache->incrementalSetup( LINKCACHE_PAGE
, $del, $add ) ) {
54 # Delete where necessary
55 if ( count( $del ) ) {
56 $batch = new LinkBatch( $del );
57 $set = $batch->constructSet( 'pl', $dbw );
58 $sql = "DELETE FROM $pagelinks WHERE pl_from={$this->mId} AND ($set)";
59 $dbw->query( $sql, $fname );
63 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mId
), $fname );
65 # Get the addition list
66 $add = $wgLinkCache->getGoodLinks();
70 if( 0 != count( $add ) ) {
72 foreach( $add as $lt => $target ) {
73 array_push( $arr, array(
74 'pl_from' => $this->mId
,
75 'pl_namespace' => $target->getNamespace(),
76 'pl_title' => $target->getDbKey() ) );
79 # The link cache was constructed without FOR UPDATE, so there may be collisions
80 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
81 # sure it's better than without IGNORE
82 $dbw->insert( 'pagelinks', $arr, $fname, array( 'IGNORE' ) );
85 #------------------------------------------------------------------------------
87 $dbw->delete('imagelinks',array('il_from'=>$this->mId
),$fname);
90 $add = $wgLinkCache->getImageLinks();
95 if ( 0 != count ( $add ) ) {
97 foreach ($add as $iname => $val ) {
98 $nt = Title
::makeTitle( $image, $iname );
100 $nt->invalidateCache();
101 array_push( $arr, array(
102 'il_from' => $this->mId
,
103 'il_to' => $iname ) );
105 $dbw->insert('imagelinks', $arr, $fname, array('IGNORE'));
108 #------------------------------------------------------------------------------
110 if( $wgUseCategoryMagic ) {
111 global $messageMemc, $wgDBname;
114 $add = $wgLinkCache->getCategoryLinks();
116 # select existing catlinks for this page
117 $res = $dbw->select( 'categorylinks',
118 array( 'cl_to', 'cl_sortkey' ),
119 array( 'cl_from' => $this->mId
),
124 if( 0 != $dbw->numRows( $res ) ) {
125 while( $row = $dbw->fetchObject( $res ) ) {
126 if( !isset( $add[$row->cl_to
] ) ||
$add[$row->cl_to
] != $row->cl_sortkey
) {
127 // in the db, but no longer in the page
128 // or sortkey has changed -> delete
129 $del[] = $row->cl_to
;
131 // remove already existing category memberships
132 // from the add array
133 unset( $add[$row->cl_to
] );
138 // delete any removed categorylinks
139 if( count( $del ) > 0) {
141 $dbw->delete( 'categorylinks',
143 'cl_from' => $this->mId
,
146 foreach( $del as $cname ){
147 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
148 $nt->invalidateCache();
149 // update the timestamp which indicates when the last article
150 // was added or removed to/from this article
151 $key = $wgDBname . ':Category:' . md5( $nt->getDBkey() ) . ':adddeltimestamp';
152 $messageMemc->set( $key , wfTimestamp( TS_MW
), 24*3600 );
156 // add any new category memberships
157 if( count( $add ) > 0 ) {
159 foreach( $add as $cname => $sortkey ) {
160 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
161 $nt->invalidateCache();
162 // update the timestamp which indicates when the last article
163 // was added or removed to/from this article
164 $key = $wgDBname . ':Category:' . md5( $nt->getDBkey() ) . ':adddeltimestamp';
165 $messageMemc->set( $key , wfTimestamp( TS_MW
), 24*3600 );
166 array_push( $arr, array(
167 'cl_from' => $this->mId
,
169 'cl_sortkey' => $sortkey ) );
171 // do the actual sql insertion
172 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
176 wfProfileOut( $fname );
180 * Link update which clears the previous entries and inserts new ones
181 * May be slower or faster depending on level of lock contention and write speed of DB
182 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
184 function doDumbUpdate() {
185 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
186 $fname = 'LinksUpdate::doDumbUpdate';
187 wfProfileIn( $fname );
190 $dbw =& wfGetDB( DB_MASTER
);
191 $pagelinks = $dbw->tableName( 'pagelinks' );
192 $imagelinks = $dbw->tableName( 'imagelinks' );
193 $categorylinks = $dbw->tableName( 'categorylinks' );
195 $dbw->delete('pagelinks', array('pl_from'=>$this->mId
),$fname);
197 $a = $wgLinkCache->getPageLinks();
198 if ( 0 != count( $a ) ) {
200 foreach( $a as $lt => $target ) {
201 array_push( $arr, array(
202 'pl_from' => $this->mId
,
203 'pl_namespace' => $target->getNamespace(),
204 'pl_title' => $target->getTitle() ) );
206 $dbw->insert( 'pagelinks', $arr, $fname, array( 'IGNORE' ) );
209 $dbw->delete('imagelinks', array('il_from'=>$this->mId
),$fname);
211 $a = $wgLinkCache->getImageLinks();
213 if ( 0 != count ( $a ) ) {
215 foreach( $a as $iname => $val )
216 array_push( $arr, array(
217 'il_from' => $this->mId
,
218 'il_to' => $iname ) );
219 $dbw->insert( 'imagelinks', $arr, $fname, array( 'IGNORE' ) );
222 if( $wgUseCategoryMagic ) {
223 $dbw->delete('categorylinks', array('cl_from'=>$this->mId
),$fname);
226 $add = $wgLinkCache->getCategoryLinks();
230 if ( 0 != count ( $add ) ) {
232 foreach( $add as $cname => $sortkey ) {
233 # FIXME: Change all this to avoid unnecessary duplication
234 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
236 $nt->invalidateCache();
237 array_push( $arr, array(
238 'cl_from' => $this->mId
,
240 'cl_sortkey' => $sortkey ) );
242 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
245 wfProfileOut( $fname );