815606d7f4aa44eca7f9508ba53d7218f6a9ccb7
[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 Action
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 Action
30 */
31 class RevertAction extends Action {
32
33 public function getName() {
34 return 'revert';
35 }
36
37 public function getRestriction() {
38 return 'read';
39 }
40
41 public function show() {
42 $this->getOutput()->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
43 }
44
45 public function execute() {}
46 }
47
48 /**
49 * Class for pages in NS_FILE
50 *
51 * @ingroup Action
52 */
53 class RevertFileAction extends FormAction {
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( $this->getTitle(), $oldimage );
76 if ( !$this->oldFile->exists() ) {
77 throw new ErrorPageError( '', 'filerevert-badversion' );
78 }
79 }
80
81 protected function alterForm( HTMLForm $form ) {
82 $form->setWrapperLegend( wfMsgHtml( 'filerevert-legend' ) );
83 $form->setSubmitText( wfMsg( 'filerevert-submit' ) );
84 $form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
85 }
86
87 protected function getFormFields() {
88 global $wgContLang;
89
90 $timestamp = $this->oldFile->getTimestamp();
91
92 return array(
93 'intro' => array(
94 'type' => 'info',
95 'vertical-label' => true,
96 'raw' => true,
97 'default' => wfMsgExt( 'filerevert-intro', 'parse', $this->getTitle()->getText(),
98 $this->getLang()->date( $timestamp, true ), $this->getLang()->time( $timestamp, true ),
99 wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ) ) )
100 ),
101 'comment' => array(
102 'type' => 'text',
103 'label-message' => 'filerevert-comment',
104 'default' => wfMsgForContent( 'filerevert-defaultcomment',
105 $wgContLang->date( $timestamp, false, false ), $wgContLang->time( $timestamp, false, false ) ),
106 )
107 );
108 }
109
110 public function onSubmit( $data ) {
111 $source = $this->page->getFile()->getArchiveVirtualUrl( $this->getRequest()->getText( 'oldimage' ) );
112 $comment = $data['comment'];
113 // TODO: Preserve file properties from database instead of reloading from file
114 return $this->page->getFile()->upload( $source, $comment, $comment );
115 }
116
117 public function onSuccess() {
118 $timestamp = $this->oldFile->getTimestamp();
119 $this->getOutput()->addHTML( wfMsgExt( 'filerevert-success', 'parse', $this->getTitle()->getText(),
120 $this->getLang()->date( $timestamp, true ),
121 $this->getLang()->time( $timestamp, true ),
122 wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ) ) ) );
123 $this->getOutput()->returnToMain( false, $this->getTitle() );
124 }
125
126 protected function getPageTitle() {
127 return wfMsg( 'filerevert', $this->getTitle()->getText() );
128 }
129
130 protected function getDescription() {
131 return wfMsg(
132 'filerevert-backlink',
133 Linker::linkKnown( $this->getTitle() )
134 );
135 }
136 }