| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
use MediaWiki\Languages\LanguageNameUtils; |
| 23 |
use MediaWiki\Pager\AllMessagesTablePager; |
| 24 |
use MediaWiki\StubObject\StubGlobalUser; |
| 25 |
use MediaWiki\Title\Title; |
| 26 |
use MediaWiki\User\User; |
| 27 |
|
| 28 |
|
| 29 |
require_once __DIR__ . '/Maintenance.php'; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class DeleteEqualMessages extends Maintenance { |
| 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 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 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 |
|
| 67 |
|
| 68 |
$l10nCache = $services->getLocalisationCache(); |
| 69 |
$messageNames = $l10nCache->getSubitemList( 'en', 'messages' ); |
| 70 |
|
| 71 |
$messageNames = array_map( [ $contLang, 'ucfirst' ], $messageNames ); |
| 72 |
|
| 73 |
$statuses = AllMessagesTablePager::getCustomisedStatuses( |
| 74 |
$messageNames, |
| 75 |
$langCode, |
| 76 |
$nonContentLanguage, |
| 77 |
$this->getReplicaDB() |
| 78 |
); |
| 79 |
|
| 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 |
|
| 92 |
|
| 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 |
|
| 126 |
if ( $langCode ) { |
| 127 |
$langCodes = $services->getLanguageNameUtils() |
| 128 |
->getLanguageNames( LanguageNameUtils::AUTONYMS, LanguageNameUtils::SUPPORTED ); |
| 129 |
if ( $langCode === '*' ) { |
| 130 |
|
| 131 |
|
| 132 |
foreach ( $langCodes as $key => $value ) { |
| 133 |
$this->fetchMessageInfo( $key, $messageInfo ); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 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 |
|
| 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 |
|
| 186 |
$userGroupManager = $services->getUserGroupManager(); |
| 187 |
$userGroupManager->addUserToGroup( $user, 'bot' ); |
| 188 |
|
| 189 |
|
| 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 |
|
| 220 |
$maintClass = DeleteEqualMessages::class; |
| 221 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 222 |
|