5b9cf54629922356773012919656d0cee808e8f0
[lhc/web/wiklou.git] / includes / LinksUpdate.php
1 <?php
2 # See deferred.doc
3
4 class LinksUpdate {
5
6 /* private */ var $mId, $mTitle;
7
8 function LinksUpdate( $id, $title )
9 {
10 $this->mId = $id;
11 $this->mTitle = $title;
12 }
13
14
15 function doUpdate()
16 {
17 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
18 global $wgEnablePersistentLC, $wgUseCategoryMagic;
19
20 /* Update link tables with outgoing links from an updated article */
21 /* Relies on the 'link cache' to be filled out */
22
23 $fname = 'LinksUpdate::doUpdate';
24 wfProfileIn( $fname );
25
26 $del = array();
27 $add = array();
28
29 $dbw =& wfGetDB( DB_MASTER );
30 $links = $dbw->tableName( 'links' );
31 $brokenlinks = $dbw->tableName( 'brokenlinks' );
32 $imagelinks = $dbw->tableName( 'imagelinks' );
33 $categorylinks = $dbw->tableName( 'categorylinks' );
34
35 #------------------------------------------------------------------------------
36 # Good links
37
38 if ( $wgLinkCache->incrementalSetup( LINKCACHE_GOOD, $del, $add ) ) {
39 # Delete where necessary
40 if ( count( $del ) ) {
41 $sql = "DELETE FROM $links WHERE l_from={$this->mId} AND l_to IN(".
42 implode( ',', $del ) . ')';
43 $dbw->query( $sql, $fname );
44 }
45 } else {
46 # Delete everything
47 $dbw->delete( 'links', array( 'l_from' => $this->mId ), $fname );
48
49 # Get the addition list
50 $add = $wgLinkCache->getGoodLinks();
51 }
52
53 # Do the insertion
54 if ( 0 != count( $add ) ) {
55 $arr=array();
56 foreach($add as $lt=>$lid)
57 array_push($arr,array(
58 'l_from'=>$this->mId,
59 'l_to'=>$lid));
60 # The link cache was constructed without FOR UPDATE, so there may be collisions
61 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
62 # sure it's better than without IGNORE
63 $dbw->insertArray('links', $arr, $fname, array('IGNORE'));
64 }
65
66 #------------------------------------------------------------------------------
67 # Bad links
68
69 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD, $del, $add ) ) {
70 # Delete where necessary
71 if ( count( $del ) ) {
72 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId} AND bl_to IN(";
73 $first = true;
74 foreach( $del as $badTitle ) {
75 if ( $first ) {
76 $first = false;
77 } else {
78 $sql .= ',';
79 }
80 $sql .= $dbw->addQuotes( $badTitle );
81 }
82 $sql .= ')';
83 $dbw->query( $sql, $fname );
84 }
85 } else {
86 # Delete all
87 $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId ) );
88
89 # Get addition list
90 $add = $wgLinkCache->getBadLinks();
91 }
92
93 # Do additions
94 $sql = '';
95 if ( 0 != count ( $add ) ) {
96 $arr=array();
97 foreach( $add as $blt ) {
98 $blt = $dbw->strencode( $blt );
99 array_push($arr,array(
100 'bl_from'=>$this->mId,
101 'bl_to'=>$blt));
102 }
103 $dbw->insertArray( 'brokenlinks',$arr,$fname,array('IGNORE'));
104 }
105
106 #------------------------------------------------------------------------------
107 # Image links
108 $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
109 $dbw->query( $sql, $fname );
110
111 # Get addition list
112 $add = $wgLinkCache->getImageLinks();
113
114 # Do the insertion
115 $sql = '';
116 $image = Namespace::getImage();
117 if ( 0 != count ( $add ) ) {
118 $arr = array();
119 foreach ($add as $iname => $val ) {
120 $nt = Title::makeTitle( $image, $iname );
121 if( !$nt ) continue;
122 $nt->invalidateCache();
123 $iname = $dbw->strencode( $iname );
124 array_push($arr,array(
125 'il_from'=>$this->mId,
126 'il_to'=>$iname));
127 }
128 $dbw->insertArray($imagelinks, $arr, $fname, array('IGNORE'));
129 }
130
131 #------------------------------------------------------------------------------
132 # Category links
133 if( $wgUseCategoryMagic ) {
134 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
135 $dbw->query( $sql, $fname );
136
137 # Get addition list
138 $add = $wgLinkCache->getCategoryLinks();
139
140 # Do the insertion
141 $sql = '';
142 if ( 0 != count ( $add ) ) {
143 $arr=array();
144 foreach( $add as $cname => $sortkey ) {
145 $nt = Title::makeTitle( NS_CATEGORY, $cname );
146 if( !$nt ) continue;
147 $nt->invalidateCache();
148 array_push($arr,array(
149 'cl_from'=>$this->mId,
150 'cl_to'=>$dbw->strencode( $cname ),
151 'cl_sortkey'=>$dbw->strencode( $sortkey )));
152 }
153 $dbw->insertArray($categorylinks,$arr,$fname,array('IGNORE'));
154 }
155 }
156
157 $this->fixBrokenLinks();
158
159 wfProfileOut( $fname );
160 }
161
162 function doDumbUpdate()
163 {
164 # Old inefficient update function
165 # Used for rebuilding the link table
166 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
167 $fname = 'LinksUpdate::doDumbUpdate';
168 wfProfileIn( $fname );
169
170
171 $dbw =& wfGetDB( DB_MASTER );
172 $links = $dbw->tableName( 'links' );
173 $brokenlinks = $dbw->tableName( 'brokenlinks' );
174 $imagelinks = $dbw->tableName( 'imagelinks' );
175 $categorylinks = $dbw->tableName( 'categorylinks' );
176
177 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
178 $dbw->query( $sql, $fname );
179
180 $a = $wgLinkCache->getGoodLinks();
181 if ( 0 != count( $a ) ) {
182 $arr=array();
183 foreach( $a as $lt => $lid ) {
184 array_push($arr,array(
185 'l_from'=>$this->mId,
186 'l_to'=>$lid));
187 }
188 $dbw->insertArray($links,$arr,$fname,array('IGNORE'));
189 }
190
191 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
192 $dbw->query( $sql, $fname );
193
194 $a = $wgLinkCache->getBadLinks();
195 if ( 0 != count ( $a ) ) {
196 $arr=array();
197 foreach( $a as $blt ) {
198 $blt = $dbw->strencode( $blt );
199 array_push($arr,array(
200 'bl_from'=>$this->mId,
201 'bl_to'=>$blt));
202 }
203 $dbw->insertArray($brokenlinks,$arr,$fname,array('IGNORE'));
204 }
205
206 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
207 $dbw->query( $sql, $fname );
208
209 $a = $wgLinkCache->getImageLinks();
210 $sql = '';
211 if ( 0 != count ( $a ) ) {
212 $arr=array();
213 foreach( $a as $iname => $val )
214 array_push($arr,array(
215 'il_from'=>$this->mId,
216 'il_to'=>$dbw->strencode( $iname )));
217 $dbw->insertArray($imagelinks,$arr,$fname,array('IGNORE'));
218 }
219
220 if( $wgUseCategoryMagic ) {
221 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
222 $dbw->query( $sql, $fname );
223
224 # Get addition list
225 $add = $wgLinkCache->getCategoryLinks();
226
227 # Do the insertion
228 $sql = '';
229 if ( 0 != count ( $add ) ) {
230 $arr=array();
231 foreach( $add as $cname => $sortkey ) {
232 # FIXME: Change all this to avoid unnecessary duplication
233 $nt = Title::makeTitle( NS_CATEGORY, $cname );
234 if( !$nt ) continue;
235 $nt->invalidateCache();
236 array_push($arr,array(
237 'cl_from'=>$this->mId,
238 'cl_to'=>$dbw->strencode( $cname ),
239 'cl_sortkey'=>$dbw->strencode( $sortkey )));
240 }
241 $dbw->insertArray($categorylinks,$arr,$fname,array('IGNORE'));
242 }
243 }
244 $this->fixBrokenLinks();
245 wfProfileOut( $fname );
246 }
247
248 function fixBrokenLinks() {
249 /* Update any brokenlinks *to* this page */
250 /* Call for a newly created page, or just to make sure state is consistent */
251 $fname = 'LinksUpdate::fixBrokenLinks';
252
253 $dbw =& wfGetDB( DB_MASTER );
254 $cur = $dbw->tableName( 'cur' );
255 $links = $dbw->tableName( 'links' );
256
257 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle ),
258 $fname, 'FOR UPDATE' );
259 if ( 0 == $dbw->numRows( $res ) ) { return; }
260
261 # Ignore errors. If a link existed in both the brokenlinks table and the links
262 # table, that's an error which can be fixed at this stage by simply ignoring collisions
263 $arr=array();
264 $now = wfTimestampNow();
265 $sql2 = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_id IN (";
266 $first = true;
267 while ( $row = $dbw->fetchObject( $res ) ) {
268 if ( ! $first ) { $sql2 .= ","; }
269 $first = false;
270 array_push($arr,array('l_from'=>$row->bl_from,'l_to'=>$this->mId));
271 $sql2 .= $row->bl_from;
272 }
273 $sql2 .= ')';
274 $dbw->insertArray($links,$arr,$fname,array('IGNORE'));
275 $dbw->query( $sql2, $fname );
276 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle ), $fname );
277 }
278 }
279
280 ?>