summaryrefslogtreecommitdiff
path: root/includes/actions/RevertAction.php
blob: f9497f4bc788c44d1ab1246b33926b5970fb9f2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
 * File reversion user interface
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 *
 * @file
 * @ingroup Actions
 * @ingroup Media
 * @author Alexandre Emsenhuber
 * @author Rob Church <robchur@gmail.com>
 */

/**
 * Dummy class for pages not in NS_FILE
 *
 * @ingroup Actions
 */
class RevertAction extends Action {

	public function getName() {
		return 'revert';
	}

	public function show() {
		$this->getOutput()->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
	}

	public function execute() {}
}

/**
 * Class for pages in NS_FILE
 *
 * @ingroup Actions
 */
class RevertFileAction extends FormAction {
	protected $oldFile;

	public function getName() {
		return 'revert';
	}

	public function getRestriction() {
		return 'upload';
	}

	protected function checkCanExecute( User $user ) {
		parent::checkCanExecute( $user );

		$oldimage = $this->getRequest()->getText( 'oldimage' );
		if ( strlen( $oldimage ) < 16
			|| strpos( $oldimage, '/' ) !== false
			|| strpos( $oldimage, '\\' ) !== false )
		{
			throw new ErrorPageError( 'internalerror', 'unexpected', array( 'oldimage', $oldimage ) );
		}

		$this->oldFile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->getTitle(), $oldimage );
		if ( !$this->oldFile->exists() ) {
			throw new ErrorPageError( '', 'filerevert-badversion' );
		}
	}

	protected function alterForm( HTMLForm $form ) {
		$form->setWrapperLegend( wfMsgHtml( 'filerevert-legend' ) );
		$form->setSubmitText( wfMsg( 'filerevert-submit' ) );
		$form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
	}

	protected function getFormFields() {
		global $wgContLang;

		$timestamp = $this->oldFile->getTimestamp();

		return array(
			'intro' => array(
				'type' => 'info',
				'vertical-label' => true,
				'raw' => true,
				'default' => wfMsgExt( 'filerevert-intro', 'parse', $this->getTitle()->getText(),
					$this->getLanguage()->date( $timestamp, true ), $this->getLanguage()->time( $timestamp, true ),
					wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
						PROTO_CURRENT
				) )
			),
			'comment' => array(
				'type' => 'text',
				'label-message' => 'filerevert-comment',
				'default' => wfMsgForContent( 'filerevert-defaultcomment',
					$wgContLang->date( $timestamp, false, false ), $wgContLang->time( $timestamp, false, false ) ),
			)
		);
	}

	public function onSubmit( $data ) {
		$source = $this->page->getFile()->getArchiveVirtualUrl( $this->getRequest()->getText( 'oldimage' ) );
		$comment = $data['comment'];
		// TODO: Preserve file properties from database instead of reloading from file
		return $this->page->getFile()->upload( $source, $comment, $comment );
	}

	public function onSuccess() {
		$timestamp = $this->oldFile->getTimestamp();
		$this->getOutput()->addHTML( wfMsgExt( 'filerevert-success', 'parse', $this->getTitle()->getText(),
			$this->getLanguage()->date( $timestamp, true ),
			$this->getLanguage()->time( $timestamp, true ),
			wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
				PROTO_CURRENT
		) ) );
		$this->getOutput()->returnToMain( false, $this->getTitle() );
	}

	protected function getPageTitle() {
		return wfMsg( 'filerevert', $this->getTitle()->getText() );
	}
	
	protected function getDescription() {
		$this->getOutput()->addBacklinkSubtitle( $this->getTitle() );
		return '';
	}
}