Make sure the TOC is there when toggleToc is called. Although the function calling...
[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 $wgUser, $wgParser, $wgUseTidy;
64
65 $reportingInterval = 100;
66 $dbr = wfGetDB( DB_SLAVE );
67 $start = intval( $start );
68
69 # Don't generate TeX PNGs (lack of a sensible current directory causes errors anyway)
70 $wgUser->setOption( 'math', MW_MATH_SOURCE );
71
72 # Don't generate extension images (e.g. Timeline)
73 if ( method_exists( $wgParser, "clearTagHooks" ) ) {
74 $wgParser->clearTagHooks();
75 }
76
77 # Don't use HTML tidy
78 $wgUseTidy = false;
79
80 $what = $redirectsOnly ? "redirects" : "links";
81
82 if ( $oldRedirectsOnly ) {
83 # This entire code path is cut-and-pasted from below. Hurrah.
84 $res = $dbr->query(
85 "SELECT page_id " .
86 "FROM page " .
87 "LEFT JOIN redirect ON page_id=rd_from " .
88 "WHERE page_is_redirect=1 AND rd_from IS NULL AND " .
89 ( $end == 0 ? "page_id >= $start"
90 : "page_id BETWEEN $start AND $end" ),
91 __METHOD__
92 );
93 $num = $dbr->numRows( $res );
94 $this->output( "Refreshing $num old redirects from $start...\n" );
95
96 foreach ( $res as $row ) {
97 if ( !( ++$i % $reportingInterval ) ) {
98 $this->output( "$i\n" );
99 wfWaitForSlaves( $maxLag );
100 }
101 $this->fixRedirect( $row->page_id );
102 }
103 } elseif ( $newOnly ) {
104 $this->output( "Refreshing $what from " );
105 $res = $dbr->select( 'page',
106 array( 'page_id' ),
107 array(
108 'page_is_new' => 1,
109 "page_id >= $start" ),
110 __METHOD__
111 );
112 $num = $dbr->numRows( $res );
113 $this->output( "$num new articles...\n" );
114
115 $i = 0;
116 foreach ( $res as $row ) {
117 if ( !( ++$i % $reportingInterval ) ) {
118 $this->output( "$i\n" );
119 wfWaitForSlaves( $maxLag );
120 }
121 if ( $redirectsOnly )
122 $this->fixRedirect( $row->page_id );
123 else
124 self::fixLinksFromArticle( $row->page_id );
125 }
126 } else {
127 if ( !$end ) {
128 $maxPage = $dbr->selectField( 'page', 'max(page_id)', false );
129 $maxRD = $dbr->selectField( 'redirect', 'max(rd_from)', false );
130 $end = max( $maxPage, $maxRD );
131 }
132 $this->output( "Refreshing redirects table.\n" );
133 $this->output( "Starting from page_id $start of $end.\n" );
134
135 for ( $id = $start; $id <= $end; $id++ ) {
136
137 if ( !( $id % $reportingInterval ) ) {
138 $this->output( "$id\n" );
139 wfWaitForSlaves( $maxLag );
140 }
141 $this->fixRedirect( $id );
142 }
143
144 if ( !$redirectsOnly ) {
145 $this->output( "Refreshing links table.\n" );
146 $this->output( "Starting from page_id $start of $end.\n" );
147
148 for ( $id = $start; $id <= $end; $id++ ) {
149
150 if ( !( $id % $reportingInterval ) ) {
151 $this->output( "$id\n" );
152 wfWaitForSlaves( $maxLag );
153 }
154 self::fixLinksFromArticle( $id );
155 }
156 }
157 }
158 }
159
160 /**
161 * Update the redirect entry for a given page
162 * @param $id int The page_id of the redirect
163 */
164 private function fixRedirect( $id ) {
165 $title = Title::newFromID( $id );
166 $dbw = wfGetDB( DB_MASTER );
167
168 if ( is_null( $title ) ) {
169 // This page doesn't exist (any more)
170 // Delete any redirect table entry for it
171 $dbw->delete( 'redirect', array( 'rd_from' => $id ),
172 __METHOD__ );
173 return;
174 }
175 $article = new Article( $title );
176
177 $rt = $article->followRedirect();
178
179 if ( !$rt || !is_object( $rt ) ) {
180 // $title is not a redirect
181 // Delete any redirect table entry for it
182 $dbw->delete( 'redirect', array( 'rd_from' => $id ),
183 __METHOD__ );
184 } else {
185 $article->updateRedirectOn( $dbw, $rt );
186 }
187 }
188
189 /**
190 * Run LinksUpdate for all links on a given page_id
191 * @param $id int The page_id
192 */
193 public static function fixLinksFromArticle( $id ) {
194 global $wgParser;
195
196 $title = Title::newFromID( $id );
197 $dbw = wfGetDB( DB_MASTER );
198
199 LinkCache::singleton()->clear();
200
201 if ( is_null( $title ) ) {
202 return;
203 }
204 $dbw->begin();
205
206 $revision = Revision::newFromTitle( $title );
207 if ( !$revision ) {
208 return;
209 }
210
211 $options = new ParserOptions;
212 $parserOutput = $wgParser->parse( $revision->getText(), $title, $options, true, true, $revision->getId() );
213 $update = new LinksUpdate( $title, $parserOutput, false );
214 $update->doUpdate();
215 $dbw->commit();
216 }
217
218 /*
219 * Removes non-existing links from pages from pagelinks, imagelinks,
220 * categorylinks, templatelinks and externallinks tables.
221 *
222 * @param $maxLag
223 * @param $batchSize The size of deletion batches
224 *
225 * @author Merlijn van Deen <valhallasw@arctus.nl>
226 */
227 private function deleteLinksFromNonexistent( $maxLag = 0, $batchSize = 100 ) {
228 wfWaitForSlaves( $maxLag );
229
230 $dbw = wfGetDB( DB_MASTER );
231
232 $lb = wfGetLBFactory()->newMainLB();
233 $dbr = $lb->getConnection( DB_SLAVE );
234 $dbr->bufferResults( false );
235
236 $linksTables = array( // table name => page_id field
237 'pagelinks' => 'pl_from',
238 'imagelinks' => 'il_from',
239 'categorylinks' => 'cl_from',
240 'templatelinks' => 'tl_from',
241 'externallinks' => 'el_from',
242 );
243
244 foreach ( $linksTables as $table => $field ) {
245 $this->output( "Retrieving illegal entries from $table... " );
246
247 // SELECT DISTINCT( $field ) FROM $table LEFT JOIN page ON $field=page_id WHERE page_id IS NULL;
248 $results = $dbr->select( array( $table, 'page' ),
249 $field,
250 array( 'page_id' => null ),
251 __METHOD__,
252 'DISTINCT',
253 array( 'page' => array( 'LEFT JOIN', "$field=page_id" ) )
254 );
255
256 $counter = 0;
257 $list = array();
258 $this->output( "0.." );
259
260 foreach ( $results as $row ) {
261 $counter++;
262 $list[] = $row->$field;
263 if ( ( $counter % $batchSize ) == 0 ) {
264 wfWaitForSlaves( 5 );
265 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
266
267 $this->output( $counter . ".." );
268 $list = array();
269 }
270 }
271 $this->output( $counter );
272 if ( count( $list ) > 0 ) {
273 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
274 }
275 $this->output( "\n" );
276 }
277 $lb->closeAll();
278 }
279 }
280
281 $maintClass = 'RefreshLinks';
282 require_once( DO_MAINTENANCE );