(Bug 25138) ApiUndelete begins a transaction, but does not commit.
[lhc/web/wiklou.git] / includes / api / ApiUndelete.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on Jul 3, 2007
6 *
7 * Copyright © 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 * 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 $titleObj = null;
46
47 if ( !$wgUser->isAllowed( 'undelete' ) ) {
48 $this->dieUsageMsg( array( 'permdenied-undelete' ) );
49 }
50
51 if ( $wgUser->isBlocked() ) {
52 $this->dieUsageMsg( array( 'blockedtext' ) );
53 }
54
55 $titleObj = Title::newFromText( $params['title'] );
56 if ( !$titleObj ) {
57 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
58 }
59
60 // Convert timestamps
61 if ( !isset( $params['timestamps'] ) ) {
62 $params['timestamps'] = array();
63 }
64 if ( !is_array( $params['timestamps'] ) ) {
65 $params['timestamps'] = array( $params['timestamps'] );
66 }
67 foreach ( $params['timestamps'] as $i => $ts ) {
68 $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
69 }
70
71 $pa = new PageArchive( $titleObj );
72 $retval = $pa->undelete( ( isset( $params['timestamps'] ) ? $params['timestamps'] : array() ), $params['reason'] );
73 if ( !is_array( $retval ) ) {
74 $this->dieUsageMsg( array( 'cannotundelete' ) );
75 }
76
77 if ( $retval[1] ) {
78 wfRunHooks( 'FileUndeleteComplete',
79 array( $titleObj, array(), $wgUser, $params['reason'] ) );
80 }
81
82 $this->setWatch( $params['watchlist'], $titleObj );
83
84 $info['title'] = $titleObj->getPrefixedText();
85 $info['revisions'] = intval( $retval[0] );
86 $info['fileversions'] = intval( $retval[1] );
87 $info['reason'] = intval( $retval[2] );
88 $this->getResult()->addValue( null, $this->getModuleName(), $info );
89 }
90
91 public function mustBePosted() {
92 return true;
93 }
94
95 public function isWriteMode() {
96 return true;
97 }
98
99 public function getAllowedParams() {
100 return array(
101 'title' => array(
102 ApiBase::PARAM_TYPE => 'string',
103 ApiBase::PARAM_REQUIRED => true
104 ),
105 'token' => null,
106 'reason' => '',
107 'timestamps' => array(
108 ApiBase::PARAM_ISMULTI => true
109 ),
110 'watchlist' => array(
111 ApiBase::PARAM_DFLT => 'preferences',
112 ApiBase::PARAM_TYPE => array(
113 'watch',
114 'unwatch',
115 'preferences',
116 'nochange'
117 ),
118 ),
119 );
120 }
121
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',
129 );
130 }
131
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'
136 );
137 }
138
139 public function getPossibleErrors() {
140 return array_merge( parent::getPossibleErrors(), array(
141 array( 'permdenied-undelete' ),
142 array( 'blockedtext' ),
143 array( 'invalidtitle', 'title' ),
144 array( 'cannotundelete' ),
145 ) );
146 }
147
148 public function getTokenSalt() {
149 return '';
150 }
151
152 protected function getExamples() {
153 return array(
154 'api.php?action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page',
155 'api.php?action=undelete&title=Main%20Page&token=123ABC&timestamps=20070703220045|20070702194856'
156 );
157 }
158
159 public function getVersion() {
160 return __CLASS__ . ': $Id$';
161 }
162 }