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