-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppropediaCategories.php
157 lines (125 loc) · 4.08 KB
/
AppropediaCategories.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
<?php
use MediaWiki\MediaWikiServices;
/**
* This class adds pages to maintenance categories
*/
class AppropediaCategories {
public static function onContentAlterParserOutput( Content $content, Title $title, ParserOutput &$output ) {
// Ignore redirects
if ( $title->isRedirect() ) {
return;
}
// The main page is always an exception
if ( $title->isMainPage() ) {
return;
}
// Multiple <h1> titles
$wikitext = $content->getText();
if ( preg_match( '/^=[^=]+=$/m', $wikitext ) ) {
$output->addCategory( 'Pages_with_more_than_one_title' );
}
// Commas in the title
$titleText = $title->getText();
if ( str_contains( $titleText, ',' ) ) {
$output->addCategory( 'Pages_with_commas_in_the_title' );
}
// No real content
if ( !trim( $wikitext ) ) {
$output->addCategory( 'Empty_pages' );
}
// Orphan pages
$parent = $title->getBaseTitle();
if ( !$parent->equals( $title ) && !$parent->exists() ) {
$output->addCategory( 'Pages_with_no_parent' );
}
// Most rules don't apply to automatic translations
$categories = $output->getCategoryNames();
if ( in_array( 'Automatic_translations', $categories ) ) {
return;
}
// Get the lead text
$match = preg_match( '/(.*?)^=/ms', $wikitext, $matches );
$lead = $match ? $matches[1] : $wikitext;
while ( preg_match( '/{{.*?}}/s', $lead ) ) {
$lead = preg_replace( '/{{.*?}}/s', '', $lead ); // Remove templates (fails with nested templates)
}
while ( preg_match( '/\[\[File:.*?\]\]/s', $lead ) ) {
$lead = preg_replace( '/\[\[File:.*?\]\]/s', '', $lead ); // Remove files (fails with nested links)
}
$lead = trim( $lead );
// Per-namespace rules
$namespace = $title->getNamespace();
switch ( $namespace ) {
case NS_MAIN:
// No lead text
if ( !$lead && !$title->isSubpage() ) {
$output->addCategory( 'Pages_with_no_lead_text' );
}
// No main image
$pageImage = $output->getPageProperty( 'page_image_free' );
if ( !$pageImage ) {
$output->addCategory( 'Pages_with_no_main_image' );
}
// Too long
$size = $content->getSize();
if ( $size > 100000 ) {
$output->addCategory( 'Pages_too_long' );
}
// Too short
if ( $size < 1000 ) {
$output->addCategory( 'Stubs' );
}
// Nested templates
if ( preg_match( '/{{[^}]+{{/', $wikitext ) ) {
$output->addCategory( 'Pages_with_nested_templates' );
}
// Sections nested too deep
if ( preg_match( '/\n=====+[^=]+=====+\n/', $wikitext ) ) {
$output->addCategory( 'Pages_with_sections_nested_too_deep' );
}
// Lists nested too deep
if ( preg_match( '/\n[*#][*#][*#]/', $wikitext ) ) {
$output->addCategory( 'Pages_with_lists_nested_too_deep' );
}
// Parser functions
if ( preg_match( '/{{#/', $wikitext ) ) {
$output->addCategory( 'Pages_with_parser_functions' );
}
// Magic words
if ( preg_match( '/__[A-Z]+?__/', $wikitext ) ) {
$output->addCategory( 'Pages_with_magic_words' );
}
// <references> without <ref>
if ( preg_match( '/<references/', $wikitext ) && !preg_match( '/<ref[> ]/', $wikitext ) ) {
$output->addCategory( 'Pages_with_references_tag_but_no_references' );
}
// <ref> without <references>
if ( preg_match( '/<ref[> ]/', $wikitext ) && !preg_match( '/<references/', $wikitext ) ) {
$output->addCategory( 'Pages_with_references_but_no_references_tag' );
}
break;
case NS_CATEGORY:
// Too much text
$size = $content->getSize();
if ( $size > 1000 ) {
$output->addCategory( 'Categories_with_too_much_text' );
}
// Self-contained categories
if ( in_array( str_replace( ' ', '_', $titleText ), $categories ) ) {
$output->addCategory( 'Categories_that_contain_themselves' );
}
break;
}
if ( $title->isTalkPage() ) {
// Talk pages with no subject page
$subject = $title->getSubjectPage();
if ( !$subject->exists() ) {
$output->addCategory( 'Talk_pages_with_no_subject_page' );
}
// Talk pages with lead text
if ( $lead ) {
$output->addCategory( 'Talk_pages_with_lead_text' );
}
}
}
}