Merge "I'm in your API classes updating your 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(
73 $this->getTitle(),
74 $oldimage
75 );
76
77 if ( !$this->oldFile->exists() ) {
78 throw new ErrorPageError( '', 'filerevert-badversion' );
79 }
80 }
81
82 protected function alterForm( HTMLForm $form ) {
83 $form->setWrapperLegendMsg( 'filerevert-legend' );
84 $form->setSubmitTextMsg( 'filerevert-submit' );
85 $form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
86 }
87
88 protected function getFormFields() {
89 global $wgContLang;
90
91 $timestamp = $this->oldFile->getTimestamp();
92
93 $user = $this->getUser();
94 $lang = $this->getLanguage();
95 $userDate = $lang->userDate( $timestamp, $user );
96 $userTime = $lang->userTime( $timestamp, $user );
97 $siteDate = $wgContLang->date( $timestamp, false, false );
98 $siteTime = $wgContLang->time( $timestamp, false, false );
99
100 return array(
101 'intro' => array(
102 'type' => 'info',
103 'vertical-label' => true,
104 'raw' => true,
105 'default' => $this->msg( 'filerevert-intro',
106 $this->getTitle()->getText(), $userDate, $userTime,
107 wfExpandUrl(
108 $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
109 PROTO_CURRENT
110 ) )->parseAsBlock()
111 ),
112 'comment' => array(
113 'type' => 'text',
114 'label-message' => 'filerevert-comment',
115 'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime
116 )->inContentLanguage()->text()
117 )
118 );
119 }
120
121 public function onSubmit( $data ) {
122 $source = $this->page->getFile()->getArchiveVirtualUrl(
123 $this->getRequest()->getText( 'oldimage' )
124 );
125 $comment = $data['comment'];
126
127 // TODO: Preserve file properties from database instead of reloading from file
128 return $this->page->getFile()->upload(
129 $source,
130 $comment,
131 $comment,
132 0,
133 false,
134 false,
135 $this->getUser()
136 );
137 }
138
139 public function onSuccess() {
140 $timestamp = $this->oldFile->getTimestamp();
141 $user = $this->getUser();
142 $lang = $this->getLanguage();
143 $userDate = $lang->userDate( $timestamp, $user );
144 $userTime = $lang->userTime( $timestamp, $user );
145
146 $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
147 $userDate, $userTime,
148 wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
149 PROTO_CURRENT
150 ) );
151 $this->getOutput()->returnToMain( false, $this->getTitle() );
152 }
153
154 protected function getPageTitle() {
155 return $this->msg( 'filerevert', $this->getTitle()->getText() );
156 }
157
158 protected function getDescription() {
159 $this->getOutput()->addBacklinkSubtitle( $this->getTitle() );
160
161 return '';
162 }
163 }