Improve detection of php binary.
[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 * @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 $doer = User::newFromName( 'Maintenance script' );
66
67 foreach ( $titles as $t ) {
68 $page = WikiPage::factory( $t );
69 $this->output( 'Processing ' . $t->getPrefixedText() . '... ' );
70 if ( !$page->commitRollback( $user, $summary, $bot, $results, $doer ) ) {
71 $this->output( "done\n" );
72 } else {
73 $this->output( "failed\n" );
74 }
75 }
76 }
77
78 /**
79 * Get all pages that should be rolled back for a given user
80 * @param $user String a name to check against rev_user_text
81 * @return array
82 */
83 private function getRollbackTitles( $user ) {
84 $dbr = wfGetDB( DB_SLAVE );
85 $titles = array();
86 $results = $dbr->select(
87 array( 'page', 'revision' ),
88 array( 'page_namespace', 'page_title' ),
89 array( 'page_latest = rev_id', 'rev_user_text' => $user ),
90 __METHOD__
91 );
92 foreach ( $results as $row ) {
93 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
94 }
95 return $titles;
96 }
97 }
98
99 $maintClass = 'RollbackEdits';
100 require_once( RUN_MAINTENANCE_IF_MAIN );