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