Merge "Allow third party code to hook-up MIME type detection"
[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 * Dummy class for pages not in NS_FILE
28 *
29 * @ingroup Actions
30 */
31 class RevertAction extends Action {
32
33 public function getName() {
34 return 'revert';
35 }
36
37 public function show() {
38 $this->getOutput()->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
39 }
40
41 public function execute() {
42 }
43 }
44
45 /**
46 * Class for pages in NS_FILE
47 *
48 * @ingroup Actions
49 */
50 class RevertFileAction extends FormAction {
51 /**
52 * @var OldLocalFile
53 */
54 protected $oldFile;
55
56 public function getName() {
57 return 'revert';
58 }
59
60 public function getRestriction() {
61 return 'upload';
62 }
63
64 protected function checkCanExecute( User $user ) {
65 parent::checkCanExecute( $user );
66
67 $oldimage = $this->getRequest()->getText( 'oldimage' );
68 if ( strlen( $oldimage ) < 16
69 || strpos( $oldimage, '/' ) !== false
70 || strpos( $oldimage, '\\' ) !== false
71 ) {
72 throw new ErrorPageError( 'internalerror', 'unexpected', array( 'oldimage', $oldimage ) );
73 }
74
75 $this->oldFile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName(
76 $this->getTitle(),
77 $oldimage
78 );
79
80 if ( !$this->oldFile->exists() ) {
81 throw new ErrorPageError( '', 'filerevert-badversion' );
82 }
83 }
84
85 protected function alterForm( HTMLForm $form ) {
86 $form->setWrapperLegendMsg( 'filerevert-legend' );
87 $form->setSubmitTextMsg( 'filerevert-submit' );
88 $form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
89 $form->setTokenSalt( array( 'revert', $this->getTitle()->getPrefixedDBkey() ) );
90 }
91
92 protected function getFormFields() {
93 global $wgContLang;
94
95 $timestamp = $this->oldFile->getTimestamp();
96
97 $user = $this->getUser();
98 $lang = $this->getLanguage();
99 $userDate = $lang->userDate( $timestamp, $user );
100 $userTime = $lang->userTime( $timestamp, $user );
101 $siteDate = $wgContLang->date( $timestamp, false, false );
102 $siteTime = $wgContLang->time( $timestamp, false, false );
103
104 return array(
105 'intro' => array(
106 'type' => 'info',
107 'vertical-label' => true,
108 'raw' => true,
109 'default' => $this->msg( 'filerevert-intro',
110 $this->getTitle()->getText(), $userDate, $userTime,
111 wfExpandUrl(
112 $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
113 PROTO_CURRENT
114 ) )->parseAsBlock()
115 ),
116 'comment' => array(
117 'type' => 'text',
118 'label-message' => 'filerevert-comment',
119 'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime
120 )->inContentLanguage()->text()
121 )
122 );
123 }
124
125 public function onSubmit( $data ) {
126 $source = $this->page->getFile()->getArchiveVirtualUrl(
127 $this->getRequest()->getText( 'oldimage' )
128 );
129 $comment = $data['comment'];
130
131 // TODO: Preserve file properties from database instead of reloading from file
132 return $this->page->getFile()->upload(
133 $source,
134 $comment,
135 $comment,
136 0,
137 false,
138 false,
139 $this->getUser()
140 );
141 }
142
143 public function onSuccess() {
144 $timestamp = $this->oldFile->getTimestamp();
145 $user = $this->getUser();
146 $lang = $this->getLanguage();
147 $userDate = $lang->userDate( $timestamp, $user );
148 $userTime = $lang->userTime( $timestamp, $user );
149
150 $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
151 $userDate, $userTime,
152 wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
153 PROTO_CURRENT
154 ) );
155 $this->getOutput()->returnToMain( false, $this->getTitle() );
156 }
157
158 protected function getPageTitle() {
159 return $this->msg( 'filerevert', $this->getTitle()->getText() );
160 }
161
162 protected function getDescription() {
163 $this->getOutput()->addBacklinkSubtitle( $this->getTitle() );
164
165 return '';
166 }
167 }