Update formatting
[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 protected $oldFile;
52
53 public function getName() {
54 return 'revert';
55 }
56
57 public function getRestriction() {
58 return 'upload';
59 }
60
61 protected function checkCanExecute( User $user ) {
62 parent::checkCanExecute( $user );
63
64 $oldimage = $this->getRequest()->getText( 'oldimage' );
65 if ( strlen( $oldimage ) < 16
66 || strpos( $oldimage, '/' ) !== false
67 || strpos( $oldimage, '\\' ) !== false
68 ) {
69 throw new ErrorPageError( 'internalerror', 'unexpected', array( 'oldimage', $oldimage ) );
70 }
71
72 $this->oldFile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->getTitle(), $oldimage );
73 if ( !$this->oldFile->exists() ) {
74 throw new ErrorPageError( '', 'filerevert-badversion' );
75 }
76 }
77
78 protected function alterForm( HTMLForm $form ) {
79 $form->setWrapperLegendMsg( 'filerevert-legend' );
80 $form->setSubmitTextMsg( 'filerevert-submit' );
81 $form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
82 }
83
84 protected function getFormFields() {
85 global $wgContLang;
86
87 $timestamp = $this->oldFile->getTimestamp();
88
89 $user = $this->getUser();
90 $lang = $this->getLanguage();
91 $userDate = $lang->userDate( $timestamp, $user );
92 $userTime = $lang->userTime( $timestamp, $user );
93 $siteDate = $wgContLang->date( $timestamp, false, false );
94 $siteTime = $wgContLang->time( $timestamp, false, false );
95
96 return array(
97 'intro' => array(
98 'type' => 'info',
99 'vertical-label' => true,
100 'raw' => true,
101 'default' => $this->msg( 'filerevert-intro',
102 $this->getTitle()->getText(), $userDate, $userTime,
103 wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
104 PROTO_CURRENT ) )->parseAsBlock()
105 ),
106 'comment' => array(
107 'type' => 'text',
108 'label-message' => 'filerevert-comment',
109 'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime
110 )->inContentLanguage()->text()
111 )
112 );
113 }
114
115 public function onSubmit( $data ) {
116 $source = $this->page->getFile()->getArchiveVirtualUrl( $this->getRequest()->getText( 'oldimage' ) );
117 $comment = $data['comment'];
118
119 // TODO: Preserve file properties from database instead of reloading from file
120 return $this->page->getFile()->upload( $source, $comment, $comment, 0, false, false, $this->getUser() );
121 }
122
123 public function onSuccess() {
124 $timestamp = $this->oldFile->getTimestamp();
125 $user = $this->getUser();
126 $lang = $this->getLanguage();
127 $userDate = $lang->userDate( $timestamp, $user );
128 $userTime = $lang->userTime( $timestamp, $user );
129
130 $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
131 $userDate, $userTime,
132 wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
133 PROTO_CURRENT
134 ) );
135 $this->getOutput()->returnToMain( false, $this->getTitle() );
136 }
137
138 protected function getPageTitle() {
139 return $this->msg( 'filerevert', $this->getTitle()->getText() );
140 }
141
142 protected function getDescription() {
143 $this->getOutput()->addBacklinkSubtitle( $this->getTitle() );
144
145 return '';
146 }
147 }