8106e5808971aadb2392c22704f4d1908a0b809d
[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( "','", array_map( "wfStrencode", $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 if( !$nt ) continue;
130 $nt->invalidateCache();
131
132 $iname = wfStrencode( $iname );
133 if ( ! $first ) { $sql .= ","; }
134 $first = false;
135
136 $sql .= "('{$this->mTitleEnc}','{$iname}')";
137 }
138 }
139 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
140
141 $this->fixBrokenLinks();
142
143 if( $wgDBtransactions ) {
144 $sql = "COMMIT";
145 wfQuery( $sql, DB_WRITE, $fname );
146 }
147 wfProfileOut( $fname );
148 }
149
150 function doDumbUpdate()
151 {
152 # Old update function. This can probably be removed eventually, if the new one
153 # proves to be stable
154 global $wgLinkCache, $wgDBtransactions;
155 $fname = "LinksUpdate::doDumbUpdate";
156 wfProfileIn( $fname );
157
158 if( $wgDBtransactions ) {
159 $sql = "BEGIN";
160 wfQuery( $sql, DB_WRITE, $fname );
161 }
162
163 $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}'";
164 wfQuery( $sql, DB_WRITE, $fname );
165
166 $a = $wgLinkCache->getGoodLinks();
167 $sql = "";
168 if ( 0 != count( $a ) ) {
169 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
170 $first = true;
171 foreach( $a as $lt => $lid ) {
172 if ( ! $first ) { $sql .= ","; }
173 $first = false;
174
175 $sql .= "('{$this->mTitleEnc}',{$lid})";
176 }
177 }
178 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
179
180 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
181 wfQuery( $sql, DB_WRITE, $fname );
182
183 $a = $wgLinkCache->getBadLinks();
184 $sql = "";
185 if ( 0 != count ( $a ) ) {
186 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
187 $first = true;
188 foreach( $a as $blt ) {
189 $blt = wfStrencode( $blt );
190 if ( ! $first ) { $sql .= ","; }
191 $first = false;
192
193 $sql .= "({$this->mId},'{$blt}')";
194 }
195 }
196 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
197
198 $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mTitleEnc}'";
199 wfQuery( $sql, DB_WRITE, $fname );
200
201 $a = $wgLinkCache->getImageLinks();
202 $sql = "";
203 if ( 0 != count ( $a ) ) {
204 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
205 $first = true;
206 foreach( $a as $iname => $val ) {
207 $iname = wfStrencode( $iname );
208 if ( ! $first ) { $sql .= ","; }
209 $first = false;
210
211 $sql .= "('{$this->mTitleEnc}','{$iname}')";
212 }
213 }
214 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
215
216 $this->fixBrokenLinks();
217
218 if( $wgDBtransactions ) {
219 $sql = "COMMIT";
220 wfQuery( $sql, DB_WRITE, $fname );
221 }
222 wfProfileOut( $fname );
223 }
224
225 function fixBrokenLinks() {
226 /* Update any brokenlinks *to* this page */
227 /* Call for a newly created page, or just to make sure state is consistent */
228
229 $sql = "SELECT bl_from FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
230 $res = wfQuery( $sql, DB_READ, $fname );
231 if ( 0 == wfNumRows( $res ) ) { return; }
232
233 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
234 $now = wfTimestampNow();
235 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
236 $first = true;
237 while ( $row = wfFetchObject( $res ) ) {
238 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
239 $first = false;
240 $nl = wfStrencode( Title::nameOf( $row->bl_from ) );
241
242 $sql .= "('{$nl}',{$this->mId})";
243 $sql2 .= $row->bl_from;
244 }
245 $sql2 .= ")";
246 wfQuery( $sql, DB_WRITE, $fname );
247 wfQuery( $sql2, DB_WRITE, $fname );
248
249 $sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
250 wfQuery( $sql, DB_WRITE, $fname );
251 }
252
253 }
254
255 ?>