-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanupILH.php
More file actions
310 lines (287 loc) · 9.43 KB
/
cleanupILH.php
File metadata and controls
310 lines (287 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
/**
*/
require_once( dirname( __FILE__ ) . '/PageMaintenance.php' );
class CleanupILH extends PageMaintenance {
static $templates = null;
static $suffix = null;
public function __construct() {
parent::__construct();
$this->addOption( 'maxlag', 'Do not run if DB lags more than this time.' );
$this->titleKnown = array();
}
static function fallbackArray( &$a, $b ) {
for ( $i = 0; $i < count( $a ); $i++ ) {
if ( !is_string( $a[$i] ) || trim( $a[$i] ) === '' ) {
$a[$i] = $b[$i];
}
}
}
static function getOptionalColonForWikiLink( $title, $onTitle ) {
if ( in_array( $title->getNamespace(), array( NS_CATEGORY, NS_FILE ) ) ) {
return ':';
}
$iw = $title->getInterwiki();
if ( $iw && !$onTitle->isTalkPage() && Language::fetchLanguageName( $iw, null, 'mw' ) ) {
return ':';
}
return '';
}
private function findAlias( $pageTitle, &$title, $lang, $interwiki, $local ) {
global $wgContLang, $wgConf, $wgDBname, $wgLocalInterwiki;
if ( is_null( self::$suffix ) ) {
list( $site, $_ ) = $wgConf->siteFromDB( $wgDBname );
self::$suffix = $site == 'wikipedia' ? 'wiki' : $site;
}
$fdbn = str_replace( '-', '_', $lang ) . self::$suffix;
try {
$fdbr = wfGetDB( DB_SLAVE, array(), $fdbn );
} catch ( DBError $e ) {
$fdbr = false;
$this->output( " (dbcerror $fdbn)" );
}
if ( !$fdbr ) {
return false;
}
# Ideally we may want to parse $interwiki on the specified site, for
# correct namespaces and $wgCapitalLinks, but it's a little too expensive.
$ftitle = Title::newFromText( $interwiki );
if ( !$ftitle ) {
return false;
}
try {
$ll_title = $fdbr->selectField(
array( 'page', 'langlinks' ),
'll_title',
array(
'page_namespace' => $ftitle->getNamespace(),
'page_title' => $ftitle->getDBKey(),
'page_id = ll_from',
'll_lang' => $wgLocalInterwiki,
),
__METHOD__
);
if ( $ll_title === false ) {
$ll_title = $fdbr->selectField(
array(
'redirect', 'langlinks',
'rdpage' => 'page', 'dstpage' => 'page',
),
'll_title',
array(
'rdpage.page_namespace' => $ftitle->getNamespace(),
'rdpage.page_title' => $ftitle->getDBKey(),
'rdpage.page_is_redirect' => true,
'rdpage.page_id = rd_from',
'dstpage.page_namespace = rd_namespace',
'dstpage.page_title = rd_title',
$fdbr->makeList( array(
# https://bugzilla.wikimedia.org/48853
$fdbr->makeList( array( 'rd_interwiki' => null ), LIST_OR ),
'rd_interwiki' => '',
), LIST_OR ),
'dstpage.page_id = ll_from',
'll_lang' => $wgLocalInterwiki,
),
__METHOD__
);
}
} catch ( DBError $e ) {
$this->output( " (dbqerror $fdbn)" );
return false;
}
if ( $ll_title === false ) {
return false;
}
$newTitle = Title::newFromText( $ll_title );
$wgContLang->findVariantLink( $ll_title, $newTitle, true );
if ( $newTitle && ( $newTitle->isKnown()
|| isset( $this->titleKnown[$newTitle->getPrefixedDBKey()] ) )
) {
# Hooray we managed to find an alias!
$redirected = false;
if ( $title ) {
$this->output( " (rd [[{$title->getPrefixedText()}]] "
. "=> [[{$newTitle->getFullText()}]]" );
# Create redirect
$contentHandler = ContentHandler::getForTitle( $title );
$redirectContent = $contentHandler->makeRedirectContent( $newTitle );
if ( WikiPage::factory( $title )->doEdit(
$redirectContent->serialize(),
wfMessage( 'ts-cleanup-ilh-redirect' )->params(
$newTitle->getFullText(),
$pageTitle->getPrefixedText(),
$lang, $interwiki, $pageTitle->getLatestRevID()
)->text(), EDIT_NEW
)->isOK() ) {
$this->output( ' done)' );
$this->titleKnown[$title->getPrefixedDBKey()] = true;
$redirected = true;
} else {
$this->output( ' ERROR)' );
}
}
if ( !$redirected ) {
$title = $newTitle;
$this->output( " (alias [[$local]] => [[{$title->getFullText()}]])" );
}
return true;
}
return false;
}
public function executeTitle( $title, $ident = '', $recur = true ) {
global $wgContLang, $wgLabs;
static $cleanedup = array();
static $parserOutput = null;
if ( $this->getPageSource() == 'random' || $this->getPageSource() == 'start' ) {
$recur = false;
}
$this->output( $ident . $title->getPrefixedText() );
if ( !$title->exists() ) {
$this->output( "\tredlink.\n" );
return;
}
if ( !$title->userCan( 'edit', $wgLabs->user ) ) {
# It's unlikely to be problematic, and usually we don't have the right
# to clean them up ( = edit them )...
$this->output( "\tprotected.\n" );
return;
}
if ( isset( $cleanedup[$title->getPrefixedDBKey()] ) ) {
$this->output( "\tskipping...\n" );
return;
} else {
$cleanedup[$title->getPrefixedDBKey()] = true;
}
$otext = $text = Revision::newFromTitle( $title )->getText();
# First, find out all links to check
$matches = array();
$lang = '(ar|be|da|de|el|en|es|fi|fr|hy|id|it|ja|ko|ms|nl|no|pl|pt|ro|ru|sv|tr|uk|uz|vi)';
$ilhRe = '/\{\{\s*(?:Internal[_ ]link[_ ]helper\/' . $lang
. '|Link-' . $lang . '|' . $lang . '-link)\s*'
. '(?:\|\s*(.*?)\s*)?' # Local page name
. '(?:\|\s*(.*?)\s*)?' # Interwiki page name
. '(?:\|\s*(.*?)\s*)?' # Text
. '\}\}/i';
$transRe = '/\{\{\s*(?:Translink|Tsl)\s*'
. '(?:\|\s*(.*?)\s*)?' # Lang
. '(?:\|\s*(.*?)\s*)?' # Interwiki page name
. '(?:\|\s*(.*?)\s*)?' # Local page name
. '(?:\|\s*(.*?)\s*)?' # Text
. '\}\}/i';
$batch = new LinkBatch();
$titles = array();
preg_match_all( $ilhRe, $text, $matches, PREG_PATTERN_ORDER );
list( $templates, $langs, $langsB, $langsC, $locals, $interwikis, $descs ) = $matches;
self::fallbackArray( $langs, $langsB );
self::fallbackArray( $langs, $langsC );
self::fallbackArray( $interwikis, $locals );
preg_match_all( $transRe, $text, $matches, PREG_PATTERN_ORDER );
list( $templates2, $langs2, $interwikis2, $locals2, $descs2 ) = $matches;
self::fallbackArray( $locals2, $interwikis2 );
$templates = array_merge( $templates, $templates2 );
$langs = array_map( 'strtolower', array_merge( $langs, $langs2 ) );
$interwikis = array_merge( $interwikis, $interwikis2 );
$locals = array_merge( $locals, $locals2 );
$descs = array_merge( $descs, $descs2 );
self::fallbackArray( $descs, $locals );
$lb = new LinkBatch( $titles );
foreach ( $locals as $titleText ) {
$lb->addObj( $titles[] = Title::newFromText( $titleText ) );
foreach ( $wgContLang->autoConvertToAllVariants( $titleText ) as $titleText ) {
$lb->addObj( Title::newFromText( $titleText ) );
}
}
$lb->execute();
for ( $i = 0; $i < count( $templates ); $i++ ) {
$wgContLang->findVariantLink( $locals[$i], $titles[$i], true );
$localKnown = $titles[$i] && ( $titles[$i]->isKnown()
|| isset( $this->titleKnown[$titles[$i]->getPrefixedDBKey()] ) );
if ( !$localKnown ) {
$localKnown = $this->findAlias( $title, $titles[$i], $langs[$i], $interwikis[$i], $locals[$i] );
}
if ( $localKnown ) {
$replace = '[[' . self::getOptionalColonForWikiLink( $titles[$i], $title );
if ( is_string( $descs[$i] ) && trim( $descs[$i] ) !== '' ) {
$nt = Title::newFromText( $descs[$i] );
$x = $descs[$i];
$wgContLang->findVariantLink( $x, $nt, true );
if ( $nt && $nt->getFullText() === $titles[$i]->getFullText() ) {
$replace .= $descs[$i];
} else {
$replace .= $titles[$i]->getFullText() . '|' . $descs[$i];
}
} else {
$replace .= $titles[$i]->getFullText();
}
$replace .= ']]';
$text = str_replace( $templates[$i], $replace, $text );
}
}
if ( $text === $otext ) {
$this->output( "\tno change." );
if ( $recur ) {
$this->output( '..' );
if ( !$parserOutput ) {
$parserOutput = new ParserOutput();
}
$u = new LinksUpdate( $title, $parserOutput );
$u->doUpdate();
$this->output( ' linksupdated.' );
}
$this->output( "\n" );
} else {
$this->output( "\tsaving..." );
if ( WikiPage::factory( $title )->doEdit( $text,
wfMessage( 'ts-cleanup-ilh' )->text(),
EDIT_MINOR, $title->getLatestRevID() )->isOK() ) {
$this->output( " done.\n" );
} else {
$this->output( " ERROR.\n" );
}
}
if ( $text === $otext && $recur ) {
# We should call executeTitle on all templates this page uses.
# Blue links may be there, maybe invisible from parsed view!
# But don't do this recursively?
foreach ( $title->getTemplateLinksFrom() as $template ) {
$this->executeTitle( $template, $ident . ' ', false );
}
}
}
public function getRandomQueryInfo() {
$db = $this->getDatabase();
return array(
'tables' => array( 'templatelinks' ),
'conds' => array(
'page_id = tl_from',
$db->makeList( array_map( function( $title ) use ( $db ) {
return $db->makeList( array(
'tl_namespace' => $title->getNamespace(),
'tl_title' => $title->getDBKey(),
), LIST_AND );
}, self::$templates ), LIST_OR ),
),
'options' => array( 'DISTINCT' ),
);
}
public function execute() {
if ( is_null( self::$templates ) ) {
self::$templates = array(
Title::makeTitleSafe( NS_TEMPLATE, 'Translink' ),
Title::makeTitleSafe( NS_TEMPLATE, 'Internal link helper' ),
);
}
if ( $this->hasOption( 'maxlag' ) ) {
$maxlag = intval( $this->getOption( 'maxlag' ) );
$lag = wfReplag();
if ( $lag > $maxlag ) {
$this->output( "Current lag: $lag, required maxlag: $maxlag, exiting.\n" );
return;
}
}
parent::execute();
}
}
$maintClass = "CleanupILH";
require_once( RUN_MAINTENANCE_IF_MAIN );