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