2007-07-29 22:00:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* File reversion user interface
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Media
|
2007-07-29 22:00:46 +00:00
|
|
|
* @author Rob Church <robchur@gmail.com>
|
|
|
|
|
*/
|
|
|
|
|
class FileRevertForm {
|
|
|
|
|
|
2008-04-05 16:46:22 +00:00
|
|
|
protected $title = null;
|
|
|
|
|
protected $file = null;
|
|
|
|
|
protected $archiveName = '';
|
|
|
|
|
protected $timestamp = false;
|
|
|
|
|
protected $oldFile;
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-29 22:00:46 +00:00
|
|
|
/**
|
|
|
|
|
* Constructor
|
|
|
|
|
*
|
|
|
|
|
* @param File $file File we're reverting
|
|
|
|
|
*/
|
|
|
|
|
public function __construct( $file ) {
|
|
|
|
|
$this->title = $file->getTitle();
|
|
|
|
|
$this->file = $file;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-29 22:00:46 +00:00
|
|
|
/**
|
|
|
|
|
* Fulfil the request; shows the form or reverts the file,
|
|
|
|
|
* pending authentication, confirmation, etc.
|
|
|
|
|
*/
|
|
|
|
|
public function execute() {
|
2008-02-13 05:59:14 +00:00
|
|
|
global $wgOut, $wgRequest, $wgUser, $wgLang;
|
2007-07-29 22:00:46 +00:00
|
|
|
$this->setHeaders();
|
|
|
|
|
|
|
|
|
|
if( wfReadOnly() ) {
|
|
|
|
|
$wgOut->readOnlyPage();
|
|
|
|
|
return;
|
|
|
|
|
} elseif( !$wgUser->isLoggedIn() ) {
|
|
|
|
|
$wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
|
|
|
|
|
return;
|
2008-04-28 17:08:29 +00:00
|
|
|
} elseif( !$this->title->userCan( 'edit' ) || !$this->title->userCan( 'upload' ) ) {
|
2007-07-29 22:00:46 +00:00
|
|
|
// The standard read-only thing doesn't make a whole lot of sense
|
|
|
|
|
// here; surely it should show the image or something? -- RC
|
|
|
|
|
$article = new Article( $this->title );
|
|
|
|
|
$wgOut->readOnlyPage( $article->getContent(), true );
|
|
|
|
|
return;
|
|
|
|
|
} elseif( $wgUser->isBlocked() ) {
|
|
|
|
|
$wgOut->blockedPage();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-04-05 16:46:22 +00:00
|
|
|
$this->archiveName = $wgRequest->getText( 'oldimage' );
|
2007-07-29 22:00:46 +00:00
|
|
|
$token = $wgRequest->getText( 'wpEditToken' );
|
|
|
|
|
if( !$this->isValidOldSpec() ) {
|
2008-04-05 16:46:22 +00:00
|
|
|
$wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars( $this->archiveName ) );
|
2007-07-29 22:00:46 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-29 22:00:46 +00:00
|
|
|
if( !$this->haveOldVersion() ) {
|
2008-11-06 22:20:29 +00:00
|
|
|
$wgOut->addHTML( wfMsgExt( 'filerevert-badversion', 'parse' ) );
|
2007-07-29 22:00:46 +00:00
|
|
|
$wgOut->returnToMain( false, $this->title );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-29 22:00:46 +00:00
|
|
|
// Perform the reversion if appropriate
|
2008-04-05 16:46:22 +00:00
|
|
|
if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->archiveName ) ) {
|
|
|
|
|
$source = $this->file->getArchiveVirtualUrl( $this->archiveName );
|
2007-07-29 22:00:46 +00:00
|
|
|
$comment = $wgRequest->getText( 'wpComment' );
|
|
|
|
|
// TODO: Preserve file properties from database instead of reloading from file
|
|
|
|
|
$status = $this->file->upload( $source, $comment, $comment );
|
|
|
|
|
if( $status->isGood() ) {
|
2008-11-06 22:20:29 +00:00
|
|
|
$wgOut->addHTML( wfMsgExt( 'filerevert-success', 'parse', $this->title->getText(),
|
2007-08-20 14:32:08 +00:00
|
|
|
$wgLang->date( $this->getTimestamp(), true ),
|
|
|
|
|
$wgLang->time( $this->getTimestamp(), true ),
|
2008-04-05 16:46:22 +00:00
|
|
|
wfExpandUrl( $this->file->getArchiveUrl( $this->archiveName ) ) ) );
|
2007-07-29 22:00:46 +00:00
|
|
|
$wgOut->returnToMain( false, $this->title );
|
|
|
|
|
} else {
|
|
|
|
|
$wgOut->addWikiText( $status->getWikiText() );
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-29 22:00:46 +00:00
|
|
|
// Show the form
|
2008-04-14 07:45:50 +00:00
|
|
|
$this->showForm();
|
2007-07-29 22:00:46 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-29 22:00:46 +00:00
|
|
|
/**
|
|
|
|
|
* Show the confirmation form
|
|
|
|
|
*/
|
2008-04-05 16:46:22 +00:00
|
|
|
protected function showForm() {
|
2008-02-13 05:59:14 +00:00
|
|
|
global $wgOut, $wgUser, $wgRequest, $wgLang, $wgContLang;
|
2007-07-29 22:00:46 +00:00
|
|
|
$timestamp = $this->getTimestamp();
|
|
|
|
|
|
|
|
|
|
$form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction() ) );
|
2008-04-05 16:46:22 +00:00
|
|
|
$form .= Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->archiveName ) );
|
2007-07-29 22:00:46 +00:00
|
|
|
$form .= '<fieldset><legend>' . wfMsgHtml( 'filerevert-legend' ) . '</legend>';
|
|
|
|
|
$form .= wfMsgExt( 'filerevert-intro', 'parse', $this->title->getText(),
|
2008-02-13 05:59:14 +00:00
|
|
|
$wgLang->date( $timestamp, true ), $wgLang->time( $timestamp, true ),
|
2008-04-05 16:46:22 +00:00
|
|
|
wfExpandUrl( $this->file->getArchiveUrl( $this->archiveName ) ) );
|
2007-07-29 22:00:46 +00:00
|
|
|
$form .= '<p>' . Xml::inputLabel( wfMsg( 'filerevert-comment' ), 'wpComment', 'wpComment',
|
2007-07-29 22:05:10 +00:00
|
|
|
60, wfMsgForContent( 'filerevert-defaultcomment',
|
2007-07-30 19:08:11 +00:00
|
|
|
$wgContLang->date( $timestamp, false, false ), $wgContLang->time( $timestamp, false, false ) ) ) . '</p>';
|
2007-07-29 22:00:46 +00:00
|
|
|
$form .= '<p>' . Xml::submitButton( wfMsg( 'filerevert-submit' ) ) . '</p>';
|
|
|
|
|
$form .= '</fieldset>';
|
|
|
|
|
$form .= '</form>';
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-11-06 22:20:29 +00:00
|
|
|
$wgOut->addHTML( $form );
|
2007-07-29 22:00:46 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-29 22:00:46 +00:00
|
|
|
/**
|
|
|
|
|
* Set headers, titles and other bits
|
|
|
|
|
*/
|
2008-04-05 16:46:22 +00:00
|
|
|
protected function setHeaders() {
|
2007-08-22 05:35:40 +00:00
|
|
|
global $wgOut, $wgUser;
|
2007-08-15 19:15:41 +00:00
|
|
|
$wgOut->setPageTitle( wfMsg( 'filerevert', $this->title->getText() ) );
|
2007-07-29 22:00:46 +00:00
|
|
|
$wgOut->setRobotPolicy( 'noindex,nofollow' );
|
2009-06-06 22:42:48 +00:00
|
|
|
$wgOut->setSubtitle( wfMsg(
|
|
|
|
|
'filerevert-backlink',
|
|
|
|
|
$wgUser->getSkin()->link(
|
|
|
|
|
$this->title,
|
|
|
|
|
null,
|
|
|
|
|
array(),
|
|
|
|
|
array(),
|
|
|
|
|
array( 'known', 'noclasses' )
|
|
|
|
|
)
|
|
|
|
|
) );
|
2007-07-29 22:00:46 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-29 22:00:46 +00:00
|
|
|
/**
|
|
|
|
|
* Is the provided `oldimage` value valid?
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2008-04-05 16:46:22 +00:00
|
|
|
protected function isValidOldSpec() {
|
|
|
|
|
return strlen( $this->archiveName ) >= 16
|
|
|
|
|
&& strpos( $this->archiveName, '/' ) === false
|
|
|
|
|
&& strpos( $this->archiveName, '\\' ) === false;
|
2007-07-29 22:00:46 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-29 22:00:46 +00:00
|
|
|
/**
|
|
|
|
|
* Does the provided `oldimage` value correspond
|
|
|
|
|
* to an existing, local, old version of this file?
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2008-04-05 16:46:22 +00:00
|
|
|
protected function haveOldVersion() {
|
|
|
|
|
return $this->getOldFile()->exists();
|
2007-07-29 22:00:46 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-29 22:00:46 +00:00
|
|
|
/**
|
|
|
|
|
* Prepare the form action
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2008-04-05 16:46:22 +00:00
|
|
|
protected function getAction() {
|
2007-07-29 22:00:46 +00:00
|
|
|
$q = array();
|
|
|
|
|
$q[] = 'action=revert';
|
2008-04-05 16:46:22 +00:00
|
|
|
$q[] = 'oldimage=' . urlencode( $this->archiveName );
|
2007-07-29 22:00:46 +00:00
|
|
|
return $this->title->getLocalUrl( implode( '&', $q ) );
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-29 22:00:46 +00:00
|
|
|
/**
|
|
|
|
|
* Extract the timestamp of the old version
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2008-04-05 16:46:22 +00:00
|
|
|
protected function getTimestamp() {
|
2007-08-20 14:57:30 +00:00
|
|
|
if( $this->timestamp === false ) {
|
2008-04-05 16:46:22 +00:00
|
|
|
$this->timestamp = $this->getOldFile()->getTimestamp();
|
2007-08-20 14:32:08 +00:00
|
|
|
}
|
2007-08-20 14:57:30 +00:00
|
|
|
return $this->timestamp;
|
2007-07-29 22:00:46 +00:00
|
|
|
}
|
2008-04-05 16:46:22 +00:00
|
|
|
|
|
|
|
|
protected function getOldFile() {
|
|
|
|
|
if ( !isset( $this->oldFile ) ) {
|
|
|
|
|
$this->oldFile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->title, $this->archiveName );
|
|
|
|
|
}
|
|
|
|
|
return $this->oldFile;
|
|
|
|
|
}
|
|
|
|
|
}
|