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