API: Give block details along with errors
[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 /**
28 * @ingroup API
29 */
30 class ApiUndelete extends ApiBase {
31
32 public function execute() {
33 $params = $this->extractRequestParams();
34
35 if ( !$this->getUser()->isAllowed( 'undelete' ) ) {
36 $this->dieUsageMsg( 'permdenied-undelete' );
37 }
38
39 if ( $this->getUser()->isBlocked() ) {
40 $this->dieUsage(
41 'You have been blocked from editing',
42 'blocked',
43 0,
44 array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $this->getUser()->getBlock() ) )
45 );
46 }
47
48 $titleObj = Title::newFromText( $params['title'] );
49 if ( !$titleObj || $titleObj->isExternal() ) {
50 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
51 }
52
53 // Convert timestamps
54 if ( !isset( $params['timestamps'] ) ) {
55 $params['timestamps'] = array();
56 }
57 if ( !is_array( $params['timestamps'] ) ) {
58 $params['timestamps'] = array( $params['timestamps'] );
59 }
60 foreach ( $params['timestamps'] as $i => $ts ) {
61 $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
62 }
63
64 $pa = new PageArchive( $titleObj, $this->getConfig() );
65 $retval = $pa->undelete(
66 ( isset( $params['timestamps'] ) ? $params['timestamps'] : array() ),
67 $params['reason'],
68 $params['fileids'],
69 false,
70 $this->getUser()
71 );
72 if ( !is_array( $retval ) ) {
73 $this->dieUsageMsg( 'cannotundelete' );
74 }
75
76 if ( $retval[1] ) {
77 Hooks::run( 'FileUndeleteComplete',
78 array( $titleObj, $params['fileids'], $this->getUser(), $params['reason'] ) );
79 }
80
81 $this->setWatch( $params['watchlist'], $titleObj );
82
83 $info['title'] = $titleObj->getPrefixedText();
84 $info['revisions'] = intval( $retval[0] );
85 $info['fileversions'] = intval( $retval[1] );
86 $info['reason'] = $retval[2];
87 $this->getResult()->addValue( null, $this->getModuleName(), $info );
88 }
89
90 public function mustBePosted() {
91 return true;
92 }
93
94 public function isWriteMode() {
95 return true;
96 }
97
98 public function getAllowedParams() {
99 return array(
100 'title' => array(
101 ApiBase::PARAM_TYPE => 'string',
102 ApiBase::PARAM_REQUIRED => true
103 ),
104 'reason' => '',
105 'timestamps' => array(
106 ApiBase::PARAM_TYPE => 'timestamp',
107 ApiBase::PARAM_ISMULTI => true,
108 ),
109 'fileids' => array(
110 ApiBase::PARAM_TYPE => 'integer',
111 ApiBase::PARAM_ISMULTI => true,
112 ),
113 'watchlist' => array(
114 ApiBase::PARAM_DFLT => 'preferences',
115 ApiBase::PARAM_TYPE => array(
116 'watch',
117 'unwatch',
118 'preferences',
119 'nochange'
120 ),
121 ),
122 );
123 }
124
125 public function needsToken() {
126 return 'csrf';
127 }
128
129 protected function getExamplesMessages() {
130 return array(
131 'action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page'
132 => 'apihelp-undelete-example-page',
133 'action=undelete&title=Main%20Page&token=123ABC' .
134 '&timestamps=2007-07-03T22:00:45Z|2007-07-02T19:48:56Z'
135 => 'apihelp-undelete-example-revisions',
136 );
137 }
138
139 public function getHelpUrls() {
140 return 'https://www.mediawiki.org/wiki/API:Undelete';
141 }
142 }