Experimental code for caching page links
[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
16 function doUpdate()
17 {
18 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
19
20 /* Update link tables with outgoing links from an updated article */
21 /* Relies on the 'link cache' to be filled out */
22
23 // Make sure links cache is regenerated on next load
24 wfQuery("DELETE FROM linkscc WHERE lcc_title = '{$safeTitle}'", DB_WRITE);
25
26 if ( !$wgUseBetterLinksUpdate ) {
27 $this->doDumbUpdate();
28 return;
29 }
30
31 $fname = "LinksUpdate::doUpdate";
32 wfProfileIn( $fname );
33
34 $del = array();
35 $add = array();
36
37 if( $wgDBtransactions ) {
38 $sql = "BEGIN";
39 wfQuery( $sql, DB_WRITE, $fname );
40 }
41
42 #------------------------------------------------------------------------------
43 # Good links
44
45 if ( $wgLinkCache->incrementalSetup( LINKCACHE_GOOD, $del, $add ) ) {
46 # Delete where necessary
47 if ( count( $del ) ) {
48 $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}' AND l_to IN(".
49 implode( ",", $del ) . ")";
50 wfQuery( $sql, DB_WRITE, $fname );
51 }
52 } else {
53 # Delete everything
54 $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}'";
55 wfQuery( $sql, DB_WRITE, $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
68 if ( ! $first ) { $sql .= ","; }
69 $first = false;
70
71 $sql .= "('{$this->mTitleEnc}',{$lid})";
72 }
73 }
74 if ( "" != $sql ) {
75 wfQuery( $sql, DB_WRITE, $fname );
76 }
77
78 #------------------------------------------------------------------------------
79 # Bad links
80
81 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD, $del, $add ) ) {
82 # Delete where necessary
83 if ( count( $del ) ) {
84 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId} AND bl_to IN('" .
85 implode( "','", $del ) . "')";
86 wfQuery( $sql, DB_WRITE, $fname );
87 }
88 } else {
89 # Delete all
90 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
91 wfQuery( $sql, DB_WRITE, $fname );
92
93 # Get addition list
94 $add = $wgLinkCache->getBadLinks();
95 }
96
97 # Do additions
98 $sql = "";
99 if ( 0 != count ( $add ) ) {
100 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
101 $first = true;
102 foreach( $add as $blt ) {
103 $blt = wfStrencode( $blt );
104 if ( ! $first ) { $sql .= ","; }
105 $first = false;
106
107 $sql .= "({$this->mId},'{$blt}')";
108 }
109 }
110 if ( "" != $sql ) {
111 wfQuery( $sql, DB_WRITE, $fname );
112 }
113
114 #------------------------------------------------------------------------------
115 # Image links
116 $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mTitleEnc}'";
117 wfQuery( $sql, DB_WRITE, $fname );
118
119 # Get addition list
120 $add = $wgLinkCache->getImageLinks();
121
122 # Do the insertion
123 $sql = "";
124 if ( 0 != count ( $add ) ) {
125 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
126 $first = true;
127 foreach( $add as $iname => $val ) {
128 $iname = wfStrencode( $iname );
129 if ( ! $first ) { $sql .= ","; }
130 $first = false;
131
132 $sql .= "('{$this->mTitleEnc}','{$iname}')";
133 }
134 }
135 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
136
137 $this->fixBrokenLinks();
138
139 if( $wgDBtransactions ) {
140 $sql = "COMMIT";
141 wfQuery( $sql, DB_WRITE, $fname );
142 }
143 wfProfileOut( $fname );
144 }
145
146 function doDumbUpdate()
147 {
148 # Old update function. This can probably be removed eventually, if the new one
149 # proves to be stable
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->mTitleEnc}'";
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->mTitleEnc}',{$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->mTitleEnc}'";
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->mTitleEnc}','{$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
225 $sql = "SELECT bl_from FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
226 $res = wfQuery( $sql, DB_READ, $fname );
227 if ( 0 == wfNumRows( $res ) ) { return; }
228
229 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
230 $now = wfTimestampNow();
231 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
232 $first = true;
233 while ( $row = wfFetchObject( $res ) ) {
234 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
235 $first = false;
236 $nl = wfStrencode( Title::nameOf( $row->bl_from ) );
237
238 $sql .= "('{$nl}',{$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 ?>