fe39d36563f902df5b2986ab036584d9631d15cc
[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 $this->mTitleEnc = wfStrencode( $title );
13 }
14
15
16 function doUpdate()
17 {
18 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
19 global $wgEnablePersistentLC;
20
21 /* Update link tables with outgoing links from an updated article */
22 /* Relies on the 'link cache' to be filled out */
23
24 $fname = "LinksUpdate::doUpdate";
25 wfProfileIn( $fname );
26
27 $del = array();
28 $add = array();
29
30 if( $wgDBtransactions ) {
31 $sql = "BEGIN";
32 wfQuery( $sql, DB_WRITE, $fname );
33 }
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 wfQuery( $sql, DB_WRITE, $fname );
44 }
45 } else {
46 # Delete everything
47 $sql = "DELETE FROM links WHERE l_from={$this->mId}";
48 wfQuery( $sql, DB_WRITE, $fname );
49
50 # Get the addition list
51 $add = $wgLinkCache->getGoodLinks();
52 }
53
54 # Do the insertion
55 $sql = "";
56 if ( 0 != count( $add ) ) {
57 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
58 $first = true;
59 foreach( $add as $lt => $lid ) {
60
61 if ( ! $first ) { $sql .= ","; }
62 $first = false;
63
64 $sql .= "({$this->mId},{$lid})";
65 }
66 }
67 if ( "" != $sql ) {
68 wfQuery( $sql, DB_WRITE, $fname );
69 }
70
71 #------------------------------------------------------------------------------
72 # Bad links
73
74 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD, $del, $add ) ) {
75 # Delete where necessary
76 if ( count( $del ) ) {
77 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId} AND bl_to IN('" .
78 implode( "','", array_map( "wfStrencode", $del ) ) . "')";
79 wfQuery( $sql, DB_WRITE, $fname );
80 }
81 } else {
82 # Delete all
83 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
84 wfQuery( $sql, DB_WRITE, $fname );
85
86 # Get addition list
87 $add = $wgLinkCache->getBadLinks();
88 }
89
90 # Do additions
91 $sql = "";
92 if ( 0 != count ( $add ) ) {
93 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
94 $first = true;
95 foreach( $add as $blt ) {
96 $blt = wfStrencode( $blt );
97 if ( ! $first ) { $sql .= ","; }
98 $first = false;
99
100 $sql .= "({$this->mId},'{$blt}')";
101 }
102 }
103 if ( "" != $sql ) {
104 wfQuery( $sql, DB_WRITE, $fname );
105 }
106
107 #------------------------------------------------------------------------------
108 # Image links
109 $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mId}'";
110 wfQuery( $sql, DB_WRITE, $fname );
111
112 # Get addition list
113 $add = $wgLinkCache->getImageLinks();
114
115 # Do the insertion
116 $sql = "";
117 $image = Namespace::getImage();
118 if ( 0 != count ( $add ) ) {
119 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
120 $first = true;
121 foreach( $add as $iname => $val ) {
122 # FIXME: Change all this to avoid unnecessary duplication
123 $nt = Title::makeTitle( $image, $iname );
124 if( !$nt ) continue;
125 $nt->invalidateCache();
126
127 $iname = wfStrencode( $iname );
128 if ( ! $first ) { $sql .= ","; }
129 $first = false;
130
131 $sql .= "({$this->mId},'{$iname}')";
132 }
133 }
134 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
135
136 $this->fixBrokenLinks();
137
138 if( $wgDBtransactions ) {
139 $sql = "COMMIT";
140 wfQuery( $sql, DB_WRITE, $fname );
141 }
142 wfProfileOut( $fname );
143 }
144
145 function doDumbUpdate()
146 {
147 # Old inefficient update function
148 # Used for rebuilding the link table
149
150 global $wgLinkCache, $wgDBtransactions;
151 $fname = "LinksUpdate::doDumbUpdate";
152 wfProfileIn( $fname );
153
154 if( $wgDBtransactions ) {
155 $sql = "BEGIN";
156 wfQuery( $sql, DB_WRITE, $fname );
157 }
158
159 $sql = "DELETE FROM links WHERE l_from={$this->mId}";
160 wfQuery( $sql, DB_WRITE, $fname );
161
162 $a = $wgLinkCache->getGoodLinks();
163 $sql = "";
164 if ( 0 != count( $a ) ) {
165 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
166 $first = true;
167 foreach( $a as $lt => $lid ) {
168 if ( ! $first ) { $sql .= ","; }
169 $first = false;
170
171 $sql .= "({$this->mId},{$lid})";
172 }
173 }
174 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
175
176 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
177 wfQuery( $sql, DB_WRITE, $fname );
178
179 $a = $wgLinkCache->getBadLinks();
180 $sql = "";
181 if ( 0 != count ( $a ) ) {
182 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
183 $first = true;
184 foreach( $a as $blt ) {
185 $blt = wfStrencode( $blt );
186 if ( ! $first ) { $sql .= ","; }
187 $first = false;
188
189 $sql .= "({$this->mId},'{$blt}')";
190 }
191 }
192 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
193
194 $sql = "DELETE FROM imagelinks WHERE il_from={$this->mId}";
195 wfQuery( $sql, DB_WRITE, $fname );
196
197 $a = $wgLinkCache->getImageLinks();
198 $sql = "";
199 if ( 0 != count ( $a ) ) {
200 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
201 $first = true;
202 foreach( $a as $iname => $val ) {
203 $iname = wfStrencode( $iname );
204 if ( ! $first ) { $sql .= ","; }
205 $first = false;
206
207 $sql .= "({$this->mId},'{$iname}')";
208 }
209 }
210 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
211
212 $this->fixBrokenLinks();
213
214 if( $wgDBtransactions ) {
215 $sql = "COMMIT";
216 wfQuery( $sql, DB_WRITE, $fname );
217 }
218 wfProfileOut( $fname );
219 }
220
221 function fixBrokenLinks() {
222 /* Update any brokenlinks *to* this page */
223 /* Call for a newly created page, or just to make sure state is consistent */
224 $fname = "LinksUpdate::fixBrokenLinks";
225
226 $sql = "SELECT bl_from FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
227 $res = wfQuery( $sql, DB_READ, $fname );
228 if ( 0 == wfNumRows( $res ) ) { return; }
229
230 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
231 $now = wfTimestampNow();
232 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
233 $first = true;
234 while ( $row = wfFetchObject( $res ) ) {
235 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
236 $first = false;
237
238 $sql .= "({$row->bl_from},{$this->mId})";
239 $sql2 .= $row->bl_from;
240 }
241 $sql2 .= ")";
242 wfQuery( $sql, DB_WRITE, $fname );
243 wfQuery( $sql2, DB_WRITE, $fname );
244
245 $sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
246 wfQuery( $sql, DB_WRITE, $fname );
247 }
248
249 }
250
251 ?>