Merge "mw.widgets.CategorySelector: Prevent duplicates"
[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 $this->useTransactionalTimeLimit();
34
35 $params = $this->extractRequestParams();
36 $user = $this->getUser();
37 if ( !$user->isAllowed( 'undelete' ) ) {
38 $this->dieUsageMsg( 'permdenied-undelete' );
39 }
40
41 if ( $user->isBlocked() ) {
42 $this->dieBlocked( $user->getBlock() );
43 }
44
45 $titleObj = Title::newFromText( $params['title'] );
46 if ( !$titleObj || $titleObj->isExternal() ) {
47 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
48 }
49
50 // Convert timestamps
51 if ( !isset( $params['timestamps'] ) ) {
52 $params['timestamps'] = array();
53 }
54 if ( !is_array( $params['timestamps'] ) ) {
55 $params['timestamps'] = array( $params['timestamps'] );
56 }
57 foreach ( $params['timestamps'] as $i => $ts ) {
58 $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
59 }
60
61 $pa = new PageArchive( $titleObj, $this->getConfig() );
62 $retval = $pa->undelete(
63 ( isset( $params['timestamps'] ) ? $params['timestamps'] : array() ),
64 $params['reason'],
65 $params['fileids'],
66 false,
67 $this->getUser()
68 );
69 if ( !is_array( $retval ) ) {
70 $this->dieUsageMsg( 'cannotundelete' );
71 }
72
73 if ( $retval[1] ) {
74 Hooks::run( 'FileUndeleteComplete',
75 array( $titleObj, $params['fileids'], $this->getUser(), $params['reason'] ) );
76 }
77
78 $this->setWatch( $params['watchlist'], $titleObj );
79
80 $info['title'] = $titleObj->getPrefixedText();
81 $info['revisions'] = intval( $retval[0] );
82 $info['fileversions'] = intval( $retval[1] );
83 $info['reason'] = $retval[2];
84 $this->getResult()->addValue( null, $this->getModuleName(), $info );
85 }
86
87 public function mustBePosted() {
88 return true;
89 }
90
91 public function isWriteMode() {
92 return true;
93 }
94
95 public function getAllowedParams() {
96 return array(
97 'title' => array(
98 ApiBase::PARAM_TYPE => 'string',
99 ApiBase::PARAM_REQUIRED => true
100 ),
101 'reason' => '',
102 'timestamps' => array(
103 ApiBase::PARAM_TYPE => 'timestamp',
104 ApiBase::PARAM_ISMULTI => true,
105 ),
106 'fileids' => array(
107 ApiBase::PARAM_TYPE => 'integer',
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 needsToken() {
123 return 'csrf';
124 }
125
126 protected function getExamplesMessages() {
127 return array(
128 'action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page'
129 => 'apihelp-undelete-example-page',
130 'action=undelete&title=Main%20Page&token=123ABC' .
131 '&timestamps=2007-07-03T22:00:45Z|2007-07-02T19:48:56Z'
132 => 'apihelp-undelete-example-revisions',
133 );
134 }
135
136 public function getHelpUrls() {
137 return 'https://www.mediawiki.org/wiki/API:Undelete';
138 }
139 }