Clear imagelinks (upport from stable)
[mediawiki.git] / includes / LinksUpdate.php
blob547aa515b62cc87db81f567f1e1acac718390842
1 <?
2 # See deferred.doc
4 class LinksUpdate {
6 /* private */ var $mId, $mTitle;
8 function LinksUpdate( $id, $title )
10 $this->mId = $id;
11 $this->mTitle = $title;
12 $this->mTitleEnc = wfStrencode( $title );
16 function doUpdate()
18 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
19 global $wgEnablePersistentLC;
21 /* Update link tables with outgoing links from an updated article */
22 /* Relies on the 'link cache' to be filled out */
24 if ( !$wgUseBetterLinksUpdate ) {
25 $this->doDumbUpdate();
26 return;
29 $fname = "LinksUpdate::doUpdate";
30 wfProfileIn( $fname );
32 $del = array();
33 $add = array();
35 if( $wgDBtransactions ) {
36 $sql = "BEGIN";
37 wfQuery( $sql, DB_WRITE, $fname );
40 #------------------------------------------------------------------------------
41 # Good links
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 );
50 } else {
51 # Delete everything
52 $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}'";
53 wfQuery( $sql, DB_WRITE, $fname );
55 # Get the addition list
56 $add = $wgLinkCache->getGoodLinks();
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 ) {
66 if ( ! $first ) { $sql .= ","; }
67 $first = false;
69 $sql .= "('{$this->mTitleEnc}',{$lid})";
72 if ( "" != $sql ) {
73 wfQuery( $sql, DB_WRITE, $fname );
76 #------------------------------------------------------------------------------
77 # Bad links
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 );
86 } else {
87 # Delete all
88 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
89 wfQuery( $sql, DB_WRITE, $fname );
91 # Get addition list
92 $add = $wgLinkCache->getBadLinks();
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;
105 $sql .= "({$this->mId},'{$blt}')";
108 if ( "" != $sql ) {
109 wfQuery( $sql, DB_WRITE, $fname );
112 #------------------------------------------------------------------------------
113 # Image links
114 $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mTitleEnc}'";
115 wfQuery( $sql, DB_WRITE, $fname );
117 # Get addition list
118 $add = $wgLinkCache->getImageLinks();
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();
131 $iname = wfStrencode( $iname );
132 if ( ! $first ) { $sql .= ","; }
133 $first = false;
135 $sql .= "('{$this->mTitleEnc}','{$iname}')";
138 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
140 $this->fixBrokenLinks();
142 if( $wgDBtransactions ) {
143 $sql = "COMMIT";
144 wfQuery( $sql, DB_WRITE, $fname );
146 wfProfileOut( $fname );
149 function doDumbUpdate()
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 );
157 if( $wgDBtransactions ) {
158 $sql = "BEGIN";
159 wfQuery( $sql, DB_WRITE, $fname );
162 $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}'";
163 wfQuery( $sql, DB_WRITE, $fname );
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;
174 $sql .= "('{$this->mTitleEnc}',{$lid})";
177 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
179 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
180 wfQuery( $sql, DB_WRITE, $fname );
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;
192 $sql .= "({$this->mId},'{$blt}')";
195 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
197 $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mTitleEnc}'";
198 wfQuery( $sql, DB_WRITE, $fname );
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;
210 $sql .= "('{$this->mTitleEnc}','{$iname}')";
213 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
215 $this->fixBrokenLinks();
217 if( $wgDBtransactions ) {
218 $sql = "COMMIT";
219 wfQuery( $sql, DB_WRITE, $fname );
221 wfProfileOut( $fname );
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 */
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; }
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 ) );
241 $sql .= "('{$nl}',{$this->mId})";
242 $sql2 .= $row->bl_from;
244 $sql2 .= ")";
245 wfQuery( $sql, DB_WRITE, $fname );
246 wfQuery( $sql2, DB_WRITE, $fname );
248 $sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
249 wfQuery( $sql, DB_WRITE, $fname );