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