fix doc grouping for actions
[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->setWrapperLegend( wfMsgHtml( 'filerevert-legend' ) );
79 $form->setSubmitText( wfMsg( '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 return array(
89 'intro' => array(
90 'type' => 'info',
91 'vertical-label' => true,
92 'raw' => true,
93 'default' => wfMsgExt( 'filerevert-intro', 'parse', $this->getTitle()->getText(),
94 $this->getLanguage()->date( $timestamp, true ), $this->getLanguage()->time( $timestamp, true ),
95 wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
96 PROTO_CURRENT
97 ) )
98 ),
99 'comment' => array(
100 'type' => 'text',
101 'label-message' => 'filerevert-comment',
102 'default' => wfMsgForContent( 'filerevert-defaultcomment',
103 $wgContLang->date( $timestamp, false, false ), $wgContLang->time( $timestamp, false, false ) ),
104 )
105 );
106 }
107
108 public function onSubmit( $data ) {
109 $source = $this->page->getFile()->getArchiveVirtualUrl( $this->getRequest()->getText( 'oldimage' ) );
110 $comment = $data['comment'];
111 // TODO: Preserve file properties from database instead of reloading from file
112 return $this->page->getFile()->upload( $source, $comment, $comment );
113 }
114
115 public function onSuccess() {
116 $timestamp = $this->oldFile->getTimestamp();
117 $this->getOutput()->addHTML( wfMsgExt( 'filerevert-success', 'parse', $this->getTitle()->getText(),
118 $this->getLanguage()->date( $timestamp, true ),
119 $this->getLanguage()->time( $timestamp, true ),
120 wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
121 PROTO_CURRENT
122 ) ) );
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 $this->getOutput()->addBacklinkSubtitle( $this->getTitle() );
132 return '';
133 }
134 }