Better translation for performance.
[mediawiki.git] / includes / LinksUpdate.php
blobde4b86182e0a879748757434f6af4186425e73f5
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 );
15 function doUpdate()
17 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
19 /* Update link tables with outgoing links from an updated article */
20 /* Relies on the 'link cache' to be filled out */
22 if ( !$wgUseBetterLinksUpdate ) {
23 $this->doDumbUpdate();
24 return;
27 $fname = "LinksUpdate::doUpdate";
28 wfProfileIn( $fname );
30 $del = array();
31 $add = array();
33 if( $wgDBtransactions ) {
34 $sql = "BEGIN";
35 wfQuery( $sql, DB_WRITE, $fname );
38 #------------------------------------------------------------------------------
39 # Good links
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, DB_WRITE, $fname );
48 } else {
49 # Delete everything
50 $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}'";
51 wfQuery( $sql, DB_WRITE, $fname );
53 # Get the addition list
54 $add = $wgLinkCache->getGoodLinks();
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 ) {
64 if ( ! $first ) { $sql .= ","; }
65 $first = false;
67 $sql .= "('{$this->mTitleEnc}',{$lid})";
70 if ( "" != $sql ) {
71 wfQuery( $sql, DB_WRITE, $fname );
74 #------------------------------------------------------------------------------
75 # Bad links
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, DB_WRITE, $fname );
84 } else {
85 # Delete all
86 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
87 wfQuery( $sql, DB_WRITE, $fname );
89 # Get addition list
90 $add = $wgLinkCache->getBadLinks();
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;
103 $sql .= "({$this->mId},'{$blt}')";
106 if ( "" != $sql ) {
107 wfQuery( $sql, DB_WRITE, $fname );
110 #------------------------------------------------------------------------------
111 # Image links
112 $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mTitleEnc}'";
113 wfQuery( $sql, DB_WRITE, $fname );
115 # Get addition list
116 $add = $wgLinkCache->getImageLinks();
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;
128 $sql .= "('{$this->mTitleEnc}','{$iname}')";
131 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
133 $this->fixBrokenLinks();
135 if( $wgDBtransactions ) {
136 $sql = "COMMIT";
137 wfQuery( $sql, DB_WRITE, $fname );
139 wfProfileOut();
142 function doDumbUpdate()
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 );
150 if( $wgDBtransactions ) {
151 $sql = "BEGIN";
152 wfQuery( $sql, DB_WRITE, $fname );
155 $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}'";
156 wfQuery( $sql, DB_WRITE, $fname );
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;
167 $sql .= "('{$this->mTitleEnc}',{$lid})";
170 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
172 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
173 wfQuery( $sql, DB_WRITE, $fname );
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;
185 $sql .= "({$this->mId},'{$blt}')";
188 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
190 $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mTitleEnc}'";
191 wfQuery( $sql, DB_WRITE, $fname );
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;
203 $sql .= "('{$this->mTitleEnc}','{$iname}')";
206 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
208 $this->fixBrokenLinks();
210 if( $wgDBtransactions ) {
211 $sql = "COMMIT";
212 wfQuery( $sql, DB_WRITE, $fname );
214 wfProfileOut();
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 */
221 $sql = "SELECT bl_from FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
222 $res = wfQuery( $sql, DB_READ, $fname );
223 if ( 0 == wfNumRows( $res ) ) { return; }
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( Title::nameOf( $row->bl_from ) );
234 $sql .= "('{$nl}',{$this->mId})";
235 $sql2 .= $row->bl_from;
237 $sql2 .= ")";
238 wfQuery( $sql, DB_WRITE, $fname );
239 wfQuery( $sql2, DB_WRITE, $fname );
241 $sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
242 wfQuery( $sql, DB_WRITE, $fname );