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