Added file old version name sanity check to recordUpload2()
[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 null;
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->getLanguage()->date( $timestamp, true ), $this->getLanguage()->time( $timestamp, true ),
99 wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
100 PROTO_CURRENT
101 ) )
102 ),
103 'comment' => array(
104 'type' => 'text',
105 'label-message' => 'filerevert-comment',
106 'default' => wfMsgForContent( 'filerevert-defaultcomment',
107 $wgContLang->date( $timestamp, false, false ), $wgContLang->time( $timestamp, false, false ) ),
108 )
109 );
110 }
111
112 public function onSubmit( $data ) {
113 $source = $this->page->getFile()->getArchiveVirtualUrl( $this->getRequest()->getText( 'oldimage' ) );
114 $comment = $data['comment'];
115 // TODO: Preserve file properties from database instead of reloading from file
116 return $this->page->getFile()->upload( $source, $comment, $comment );
117 }
118
119 public function onSuccess() {
120 $timestamp = $this->oldFile->getTimestamp();
121 $this->getOutput()->addHTML( wfMsgExt( 'filerevert-success', 'parse', $this->getTitle()->getText(),
122 $this->getLanguage()->date( $timestamp, true ),
123 $this->getLanguage()->time( $timestamp, true ),
124 wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
125 PROTO_CURRENT
126 ) ) );
127 $this->getOutput()->returnToMain( false, $this->getTitle() );
128 }
129
130 protected function getPageTitle() {
131 return wfMsg( 'filerevert', $this->getTitle()->getText() );
132 }
133
134 protected function getDescription() {
135 $this->getOutput()->addBacklinkSubtitle( $this->getTitle() );
136 return '';
137 }
138 }