-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanupDuplicateArgs.php
More file actions
132 lines (121 loc) · 3.69 KB
/
cleanupDuplicateArgs.php
File metadata and controls
132 lines (121 loc) · 3.69 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
<?php
require_once( dirname( __FILE__ ) . '/PageDomMaintenanceExt.php' );
class CleanupDuplicateArgs extends PageDomMaintenanceExt {
static $exemptedTemplates = array(
'Template:Chess diagram',
'Template:Xiangqi diagram',
'Template:Xiangqi-position',
'Template:Shogi diagram',
);
public function __construct() {
parent::__construct();
$this->addOption( 'bot', 'Mark edits as "bot".', false );
$this->insideTemplateName = 0;
}
public function executeTitleDom( $title, $dom, $rev, $data ) {
$this->domModified = false;
$this->title = $title;
$text = $this->nodeToWikitext( $dom );
if ( $this->domModified ) {
$this->output( "saving..." );
if ( WikiPage::factory( $title )->doEdit( $text,
wfMessage( 'ts-cleanup-dupargs' )->text(),
EDIT_MINOR | ( $this->hasOption( 'bot' ) ? EDIT_SUPPRESS_RC : 0 ),
$rev ? $rev->getId() : false
)->isOK() ) {
$this->output( " done.\n" );
} else {
$this->output( " ERROR.\n" );
}
} else {
$this->output( "no change.\n" );
}
}
public function executeTemplate( $node, $arrayNode ) {
$args = array();
$pieces = array( '{{' );
for ( $i = 0; $i < $arrayNode->getLength(); $i++ ) {
$childNode = $arrayNode->item( $i );
switch ( $childNode->getName() ) {
case 'title':
$pieces[] = $this->nodeToWikitext( $childNode );
$this->insideTemplateName++;
$templateName = $this->nodeToWikitext( $childNode );
$this->insideTemplateName--;
$templateName = trim( $templateName );
$templateTitle = Title::newFromText( $templateName, NS_TEMPLATE );
if ( !$templateTitle ) {
$this->output( "Skipping non-direct template call: $templateName\n" );
return;
}
$colonPos = strpos( $templateName, ':' );
if ( $colonPos !== false ) {
$function = substr( $templateName, 0, $colonPos );
global $wgParser;
if ( isset( $wgParser->mFunctionSynonyms[1][$function] ) ) {
$this->output( "Skipping case-sensitive parser function: $function\n" );
return;
}
global $wgContLang;
$function = $wgContLang->lc( $function );
if ( isset( $wgParser->mFunctionSynonyms[0][$function] ) ) {
$this->output( "Skipping case-insensitive parser function: $function\n" );
return;
}
}
try {
$templatePage = WikiPage::factory( $templateTitle );
} catch ( MWException $e ) {
$templatePage = null;
}
if ( $templatePage ) {
$redirectTitle = $templatePage->getRedirectTarget();
if ( $redirectTitle ) {
$templateTitle = $redirectTitle;
}
}
if ( in_array( $templateTitle->getPrefixedText(), static::$exemptedTemplates ) ) {
$this->output( "Skipping exempted template: {$templateTitle->getPrefixedText()}\n" );
return;
}
break;
case 'part':
$arg = $childNode->splitArg();
if ( $arg['index'] ) {
$argkr = $argk = $arg['index'];
$argvr = $argv = $this->nodeToWikitext( $arg['value'] );
} else {
$argk = $this->nodeToWikitext( $arg['name'] );
$argkr = trim( $argk );
$argv = $this->nodeToWikitext( $arg['value'] );
$argvr = trim( $argv );
}
if ( isset( $args[$argkr][$argvr] ) ) {
# Drop this argument.
$this->domModified = true;
$this->output( "Dropping duplicated argument: $argk=$argv\n" );
break;
}
$args[$argkr][$argvr] = 0;
$pieces[] = '|';
if ( !$arg['index'] ) {
$pieces[] = $argk;
$pieces[] = '=';
}
$pieces[] = $argv;
break;
}
}
$pieces[] = '}}';
return implode( '', $pieces );
}
public function executeComment( $node, $arrayNode ) {
if ( $this->insideTemplateName ) {
return '';
} else {
return null;
}
}
}
$maintClass = "CleanupDuplicateArgs";
require_once( RUN_MAINTENANCE_IF_MAIN );