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