Merge "Added some extra tests for ORMRow class"
[lhc/web/wiklou.git] / maintenance / refreshLinks.php
1 <?php
2 /**
3 * Refresh link tables.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @ingroup Maintenance
21 */
22
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
24
25 class RefreshLinks extends Maintenance {
26 public function __construct() {
27 parent::__construct();
28 $this->mDescription = "Refresh link tables";
29 $this->addOption( 'dfn-only', 'Delete links from nonexistent articles only' );
30 $this->addOption( 'new-only', 'Only affect articles with just a single edit' );
31 $this->addOption( 'redirects-only', 'Only fix redirects, not all links' );
32 $this->addOption( 'old-redirects-only', 'Only fix redirects with no redirect table entry' );
33 $this->addOption( 'm', 'Maximum replication lag', false, true );
34 $this->addOption( 'e', 'Last page id to refresh', false, true );
35 $this->addArg( 'start', 'Page_id to start from, default 1', false );
36 $this->setBatchSize( 100 );
37 }
38
39 public function execute() {
40 $max = $this->getOption( 'm', 0 );
41 if ( !$this->hasOption( 'dfn-only' ) ) {
42 $start = $this->getArg( 0, 1 );
43 $new = $this->getOption( 'new-only', false );
44 $end = $this->getOption( 'e', 0 );
45 $redir = $this->getOption( 'redirects-only', false );
46 $oldRedir = $this->getOption( 'old-redirects-only', false );
47 $this->doRefreshLinks( $start, $new, $max, $end, $redir, $oldRedir );
48 }
49 $this->deleteLinksFromNonexistent( $max, $this->mBatchSize );
50 }
51
52 /**
53 * Do the actual link refreshing.
54 * @param $start int Page_id to start from
55 * @param $newOnly bool Only do pages with 1 edit
56 * @param $maxLag int Max DB replication lag
57 * @param $end int Page_id to stop at
58 * @param $redirectsOnly bool Only fix redirects
59 * @param $oldRedirectsOnly bool Only fix redirects without redirect entries
60 */
61 private function doRefreshLinks( $start, $newOnly = false, $maxLag = false,
62 $end = 0, $redirectsOnly = false, $oldRedirectsOnly = false ) {
63 global $wgParser, $wgUseTidy;
64
65 $reportingInterval = 100;
66 $dbr = wfGetDB( DB_SLAVE );
67 $start = intval( $start );
68
69 // Give extensions a chance to optimize settings
70 wfRunHooks( 'MaintenanceRefreshLinksInit', array( $this ) );
71
72 # Don't generate extension images (e.g. Timeline)
73 $wgParser->clearTagHooks();
74
75 # Don't use HTML tidy
76 $wgUseTidy = false;
77
78 $what = $redirectsOnly ? "redirects" : "links";
79
80 if ( $oldRedirectsOnly ) {
81 # This entire code path is cut-and-pasted from below. Hurrah.
82
83 $conds = array(
84 "page_is_redirect=1",
85 "rd_from IS NULL"
86 );
87
88 if ( $end == 0 ) {
89 $conds[] = "page_id >= $start";
90 } else {
91 $conds[] = "page_id BETWEEN $start AND $end";
92 }
93
94 $res = $dbr->select(
95 array( 'page', 'redirect' ),
96 'page_id',
97 $conds,
98 __METHOD__,
99 array(),
100 array( 'redirect' => array( "LEFT JOIN", "page_id=rd_from" ) )
101 );
102 $num = $dbr->numRows( $res );
103 $this->output( "Refreshing $num old redirects from $start...\n" );
104
105 $i = 0;
106
107 foreach ( $res as $row ) {
108 if ( !( ++$i % $reportingInterval ) ) {
109 $this->output( "$i\n" );
110 wfWaitForSlaves();
111 }
112 $this->fixRedirect( $row->page_id );
113 }
114 } elseif ( $newOnly ) {
115 $this->output( "Refreshing $what from " );
116 $res = $dbr->select( 'page',
117 array( 'page_id' ),
118 array(
119 'page_is_new' => 1,
120 "page_id >= $start" ),
121 __METHOD__
122 );
123 $num = $dbr->numRows( $res );
124 $this->output( "$num new articles...\n" );
125
126 $i = 0;
127 foreach ( $res as $row ) {
128 if ( !( ++$i % $reportingInterval ) ) {
129 $this->output( "$i\n" );
130 wfWaitForSlaves();
131 }
132 if ( $redirectsOnly ) {
133 $this->fixRedirect( $row->page_id );
134 } else {
135 self::fixLinksFromArticle( $row->page_id );
136 }
137 }
138 } else {
139 if ( !$end ) {
140 $maxPage = $dbr->selectField( 'page', 'max(page_id)', false );
141 $maxRD = $dbr->selectField( 'redirect', 'max(rd_from)', false );
142 $end = max( $maxPage, $maxRD );
143 }
144 $this->output( "Refreshing redirects table.\n" );
145 $this->output( "Starting from page_id $start of $end.\n" );
146
147 for ( $id = $start; $id <= $end; $id++ ) {
148
149 if ( !( $id % $reportingInterval ) ) {
150 $this->output( "$id\n" );
151 wfWaitForSlaves();
152 }
153 $this->fixRedirect( $id );
154 }
155
156 if ( !$redirectsOnly ) {
157 $this->output( "Refreshing links table.\n" );
158 $this->output( "Starting from page_id $start of $end.\n" );
159
160 for ( $id = $start; $id <= $end; $id++ ) {
161
162 if ( !( $id % $reportingInterval ) ) {
163 $this->output( "$id\n" );
164 wfWaitForSlaves();
165 }
166 self::fixLinksFromArticle( $id );
167 }
168 }
169 }
170 }
171
172 /**
173 * Update the redirect entry for a given page
174 * @param $id int The page_id of the redirect
175 */
176 private function fixRedirect( $id ) {
177 $page = WikiPage::newFromID( $id );
178 $dbw = wfGetDB( DB_MASTER );
179
180 if ( $page === null ) {
181 // This page doesn't exist (any more)
182 // Delete any redirect table entry for it
183 $dbw->delete( 'redirect', array( 'rd_from' => $id ),
184 __METHOD__ );
185 return;
186 }
187
188 $rt = $page->getRedirectTarget();
189
190 if ( $rt === null ) {
191 // The page is not a redirect
192 // Delete any redirect table entry for it
193 $dbw->delete( 'redirect', array( 'rd_from' => $id ),
194 __METHOD__ );
195 }
196 }
197
198 /**
199 * Run LinksUpdate for all links on a given page_id
200 * @param $id int The page_id
201 */
202 public static function fixLinksFromArticle( $id ) {
203 global $wgParser, $wgContLang;
204
205 $page = WikiPage::newFromID( $id );
206
207 LinkCache::singleton()->clear();
208
209 if ( $page === null ) {
210 return;
211 }
212
213 $text = $page->getRawText();
214 if ( $text === false ) {
215 return;
216 }
217
218 $dbw = wfGetDB( DB_MASTER );
219 $dbw->begin( __METHOD__ );
220
221 $options = ParserOptions::newFromUserAndLang( new User, $wgContLang );
222 $parserOutput = $wgParser->parse( $text, $page->getTitle(), $options, true, true, $page->getLatest() );
223 $update = new LinksUpdate( $page->getTitle(), $parserOutput, false );
224 $update->doUpdate();
225
226 $dbw->commit( __METHOD__ );
227 }
228
229 /**
230 * Removes non-existing links from pages from pagelinks, imagelinks,
231 * categorylinks, templatelinks, externallinks, interwikilinks, langlinks and redirect tables.
232 *
233 * @param $maxLag int
234 * @param $batchSize int The size of deletion batches
235 *
236 * @author Merlijn van Deen <valhallasw@arctus.nl>
237 */
238 private function deleteLinksFromNonexistent( $maxLag = 0, $batchSize = 100 ) {
239 wfWaitForSlaves();
240
241 $dbw = wfGetDB( DB_MASTER );
242
243 $lb = wfGetLBFactory()->newMainLB();
244 $dbr = $lb->getConnection( DB_SLAVE );
245 $dbr->bufferResults( false );
246
247 $linksTables = array( // table name => page_id field
248 'pagelinks' => 'pl_from',
249 'imagelinks' => 'il_from',
250 'categorylinks' => 'cl_from',
251 'templatelinks' => 'tl_from',
252 'externallinks' => 'el_from',
253 'iwlinks' => 'iwl_from',
254 'langlinks' => 'll_from',
255 'redirect' => 'rd_from',
256 'page_props' => 'pp_page',
257 );
258
259 foreach ( $linksTables as $table => $field ) {
260 $this->output( "Retrieving illegal entries from $table... " );
261
262 // SELECT DISTINCT( $field ) FROM $table LEFT JOIN page ON $field=page_id WHERE page_id IS NULL;
263 $results = $dbr->select( array( $table, 'page' ),
264 $field,
265 array( 'page_id' => null ),
266 __METHOD__,
267 'DISTINCT',
268 array( 'page' => array( 'LEFT JOIN', "$field=page_id" ) )
269 );
270
271 $counter = 0;
272 $list = array();
273 $this->output( "0.." );
274 foreach ( $results as $row ) {
275 $counter++;
276 $list[] = $row->$field;
277 if ( ( $counter % $batchSize ) == 0 ) {
278 wfWaitForSlaves();
279 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
280
281 $this->output( $counter . ".." );
282 $list = array();
283 }
284 }
285 $this->output( $counter );
286 if ( count( $list ) > 0 ) {
287 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
288 }
289 $this->output( "\n" );
290 wfWaitForSlaves();
291 }
292 $lb->closeAll();
293 }
294 }
295
296 $maintClass = 'RefreshLinks';
297 require_once( RUN_MAINTENANCE_IF_MAIN );