Followup r64197
[lhc/web/wiklou.git] / includes / api / ApiUndelete.php
1 <?php
2
3 /*
4 * Created on Jul 3, 2007
5 * API for MediaWiki 1.8+
6 *
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
8 *
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.
13 *
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.
18 *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 */
24
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once( "ApiBase.php" );
28 }
29
30 /**
31 * @ingroup API
32 */
33 class ApiUndelete extends ApiBase {
34
35 public function __construct( $main, $action ) {
36 parent::__construct( $main, $action );
37 }
38
39 public function execute() {
40 global $wgUser;
41 $params = $this->extractRequestParams();
42
43 $titleObj = null;
44 if ( !isset( $params['title'] ) ) {
45 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
46 }
47
48 if ( !$wgUser->isAllowed( 'undelete' ) ) {
49 $this->dieUsageMsg( array( 'permdenied-undelete' ) );
50 }
51
52 if ( $wgUser->isBlocked() ) {
53 $this->dieUsageMsg( array( 'blockedtext' ) );
54 }
55
56 $titleObj = Title::newFromText( $params['title'] );
57 if ( !$titleObj ) {
58 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
59 }
60
61 // Convert timestamps
62 if ( !isset( $params['timestamps'] ) ) {
63 $params['timestamps'] = array();
64 }
65 if ( !is_array( $params['timestamps'] ) ) {
66 $params['timestamps'] = array( $params['timestamps'] );
67 }
68 foreach ( $params['timestamps'] as $i => $ts ) {
69 $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
70 }
71
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' ) );
78 }
79
80 if ( $retval[1] ) {
81 wfRunHooks( 'FileUndeleteComplete',
82 array( $titleObj, array(), $wgUser, $params['reason'] ) );
83 }
84
85 $watch = $this->getWatchlistValue( $params['watchlist'], $titleObj );
86
87 if ( $watch !== null ) {
88 if ( $watch ) {
89 $wgUser->addWatch( $titleObj );
90 } else {
91 $wgUser->removeWatch( $titleObj );
92 }
93 }
94
95 $info['title'] = $titleObj->getPrefixedText();
96 $info['revisions'] = intval( $retval[0] );
97 $info['fileversions'] = intval( $retval[1] );
98 $info['reason'] = intval( $retval[2] );
99 $this->getResult()->addValue( null, $this->getModuleName(), $info );
100 }
101
102 public function mustBePosted() {
103 return true;
104 }
105
106 public function isWriteMode() {
107 return true;
108 }
109
110 public function getAllowedParams() {
111 return array(
112 'title' => null,
113 'token' => null,
114 'reason' => '',
115 'timestamps' => array(
116 ApiBase::PARAM_ISMULTI => true
117 ),
118 'watchlist' => array(
119 ApiBase::PARAM_DFLT => 'preferences',
120 ApiBase::PARAM_TYPE => array(
121 'watch',
122 'unwatch',
123 'preferences',
124 'nochange'
125 ),
126 ),
127 );
128 }
129
130 public function getParamDescription() {
131 return array(
132 'title' => 'Title of the page you want to restore.',
133 'token' => 'An undelete token previously retrieved through list=deletedrevs',
134 'reason' => 'Reason for restoring (optional)',
135 'timestamps' => 'Timestamps of the revisions to restore. If not set, all revisions will be restored.',
136 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
137 );
138 }
139
140 public function getDescription() {
141 return array(
142 'Restore certain revisions of a deleted page. A list of deleted revisions (including timestamps) can be',
143 'retrieved through list=deletedrevs'
144 );
145 }
146
147 public function getPossibleErrors() {
148 return array_merge( parent::getPossibleErrors(), array(
149 array( 'missingparam', 'title' ),
150 array( 'permdenied-undelete' ),
151 array( 'blockedtext' ),
152 array( 'invalidtitle', 'title' ),
153 array( 'cannotundelete' ),
154 ) );
155 }
156
157 public function getTokenSalt() {
158 return '';
159 }
160
161 protected function getExamples() {
162 return array(
163 'api.php?action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page',
164 'api.php?action=undelete&title=Main%20Page&token=123ABC&timestamps=20070703220045|20070702194856'
165 );
166 }
167
168 public function getVersion() {
169 return __CLASS__ . ': $Id$';
170 }
171 }