MediaWiki  master
deleteEqualMessages.php
Go to the documentation of this file.
1 <?php
23 
24 require_once __DIR__ . '/Maintenance.php';
25 
33  public function __construct() {
34  parent::__construct();
35  $this->addDescription( 'Deletes all pages in the MediaWiki namespace that are equal to '
36  . 'the default message' );
37  $this->addOption( 'delete', 'Actually delete the pages (default: dry run)' );
38  $this->addOption( 'delete-talk', 'Don\'t leave orphaned talk pages behind during deletion' );
39  $this->addOption( 'lang-code', 'Check for subpages of this language code (default: root '
40  . 'page against content language). Use value "*" to run for all mwfile language code '
41  . 'subpages (including the base pages that override content language).', false, true );
42  }
43 
48  protected function fetchMessageInfo( $langCode, array &$messageInfo ) {
49  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
50  if ( $langCode ) {
51  $this->output( "\n... fetching message info for language: $langCode" );
52  $nonContentLanguage = true;
53  } else {
54  $this->output( "\n... fetching message info for content language" );
55  $langCode = $contLang->getCode();
56  $nonContentLanguage = false;
57  }
58 
59  /* Based on SpecialAllmessages::reallyDoQuery #filter=modified */
60 
61  $l10nCache = Language::getLocalisationCache();
62  $messageNames = $l10nCache->getSubitemList( 'en', 'messages' );
63  // Normalise message names for NS_MEDIAWIKI page_title
64  $messageNames = array_map( [ $contLang, 'ucfirst' ], $messageNames );
65 
67  $messageNames, $langCode, $nonContentLanguage );
68  // getCustomisedStatuses is stripping the sub page from the page titles, add it back
69  $titleSuffix = $nonContentLanguage ? "/$langCode" : '';
70 
71  foreach ( $messageNames as $key ) {
72  $customised = isset( $statuses['pages'][$key] );
73  if ( $customised ) {
74  $actual = wfMessage( $key )->inLanguage( $langCode )->plain();
75  $default = wfMessage( $key )->inLanguage( $langCode )->useDatabase( false )->plain();
76 
77  $messageInfo['relevantPages']++;
78 
79  if (
80  // Exclude messages that are empty by default, such as sitenotice, specialpage
81  // summaries and accesskeys.
82  $default !== '' && $default !== '-' &&
83  $actual === $default
84  ) {
85  $hasTalk = isset( $statuses['talks'][$key] );
86  $messageInfo['results'][] = [
87  'title' => $key . $titleSuffix,
88  'hasTalk' => $hasTalk,
89  ];
90  $messageInfo['equalPages']++;
91  if ( $hasTalk ) {
92  $messageInfo['equalPagesTalks']++;
93  }
94  }
95  }
96  }
97  }
98 
99  public function execute() {
100  $doDelete = $this->hasOption( 'delete' );
101  $doDeleteTalk = $this->hasOption( 'delete-talk' );
102  $langCode = $this->getOption( 'lang-code' );
103 
104  $messageInfo = [
105  'relevantPages' => 0,
106  'equalPages' => 0,
107  'equalPagesTalks' => 0,
108  'results' => [],
109  ];
110 
111  $this->output( 'Checking for pages with default message...' );
112 
113  // Load message information
114  if ( $langCode ) {
115  $langCodes = Language::fetchLanguageNames( null, 'mwfile' );
116  if ( $langCode === '*' ) {
117  // All valid lang-code subpages in NS_MEDIAWIKI that
118  // override the messsages in that language
119  foreach ( $langCodes as $key => $value ) {
120  $this->fetchMessageInfo( $key, $messageInfo );
121  }
122  // Lastly, the base pages in NS_MEDIAWIKI that override
123  // messages in content language
124  $this->fetchMessageInfo( false, $messageInfo );
125  } else {
126  if ( !isset( $langCodes[$langCode] ) ) {
127  $this->fatalError( 'Invalid language code: ' . $langCode );
128  }
129  $this->fetchMessageInfo( $langCode, $messageInfo );
130  }
131  } else {
132  $this->fetchMessageInfo( false, $messageInfo );
133  }
134 
135  if ( $messageInfo['equalPages'] === 0 ) {
136  // No more equal messages left
137  $this->output( "\ndone.\n" );
138 
139  return;
140  }
141 
142  $this->output( "\n{$messageInfo['relevantPages']} pages in the MediaWiki namespace "
143  . "override messages." );
144  $this->output( "\n{$messageInfo['equalPages']} pages are equal to the default message "
145  . "(+ {$messageInfo['equalPagesTalks']} talk pages).\n" );
146 
147  if ( !$doDelete ) {
148  $list = '';
149  foreach ( $messageInfo['results'] as $result ) {
150  $title = Title::makeTitle( NS_MEDIAWIKI, $result['title'] );
151  $list .= "* [[$title]]\n";
152  if ( $result['hasTalk'] ) {
153  $title = Title::makeTitle( NS_MEDIAWIKI_TALK, $result['title'] );
154  $list .= "* [[$title]]\n";
155  }
156  }
157  $this->output( "\nList:\n$list\nRun the script again with --delete to delete these pages" );
158  if ( $messageInfo['equalPagesTalks'] !== 0 ) {
159  $this->output( " (include --delete-talk to also delete the talk pages)" );
160  }
161  $this->output( "\n" );
162 
163  return;
164  }
165 
166  $user = User::newSystemUser( 'MediaWiki default', [ 'steal' => true ] );
167  if ( !$user ) {
168  $this->fatalError( "Invalid username" );
169  }
170  global $wgUser;
171  $wgUser = $user;
172 
173  // Hide deletions from RecentChanges
174  $user->addGroup( 'bot' );
175 
176  // Handle deletion
177  $this->output( "\n...deleting equal messages (this may take a long time!)..." );
178  $dbw = $this->getDB( DB_MASTER );
179  foreach ( $messageInfo['results'] as $result ) {
180  wfWaitForSlaves();
181  $dbw->ping();
182  $title = Title::makeTitle( NS_MEDIAWIKI, $result['title'] );
183  $this->output( "\n* [[$title]]" );
184  $page = WikiPage::factory( $title );
185  $error = ''; // Passed by ref
186  $success = $page->doDeleteArticle( 'No longer required', false, 0, true, $error, $user );
187  if ( !$success ) {
188  $this->output( " (Failed!)" );
189  }
190  if ( $result['hasTalk'] && $doDeleteTalk ) {
191  $title = Title::makeTitle( NS_MEDIAWIKI_TALK, $result['title'] );
192  $this->output( "\n* [[$title]]" );
193  $page = WikiPage::factory( $title );
194  $error = ''; // Passed by ref
195  $success = $page->doDeleteArticle( 'Orphaned talk page of no longer required message',
196  false, 0, true, $error, $user );
197  if ( !$success ) {
198  $this->output( " (Failed!)" );
199  }
200  }
201  }
202  $this->output( "\n\ndone!\n" );
203  }
204 }
205 
206 $maintClass = DeleteEqualMessages::class;
207 require_once RUN_MAINTENANCE_IF_MAIN;
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:142
static getLocalisationCache()
Get the LocalisationCache instance.
Definition: Language.php:420
static fetchLanguageNames( $inLanguage=self::AS_AUTONYMS, $include='mw')
Get an array of language names, indexed by code.
Definition: Language.php:814
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
$success
fetchMessageInfo( $langCode, array &$messageInfo)
getOption( $name, $default=null)
Get an option, or return the default.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:86
hasOption( $name)
Checks to see if a particular option exists.
const DB_MASTER
Definition: defines.php:26
static getCustomisedStatuses( $messageNames, $langcode='en', $foreign=false)
Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
const NS_MEDIAWIKI_TALK
Definition: Defines.php:69
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
addDescription( $text)
Set the description text.
output( $out, $channel=null)
Throw some output to the user.
const NS_MEDIAWIKI
Definition: Defines.php:68
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
Maintenance script that deletes all pages in the MediaWiki namespace of which the content is equal to...
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:741