* Removing the if($wgMetaNamespace === FALSE) line, inherited from the parent.
[mediawiki.git] / includes / LinksUpdate.php
blobcbec2d87e48798e1d9abdcf07a08d9f58bae63c4
1 <?php
2 /**
3 * See deferred.doc
4 * @package MediaWiki
5 */
7 /**
8 * @todo document
9 * @package MediaWiki
11 class LinksUpdate {
13 /**#@+
14 * @access private
16 var $mId, $mTitle;
17 /**#@-*/
19 /**
20 * Constructor
21 * Initialize private variables
22 * @param integer $id
23 * @param string $title
25 function LinksUpdate( $id, $title ) {
26 $this->mId = $id;
27 $this->mTitle = $title;
30 /**
31 * Update link tables with outgoing links from an updated article
32 * Relies on the 'link cache' to be filled out.
35 function doUpdate() {
36 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
37 global $wgEnablePersistentLC, $wgUseCategoryMagic;
39 $fname = 'LinksUpdate::doUpdate';
40 wfProfileIn( $fname );
42 $del = array();
43 $add = array();
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 #------------------------------------------------------------------------------
52 # Good links
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 );
61 } else {
62 # Delete everything
63 $dbw->delete( 'links', array( 'l_from' => $this->mId ), $fname );
65 # Get the addition list
66 $add = $wgLinkCache->getGoodLinks();
69 # Do the insertion
70 if ( 0 != count( $add ) ) {
71 $arr=array();
72 foreach($add as $lt=>$lid)
73 array_push( $arr, array(
74 'l_from' => $this->mId,
75 'l_to' => $lid ) );
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 #------------------------------------------------------------------------------
83 # Bad links
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(";
89 $first = true;
90 foreach( $del as $badTitle ) {
91 if ( $first ) {
92 $first = false;
93 } else {
94 $sql .= ',';
96 $sql .= $dbw->addQuotes( $badTitle );
98 $sql .= ')';
99 $dbw->query( $sql, $fname );
101 } else {
102 # Delete all
103 $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId ) );
105 # Get addition list
106 $add = $wgLinkCache->getBadLinks();
109 # Do additions
110 $sql = '';
111 if ( 0 != count ( $add ) ) {
112 $arr = array();
113 foreach( $add as $blt ) {
114 array_push( $arr, array(
115 'bl_from' => $this->mId,
116 'bl_to' => $blt ) );
118 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
121 #------------------------------------------------------------------------------
122 # Image links
123 $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
124 $dbw->query( $sql, $fname );
126 # Get addition list
127 $add = $wgLinkCache->getImageLinks();
129 # Do the insertion
130 $sql = '';
131 $image = NS_IMAGE;
132 if ( 0 != count ( $add ) ) {
133 $arr = array();
134 foreach ($add as $iname => $val ) {
135 $nt = Title::makeTitle( $image, $iname );
136 if( !$nt ) continue;
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 #------------------------------------------------------------------------------
146 # Category links
147 if( $wgUseCategoryMagic ) {
148 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
149 $dbw->query( $sql, $fname );
151 # Get addition list
152 $add = $wgLinkCache->getCategoryLinks();
154 # Do the insertion
155 $sql = '';
156 if ( 0 != count ( $add ) ) {
157 $arr = array();
158 foreach( $add as $cname => $sortkey ) {
159 $nt = Title::makeTitle( NS_CATEGORY, $cname );
160 if( !$nt ) continue;
161 $nt->invalidateCache();
162 array_push( $arr, array(
163 'cl_from' => $this->mId,
164 'cl_to' => $cname,
165 'cl_sortkey' => $sortkey ) );
167 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
171 $this->fixBrokenLinks();
173 wfProfileOut( $fname );
177 * Link update which clears the previous entries and inserts new ones
178 * May be slower or faster depending on level of lock contention and write speed of DB
179 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
181 function doDumbUpdate() {
182 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
183 $fname = 'LinksUpdate::doDumbUpdate';
184 wfProfileIn( $fname );
187 $dbw =& wfGetDB( DB_MASTER );
188 $links = $dbw->tableName( 'links' );
189 $brokenlinks = $dbw->tableName( 'brokenlinks' );
190 $imagelinks = $dbw->tableName( 'imagelinks' );
191 $categorylinks = $dbw->tableName( 'categorylinks' );
193 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
194 $dbw->query( $sql, $fname );
196 $a = $wgLinkCache->getGoodLinks();
197 if ( 0 != count( $a ) ) {
198 $arr = array();
199 foreach( $a as $lt => $lid ) {
200 array_push( $arr, array(
201 'l_from' => $this->mId,
202 'l_to' => $lid ) );
204 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
207 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
208 $dbw->query( $sql, $fname );
210 $a = $wgLinkCache->getBadLinks();
211 if ( 0 != count ( $a ) ) {
212 $arr = array();
213 foreach( $a as $blt ) {
214 array_push($arr,array(
215 'bl_from' => $this->mId,
216 'bl_to' => $blt));
218 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
221 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
222 $dbw->query( $sql, $fname );
224 $a = $wgLinkCache->getImageLinks();
225 $sql = '';
226 if ( 0 != count ( $a ) ) {
227 $arr = array();
228 foreach( $a as $iname => $val )
229 array_push( $arr, array(
230 'il_from' => $this->mId,
231 'il_to' => $iname ) );
232 $dbw->insert( 'imagelinks', $arr, $fname, array( 'IGNORE' ) );
235 if( $wgUseCategoryMagic ) {
236 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
237 $dbw->query( $sql, $fname );
239 # Get addition list
240 $add = $wgLinkCache->getCategoryLinks();
242 # Do the insertion
243 $sql = '';
244 if ( 0 != count ( $add ) ) {
245 $arr = array();
246 foreach( $add as $cname => $sortkey ) {
247 # FIXME: Change all this to avoid unnecessary duplication
248 $nt = Title::makeTitle( NS_CATEGORY, $cname );
249 if( !$nt ) continue;
250 $nt->invalidateCache();
251 array_push( $arr, array(
252 'cl_from' => $this->mId,
253 'cl_to' => $cname,
254 'cl_sortkey' => $sortkey ) );
256 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
259 $this->fixBrokenLinks();
260 wfProfileOut( $fname );
264 * Update any brokenlinks *to* this page
265 * Call for a newly created page, or just to make sure state is consistent
267 function fixBrokenLinks() {
268 $fname = 'LinksUpdate::fixBrokenLinks';
270 $dbw =& wfGetDB( DB_MASTER );
271 $page = $dbw->tableName( 'page' );
272 $links = $dbw->tableName( 'links' );
274 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle ),
275 $fname, 'FOR UPDATE' );
276 if ( 0 == $dbw->numRows( $res ) ) { return; }
278 $arr=array();
279 $now = $dbw->timestamp();
280 $sql2 = "UPDATE $page SET page_touched='{$now}' WHERE page_id IN (";
281 $first = true;
282 while ( $row = $dbw->fetchObject( $res ) ) {
283 if ( ! $first ) { $sql2 .= ","; }
284 $first = false;
285 array_push( $arr, array(
286 'l_from' => $row->bl_from,
287 'l_to' => $this->mId ) );
288 $sql2 .= $row->bl_from;
290 $sql2 .= ')';
292 # Ignore errors. If a link existed in both the brokenlinks table and the links
293 # table, that's an error which can be fixed at this stage by simply ignoring collisions
294 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
295 $dbw->query( $sql2, $fname );
296 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle ), $fname );