-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStandardWikitext.php
584 lines (478 loc) · 16.7 KB
/
StandardWikitext.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
<?php
use MediaWiki\MediaWikiServices;
class StandardWikitext {
/**
* @param array &$ids
*/
public static function onGetDoubleUnderscoreIDs( &$ids ) {
$ids[] = 'NOSTANDARDWIKITEXT';
}
/**
* @param WikiPage $wikiPage
* @param \MediaWiki\User\UserIdentity $user
* @param string $summary
* @param int $flags
* @param \MediaWiki\Revision\RevisionRecord $revisionRecord
* @param \MediaWiki\Storage\EditResult $editResult
* @return void
*/
public static function onPageSaveComplete(
WikiPage $wikiPage,
MediaWiki\User\UserIdentity $user,
string $summary,
int $flags,
MediaWiki\Revision\RevisionRecord $revisionRecord,
MediaWiki\Storage\EditResult $editResult
) {
// Prevent infinite loops
$config = MediaWikiServices::getInstance()->getMainConfig();
$account = $config->get( 'StandardWikitextAccount' );
if ( $user->getName() === $account ) {
return;
}
// Don't fix pages that are explicitly marked so
if ( $wikiPage->getParserOutput()->getPageProperty( 'NOSTANDARDWIKITEXT' ) !== null ) {
return;
}
// If a user tries to revert an edit done by this script, don't insist
if ( $editResult->isRevert() ) {
return;
}
// Don't fix redirects
$title = $wikiPage->getTitle();
if ( $title->isRedirect() ) {
return;
}
// Only fix wikitext pages
$contentModel = $title->getContentModel();
if ( $contentModel !== CONTENT_MODEL_WIKITEXT ) {
return;
}
// Only fix in configured namespaces
$namespace = $title->getNamespace();
$namespaces = $config->get( 'StandardWikitextNamespaces' );
if ( !in_array( $namespace, $namespaces ) ) {
return;
}
// Fix the wikitext and check if anything changed
$content = $wikiPage->getContent();
$wikitext = $content->getText();
$fixed = self::fixWikitext( $wikitext );
if ( $fixed === $wikitext ) {
return;
}
// Save the fixed wikitext
self::saveWikitext( $fixed, $wikiPage );
}
/**
* @param string $wikitext
* @param WikiPage $wikiPage
* @return void
*/
public static function saveWikitext( string $wikitext, WikiPage $wikiPage ) {
$config = MediaWikiServices::getInstance()->getMainConfig();
$account = $config->get( 'StandardWikitextAccount' );
$user = User::newSystemUser( $account );
$updater = $wikiPage->newPageUpdater( $user );
$title = $wikiPage->getTitle();
$content = ContentHandler::makeContent( $wikitext, $title );
$updater->setContent( 'main', $content );
$summary = wfMessage( 'standardwikitext-summary' )->inContentLanguage()->text();
$comment = CommentStoreComment::newUnsavedComment( $summary );
$updater->saveRevision( $comment, EDIT_SUPPRESS_RC | EDIT_FORCE_BOT | EDIT_MINOR | EDIT_INTERNAL );
}
/**
* @param string $wikitext
* @return array|string|string[]|null
*/
public static function fixWikitext( string $wikitext ) {
// Don't try to fix stuff inside <html> blocks
$htmls = self::getElements( '<html>', '</html>', $wikitext );
foreach ( $htmls as $i => $html ) {
$wikitext = str_replace( $html, "@@@html$i@@@", $wikitext );
}
$wikitext = self::fixTemplates( $wikitext );
$wikitext = self::fixTables( $wikitext );
$wikitext = self::fixLinks( $wikitext );
$wikitext = self::fixReferences( $wikitext );
$wikitext = self::fixLists( $wikitext );
$wikitext = self::fixSections( $wikitext );
$wikitext = self::fixCategories( $wikitext );
$wikitext = self::fixSpacing( $wikitext );
// Restore <html> blocks
foreach ( $htmls as $i => $html ) {
$wikitext = str_replace( "@@@html$i@@@", $html, $wikitext );
}
return $wikitext;
}
/**
* @param string $wikitext
* @return array|string|string[]
*/
public static function fixTemplates( string $wikitext ) {
$templates = self::getElements( '{{', '}}', $wikitext );
foreach ( $templates as $template ) {
// Store original wikitext to be able to replace it later
$original = $template;
// Remove outer braces
$template = preg_replace( "/^\{\{/", "", $template );
$template = preg_replace( "/\}\}$/", "", $template );
// Replace all nested templates, tables and links to prevent havoc
$subelements = self::getElements( '{{', '}}', $template );
$subelements += self::getElements( '{|', '|}', $template );
$subelements += self::getElements( '[[', ']]', $template );
foreach ( $subelements as $key => $subelement ) {
$placeholder = '@@@' . $key . '@@@';
$template = str_replace( $subelement, $placeholder, $template );
}
// Get the template parts
$params = explode( '|', $template );
$params = array_map( 'trim', $params );
$title = array_shift( $params );
// Inline format
if ( strpos( $template, "\n|" ) === false ) {
// Rebuild the template
$template = $title;
foreach ( $params as $param ) {
$parts = explode( '=', $param, 2 );
if ( count( $parts ) === 2 ) {
$key = trim( $parts[0] );
$value = trim( $parts[1] );
// Restore newlines before lists
$value = preg_replace( "/^([*#])/", "\n$1", $value );
if ( $value ) {
$template .= "|$key=$value";
}
} else {
$value = trim( $parts[0] );
// Restore newlines before lists
$value = preg_replace( "/^([*#])/", "\n$1", $value );
$template .= "|$value";
}
}
// Block format
} else {
// Force capitalization
$title = ucfirst( $title );
// Rebuild the template
$template = $title;
foreach ( $params as $param ) {
$parts = explode( '=', $param, 2 );
if ( count( $parts ) === 2 ) {
$key = trim( $parts[0] );
$value = trim( $parts[1] );
// Restore newlines before lists
$value = preg_replace( "/^([*#])/", "\n$1", $value );
if ( $value ) {
$template .= "\n| $key = $value";
}
} else {
$value = trim( $parts[0] );
// Restore newlines before lists
$value = preg_replace( "/^([*#])/", "\n$1", $value );
$template .= "\n| $value";
}
}
$template .= "\n";
}
// Restore replaced subelements
foreach ( $subelements as $key => $subelement ) {
$placeholder = '@@@' . $key . '@@@';
$template = str_replace( $placeholder, $subelement, $template );
}
// Restore outer braces
$template = '{{' . $template . '}}';
// Replace original wikitext for fixed one
$wikitext = str_replace( $original, $template, $wikitext );
}
return $wikitext;
}
/**
* @param string $wikitext
* @return array|string|string[]
*/
public static function fixTables( string $wikitext ) {
$tables = self::getElements( "{|", "\n|}", $wikitext );
foreach ( $tables as $table ) {
// Store original wikitext to replace it later
$original = $table;
// Flatten the table
$table = preg_replace( "/\!\!/", "\n!", $table );
$table = preg_replace( "/\|\|/", "\n|", $table );
// Add leading spaces
// Headers
$table = preg_replace( "/^!([^ \n])/m", "! $1", $table );
// Captions
$table = preg_replace( "/^\|\+([^ \n])/m", "|+ $1", $table );
// Newrows
$table = preg_replace( "/^\|-([^ \n])/m", "|- $1", $table );
// Cells
$table = preg_replace( "/^\|([^ \n}+-])/m", "| $1", $table );
// Remove empty captions
$table = preg_replace( "/^\|\+ *\n/m", "", $table );
// Remove newrow after caption
$table = preg_replace( "/^(\|\+[^\n]+)\n\|\-/m", "$1", $table );
// Remove bold from captions
$table = preg_replace( "/^\|\+ *'''(.*)'''/m", "|+ $1", $table );
// Remove bold from headers
$table = preg_replace( "/^! *'''(.*)'''/m", "! $1", $table );
// Fix pseudo-headers
$table = preg_replace( "/^\| *'''(.*)'''/m", "! $1", $table );
// Remove leading newrow
$table = preg_replace( "/^(\{\|[^\n]*\n)\|\-\n/", "$1", $table );
// Remove trailing newrow
$table = preg_replace( "/\n\|\-\n\|\}$/", "\n|}", $table );
// Replace original wikitext for fixed one
$wikitext = str_replace( $original, $table, $wikitext );
}
return $wikitext;
}
/**
* @param string $wikitext
* @return array|string|string[]
*/
public static function fixLinks( string $wikitext ) {
$links = self::getElements( '[[', ']]', $wikitext );
foreach ( $links as $link ) {
// Store original wikitext to replace it later
$original = $link;
// Remove the outer braces
$link = preg_replace( "/^\[\[/", '', $link );
$link = preg_replace( "/\]\]$/", '', $link );
$parts = explode( '|', $link );
$title = $parts[0];
// Fix fake external link
// @todo Make more robust
if ( preg_match( '#^https?://#', $title ) ) {
$link = "[$link]";
$wikitext = str_replace( $original, $link, $wikitext );
continue;
}
$params = array_slice( $parts, 1 );
// [[ foo ]] → [[foo]]
$title = trim( $title );
// [[Fo%C3%B3]] → [[Foó]]
$title = rawurldecode( $title );
// [[test_link]] → [[test link]]
$title = str_replace( '_', ' ', $title );
$Title = Title::newFromText( $title );
if ( !$Title ) {
continue;
}
$namespace = $Title->getNamespace();
// File link: [[File:Foo.jpg|thumb|Caption with [[sub_link]].]]
if ( $namespace === 6 ) {
$link = $title;
foreach ( $params as $param ) {
// [[File:Foo.jpg| thumb ]] → [[File:Foo.jpg|thumb]]
$param = trim( $param );
// [[File:Foo.jpg|thumb|Caption with [[sub_link]].]] → [[File:Foo.jpg|thumb|Caption with [[sub link]].]]
$param = preg_replace_callback( "/\[\[[^\]]+\]\]/", function ( $matches ) {
$link = $matches[0];
return self::fixLinks( $link );
}, $param );
$link .= '|' . $param;
}
// Remove redundant parameters
$link = str_replace( 'thumb|right', 'thumb', $link );
$link = str_replace( 'right|thumb', 'thumb', $link );
$link = str_replace( '|alt=|', '|', $link );
// Link with alternative text: [[Title|text]]
} elseif ( $params ) {
$text = $params[0];
// [[Foo| bar ]] → [[Foo|bar]]
$text = trim( $text );
// [[foo|bar]] → [[Foo|bar]]
$title = ucfirst( $title );
// [[Foo|foo]] → [[foo]]
if ( lcfirst( $title ) === $text ) {
$link = $text;
// Else just build the link
} else {
$link = "$title|$text";
}
// Plain link: [[link]]
} else {
$link = $title;
}
// Restore outer braces
$link = "[[$link]]";
// Replace original wikitext for fixed one
$wikitext = str_replace( $original, $link, $wikitext );
}
return $wikitext;
}
/**
* @param string $wikitext
* @return array|string|string[]|null
*/
public static function fixReferences( string $wikitext ) {
// Fix spacing
$wikitext = preg_replace( "/<ref +name += +/", "<ref name=", $wikitext );
$wikitext = preg_replace( "/<ref([^>]+[^ ]+)\/>/", "<ref$1 />", $wikitext );
// Fix quotes
$wikitext = preg_replace( "/<ref name=' *([^']+) *'/", "<ref name=\"$1\"", $wikitext );
$wikitext = preg_replace( "/<ref name=([^\" \/>]+)/", "<ref name=\"$1\"", $wikitext );
// Remove spaces or newlines after opening ref tags
$wikitext = preg_replace( "/<ref([^>\/]*)>[ \n]+/", "<ref$1>", $wikitext );
// Fix empty references with name
$wikitext = preg_replace( "/<ref name=\"([^\"]+)\"><\/ref>/", "<ref name=\"$1\" />", $wikitext );
// Remove empty references
$wikitext = preg_replace( "/<ref><\/ref>/", "", $wikitext );
// Remove spaces or newlines around opening ref tags
$wikitext = preg_replace( "/[ \n]*<ref([^>\/]*)>[ \n]*/", "<ref$1>", $wikitext );
// Remove spaces or newlines before closing ref tags
$wikitext = preg_replace( "/[ \n]+<\/ref>/", "</ref>", $wikitext );
// Move references after punctuation
$wikitext = preg_replace( "/<ref([^<]+)<\/ref>([.,;:])/", "$2<ref$1</ref>", $wikitext );
$wikitext = preg_replace( "/<ref([^>]+)\/>([.,;:])/", "$2<ref$1/>", $wikitext );
return $wikitext;
}
/**
* @param string $wikitext
* @return array|string|string[]|null
*/
public static function fixLists( string $wikitext ) {
// Remove extra spaces between list items
$wikitext = preg_replace( "/^([*#]) ?([*#])? ?([*#])?/m", "$1$2$3", $wikitext );
// Remove empty list items
$wikitext = preg_replace( "/^([*#]+)$/m", "", $wikitext );
// Add initial space to list items
$wikitext = preg_replace( "/^([*#]+)([^ ]?)/m", "$1 $2", $wikitext );
// Remove newlines between lists
$wikitext = preg_replace( "/^\n+([*#]+)/m", "$1", $wikitext );
// Give lists some room
$wikitext = preg_replace( "/^([^*#][^\n]+)\n([*#])/m", "$1\n\n$2", $wikitext );
$wikitext = preg_replace( "/^([*#][^\n]+)\n([^*#])/m", "$1\n\n$2", $wikitext );
// Restore redirect
$wikitext = preg_replace( "/^@@@/", "#", $wikitext );
return $wikitext;
}
/**
* @param string $wikitext
* @return array|string|string[]|null
*/
public static function fixSections( string $wikitext ) {
// Fix spacing
$wikitext = preg_replace( "/^(=+) *(.+?) *(=+) *$/m", "\n\n$1 $2 $3\n\n", $wikitext );
$wikitext = preg_replace( "/\n\n\n+/m", "\n\n", $wikitext );
$wikitext = trim( $wikitext );
// Remove bold
$wikitext = preg_replace( "/^(=+) '''(.+?)''' (=+)$/m", "$1 $2 $3", $wikitext );
// Remove trailing colon
$wikitext = preg_replace( "/^(=+) (.+?): (=+)$/m", "$1 $2 $3", $wikitext );
return $wikitext;
}
/**
* Move categories to the bottom
* and remove duplicate categories
* @todo Only works in English
*
* @param string $wikitext
* @return array|string|string[]
*/
public static function fixCategories( string $wikitext ) {
// Don't replace category links inside templates and parser functions
$templates = self::getElements( '{{', '}}', $wikitext );
foreach ( $templates as $i => $template ) {
$wikitext = str_replace( $template, "@@$i@@", $wikitext );
}
$count = preg_match_all( "/\n*\[\[ ?[Cc]ategory ?: ?([^]]+) ?\]\]/", $wikitext, $matches );
if ( $count ) {
foreach ( $matches[0] as $match ) {
$wikitext = str_replace( $match, '', $wikitext );
}
$categories = $matches[1];
$categories = array_unique( $categories );
$wikitext .= "\n";
foreach ( $categories as $category ) {
$wikitext .= "\n[[Category:$category]]";
}
}
// Restore templates
foreach ( $templates as $i => $template ) {
$wikitext = str_replace( "@@$i@@", $template, $wikitext );
}
return $wikitext;
}
/**
* @param string $wikitext
* @return array|string|string[]|null
*/
public static function fixSpacing( string $wikitext ) {
// Give block templates some room
$templates = self::getElements( "\n{{", "}}\n", $wikitext );
foreach ( $templates as $template ) {
$wikitext = str_replace( $template, "\n$template\n", $wikitext );
}
// Give tables some room
$tables = self::getElements( "\n{|", "\n|}", $wikitext );
foreach ( $tables as $table ) {
$wikitext = str_replace( $table, "\n$table\n", $wikitext );
}
// Give standalone file links some room
// @todo i18n
$links = self::getElements( "\n[[File:", "]]\n", $wikitext );
foreach ( $links as $link ) {
$wikitext = str_replace( $link, "\n$link\n", $wikitext );
}
// Fix tabs in code blocks
$wikitext = preg_replace( "/^ {8}/m", " \t\t\t\t", $wikitext );
$wikitext = preg_replace( "/^ {6}/m", " \t\t\t", $wikitext );
$wikitext = preg_replace( "/^ {4}/m", " \t\t", $wikitext );
$wikitext = preg_replace( "/^ {2}/m", " \t", $wikitext );
// Fix remaining tabs (for example in <pre> blocks)
// @todo Make more robust
$wikitext = preg_replace( "/ {4}/", "\t", $wikitext );
// Remove excessive spaces
$wikitext = preg_replace( "/ +/", " ", $wikitext );
// Remove trailing spaces
// Exception for code blocks
$wikitext = preg_replace( "/^ $/m", "@@@", $wikitext );
$wikitext = preg_replace( "/ +$/m", "", $wikitext );
// Restore code block
$wikitext = preg_replace( "/^@@@$/m", " ", $wikitext );
// Fix line breaks
$wikitext = preg_replace( "/ *<br ?\/?> */", "<br>", $wikitext );
// Remove excessive newlines
$wikitext = preg_replace( "/^\n\n+/m", "\n", $wikitext );
// Remove leading newlines
$wikitext = preg_replace( "/^\n+/", "", $wikitext );
// Remove trailing newlines
$wikitext = preg_replace( "/\n+$/", "", $wikitext );
return $wikitext;
}
/**
* Helper method to get elements that may have similar elements nested inside
*
* @param string $prefix
* @param string $suffix
* @param string $wikitext
* @return array
*/
public static function getElements( $prefix, $suffix, $wikitext ) {
$elements = [];
$start = strpos( $wikitext, $prefix );
while ( $start !== false ) {
$depth = 0;
for ( $position = $start; $position < strlen( $wikitext ); $position++ ) {
if ( substr( $wikitext, $position, strlen( $prefix ) ) === $prefix ) {
$position += strlen( $prefix ) - 1;
$depth++;
}
if ( substr( $wikitext, $position, strlen( $suffix ) ) === $suffix ) {
$position += strlen( $suffix ) - 1;
$depth--;
}
if ( !$depth ) {
break;
}
}
$end = $position - $start + 1;
$element = substr( $wikitext, $start, $end );
$elements[] = $element;
$start = strpos( $wikitext, $prefix, $start + 1 );
}
return $elements;
}
}