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.
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.
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
19 * @ingroup Maintenance
22 require_once __DIR__
. '/Maintenance.php';
25 * Maintenance script that deletes all pages in the MediaWiki namespace
26 * of which the content is equal to the system default.
28 * @ingroup Maintenance
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 );
41 * @param string|bool $langCode See --lang-code option.
43 protected function fetchMessageInfo( $langCode, array &$messageInfo ) {
47 $this->output( "\n... fetching message info for language: $langCode" );
50 $this->output( "\n... fetching message info for content language" );
51 $langCode = $wgContLang->getCode();
55 /* Based on SpecialAllmessages::reallyDoQuery #filter=modified */
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 );
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" : '';
66 foreach ( $messageNames as $key ) {
67 $customised = isset( $statuses['pages'][$key] );
69 $actual = wfMessage( $key )->inLanguage( $langCode )->plain();
70 $default = wfMessage( $key )->inLanguage( $langCode )->useDatabase( false )->plain();
72 $messageInfo['relevantPages']++
;
75 // Exclude messages that are empty by default, such as sitenotice, specialpage
76 // summaries and accesskeys.
77 $default !== '' && $default !== '-' &&
80 $hasTalk = isset( $statuses['talks'][$key] );
81 $messageInfo['results'][] = array(
82 'title' => $key . $titleSuffix,
83 'hasTalk' => $hasTalk,
85 $messageInfo['equalPages']++
;
87 $messageInfo['equalPagesTalks']++
;
94 public function execute() {
95 $doDelete = $this->hasOption( 'delete' );
96 $doDeleteTalk = $this->hasOption( 'delete-talk' );
97 $langCode = $this->getOption( 'lang-code' );
100 'relevantPages' => 0,
102 'equalPagesTalks' => 0,
103 'results' => array(),
106 $this->output( 'Checking for pages with default message...' );
108 // Load message information
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 );
117 // Lastly, the base pages in NS_MEDIAWIKI that override
118 // messages in content language
119 $this->fetchMessageInfo( false, $messageInfo );
121 if ( !isset( $langCodes[$langCode] ) ) {
122 $this->error( 'Invalid language code: ' . $langCode, 1 );
124 $this->fetchMessageInfo( $langCode, $messageInfo );
127 $this->fetchMessageInfo( false, $messageInfo );
130 if ( $messageInfo['equalPages'] === 0 ) {
131 // No more equal messages left
132 $this->output( "\ndone.\n" );
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" );
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";
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)" );
154 $this->output( "\n" );
159 $user = User
::newFromName( 'MediaWiki default' );
161 $this->error( "Invalid username", true );
166 // Hide deletions from RecentChanges
167 $user->addGroup( 'bot' );
170 $this->output( "\n...deleting equal messages (this may take a long time!)..." );
171 $dbw = wfGetDB( DB_MASTER
);
172 foreach ( $messageInfo['results'] as $result ) {
175 $dbw->begin( __METHOD__
);
176 $title = Title
::makeTitle( NS_MEDIAWIKI
, $result['title'] );
177 $this->output( "\n* [[$title]]" );
178 $page = WikiPage
::factory( $title );
179 $error = ''; // Passed by ref
180 $page->doDeleteArticle( 'No longer required', false, 0, false, $error, $user );
181 if ( $result['hasTalk'] && $doDeleteTalk ) {
182 $title = Title
::makeTitle( NS_MEDIAWIKI_TALK
, $result['title'] );
183 $this->output( "\n* [[$title]]" );
184 $page = WikiPage
::factory( $title );
185 $error = ''; // Passed by ref
186 $page->doDeleteArticle( 'Orphaned talk page of no longer required message', false, 0, false, $error, $user );
188 $dbw->commit( __METHOD__
);
190 $this->output( "\n\ndone!\n" );
194 $maintClass = "DeleteEqualMessages";
195 require_once RUN_MAINTENANCE_IF_MAIN
;