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