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