4 * File reversion user interface
7 * @author Rob Church <robchur@gmail.com>
11 protected $title = null;
12 protected $file = null;
13 protected $archiveName = '';
14 protected $timestamp = false;
20 * @param File $file File we're reverting
22 public function __construct( $file ) {
23 $this->title
= $file->getTitle();
28 * Fulfil the request; shows the form or reverts the file,
29 * pending authentication, confirmation, etc.
31 public function execute() {
32 global $wgOut, $wgRequest, $wgUser, $wgLang;
36 $wgOut->readOnlyPage();
38 } elseif( !$wgUser->isLoggedIn() ) {
39 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
41 } elseif( !$this->title
->userCan( 'edit' ) ||
!$this->title
->userCan( 'upload' ) ) {
42 // The standard read-only thing doesn't make a whole lot of sense
43 // here; surely it should show the image or something? -- RC
44 $article = new Article( $this->title
);
45 $wgOut->readOnlyPage( $article->getContent(), true );
47 } elseif( $wgUser->isBlocked() ) {
48 $wgOut->blockedPage();
52 $this->archiveName
= $wgRequest->getText( 'oldimage' );
53 $token = $wgRequest->getText( 'wpEditToken' );
54 if( !$this->isValidOldSpec() ) {
55 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars( $this->archiveName
) );
59 if( !$this->haveOldVersion() ) {
60 $wgOut->addHtml( wfMsgExt( 'filerevert-badversion', 'parse' ) );
61 $wgOut->returnToMain( false, $this->title
);
65 // Perform the reversion if appropriate
66 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->archiveName
) ) {
67 $source = $this->file
->getArchiveVirtualUrl( $this->archiveName
);
68 $comment = $wgRequest->getText( 'wpComment' );
69 // TODO: Preserve file properties from database instead of reloading from file
70 $status = $this->file
->upload( $source, $comment, $comment );
71 if( $status->isGood() ) {
72 $wgOut->addHtml( wfMsgExt( 'filerevert-success', 'parse', $this->title
->getText(),
73 $wgLang->date( $this->getTimestamp(), true ),
74 $wgLang->time( $this->getTimestamp(), true ),
75 wfExpandUrl( $this->file
->getArchiveUrl( $this->archiveName
) ) ) );
76 $wgOut->returnToMain( false, $this->title
);
78 $wgOut->addWikiText( $status->getWikiText() );
88 * Show the confirmation form
90 protected function showForm() {
91 global $wgOut, $wgUser, $wgRequest, $wgLang, $wgContLang;
92 $timestamp = $this->getTimestamp();
94 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction() ) );
95 $form .= Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->archiveName
) );
96 $form .= '<fieldset><legend>' . wfMsgHtml( 'filerevert-legend' ) . '</legend>';
97 $form .= wfMsgExt( 'filerevert-intro', 'parse', $this->title
->getText(),
98 $wgLang->date( $timestamp, true ), $wgLang->time( $timestamp, true ),
99 wfExpandUrl( $this->file
->getArchiveUrl( $this->archiveName
) ) );
100 $form .= '<p>' . Xml
::inputLabel( wfMsg( 'filerevert-comment' ), 'wpComment', 'wpComment',
101 60, wfMsgForContent( 'filerevert-defaultcomment',
102 $wgContLang->date( $timestamp, false, false ), $wgContLang->time( $timestamp, false, false ) ) ) . '</p>';
103 $form .= '<p>' . Xml
::submitButton( wfMsg( 'filerevert-submit' ) ) . '</p>';
104 $form .= '</fieldset>';
107 $wgOut->addHtml( $form );
111 * Set headers, titles and other bits
113 protected function setHeaders() {
114 global $wgOut, $wgUser;
115 $wgOut->setPageTitle( wfMsg( 'filerevert', $this->title
->getText() ) );
116 $wgOut->setRobotPolicy( 'noindex,nofollow' );
117 $wgOut->setSubtitle( wfMsg( 'filerevert-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title
) ) );
121 * Is the provided `oldimage` value valid?
125 protected function isValidOldSpec() {
126 return strlen( $this->archiveName
) >= 16
127 && strpos( $this->archiveName
, '/' ) === false
128 && strpos( $this->archiveName
, '\\' ) === false;
132 * Does the provided `oldimage` value correspond
133 * to an existing, local, old version of this file?
137 protected function haveOldVersion() {
138 return $this->getOldFile()->exists();
142 * Prepare the form action
146 protected function getAction() {
148 $q[] = 'action=revert';
149 $q[] = 'oldimage=' . urlencode( $this->archiveName
);
150 return $this->title
->getLocalUrl( implode( '&', $q ) );
154 * Extract the timestamp of the old version
158 protected function getTimestamp() {
159 if( $this->timestamp
=== false ) {
160 $this->timestamp
= $this->getOldFile()->getTimestamp();
162 return $this->timestamp
;
165 protected function getOldFile() {
166 if ( !isset( $this->oldFile
) ) {
167 $this->oldFile
= RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $this->title
, $this->archiveName
);
169 return $this->oldFile
;