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