Make the deprecation notice actually useful by listing the class
[lhc/web/wiklou.git] / maintenance / deleteEqualMessages.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Maintenance
20 */
21
22 require_once __DIR__ . '/Maintenance.php';
23
24 /**
25 * Maintenance script that deletes all pages in the MediaWiki namespace
26 * of which the content is equal to the system default.
27 *
28 * @ingroup Maintenance
29 */
30 class DeleteEqualMessages extends Maintenance {
31 public function __construct() {
32 parent::__construct();
33 $this->mDescription = "Deletes all pages in the MediaWiki namespace that are equal to the default message";
34 $this->addOption( 'delete', 'Actually delete the pages (default: dry run)' );
35 $this->addOption( 'delete-talk', 'Don\'t leave orphaned talk pages behind during deletion' );
36 $this->addOption( 'lang-code', 'Check for subpages of this language code (default: root page against content language). ' .
37 'Use value "*" to run for all mwfile language code subpages (including the base pages that override content language).', false, true );
38 }
39
40 /**
41 * @param string|bool $langCode See --lang-code option.
42 */
43 protected function fetchMessageInfo( $langCode, array &$messageInfo ) {
44 global $wgContLang;
45
46 if ( $langCode ) {
47 $this->output( "\n... fetching message info for language: $langCode" );
48 $nonContLang = true;
49 } else {
50 $this->output( "\n... fetching message info for content language" );
51 $langCode = $wgContLang->getCode();
52 $nonContLang = false;
53 }
54
55 /* Based on SpecialAllmessages::reallyDoQuery #filter=modified */
56
57 $l10nCache = Language::getLocalisationCache();
58 $messageNames = $l10nCache->getSubitemList( 'en', 'messages' );
59 // Normalise message names for NS_MEDIAWIKI page_title
60 $messageNames = array_map( array( $wgContLang, 'ucfirst' ), $messageNames );
61
62 $statuses = AllMessagesTablePager::getCustomisedStatuses( $messageNames, $langCode, $nonContLang );
63 // getCustomisedStatuses is stripping the sub page from the page titles, add it back
64 $titleSuffix = $nonContLang ? "/$langCode" : '';
65
66 foreach ( $messageNames as $key ) {
67 $customised = isset( $statuses['pages'][$key] );
68 if ( $customised ) {
69 $actual = wfMessage( $key )->inLanguage( $langCode )->plain();
70 $default = wfMessage( $key )->inLanguage( $langCode )->useDatabase( false )->plain();
71
72 $messageInfo['relevantPages']++;
73
74 if (
75 // Exclude messages that are empty by default, such as sitenotice, specialpage
76 // summaries and accesskeys.
77 $default !== '' && $default !== '-' &&
78 $actual === $default
79 ) {
80 $hasTalk = isset( $statuses['talks'][$key] );
81 $messageInfo['results'][] = array(
82 'title' => $key . $titleSuffix,
83 'hasTalk' => $hasTalk,
84 );
85 $messageInfo['equalPages']++;
86 if ( $hasTalk ) {
87 $messageInfo['equalPagesTalks']++;
88 }
89 }
90 }
91 }
92 }
93
94 public function execute() {
95 $doDelete = $this->hasOption( 'delete' );
96 $doDeleteTalk = $this->hasOption( 'delete-talk' );
97 $langCode = $this->getOption( 'lang-code' );
98
99 $messageInfo = array(
100 'relevantPages' => 0,
101 'equalPages' => 0,
102 'equalPagesTalks' => 0,
103 'results' => array(),
104 );
105
106 $this->output( 'Checking for pages with default message...' );
107
108 // Load message information
109 if ( $langCode ) {
110 $langCodes = Language::fetchLanguageNames( null, 'mwfile' );
111 if ( $langCode === '*' ) {
112 // All valid lang-code subpages in NS_MEDIAWIKI that
113 // override the messsages in that language
114 foreach ( $langCodes as $key => $value ) {
115 $this->fetchMessageInfo( $key, $messageInfo );
116 }
117 // Lastly, the base pages in NS_MEDIAWIKI that override
118 // messages in content language
119 $this->fetchMessageInfo( false, $messageInfo );
120 } else {
121 if ( !isset( $langCodes[$langCode] ) ) {
122 $this->error( 'Invalid language code: ' . $langCode, 1 );
123 }
124 $this->fetchMessageInfo( $langCode, $messageInfo );
125 }
126 } else {
127 $this->fetchMessageInfo( false, $messageInfo );
128 }
129
130 if ( $messageInfo['equalPages'] === 0 ) {
131 // No more equal messages left
132 $this->output( "\ndone.\n" );
133
134 return;
135 }
136
137 $this->output( "\n{$messageInfo['relevantPages']} pages in the MediaWiki namespace override messages." );
138 $this->output( "\n{$messageInfo['equalPages']} pages are equal to the default message (+ {$messageInfo['equalPagesTalks']} talk pages).\n" );
139
140 if ( !$doDelete ) {
141 $list = '';
142 foreach ( $messageInfo['results'] as $result ) {
143 $title = Title::makeTitle( NS_MEDIAWIKI, $result['title'] );
144 $list .= "* [[$title]]\n";
145 if ( $result['hasTalk'] ) {
146 $title = Title::makeTitle( NS_MEDIAWIKI_TALK, $result['title'] );
147 $list .= "* [[$title]]\n";
148 }
149 }
150 $this->output( "\nList:\n$list\nRun the script again with --delete to delete these pages" );
151 if ( $messageInfo['equalPagesTalks'] !== 0 ) {
152 $this->output( " (include --delete-talk to also delete the talk pages)" );
153 }
154 $this->output( "\n" );
155
156 return;
157 }
158
159 $user = User::newFromName( 'MediaWiki default' );
160 if ( !$user ) {
161 $this->error( "Invalid username", true );
162 }
163 global $wgUser;
164 $wgUser = $user;
165
166 // Hide deletions from RecentChanges
167 $user->addGroup( 'bot' );
168
169 // Handle deletion
170 $this->output( "\n...deleting equal messages (this may take a long time!)..." );
171 $dbw = wfGetDB( DB_MASTER );
172 foreach ( $messageInfo['results'] as $result ) {
173 wfWaitForSlaves();
174 $dbw->ping();
175 $title = Title::makeTitle( NS_MEDIAWIKI, $result['title'] );
176 $this->output( "\n* [[$title]]" );
177 $page = WikiPage::factory( $title );
178 $error = ''; // Passed by ref
179 $page->doDeleteArticle( 'No longer required', false, 0, false, $error, $user );
180 if ( $result['hasTalk'] && $doDeleteTalk ) {
181 $title = Title::makeTitle( NS_MEDIAWIKI_TALK, $result['title'] );
182 $this->output( "\n* [[$title]]" );
183 $page = WikiPage::factory( $title );
184 $error = ''; // Passed by ref
185 $page->doDeleteArticle( 'Orphaned talk page of no longer required message', false, 0, false, $error, $user );
186 }
187 }
188 $this->output( "\n\ndone!\n" );
189 }
190 }
191
192 $maintClass = "DeleteEqualMessages";
193 require_once RUN_MAINTENANCE_IF_MAIN;