]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/rollbackEdits.php
MediaWiki 1.17.1-scripts
[autoinstalls/mediawiki.git] / maintenance / rollbackEdits.php
1 <?php
2 /**
3  * Rollback all edits by a given user or IP provided they're the most
4  * recent edit (just like real rollback)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  * http://www.gnu.org/copyleft/gpl.html
20  *
21  * @ingroup Maintenance
22  */
23
24 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
25
26 class RollbackEdits extends Maintenance {
27         public function __construct() {
28                 parent::__construct();
29                 $this->mDescription = "Rollback all edits by a given user or IP provided they're the most recent edit";
30                 $this->addOption( 'titles', 'A list of titles, none means all titles where the given user is the most recent', false, true );
31                 $this->addOption( 'user', 'A user or IP to rollback all edits for', true, true );
32                 $this->addOption( 'summary', 'Edit summary to use', false, true );
33                 $this->addOption( 'bot', 'Mark the edits as bot' );
34         }
35
36         public function execute() {
37                 $user = $this->getOption( 'user' );
38                 $username = User::isIP( $user ) ? $user : User::getCanonicalName( $user );
39                 if ( !$username ) {
40                         $this->error( 'Invalid username', true );
41                 }
42
43                 $bot = $this->hasOption( 'bot' );
44                 $summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' );
45                 $titles = array();
46                 $results = array();
47                 if ( $this->hasOption( 'titles' ) ) {
48                         foreach ( explode( '|', $this->getOption( 'titles' ) ) as $title ) {
49                                 $t = Title::newFromText( $title );
50                                 if ( !$t ) {
51                                         $this->error( 'Invalid title, ' . $title );
52                                 } else {
53                                         $titles[] = $t;
54                                 }
55                         }
56                 } else {
57                         $titles = $this->getRollbackTitles( $user );
58                 }
59
60                 if ( !$titles ) {
61                         $this->output( 'No suitable titles to be rolled back' );
62                         return;
63                 }
64
65                 foreach ( $titles as $t ) {
66                         $a = new Article( $t );
67                         $this->output( 'Processing ' . $t->getPrefixedText() . '...' );
68                         if ( !$a->commitRollback( $user, $summary, $bot, $results ) ) {
69                                 $this->output( "done\n" );
70                         } else {
71                                 $this->output( "failed\n" );
72                         }
73                 }
74         }
75
76         /**
77          * Get all pages that should be rolled back for a given user
78          * @param $user String a name to check against rev_user_text
79          */
80         private function getRollbackTitles( $user ) {
81                 $dbr = wfGetDB( DB_SLAVE );
82                 $titles = array();
83                 $results = $dbr->select(
84                         array( 'page', 'revision' ),
85                         array( 'page_namespace', 'page_title' ),
86                         array( 'page_latest = rev_id', 'rev_user_text' => $user ),
87                         __METHOD__
88                 );
89                 foreach ( $results as $row ) {
90                         $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
91                 }
92                 return $titles;
93         }
94 }
95
96 $maintClass = 'RollbackEdits';
97 require_once( RUN_MAINTENANCE_IF_MAIN );