Change layout of the mustBePosted format to standardise it
[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 $titleObj = null;
44 if ( !isset( $params['title'] ) )
45 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
46 if ( !isset( $params['token'] ) )
47 $this->dieUsageMsg( array( 'missingparam', 'token' ) );
48
49 if ( !$wgUser->isAllowed( 'undelete' ) )
50 $this->dieUsageMsg( array( 'permdenied-undelete' ) );
51
52 if ( $wgUser->isBlocked() )
53 $this->dieUsageMsg( array( 'blockedtext' ) );
54
55 if ( !$wgUser->matchEditToken( $params['token'] ) )
56 $this->dieUsageMsg( array( 'sessionfailure' ) );
57
58 $titleObj = Title::newFromText( $params['title'] );
59 if ( !$titleObj )
60 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
61
62 // Convert timestamps
63 if ( !isset( $params['timestamps'] ) )
64 $params['timestamps'] = array();
65 if ( !is_array( $params['timestamps'] ) )
66 $params['timestamps'] = array( $params['timestamps'] );
67 foreach ( $params['timestamps'] as $i => $ts )
68 $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
69
70 $pa = new PageArchive( $titleObj );
71 $dbw = wfGetDB( DB_MASTER );
72 $dbw->begin();
73 $retval = $pa->undelete( ( isset( $params['timestamps'] ) ? $params['timestamps'] : array() ), $params['reason'] );
74 if ( !is_array( $retval ) )
75 $this->dieUsageMsg( array( 'cannotundelete' ) );
76
77 if ( $retval[1] )
78 wfRunHooks( 'FileUndeleteComplete',
79 array( $titleObj, array(), $wgUser, $params['reason'] ) );
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' => null,
99 'token' => null,
100 'reason' => "",
101 'timestamps' => array(
102 ApiBase :: PARAM_ISMULTI => true
103 )
104 );
105 }
106
107 public function getParamDescription() {
108 return array (
109 'title' => 'Title of the page you want to restore.',
110 'token' => 'An undelete token previously retrieved through list=deletedrevs',
111 'reason' => 'Reason for restoring (optional)',
112 'timestamps' => 'Timestamps of the revisions to restore. If not set, all revisions will be restored.'
113 );
114 }
115
116 public function getDescription() {
117 return array(
118 'Restore certain revisions of a deleted page. A list of deleted revisions (including timestamps) can be',
119 'retrieved through list=deletedrevs'
120 );
121 }
122
123 public function getPossibleErrors() {
124 return array_merge( parent::getPossibleErrors(), array(
125 array( 'missingparam', 'title' ),
126 array( 'missingparam', 'token' ),
127 array( 'permdenied-undelete' ),
128 array( 'blockedtext' ),
129 array( 'sessionfailure' ),
130 array( 'invalidtitle', 'title' ),
131 array( 'cannotundelete' ),
132 ) );
133 }
134
135 protected function getExamples() {
136 return array (
137 'api.php?action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page',
138 'api.php?action=undelete&title=Main%20Page&token=123ABC&timestamps=20070703220045|20070702194856'
139 );
140 }
141
142 public function getVersion() {
143 return __CLASS__ . ': $Id$';
144 }
145 }