Merge "Whitelist the <wbr> element."
[mediawiki.git] / maintenance / deleteEqualMessages.php
blob1e363639191aa1f5f224162d4c55b4cb8914de1c
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.
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
18 * @file
19 * @ingroup Maintenance
22 require_once __DIR__ . '/Maintenance.php';
24 /**
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 );
40 /**
41 * @param string|bool $langCode See --lang-code option.
43 protected function fetchMessageInfo( $langCode, array &$messageInfo ) {
44 global $wgContLang;
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;
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] );
68 if ( $customised ) {
69 $actual = wfMessage( $key )->inLanguage( $langCode )->plain();
70 $default = wfMessage( $key )->inLanguage( $langCode )->useDatabase( false )->plain();
72 $messageInfo['relevantPages']++;
73 if ( $actual === $default ) {
74 $hasTalk = isset( $statuses['talks'][$key] );
75 $messageInfo['results'][] = array(
76 'title' => $key . $titleSuffix,
77 'hasTalk' => $hasTalk,
79 $messageInfo['equalPages']++;
80 if ( $hasTalk ) {
81 $messageInfo['equalPagesTalks']++;
88 public function execute() {
89 $doDelete = $this->hasOption( 'delete' );
90 $doDeleteTalk = $this->hasOption( 'delete-talk' );
91 $langCode = $this->getOption( 'lang-code' );
93 $messageInfo = array(
94 'relevantPages' => 0,
95 'equalPages' => 0,
96 'equalPagesTalks' => 0,
97 'results' => array(),
100 $this->output( 'Checking for pages with default message...' );
102 // Load message information
103 if ( $langCode ) {
104 $langCodes = Language::fetchLanguageNames( null, 'mwfile' );
105 if ( $langCode === '*' ) {
106 // All valid lang-code subpages in NS_MEDIAWIKI that
107 // override the messsages in that language
108 foreach ( $langCodes as $key => $value ) {
109 $this->fetchMessageInfo( $key, $messageInfo );
111 // Lastly, the base pages in NS_MEDIAWIKI that override
112 // messages in content language
113 $this->fetchMessageInfo( false, $messageInfo );
114 } else {
115 if ( !isset( $langCodes[$langCode] ) ) {
116 $this->error( 'Invalid language code: ' . $langCode, 1 );
118 $this->fetchMessageInfo( $langCode, $messageInfo );
120 } else {
121 $this->fetchMessageInfo( false, $messageInfo );
124 if ( $messageInfo['equalPages'] === 0 ) {
125 // No more equal messages left
126 $this->output( "\ndone.\n" );
127 return;
130 $this->output( "\n{$messageInfo['relevantPages']} pages in the MediaWiki namespace override messages." );
131 $this->output( "\n{$messageInfo['equalPages']} pages are equal to the default message (+ {$messageInfo['equalPagesTalks']} talk pages).\n" );
133 if ( !$doDelete ) {
134 $list = '';
135 foreach ( $messageInfo['results'] as $result ) {
136 $title = Title::makeTitle( NS_MEDIAWIKI, $result['title'] );
137 $list .= "* [[$title]]\n";
138 if ( $result['hasTalk'] ) {
139 $title = Title::makeTitle( NS_MEDIAWIKI_TALK, $result['title'] );
140 $list .= "* [[$title]]\n";
143 $this->output( "\nList:\n$list\nRun the script again with --delete to delete these pages" );
144 if ( $messageInfo['equalPagesTalks'] !== 0 ) {
145 $this->output( " (include --delete-talk to also delete the talk pages)" );
147 $this->output( "\n" );
148 return;
151 $user = User::newFromName( 'MediaWiki default' );
152 if ( !$user ) {
153 $this->error( "Invalid username", true );
155 global $wgUser;
156 $wgUser = $user;
158 // Hide deletions from RecentChanges
159 $user->addGroup( 'bot' );
161 // Handle deletion
162 $this->output( "\n...deleting equal messages (this may take a long time!)..." );
163 $dbw = wfGetDB( DB_MASTER );
164 foreach ( $messageInfo['results'] as $result ) {
165 wfWaitForSlaves();
166 $dbw->ping();
167 $dbw->begin( __METHOD__ );
168 $title = Title::makeTitle( NS_MEDIAWIKI, $result['title'] );
169 $this->output( "\n* [[$title]]" );
170 $page = WikiPage::factory( $title );
171 $error = ''; // Passed by ref
172 $page->doDeleteArticle( 'No longer required', false, 0, false, $error, $user );
173 if ( $result['hasTalk'] && $doDeleteTalk ) {
174 $title = Title::makeTitle( NS_MEDIAWIKI_TALK, $result['title'] );
175 $this->output( "\n* [[$title]]" );
176 $page = WikiPage::factory( $title );
177 $error = ''; // Passed by ref
178 $page->doDeleteArticle( 'Orphaned talk page of no longer required message', false, 0, false, $error, $user );
180 $dbw->commit( __METHOD__ );
182 $this->output( "\n\ndone!\n" );
186 $maintClass = "DeleteEqualMessages";
187 require_once RUN_MAINTENANCE_IF_MAIN;