]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/FileRevertForm.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.git] / includes / FileRevertForm.php
1 <?php
2
3 /**
4  * File reversion user interface
5  *
6  * @ingroup Media
7  * @author Rob Church <robchur@gmail.com>
8  */
9 class FileRevertForm {
10
11         protected $title = null;
12         protected $file = null;
13         protected $archiveName = '';
14         protected $timestamp = false;
15         protected $oldFile;
16
17         /**
18          * Constructor
19          *
20          * @param $file File we're reverting
21          */
22         public function __construct( $file ) {
23                 $this->title = $file->getTitle();
24                 $this->file = $file;
25         }
26
27         /**
28          * Fulfil the request; shows the form or reverts the file,
29          * pending authentication, confirmation, etc.
30          */
31         public function execute() {
32                 global $wgOut, $wgRequest, $wgUser, $wgLang;
33                 $this->setHeaders();
34
35                 if( wfReadOnly() ) {
36                         $wgOut->readOnlyPage();
37                         return;
38                 } elseif( !$wgUser->isLoggedIn() ) {
39                         $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
40                         return;
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 );
46                         return;
47                 } elseif( $wgUser->isBlocked() ) {
48                         $wgOut->blockedPage();
49                         return;
50                 }
51
52                 $this->archiveName = $wgRequest->getText( 'oldimage' );
53                 $token = $wgRequest->getText( 'wpEditToken' );
54                 if( !$this->isValidOldSpec() ) {
55                         $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars( $this->archiveName ) );
56                         return;
57                 }
58
59                 if( !$this->haveOldVersion() ) {
60                         $wgOut->addHTML( wfMsgExt( 'filerevert-badversion', 'parse' ) );
61                         $wgOut->returnToMain( false, $this->title );
62                         return;
63                 }
64
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 );
77                         } else {
78                                 $wgOut->addWikiText( $status->getWikiText() );
79                         }
80                         return;
81                 }
82
83                 // Show the form
84                 $this->showForm();
85         }
86
87         /**
88          * Show the confirmation form
89          */
90         protected function showForm() {
91                 global $wgOut, $wgUser, $wgLang, $wgContLang;
92                 $timestamp = $this->getTimestamp();
93
94                 $form  = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction() ) );
95                 $form .= Html::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>';
105                 $form .= '</form>';
106
107                 $wgOut->addHTML( $form );
108         }
109
110         /**
111          * Set headers, titles and other bits
112          */
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(
118                         'filerevert-backlink',
119                         $wgUser->getSkin()->link(
120                                 $this->title,
121                                 null,
122                                 array(),
123                                 array(),
124                                 array( 'known', 'noclasses' )
125                         )
126                 ) );
127         }
128
129         /**
130          * Is the provided `oldimage` value valid?
131          *
132          * @return bool
133          */
134         protected function isValidOldSpec() {
135                 return strlen( $this->archiveName ) >= 16
136                         && strpos( $this->archiveName, '/' ) === false
137                         && strpos( $this->archiveName, '\\' ) === false;
138         }
139
140         /**
141          * Does the provided `oldimage` value correspond
142          * to an existing, local, old version of this file?
143          *
144          * @return bool
145          */
146         protected function haveOldVersion() {
147                 return $this->getOldFile()->exists();
148         }
149
150         /**
151          * Prepare the form action
152          *
153          * @return string
154          */
155         protected function getAction() {
156                 $q = array();
157                 $q[] = 'action=revert';
158                 $q[] = 'oldimage=' . urlencode( $this->archiveName );
159                 return $this->title->getLocalUrl( implode( '&', $q ) );
160         }
161
162         /**
163          * Extract the timestamp of the old version
164          *
165          * @return string
166          */
167         protected function getTimestamp() {
168                 if( $this->timestamp === false ) {
169                         $this->timestamp = $this->getOldFile()->getTimestamp();
170                 }
171                 return $this->timestamp;
172         }
173
174         protected function getOldFile() {
175                 if ( !isset( $this->oldFile ) ) {
176                         $this->oldFile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->title, $this->archiveName );
177                 }
178                 return $this->oldFile;
179         }
180 }