-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppropediaWikitext.php
217 lines (192 loc) · 6.22 KB
/
AppropediaWikitext.php
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
<?php
/**
* This class fixes the wikitext of some pages
* according to Appropedia standards for each namespace
*/
class AppropediaWikitext {
/**
* This array will contain the fixes actually performed
* in order to generate an informative edit summary
*/
public static $fixes = [];
/**
* This hook fires after each page save
* and triggers the fixes that will be performed by a bot account
*/
public static function onPageSaveComplete( WikiPage $wikiPage, MediaWiki\User\UserIdentity $user, string $summary, int $flags, MediaWiki\Revision\RevisionRecord $revisionRecord, MediaWiki\Storage\EditResult $editResult ) {
global $wgAppropediaBotAccount;
// The main page is always an exception
$title = $wikiPage->getTitle();
if ( $title->isMainPage() ) {
return;
}
// Prevent infinite loops
if ( $user->getName() === $wgAppropediaBotAccount ) {
return;
}
// If a user reverts an edit done by this script, don't insist
if ( $editResult->isRevert() ) {
return;
}
// Only for wikitext
$contentModel = $title->getContentModel();
if ( $contentModel !== CONTENT_MODEL_WIKITEXT ) {
return;
}
// Don't fix redirects
$content = $wikiPage->getContent();
$wikitext = $content->getText();
if ( preg_match( "/^#(\S+ ?\[\[.+\]\])/", $wikitext ) ) {
return;
}
// Do general fixes
$namespace = $wikiPage->getNamespace();
switch ( $namespace ) {
case 0:
$fixed = self::fixContentPage( $wikitext, $title );
break;
case 2:
$fixed = self::fixUserPage( $wikitext, $title );
break;
case 6:
$fixed = self::fixFilePage( $wikitext, $title );
break;
case 14:
$fixed = self::fixCategoryPage( $wikitext, $title );
break;
default:
return;
}
// Check if anything changed
if ( !self::$fixes ) {
return;
}
// Save the fixed wikitext
$content = ContentHandler::makeContent( $fixed, $title );
$user = User::newSystemUser( $wgAppropediaBotAccount );
$updater = $wikiPage->newPageUpdater( $user );
$updater->setContent( 'main', $content );
$summary = implode( ' + ', self::$fixes );
$comment = CommentStoreComment::newUnsavedComment( $summary);
$updater->saveRevision( $comment, EDIT_SUPPRESS_RC | EDIT_FORCE_BOT | EDIT_MINOR | EDIT_INTERNAL );
}
/**
* Fix the wikitext of a content page
*/
public static function fixContentPage( $wikitext, $title ) {
// Append {{Page data}}
if ( !preg_match( '/{{[Pp]age[_ ]data/', $wikitext )
&& !preg_match( '/^{{Automatic translation notice/', $wikitext ) // except automatic translations
&& !preg_match( '/^#(.+ ?\[\[.+\]\])/', $wikitext ) // and redirects
) {
$wikitext .= "\n\n{{Page data}}";
self::$fixes[] = 'Add [[Template:Page data]]';
}
return $wikitext;
}
/**
* Fix the wikitext of a user page
*/
public static function fixUserPage( $wikitext, $title ) {
if ( $title->isSubpage() ) {
return;
}
// Prepend {{User data}}
if ( !preg_match( '/{{[Uu]ser[_ ]data/', $wikitext ) ) {
$wikitext = "{{User data}}\n\n$wikitext";
self::$fixes[] = 'Add [[Template:User data]]';
}
return $wikitext;
}
/**
* Fix the wikitext of a category page
*/
public static function fixCategoryPage( $wikitext, $title ) {
// Prepend {{Category data}}
if ( !preg_match( '/{{[Cc]ategory[_ ]data/', $wikitext ) ) {
$wikitext = "{{Category data}}\n\n$wikitext";
self::$fixes[] = 'Add [[Template:Category data]]';
}
return $wikitext;
}
/**
* Fix the wikitext of a file page
*/
public static function fixFilePage( $wikitext, $title ) {
// This ugly contraption is here because Extension:UploadWizard has hard-coded
// the structure of the file pages it creates, so we can't modify them via config
// Therefore, we check every single page save and if it has the structure of
// a file page created by Upload Wizard, we transform it to our preferred structure
if ( preg_match( "/=={{int:filedesc}}==\n{{Information\n\|description={{en\|1=(.*)}}\n\|date=(.*)\n\|source=(.*)\n\|author=(.*)\n\|permission=(.*)\n\|other versions=(.*)\n}}\n\n=={{int:license-header}}==\n{{(.*)}}\n*(.*)/s", $wikitext, $matches ) ) {
// Get data
$description = trim( $matches[1] );
$date = $matches[2];
$source = $matches[3];
$author = $matches[4];
$permission = $matches[5];
$otherVersions = $matches[6];
$license = $matches[7];
$licenseDetails = $matches[8];
// Process data
if ( $source === '{{own}}' ) {
$source = 'Own work';
}
if ( preg_match( '/\[\[([^|]+)\|[^]]+\]\]/', $author, $matches ) ) {
$author = $matches[1];
}
if ( $license === 'subst:uwl' ) {
$license = null; // Unknown license
} else if ( preg_match( '/self\|(.*)/', $license, $matches ) ) {
$license = strtoupper( $matches[1] );
} else {
$license = strtoupper( $license );
}
if ( $license === 'PD' ) {
$license = 'Public domain';
}
if ( $license === 'PD-USGOV' ) {
$license = 'Public domain';
}
if ( $license === 'FAIR USE' ) {
$license = 'Fair use';
}
if ( $licenseDetails ) {
$license = $licenseDetails;
}
$params = [
'date' => $date,
'author' => $author,
'source' => $source,
'license' => $license,
];
$params = array_filter( $params );
// Build wikitext
$wikitext = "$description\n\n{{File data";
foreach ( $params as $param => $value ) {
$wikitext .= "\n| $param = $value";
}
$wikitext .= "\n}}";
self::$fixes[] = 'Fix file page';
}
// Fix file pages created with the visual editor
// Most is done from LocalSettings.php
// here we just delink the author
if ( preg_match( '/\| author = \[\[([^|]+)\|[^]]+\]\]/', $wikitext ) ) {
$wikitext = preg_replace( '/\| author = \[\[([^|]+)\|[^]]+\]\]/', '| author = $1', $wikitext );
self::$fixes[] = 'Delink author';
}
// Fix file pages created via Special:Upload
if ( preg_match( "/== Summary ==\n(.*)\n== Licensing ==\n{{(.*)}}/s", $wikitext, $matches ) ) {
$description = trim( $matches[1] );
$license = $matches[2];
$wikitext = "$description\n\n{{File data\n| license = $license\n}}";
self::$fixes[] = 'Fix file page';
}
// Fix empty file pages
if ( !preg_match( '/{{[Ff]ile[_ ]data/', $wikitext ) ) {
$wikitext .= "\n\n{{File data}}";
self::$fixes[] = 'Add [[Template:File data]]';
}
return $wikitext;
}
}