* links and brokenlinks tables merged to pagelinks; this will reduce pain
[lhc/web/wiklou.git] / includes / LinksUpdate.php
1 <?php
2 /**
3 * See deferred.txt
4 * @package MediaWiki
5 */
6
7 /**
8 * @todo document
9 * @package MediaWiki
10 */
11 class LinksUpdate {
12
13 /**#@+
14 * @access private
15 */
16 var $mId, $mTitle;
17 /**#@-*/
18
19 /**
20 * Constructor
21 * Initialize private variables
22 * @param integer $id
23 * @param string $title
24 */
25 function LinksUpdate( $id, $title ) {
26 $this->mId = $id;
27 $this->mTitle = $title;
28 }
29
30 /**
31 * Update link tables with outgoing links from an updated article
32 * Relies on the 'link cache' to be filled out.
33 */
34
35 function doUpdate() {
36 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
37 global $wgEnablePersistentLC, $wgUseCategoryMagic;
38
39 $fname = 'LinksUpdate::doUpdate';
40 wfProfileIn( $fname );
41
42 $del = array();
43 $add = array();
44
45 $dbw =& wfGetDB( DB_MASTER );
46 $pagelinks = $dbw->tableName( 'pagelinks' );
47 $imagelinks = $dbw->tableName( 'imagelinks' );
48 $categorylinks = $dbw->tableName( 'categorylinks' );
49
50 #------------------------------------------------------------------------------
51 # Good links
52
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 );
60 }
61 } else {
62 # Delete everything
63 $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mId ), $fname );
64
65 # Get the addition list
66 $add = $wgLinkCache->getGoodLinks();
67 }
68
69 # Do the insertion
70 if( 0 != count( $add ) ) {
71 $arr = array();
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() ) );
77 }
78
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' ) );
83 }
84
85 #------------------------------------------------------------------------------
86 # Image links
87 $dbw->delete('imagelinks',array('il_from'=>$this->mId),$fname);
88
89 # Get addition list
90 $add = $wgLinkCache->getImageLinks();
91
92 # Do the insertion
93 $sql = '';
94 $image = NS_IMAGE;
95 if ( 0 != count ( $add ) ) {
96 $arr = array();
97 foreach ($add as $iname => $val ) {
98 $nt = Title::makeTitle( $image, $iname );
99 if( !$nt ) continue;
100 $nt->invalidateCache();
101 array_push( $arr, array(
102 'il_from' => $this->mId,
103 'il_to' => $iname ) );
104 }
105 $dbw->insert('imagelinks', $arr, $fname, array('IGNORE'));
106 }
107
108 #------------------------------------------------------------------------------
109 # Category links
110 if( $wgUseCategoryMagic ) {
111 global $messageMemc, $wgDBname;
112
113 # Get addition list
114 $add = $wgLinkCache->getCategoryLinks();
115
116 # select existing catlinks for this page
117 $res = $dbw->select( 'categorylinks',
118 array( 'cl_to', 'cl_sortkey' ),
119 array( 'cl_from' => $this->mId ),
120 $fname,
121 'FOR UPDATE' );
122
123 $del = array();
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;
130 } else {
131 // remove already existing category memberships
132 // from the add array
133 unset( $add[$row->cl_to] );
134 }
135 }
136 }
137
138 // delete any removed categorylinks
139 if( count( $del ) > 0) {
140 // delete old ones
141 $dbw->delete( 'categorylinks',
142 array(
143 'cl_from' => $this->mId,
144 'cl_to' => $del ),
145 $fname );
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 );
153 }
154 }
155
156 // add any new category memberships
157 if( count( $add ) > 0 ) {
158 $arr = array();
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,
168 'cl_to' => $cname,
169 'cl_sortkey' => $sortkey ) );
170 }
171 // do the actual sql insertion
172 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
173 }
174 }
175
176 wfProfileOut( $fname );
177 }
178
179 /**
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
183 */
184 function doDumbUpdate() {
185 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
186 $fname = 'LinksUpdate::doDumbUpdate';
187 wfProfileIn( $fname );
188
189
190 $dbw =& wfGetDB( DB_MASTER );
191 $pagelinks = $dbw->tableName( 'pagelinks' );
192 $imagelinks = $dbw->tableName( 'imagelinks' );
193 $categorylinks = $dbw->tableName( 'categorylinks' );
194
195 $dbw->delete('pagelinks', array('pl_from'=>$this->mId),$fname);
196
197 $a = $wgLinkCache->getPageLinks();
198 if ( 0 != count( $a ) ) {
199 $arr = array();
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() ) );
205 }
206 $dbw->insert( 'pagelinks', $arr, $fname, array( 'IGNORE' ) );
207 }
208
209 $dbw->delete('imagelinks', array('il_from'=>$this->mId),$fname);
210
211 $a = $wgLinkCache->getImageLinks();
212 $sql = '';
213 if ( 0 != count ( $a ) ) {
214 $arr = array();
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' ) );
220 }
221
222 if( $wgUseCategoryMagic ) {
223 $dbw->delete('categorylinks', array('cl_from'=>$this->mId),$fname);
224
225 # Get addition list
226 $add = $wgLinkCache->getCategoryLinks();
227
228 # Do the insertion
229 $sql = '';
230 if ( 0 != count ( $add ) ) {
231 $arr = array();
232 foreach( $add as $cname => $sortkey ) {
233 # FIXME: Change all this to avoid unnecessary duplication
234 $nt = Title::makeTitle( NS_CATEGORY, $cname );
235 if( !$nt ) continue;
236 $nt->invalidateCache();
237 array_push( $arr, array(
238 'cl_from' => $this->mId,
239 'cl_to' => $cname,
240 'cl_sortkey' => $sortkey ) );
241 }
242 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
243 }
244 }
245 wfProfileOut( $fname );
246 }
247 }
248 ?>