Bug fixes
[mediawiki.git] / includes / LinksUpdate.php
blob8106e5808971aadb2392c22704f4d1908a0b809d
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( "','", array_map( "wfStrencode", $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 if( !$nt ) continue;
130 $nt->invalidateCache();
132 $iname = wfStrencode( $iname );
133 if ( ! $first ) { $sql .= ","; }
134 $first = false;
136 $sql .= "('{$this->mTitleEnc}','{$iname}')";
139 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
141 $this->fixBrokenLinks();
143 if( $wgDBtransactions ) {
144 $sql = "COMMIT";
145 wfQuery( $sql, DB_WRITE, $fname );
147 wfProfileOut( $fname );
150 function doDumbUpdate()
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 );
158 if( $wgDBtransactions ) {
159 $sql = "BEGIN";
160 wfQuery( $sql, DB_WRITE, $fname );
163 $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}'";
164 wfQuery( $sql, DB_WRITE, $fname );
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;
175 $sql .= "('{$this->mTitleEnc}',{$lid})";
178 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
180 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
181 wfQuery( $sql, DB_WRITE, $fname );
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;
193 $sql .= "({$this->mId},'{$blt}')";
196 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
198 $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mTitleEnc}'";
199 wfQuery( $sql, DB_WRITE, $fname );
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;
211 $sql .= "('{$this->mTitleEnc}','{$iname}')";
214 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
216 $this->fixBrokenLinks();
218 if( $wgDBtransactions ) {
219 $sql = "COMMIT";
220 wfQuery( $sql, DB_WRITE, $fname );
222 wfProfileOut( $fname );
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 */
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; }
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 ) );
242 $sql .= "('{$nl}',{$this->mId})";
243 $sql2 .= $row->bl_from;
245 $sql2 .= ")";
246 wfQuery( $sql, DB_WRITE, $fname );
247 wfQuery( $sql2, DB_WRITE, $fname );
249 $sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
250 wfQuery( $sql, DB_WRITE, $fname );