MediaWiki master
deleteEqualMessages.php
Go to the documentation of this file.
1<?php
27
28// @codeCoverageIgnoreStart
29require_once __DIR__ . '/Maintenance.php';
30// @codeCoverageIgnoreEnd
31
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription( 'Deletes all pages in the MediaWiki namespace that are equal to '
42 . 'the default message' );
43 $this->addOption( 'delete', 'Actually delete the pages (default: dry run)' );
44 $this->addOption( 'delete-talk', 'Don\'t leave orphaned talk pages behind during deletion' );
45 $this->addOption( 'lang-code', 'Check for subpages of this language code (default: root '
46 . 'page against content language). Use value "*" to run for all mwfile language code '
47 . 'subpages (including the base pages that override content language).', false, true );
48 }
49
54 protected function fetchMessageInfo( $langCode, array &$messageInfo ) {
55 $services = $this->getServiceContainer();
56 $contLang = $services->getContentLanguage();
57 if ( $langCode ) {
58 $this->output( "\n... fetching message info for language: $langCode" );
59 $nonContentLanguage = true;
60 } else {
61 $this->output( "\n... fetching message info for content language" );
62 $langCode = $contLang->getCode();
63 $nonContentLanguage = false;
64 }
65
66 /* Based on SpecialAllmessages::reallyDoQuery #filter=modified */
67
68 $l10nCache = $services->getLocalisationCache();
69 $messageNames = $l10nCache->getSubitemList( 'en', 'messages' );
70 // Normalise message names for NS_MEDIAWIKI page_title
71 $messageNames = array_map( [ $contLang, 'ucfirst' ], $messageNames );
72
73 $statuses = AllMessagesTablePager::getCustomisedStatuses(
74 $messageNames,
75 $langCode,
76 $nonContentLanguage,
77 $this->getReplicaDB()
78 );
79 // getCustomisedStatuses is stripping the subpage from the page titles, add it back
80 $titleSuffix = $nonContentLanguage ? "/$langCode" : '';
81
82 foreach ( $messageNames as $key ) {
83 $customised = isset( $statuses['pages'][$key] );
84 if ( $customised ) {
85 $actual = wfMessage( $key )->inLanguage( $langCode )->plain();
86 $default = wfMessage( $key )->inLanguage( $langCode )->useDatabase( false )->plain();
87
88 $messageInfo['relevantPages']++;
89
90 if (
91 // Exclude messages that are empty by default, such as sitenotice, specialpage
92 // summaries and accesskeys.
93 $default !== '' && $default !== '-' &&
94 $actual === $default
95 ) {
96 $hasTalk = isset( $statuses['talks'][$key] );
97 $messageInfo['results'][] = [
98 'title' => $key . $titleSuffix,
99 'hasTalk' => $hasTalk,
100 ];
101 $messageInfo['equalPages']++;
102 if ( $hasTalk ) {
103 $messageInfo['equalPagesTalks']++;
104 }
105 }
106 }
107 }
108 }
109
110 public function execute() {
111 $doDelete = $this->hasOption( 'delete' );
112 $doDeleteTalk = $this->hasOption( 'delete-talk' );
113 $langCode = $this->getOption( 'lang-code' );
114
115 $messageInfo = [
116 'relevantPages' => 0,
117 'equalPages' => 0,
118 'equalPagesTalks' => 0,
119 'results' => [],
120 ];
121
122 $this->output( 'Checking for pages with default message...' );
123
124 $services = $this->getServiceContainer();
125 // Load message information
126 if ( $langCode ) {
127 $langCodes = $services->getLanguageNameUtils()
128 ->getLanguageNames( LanguageNameUtils::AUTONYMS, LanguageNameUtils::SUPPORTED );
129 if ( $langCode === '*' ) {
130 // All valid lang-code subpages in NS_MEDIAWIKI that
131 // override the messages in that language
132 foreach ( $langCodes as $key => $value ) {
133 $this->fetchMessageInfo( $key, $messageInfo );
134 }
135 // Lastly, the base pages in NS_MEDIAWIKI that override
136 // messages in content language
137 $this->fetchMessageInfo( false, $messageInfo );
138 } else {
139 if ( !isset( $langCodes[$langCode] ) ) {
140 $this->fatalError( 'Invalid language code: ' . $langCode );
141 }
142 $this->fetchMessageInfo( $langCode, $messageInfo );
143 }
144 } else {
145 $this->fetchMessageInfo( false, $messageInfo );
146 }
147
148 if ( $messageInfo['equalPages'] === 0 ) {
149 // No more equal messages left
150 $this->output( "\ndone.\n" );
151
152 return;
153 }
154
155 $this->output( "\n{$messageInfo['relevantPages']} pages in the MediaWiki namespace "
156 . "override messages." );
157 $this->output( "\n{$messageInfo['equalPages']} pages are equal to the default message "
158 . "(+ {$messageInfo['equalPagesTalks']} talk pages).\n" );
159
160 if ( !$doDelete ) {
161 $list = '';
162 foreach ( $messageInfo['results'] as $result ) {
163 $title = Title::makeTitle( NS_MEDIAWIKI, $result['title'] );
164 $list .= "* [[$title]]\n";
165 if ( $result['hasTalk'] ) {
166 $title = Title::makeTitle( NS_MEDIAWIKI_TALK, $result['title'] );
167 $list .= "* [[$title]]\n";
168 }
169 }
170 $this->output( "\nList:\n$list\nRun the script again with --delete to delete these pages" );
171 if ( $messageInfo['equalPagesTalks'] !== 0 ) {
172 $this->output( " (include --delete-talk to also delete the talk pages)" );
173 }
174 $this->output( "\n" );
175
176 return;
177 }
178
179 $user = User::newSystemUser( 'MediaWiki default', [ 'steal' => true ] );
180 if ( !$user ) {
181 $this->fatalError( "Invalid username" );
182 }
183 StubGlobalUser::setUser( $user );
184
185 // Hide deletions from RecentChanges
186 $userGroupManager = $services->getUserGroupManager();
187 $userGroupManager->addUserToGroup( $user, 'bot' );
188
189 // Handle deletion
190 $this->output( "\n...deleting equal messages (this may take a long time!)..." );
191 $dbw = $this->getPrimaryDB();
192 $wikiPageFactory = $services->getWikiPageFactory();
193 $delPageFactory = $services->getDeletePageFactory();
194 foreach ( $messageInfo['results'] as $result ) {
195 $this->waitForReplication();
196 $dbw->ping();
197 $title = Title::makeTitle( NS_MEDIAWIKI, $result['title'] );
198 $this->output( "\n* [[$title]]" );
199 $page = $wikiPageFactory->newFromTitle( $title );
200 $status = $delPageFactory->newDeletePage( $page, $user )->deleteUnsafe( 'No longer required' );
201 if ( !$status->isOK() ) {
202 $this->output( " (Failed!)" );
203 }
204 if ( $result['hasTalk'] && $doDeleteTalk ) {
205 $title = Title::makeTitle( NS_MEDIAWIKI_TALK, $result['title'] );
206 $this->output( "\n* [[$title]]" );
207 $page = $wikiPageFactory->newFromTitle( $title );
208 $status = $delPageFactory->newDeletePage( $page, $user )
209 ->deleteUnsafe( 'Orphaned talk page of no longer required message' );
210 if ( !$status->isOK() ) {
211 $this->output( " (Failed!)" );
212 }
213 }
214 }
215 $this->output( "\n\ndone!\n" );
216 }
217}
218
219// @codeCoverageIgnoreStart
220$maintClass = DeleteEqualMessages::class;
221require_once RUN_MAINTENANCE_IF_MAIN;
222// @codeCoverageIgnoreEnd
const NS_MEDIAWIKI_TALK
Definition Defines.php:74
const NS_MEDIAWIKI
Definition Defines.php:73
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Maintenance script that deletes all pages in the MediaWiki namespace of which the content is equal to...
__construct()
Default constructor.
execute()
Do the actual work.
fetchMessageInfo( $langCode, array &$messageInfo)
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
A service that provides utilities to do with language names and codes.
Use TablePager for prettified output.
Stub object for the global user ($wgUser) that makes it possible to change the relevant underlying ob...
Represents a title within MediaWiki.
Definition Title.php:78
internal since 1.36
Definition User.php:93