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