3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @ingroup Maintenance
22 require_once( __DIR__
. '/Maintenance.php' );
25 * Maintenance script that deletes all pages in the MediaWiki namespace
26 * of which the content is equal to the system default.
28 * @ingroup Maintenance
30 class DeleteEqualMessages
extends Maintenance
{
31 public function __construct() {
32 parent
::__construct();
33 $this->mDescription
= "Deletes all pages in the MediaWiki namespace that are equal to the default message";
34 $this->addOption( 'delete', 'Actually delete the pages' );
35 $this->addOption( 'delete-talk', 'Don\'t leave orphaned talk pages behind' );
36 $this->addOption( 'lang-code', 'Check for subpages of this lang-code (default: root page against content language)', false, true );
39 public function execute() {
40 global $wgUser, $wgContLang;
42 $doDelete = $this->hasOption( 'delete' );
43 $doDeleteTalk = $this->hasOption( 'delete-talk' );
44 $forLangCode = $this->getOption( 'lang-code' );
47 $langObj = Language
::factory( $forLangCode );
48 $langCode = $langObj->getCode();
51 $langObj = $wgContLang;
52 $langCode = $wgContLang->getCode();
56 $this->output( "Checking for pages with default message..." );
58 /* Based on SpecialAllmessages::reallyDoQuery #filter=modified */
60 $messageNames = Language
::getLocalisationCache()->getSubitemList( 'en', 'messages' );
61 // Normalise message names for NS_MEDIAWIKI page_title
62 $messageNames = array_map( array( $langObj, 'ucfirst' ), $messageNames );
63 // TODO: Do the below for each language code (e.g. delete /xxx subpage if equal to MessagesXxx)
64 // Right now it only takes care of the root override, which is enough since most wikis aren't multi-lang wikis.
65 $statuses = AllmessagesTablePager
::getCustomisedStatuses( $messageNames, $langCode, $nonContLang );
71 foreach ( $messageNames as $key ) {
72 $customised = isset( $statuses['pages'][$key] );
74 $actual = wfMessage( $key )->inLanguage( $langCode )->plain();
75 $default = wfMessage( $key )->inLanguage( $langCode )->useDatabase( false )->plain();
78 if ( $actual === $default ) {
79 $hasTalk = isset( $statuses['talks'][$key] );
82 'hasTalk' => $hasTalk,
92 if ( $equalPages === 0 ) {
93 // No more equal messages left
94 $this->output( "done.\n" );
98 $this->output( "\n{$relevantPages} pages in the MediaWiki namespace override messages." );
99 $this->output( "\n{$equalPages} pages are equal to the default message ({$equalPagesTalks} talk pages).\n" );
102 $this->output( "\nRun the script again with --delete to delete these pages" );
103 if ( $equalPagesTalks !== 0 ) {
104 $this->output( " (include --delete-talk to also delete the talk pages)" );
106 $this->output( "\n" );
110 $user = User
::newFromName( 'MediaWiki default' );
112 $this->error( "Invalid username", true );
116 // Hide deletions from RecentChanges
117 $user->addGroup( 'bot' );
120 $this->output( "\n...deleting equal messages (this may take a long time!)...", 'msg' );
121 $dbw = wfGetDB( DB_MASTER
);
122 foreach ( $results as $result ) {
125 $dbw->begin( __METHOD__
);
126 $title = Title
::makeTitle( NS_MEDIAWIKI
, $result['title'] );
127 $page = WikiPage
::factory( $title );
128 $error = ''; // Passed by ref
129 $page->doDeleteArticle( 'No longer required', false, 0, false, $error, $user );
130 if ( $result['hasTalk'] && $doDeleteTalk ) {
131 $title = Title
::makeTitle( NS_MEDIAWIKI_TALK
, $result['title'] );
132 $page = WikiPage
::factory( $title );
133 $error = ''; // Passed by ref
134 $page->doDeleteArticle( 'Orphaned talk page of no longer required message', false, 0, false, $error, $user );
136 $dbw->commit( __METHOD__
);
138 $this->output( "done!\n", 'msg' );
142 $maintClass = "DeleteEqualMessages";
143 require_once( RUN_MAINTENANCE_IF_MAIN
);