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