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