Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.64% |
106 / 112 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| DeleteEqualMessages | |
94.64% |
106 / 112 |
|
66.67% |
2 / 3 |
26.10 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| fetchMessageInfo | |
100.00% |
34 / 34 |
|
100.00% |
1 / 1 |
9 | |||
| execute | |
91.43% |
64 / 70 |
|
0.00% |
0 / 1 |
16.16 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | * @ingroup Maintenance |
| 6 | */ |
| 7 | |
| 8 | use MediaWiki\Language\LanguageNameUtils; |
| 9 | use MediaWiki\Maintenance\Maintenance; |
| 10 | use MediaWiki\Pager\AllMessagesTablePager; |
| 11 | use MediaWiki\StubObject\StubGlobalUser; |
| 12 | use MediaWiki\Title\Title; |
| 13 | use MediaWiki\User\User; |
| 14 | |
| 15 | // @codeCoverageIgnoreStart |
| 16 | require_once __DIR__ . '/Maintenance.php'; |
| 17 | // @codeCoverageIgnoreEnd |
| 18 | |
| 19 | /** |
| 20 | * Maintenance script that deletes all pages in the MediaWiki namespace |
| 21 | * of which the content is equal to the system default. |
| 22 | * |
| 23 | * @ingroup Maintenance |
| 24 | */ |
| 25 | class DeleteEqualMessages extends Maintenance { |
| 26 | public function __construct() { |
| 27 | parent::__construct(); |
| 28 | $this->addDescription( 'Deletes all pages in the MediaWiki namespace that are equal to ' |
| 29 | . 'the default message' ); |
| 30 | $this->addOption( 'delete', 'Actually delete the pages (default: dry run)' ); |
| 31 | $this->addOption( 'delete-talk', 'Don\'t leave orphaned talk pages behind during deletion' ); |
| 32 | $this->addOption( 'lang-code', 'Check for subpages of this language code (default: root ' |
| 33 | . 'page against content language). Use value "*" to run for all mwfile language code ' |
| 34 | . 'subpages (including the base pages that override content language).', false, true ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param string|false $langCode See --lang-code option. |
| 39 | * @param array &$messageInfo |
| 40 | */ |
| 41 | protected function fetchMessageInfo( $langCode, array &$messageInfo ) { |
| 42 | $services = $this->getServiceContainer(); |
| 43 | $contLang = $services->getContentLanguage(); |
| 44 | if ( $langCode ) { |
| 45 | $this->output( "\n... fetching message info for language: $langCode" ); |
| 46 | $nonContentLanguage = true; |
| 47 | } else { |
| 48 | $this->output( "\n... fetching message info for content language" ); |
| 49 | $langCode = $contLang->getCode(); |
| 50 | $nonContentLanguage = false; |
| 51 | } |
| 52 | |
| 53 | /* Based on SpecialAllmessages::reallyDoQuery #filter=modified */ |
| 54 | |
| 55 | $l10nCache = $services->getLocalisationCache(); |
| 56 | $messageNames = $l10nCache->getSubitemList( 'en', 'messages' ); |
| 57 | // Normalise message names for NS_MEDIAWIKI page_title |
| 58 | $messageNames = array_map( $contLang->ucfirst( ... ), $messageNames ); |
| 59 | |
| 60 | $statuses = AllMessagesTablePager::getCustomisedStatuses( |
| 61 | $messageNames, |
| 62 | $langCode, |
| 63 | $nonContentLanguage, |
| 64 | $this->getReplicaDB() |
| 65 | ); |
| 66 | // getCustomisedStatuses is stripping the subpage from the page titles, add it back |
| 67 | $titleSuffix = $nonContentLanguage ? "/$langCode" : ''; |
| 68 | |
| 69 | foreach ( $messageNames as $key ) { |
| 70 | $customised = isset( $statuses['pages'][$key] ); |
| 71 | if ( $customised ) { |
| 72 | $actual = wfMessage( $key )->inLanguage( $langCode )->plain(); |
| 73 | $default = wfMessage( $key )->inLanguage( $langCode )->useDatabase( false )->plain(); |
| 74 | |
| 75 | $messageInfo['relevantPages']++; |
| 76 | |
| 77 | if ( |
| 78 | // Exclude messages that are empty by default, such as sitenotice, specialpage |
| 79 | // summaries and accesskeys. |
| 80 | $default !== '' && $default !== '-' && |
| 81 | $actual === $default |
| 82 | ) { |
| 83 | $hasTalk = isset( $statuses['talks'][$key] ); |
| 84 | $messageInfo['results'][] = [ |
| 85 | 'title' => $key . $titleSuffix, |
| 86 | 'hasTalk' => $hasTalk, |
| 87 | ]; |
| 88 | $messageInfo['equalPages']++; |
| 89 | if ( $hasTalk ) { |
| 90 | $messageInfo['equalPagesTalks']++; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public function execute() { |
| 98 | $doDelete = $this->hasOption( 'delete' ); |
| 99 | $doDeleteTalk = $this->hasOption( 'delete-talk' ); |
| 100 | $langCode = $this->getOption( 'lang-code' ); |
| 101 | |
| 102 | $messageInfo = [ |
| 103 | 'relevantPages' => 0, |
| 104 | 'equalPages' => 0, |
| 105 | 'equalPagesTalks' => 0, |
| 106 | 'results' => [], |
| 107 | ]; |
| 108 | |
| 109 | $this->output( 'Checking for pages with default message...' ); |
| 110 | |
| 111 | $services = $this->getServiceContainer(); |
| 112 | // Load message information |
| 113 | if ( $langCode ) { |
| 114 | $langCodes = $services->getLanguageNameUtils() |
| 115 | ->getLanguageNames( LanguageNameUtils::AUTONYMS, LanguageNameUtils::SUPPORTED ); |
| 116 | if ( $langCode === '*' ) { |
| 117 | // All valid lang-code subpages in NS_MEDIAWIKI that |
| 118 | // override the messages 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 | StubGlobalUser::setUser( $user ); |
| 171 | |
| 172 | // Hide deletions from RecentChanges |
| 173 | $userGroupManager = $services->getUserGroupManager(); |
| 174 | $userGroupManager->addUserToGroup( $user, 'bot' ); |
| 175 | |
| 176 | // Handle deletion |
| 177 | $this->output( "\n...deleting equal messages (this may take a long time!)..." ); |
| 178 | $dbw = $this->getPrimaryDB(); |
| 179 | $wikiPageFactory = $services->getWikiPageFactory(); |
| 180 | $delPageFactory = $services->getDeletePageFactory(); |
| 181 | foreach ( $messageInfo['results'] as $result ) { |
| 182 | $this->waitForReplication(); |
| 183 | $dbw->ping(); |
| 184 | $title = Title::makeTitle( NS_MEDIAWIKI, $result['title'] ); |
| 185 | $this->output( "\n* [[$title]]" ); |
| 186 | $page = $wikiPageFactory->newFromTitle( $title ); |
| 187 | $status = $delPageFactory->newDeletePage( $page, $user )->deleteUnsafe( 'No longer required' ); |
| 188 | if ( !$status->isOK() ) { |
| 189 | $this->output( " (Failed!)" ); |
| 190 | } |
| 191 | if ( $result['hasTalk'] && $doDeleteTalk ) { |
| 192 | $title = Title::makeTitle( NS_MEDIAWIKI_TALK, $result['title'] ); |
| 193 | $this->output( "\n* [[$title]]" ); |
| 194 | $page = $wikiPageFactory->newFromTitle( $title ); |
| 195 | $status = $delPageFactory->newDeletePage( $page, $user ) |
| 196 | ->deleteUnsafe( 'Orphaned talk page of no longer required message' ); |
| 197 | if ( !$status->isOK() ) { |
| 198 | $this->output( " (Failed!)" ); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | $this->output( "\n\ndone!\n" ); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // @codeCoverageIgnoreStart |
| 207 | $maintClass = DeleteEqualMessages::class; |
| 208 | require_once RUN_MAINTENANCE_IF_MAIN; |
| 209 | // @codeCoverageIgnoreEnd |