Merge "Add pp_propname_page index to page_props"
[lhc/web/wiklou.git] / maintenance / deleteEqualMessages.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup Maintenance
20 */
21
22 require_once( __DIR__ . '/Maintenance.php' );
23
24 /**
25 * Maintenance script that deletes all pages in the MediaWiki namespace
26 * of which the content is equal to the system default.
27 *
28 * @ingroup Maintenance
29 */
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 );
37 }
38
39 public function execute() {
40 global $wgUser, $wgContLang;
41
42 $doDelete = $this->hasOption( 'delete' );
43 $doDeleteTalk = $this->hasOption( 'delete-talk' );
44 $forLangCode = $this->getOption( 'lang-code' );
45
46 if ( $forLangCode ) {
47 $langObj = Language::factory( $forLangCode );
48 $langCode = $langObj->getCode();
49 $nonContLang = true;
50 } else {
51 $langObj = $wgContLang;
52 $langCode = $wgContLang->getCode();
53 $nonContLang = false;
54 }
55
56 $this->output( "Checking for pages with default message..." );
57
58 /* Based on SpecialAllmessages::reallyDoQuery #filter=modified */
59
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 );
66
67 $relevantPages = 0;
68 $equalPages = 0;
69 $equalPagesTalks = 0;
70 $results = array();
71 foreach ( $messageNames as $key ) {
72 $customised = isset( $statuses['pages'][$key] );
73 if ( $customised ) {
74 $actual = wfMessage( $key )->inLanguage( $langCode )->plain();
75 $default = wfMessage( $key )->inLanguage( $langCode )->useDatabase( false )->plain();
76
77 $relevantPages++;
78 if ( $actual === $default ) {
79 $hasTalk = isset( $statuses['talks'][$key] );
80 $results[] = array(
81 'title' => $key,
82 'hasTalk' => $hasTalk,
83 );
84 $equalPages++;
85 if ( $hasTalk ) {
86 $equalPagesTalks++;
87 }
88 }
89 }
90 }
91
92 if ( $equalPages === 0 ) {
93 // No more equal messages left
94 $this->output( "done.\n" );
95 return;
96 }
97
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" );
100
101 if ( !$doDelete ) {
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)" );
105 }
106 $this->output( "\n" );
107 return;
108 }
109
110 $user = User::newFromName( 'MediaWiki default' );
111 if ( !$user ) {
112 $this->error( "Invalid username", true );
113 }
114 $wgUser = $user;
115
116 // Hide deletions from RecentChanges
117 $user->addGroup( 'bot' );
118
119 // Handle deletion
120 $this->output( "\n...deleting equal messages (this may take a long time!)...", 'msg' );
121 $dbw = wfGetDB( DB_MASTER );
122 foreach ( $results as $result ) {
123 wfWaitForSlaves();
124 $dbw->ping();
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 );
135 }
136 $dbw->commit( __METHOD__ );
137 }
138 $this->output( "done!\n", 'msg' );
139 }
140 }
141
142 $maintClass = "DeleteEqualMessages";
143 require_once( RUN_MAINTENANCE_IF_MAIN );