fix phpdoc comment
[lhc/web/wiklou.git] / includes / LinksUpdate.php
1 <?php
2 /**
3 * See deferred.doc
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 $links = $dbw->tableName( 'links' );
47 $brokenlinks = $dbw->tableName( 'brokenlinks' );
48 $imagelinks = $dbw->tableName( 'imagelinks' );
49 $categorylinks = $dbw->tableName( 'categorylinks' );
50
51 #------------------------------------------------------------------------------
52 # Good links
53
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 );
60 }
61 } else {
62 # Delete everything
63 $dbw->delete( 'links', array( 'l_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=>$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' ) );
80 }
81
82 #------------------------------------------------------------------------------
83 # Bad links
84
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 .= ',';
95 }
96 $sql .= $dbw->addQuotes( $badTitle );
97 }
98 $sql .= ')';
99 $dbw->query( $sql, $fname );
100 }
101 } else {
102 # Delete all
103 $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId ) );
104
105 # Get addition list
106 $add = $wgLinkCache->getBadLinks();
107 }
108
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 ) );
117 }
118 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
119 }
120
121 #------------------------------------------------------------------------------
122 # Image links
123 $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
124 $dbw->query( $sql, $fname );
125
126 # Get addition list
127 $add = $wgLinkCache->getImageLinks();
128
129 # Do the insertion
130 $sql = '';
131 $image = Namespace::getImage();
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 ) );
141 }
142 $dbw->insert('imagelinks', $arr, $fname, array('IGNORE'));
143 }
144
145 #------------------------------------------------------------------------------
146 # Category links
147 if( $wgUseCategoryMagic ) {
148 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
149 $dbw->query( $sql, $fname );
150
151 # Get addition list
152 $add = $wgLinkCache->getCategoryLinks();
153
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 ) );
166 }
167 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
168 }
169 }
170
171 $this->fixBrokenLinks();
172
173 wfProfileOut( $fname );
174 }
175
176 /**
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
180 * @deprecated
181 */
182 function doDumbUpdate() {
183 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
184 $fname = 'LinksUpdate::doDumbUpdate';
185 wfProfileIn( $fname );
186
187
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' );
193
194 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
195 $dbw->query( $sql, $fname );
196
197 $a = $wgLinkCache->getGoodLinks();
198 if ( 0 != count( $a ) ) {
199 $arr = array();
200 foreach( $a as $lt => $lid ) {
201 array_push( $arr, array(
202 'l_from' => $this->mId,
203 'l_to' => $lid ) );
204 }
205 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
206 }
207
208 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
209 $dbw->query( $sql, $fname );
210
211 $a = $wgLinkCache->getBadLinks();
212 if ( 0 != count ( $a ) ) {
213 $arr = array();
214 foreach( $a as $blt ) {
215 array_push($arr,array(
216 'bl_from' => $this->mId,
217 'bl_to' => $blt));
218 }
219 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
220 }
221
222 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
223 $dbw->query( $sql, $fname );
224
225 $a = $wgLinkCache->getImageLinks();
226 $sql = '';
227 if ( 0 != count ( $a ) ) {
228 $arr = array();
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' ) );
234 }
235
236 if( $wgUseCategoryMagic ) {
237 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
238 $dbw->query( $sql, $fname );
239
240 # Get addition list
241 $add = $wgLinkCache->getCategoryLinks();
242
243 # Do the insertion
244 $sql = '';
245 if ( 0 != count ( $add ) ) {
246 $arr = array();
247 foreach( $add as $cname => $sortkey ) {
248 # FIXME: Change all this to avoid unnecessary duplication
249 $nt = Title::makeTitle( NS_CATEGORY, $cname );
250 if( !$nt ) continue;
251 $nt->invalidateCache();
252 array_push( $arr, array(
253 'cl_from' => $this->mId,
254 'cl_to' => $cname,
255 'cl_sortkey' => $sortkey ) );
256 }
257 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
258 }
259 }
260 $this->fixBrokenLinks();
261 wfProfileOut( $fname );
262 }
263
264 /**
265 * Update any brokenlinks *to* this page
266 * Call for a newly created page, or just to make sure state is consistent
267 */
268 function fixBrokenLinks() {
269 $fname = 'LinksUpdate::fixBrokenLinks';
270
271 $dbw =& wfGetDB( DB_MASTER );
272 $page = $dbw->tableName( 'page' );
273 $links = $dbw->tableName( 'links' );
274
275 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle ),
276 $fname, 'FOR UPDATE' );
277 if ( 0 == $dbw->numRows( $res ) ) { return; }
278
279 $arr=array();
280 $now = $dbw->timestamp();
281 $sql2 = "UPDATE $page SET page_touched='{$now}' WHERE page_id IN (";
282 $first = true;
283 while ( $row = $dbw->fetchObject( $res ) ) {
284 if ( ! $first ) { $sql2 .= ","; }
285 $first = false;
286 array_push( $arr, array(
287 'l_from' => $row->bl_from,
288 'l_to' => $this->mId ) );
289 $sql2 .= $row->bl_from;
290 }
291 $sql2 .= ')';
292
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 );
298 }
299 }
300 ?>