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, $wgServer;
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 $wgServer . $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, $wgServer;
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 ), $wgServer . $this->file
->getArchiveUrl( $this->oldimage
) );
98 $form .= '<p>' . Xml
::inputLabel( wfMsg( 'filerevert-comment' ), 'wpComment', 'wpComment',
99 60, wfMsgForContent( 'filerevert-defaultcomment',
100 $wgContLang->date( $timestamp, false, false ), $wgContLang->time( $timestamp, false, false ) ) ) . '</p>';
101 $form .= '<p>' . Xml
::submitButton( wfMsg( 'filerevert-submit' ) ) . '</p>';
102 $form .= '</fieldset>';
105 $wgOut->addHtml( $form );
109 * Set headers, titles and other bits
111 private function setHeaders() {
112 global $wgOut, $wgUser;
113 $wgOut->setPageTitle( wfMsg( 'filerevert', $this->title
->getText() ) );
114 $wgOut->setRobotPolicy( 'noindex,nofollow' );
115 $wgOut->setSubtitle( wfMsg( 'filerevert-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title
) ) );
119 * Is the provided `oldimage` value valid?
123 private function isValidOldSpec() {
124 return strlen( $this->oldimage
) >= 16
125 && strpos( $this->oldimage
, '/' ) === false
126 && strpos( $this->oldimage
, '\\' ) === false;
130 * Does the provided `oldimage` value correspond
131 * to an existing, local, old version of this file?
135 private function haveOldVersion() {
136 $file = wfFindFile( $this->title
, $this->oldimage
);
137 return $file && $file->exists() && $file->isLocal();
141 * Prepare the form action
145 private function getAction() {
147 $q[] = 'action=revert';
148 $q[] = 'oldimage=' . urlencode( $this->oldimage
);
149 return $this->title
->getLocalUrl( implode( '&', $q ) );
153 * Extract the timestamp of the old version
157 private function getTimestamp() {
158 if( $this->timestamp
=== false ) {
159 $file = RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $this->title
, $this->oldimage
);
160 $this->timestamp
= $file->getTimestamp();
162 return $this->timestamp
;