Merge "Added a separate error message for mkdir failures"
[lhc/web/wiklou.git] / includes / actions / RevertAction.php
1 <?php
2 /**
3 * File reversion user interface
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 *
19 * @file
20 * @ingroup Actions
21 * @ingroup Media
22 * @author Alexandre Emsenhuber
23 * @author Rob Church <robchur@gmail.com>
24 */
25
26 /**
27 * File reversion user interface
28 *
29 * @ingroup Actions
30 */
31 class RevertAction extends FormAction {
32 /**
33 * @var OldLocalFile
34 */
35 protected $oldFile;
36
37 public function getName() {
38 return 'revert';
39 }
40
41 public function getRestriction() {
42 return 'upload';
43 }
44
45 protected function checkCanExecute( User $user ) {
46 if ( $this->getTitle()->getNamespace() !== NS_FILE ) {
47 throw new ErrorPageError( $this->msg( 'nosuchaction' ), $this->msg( 'nosuchactiontext' ) );
48 }
49 parent::checkCanExecute( $user );
50
51 $oldimage = $this->getRequest()->getText( 'oldimage' );
52 if ( strlen( $oldimage ) < 16
53 || strpos( $oldimage, '/' ) !== false
54 || strpos( $oldimage, '\\' ) !== false
55 ) {
56 throw new ErrorPageError( 'internalerror', 'unexpected', [ 'oldimage', $oldimage ] );
57 }
58
59 $this->oldFile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName(
60 $this->getTitle(),
61 $oldimage
62 );
63
64 if ( !$this->oldFile->exists() ) {
65 throw new ErrorPageError( '', 'filerevert-badversion' );
66 }
67 }
68
69 protected function usesOOUI() {
70 return true;
71 }
72
73 protected function alterForm( HTMLForm $form ) {
74 $form->setWrapperLegendMsg( 'filerevert-legend' );
75 $form->setSubmitTextMsg( 'filerevert-submit' );
76 $form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
77 $form->setTokenSalt( [ 'revert', $this->getTitle()->getPrefixedDBkey() ] );
78 }
79
80 protected function getFormFields() {
81 global $wgContLang;
82
83 $timestamp = $this->oldFile->getTimestamp();
84
85 $user = $this->getUser();
86 $lang = $this->getLanguage();
87 $userDate = $lang->userDate( $timestamp, $user );
88 $userTime = $lang->userTime( $timestamp, $user );
89 $siteTs = MWTimestamp::getLocalInstance( $timestamp );
90 $ts = $siteTs->format( 'YmdHis' );
91 $siteDate = $wgContLang->date( $ts, false, false );
92 $siteTime = $wgContLang->time( $ts, false, false );
93 $tzMsg = $siteTs->getTimezoneMessage()->inContentLanguage()->text();
94
95 return [
96 'intro' => [
97 'type' => 'info',
98 'vertical-label' => true,
99 'raw' => true,
100 'default' => $this->msg( 'filerevert-intro',
101 $this->getTitle()->getText(), $userDate, $userTime,
102 wfExpandUrl(
103 $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
104 PROTO_CURRENT
105 ) )->parseAsBlock()
106 ],
107 'comment' => [
108 'type' => 'text',
109 'label-message' => 'filerevert-comment',
110 'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime,
111 $tzMsg )->inContentLanguage()->text()
112 ]
113 ];
114 }
115
116 public function onSubmit( $data ) {
117 $this->useTransactionalTimeLimit();
118
119 $old = $this->getRequest()->getText( 'oldimage' );
120 $localFile = $this->page->getFile();
121 $oldFile = OldLocalFile::newFromArchiveName( $this->getTitle(), $localFile->getRepo(), $old );
122
123 $source = $localFile->getArchiveVirtualUrl( $old );
124 $comment = $data['comment'];
125
126 if ( $localFile->getSha1() === $oldFile->getSha1() ) {
127 return Status::newFatal( 'filerevert-identical' );
128 }
129
130 // TODO: Preserve file properties from database instead of reloading from file
131 return $localFile->upload(
132 $source,
133 $comment,
134 $comment,
135 0,
136 false,
137 false,
138 $this->getUser()
139 );
140 }
141
142 public function onSuccess() {
143 $timestamp = $this->oldFile->getTimestamp();
144 $user = $this->getUser();
145 $lang = $this->getLanguage();
146 $userDate = $lang->userDate( $timestamp, $user );
147 $userTime = $lang->userTime( $timestamp, $user );
148
149 $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
150 $userDate, $userTime,
151 wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
152 PROTO_CURRENT
153 ) );
154 $this->getOutput()->returnToMain( false, $this->getTitle() );
155 }
156
157 protected function getPageTitle() {
158 return $this->msg( 'filerevert', $this->getTitle()->getText() );
159 }
160
161 protected function getDescription() {
162 return OutputPage::buildBacklinkSubtitle( $this->getTitle() );
163 }
164
165 public function doesWrites() {
166 return true;
167 }
168 }