MediaWiki  REL1_22
deleteEqualMessages.php
Go to the documentation of this file.
00001 <?php
00022 require_once __DIR__ . '/Maintenance.php';
00023 
00030 class DeleteEqualMessages extends Maintenance {
00031     public function __construct() {
00032         parent::__construct();
00033         $this->mDescription = "Deletes all pages in the MediaWiki namespace that are equal to the default message";
00034         $this->addOption( 'delete', 'Actually delete the pages (default: dry run)' );
00035         $this->addOption( 'delete-talk', 'Don\'t leave orphaned talk pages behind during deletion' );
00036         $this->addOption( 'lang-code', 'Check for subpages of this language code (default: root page against content language). ' .
00037             'Use value "*" to run for all mwfile language code subpages (including the base pages that override content language).', false, true );
00038     }
00039 
00043     protected function fetchMessageInfo( $langCode, array &$messageInfo ) {
00044         global $wgContLang;
00045 
00046         if ( $langCode ) {
00047             $this->output( "\n... fetching message info for language: $langCode" );
00048             $nonContLang = true;
00049         } else {
00050             $this->output( "\n... fetching message info for content language" );
00051             $langCode = $wgContLang->getCode();
00052             $nonContLang = false;
00053         }
00054 
00055         /* Based on SpecialAllmessages::reallyDoQuery #filter=modified */
00056 
00057         $l10nCache = Language::getLocalisationCache();
00058         $messageNames = $l10nCache->getSubitemList( 'en', 'messages' );
00059         // Normalise message names for NS_MEDIAWIKI page_title
00060         $messageNames = array_map( array( $wgContLang, 'ucfirst' ), $messageNames );
00061 
00062         $statuses = AllmessagesTablePager::getCustomisedStatuses( $messageNames, $langCode, $nonContLang );
00063         // getCustomisedStatuses is stripping the sub page from the page titles, add it back
00064         $titleSuffix = $nonContLang ? "/$langCode" : '';
00065 
00066         foreach ( $messageNames as $key ) {
00067             $customised = isset( $statuses['pages'][$key] );
00068             if ( $customised ) {
00069                 $actual = wfMessage( $key )->inLanguage( $langCode )->plain();
00070                 $default = wfMessage( $key )->inLanguage( $langCode )->useDatabase( false )->plain();
00071 
00072                 $messageInfo['relevantPages']++;
00073 
00074                 if (
00075                     // Exclude messages that are empty by default, such as sitenotice, specialpage
00076                     // summaries and accesskeys.
00077                     $default !== '' && $default !== '-' &&
00078                         $actual === $default
00079                 ) {
00080                     $hasTalk = isset( $statuses['talks'][$key] );
00081                     $messageInfo['results'][] = array(
00082                         'title' => $key . $titleSuffix,
00083                         'hasTalk' => $hasTalk,
00084                     );
00085                     $messageInfo['equalPages']++;
00086                     if ( $hasTalk ) {
00087                         $messageInfo['equalPagesTalks']++;
00088                     }
00089                 }
00090             }
00091         }
00092     }
00093 
00094     public function execute() {
00095         $doDelete = $this->hasOption( 'delete' );
00096         $doDeleteTalk = $this->hasOption( 'delete-talk' );
00097         $langCode = $this->getOption( 'lang-code' );
00098 
00099         $messageInfo = array(
00100             'relevantPages' => 0,
00101             'equalPages' => 0,
00102             'equalPagesTalks' => 0,
00103             'results' => array(),
00104         );
00105 
00106         $this->output( 'Checking for pages with default message...' );
00107 
00108         // Load message information
00109         if ( $langCode ) {
00110             $langCodes = Language::fetchLanguageNames( null, 'mwfile' );
00111             if ( $langCode === '*' ) {
00112                 // All valid lang-code subpages in NS_MEDIAWIKI that
00113                 // override the messsages in that language
00114                 foreach ( $langCodes as $key => $value ) {
00115                     $this->fetchMessageInfo( $key, $messageInfo );
00116                 }
00117                 // Lastly, the base pages in NS_MEDIAWIKI that override
00118                 // messages in content language
00119                 $this->fetchMessageInfo( false, $messageInfo );
00120             } else {
00121                 if ( !isset( $langCodes[$langCode] ) ) {
00122                     $this->error( 'Invalid language code: ' . $langCode, 1 );
00123                 }
00124                 $this->fetchMessageInfo( $langCode, $messageInfo );
00125             }
00126         } else {
00127             $this->fetchMessageInfo( false, $messageInfo );
00128         }
00129 
00130         if ( $messageInfo['equalPages'] === 0 ) {
00131             // No more equal messages left
00132             $this->output( "\ndone.\n" );
00133             return;
00134         }
00135 
00136         $this->output( "\n{$messageInfo['relevantPages']} pages in the MediaWiki namespace override messages." );
00137         $this->output( "\n{$messageInfo['equalPages']} pages are equal to the default message (+ {$messageInfo['equalPagesTalks']} talk pages).\n" );
00138 
00139         if ( !$doDelete ) {
00140             $list = '';
00141             foreach ( $messageInfo['results'] as $result ) {
00142                 $title = Title::makeTitle( NS_MEDIAWIKI, $result['title'] );
00143                 $list .= "* [[$title]]\n";
00144                 if ( $result['hasTalk'] ) {
00145                     $title = Title::makeTitle( NS_MEDIAWIKI_TALK, $result['title'] );
00146                     $list .= "* [[$title]]\n";
00147                 }
00148             }
00149             $this->output( "\nList:\n$list\nRun the script again with --delete to delete these pages" );
00150             if ( $messageInfo['equalPagesTalks'] !== 0 ) {
00151                 $this->output( " (include --delete-talk to also delete the talk pages)" );
00152             }
00153             $this->output( "\n" );
00154             return;
00155         }
00156 
00157         $user = User::newFromName( 'MediaWiki default' );
00158         if ( !$user ) {
00159             $this->error( "Invalid username", true );
00160         }
00161         global $wgUser;
00162         $wgUser = $user;
00163 
00164         // Hide deletions from RecentChanges
00165         $user->addGroup( 'bot' );
00166 
00167         // Handle deletion
00168         $this->output( "\n...deleting equal messages (this may take a long time!)..." );
00169         $dbw = wfGetDB( DB_MASTER );
00170         foreach ( $messageInfo['results'] as $result ) {
00171             wfWaitForSlaves();
00172             $dbw->ping();
00173             $dbw->begin( __METHOD__ );
00174             $title = Title::makeTitle( NS_MEDIAWIKI, $result['title'] );
00175             $this->output( "\n* [[$title]]" );
00176             $page = WikiPage::factory( $title );
00177             $error = ''; // Passed by ref
00178             $page->doDeleteArticle( 'No longer required', false, 0, false, $error, $user );
00179             if ( $result['hasTalk'] && $doDeleteTalk ) {
00180                 $title = Title::makeTitle( NS_MEDIAWIKI_TALK, $result['title'] );
00181                 $this->output( "\n* [[$title]]" );
00182                 $page = WikiPage::factory( $title );
00183                 $error = ''; // Passed by ref
00184                 $page->doDeleteArticle( 'Orphaned talk page of no longer required message', false, 0, false, $error, $user );
00185             }
00186             $dbw->commit( __METHOD__ );
00187         }
00188         $this->output( "\n\ndone!\n" );
00189     }
00190 }
00191 
00192 $maintClass = "DeleteEqualMessages";
00193 require_once RUN_MAINTENANCE_IF_MAIN;