Add missing global $wgUser
[mediawiki.git] / includes / api / ApiUndelete.php
blob89f4a71c75d2b149f54e3d1ed37353bc8437fe83
1 <?php
3 /*
4 * Created on Jul 3, 2007
5 * API for MediaWiki 1.8+
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once( "ApiBase.php" );
30 /**
31 * @ingroup API
33 class ApiUndelete extends ApiBase {
35 public function __construct( $main, $action ) {
36 parent::__construct( $main, $action );
39 public function execute() {
40 global $wgUser;
41 $params = $this->extractRequestParams();
43 $titleObj = null;
44 if ( !isset( $params['title'] ) ) {
45 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
48 if ( !$wgUser->isAllowed( 'undelete' ) ) {
49 $this->dieUsageMsg( array( 'permdenied-undelete' ) );
52 if ( $wgUser->isBlocked() ) {
53 $this->dieUsageMsg( array( 'blockedtext' ) );
56 $titleObj = Title::newFromText( $params['title'] );
57 if ( !$titleObj ) {
58 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
61 // Convert timestamps
62 if ( !isset( $params['timestamps'] ) ) {
63 $params['timestamps'] = array();
65 if ( !is_array( $params['timestamps'] ) ) {
66 $params['timestamps'] = array( $params['timestamps'] );
68 foreach ( $params['timestamps'] as $i => $ts ) {
69 $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
72 $pa = new PageArchive( $titleObj );
73 $dbw = wfGetDB( DB_MASTER );
74 $dbw->begin();
75 $retval = $pa->undelete( ( isset( $params['timestamps'] ) ? $params['timestamps'] : array() ), $params['reason'] );
76 if ( !is_array( $retval ) ) {
77 $this->dieUsageMsg( array( 'cannotundelete' ) );
80 if ( $retval[1] ) {
81 wfRunHooks( 'FileUndeleteComplete',
82 array( $titleObj, array(), $wgUser, $params['reason'] ) );
85 $this->setWatch( $params['watchlist'], $titleObj );
87 $info['title'] = $titleObj->getPrefixedText();
88 $info['revisions'] = intval( $retval[0] );
89 $info['fileversions'] = intval( $retval[1] );
90 $info['reason'] = intval( $retval[2] );
91 $this->getResult()->addValue( null, $this->getModuleName(), $info );
94 public function mustBePosted() {
95 return true;
98 public function isWriteMode() {
99 return true;
102 public function getAllowedParams() {
103 return array(
104 'title' => null,
105 'token' => null,
106 'reason' => '',
107 'timestamps' => array(
108 ApiBase::PARAM_ISMULTI => true
110 'watchlist' => array(
111 ApiBase::PARAM_DFLT => 'preferences',
112 ApiBase::PARAM_TYPE => array(
113 'watch',
114 'unwatch',
115 'preferences',
116 'nochange'
122 public function getParamDescription() {
123 return array(
124 'title' => 'Title of the page you want to restore',
125 'token' => 'An undelete token previously retrieved through list=deletedrevs',
126 'reason' => 'Reason for restoring (optional)',
127 'timestamps' => 'Timestamps of the revisions to restore. If not set, all revisions will be restored.',
128 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
132 public function getDescription() {
133 return array(
134 'Restore certain revisions of a deleted page. A list of deleted revisions (including timestamps) can be',
135 'retrieved through list=deletedrevs'
139 public function getPossibleErrors() {
140 return array_merge( parent::getPossibleErrors(), array(
141 array( 'missingparam', 'title' ),
142 array( 'permdenied-undelete' ),
143 array( 'blockedtext' ),
144 array( 'invalidtitle', 'title' ),
145 array( 'cannotundelete' ),
146 ) );
149 public function getTokenSalt() {
150 return '';
153 protected function getExamples() {
154 return array(
155 'api.php?action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page',
156 'api.php?action=undelete&title=Main%20Page&token=123ABC&timestamps=20070703220045|20070702194856'
160 public function getVersion() {
161 return __CLASS__ . ': $Id$';