phpcs: More require/include is not a function
[lhc/web/wiklou.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 * @file
22 * @ingroup Maintenance
23 */
24
25 require_once __DIR__ . '/Maintenance.php';
26
27 /**
28 * Maintenance script to rollback all edits by a given user or IP provided
29 * they're the most recent edit.
30 *
31 * @ingroup Maintenance
32 */
33 class RollbackEdits extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->mDescription = "Rollback all edits by a given user or IP provided they're the most recent edit";
37 $this->addOption( 'titles', 'A list of titles, none means all titles where the given user is the most recent', false, true );
38 $this->addOption( 'user', 'A user or IP to rollback all edits for', true, true );
39 $this->addOption( 'summary', 'Edit summary to use', false, true );
40 $this->addOption( 'bot', 'Mark the edits as bot' );
41 }
42
43 public function execute() {
44 $user = $this->getOption( 'user' );
45 $username = User::isIP( $user ) ? $user : User::getCanonicalName( $user );
46 if ( !$username ) {
47 $this->error( 'Invalid username', true );
48 }
49
50 $bot = $this->hasOption( 'bot' );
51 $summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' );
52 $titles = array();
53 $results = array();
54 if ( $this->hasOption( 'titles' ) ) {
55 foreach ( explode( '|', $this->getOption( 'titles' ) ) as $title ) {
56 $t = Title::newFromText( $title );
57 if ( !$t ) {
58 $this->error( 'Invalid title, ' . $title );
59 } else {
60 $titles[] = $t;
61 }
62 }
63 } else {
64 $titles = $this->getRollbackTitles( $user );
65 }
66
67 if ( !$titles ) {
68 $this->output( 'No suitable titles to be rolled back' );
69 return;
70 }
71
72 $doer = User::newFromName( 'Maintenance script' );
73
74 foreach ( $titles as $t ) {
75 $page = WikiPage::factory( $t );
76 $this->output( 'Processing ' . $t->getPrefixedText() . '... ' );
77 if ( !$page->commitRollback( $user, $summary, $bot, $results, $doer ) ) {
78 $this->output( "done\n" );
79 } else {
80 $this->output( "failed\n" );
81 }
82 }
83 }
84
85 /**
86 * Get all pages that should be rolled back for a given user
87 * @param $user String a name to check against rev_user_text
88 * @return array
89 */
90 private function getRollbackTitles( $user ) {
91 $dbr = wfGetDB( DB_SLAVE );
92 $titles = array();
93 $results = $dbr->select(
94 array( 'page', 'revision' ),
95 array( 'page_namespace', 'page_title' ),
96 array( 'page_latest = rev_id', 'rev_user_text' => $user ),
97 __METHOD__
98 );
99 foreach ( $results as $row ) {
100 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
101 }
102 return $titles;
103 }
104 }
105
106 $maintClass = 'RollbackEdits';
107 require_once RUN_MAINTENANCE_IF_MAIN;