MediaWiki  REL1_19
ApiUndelete.php
Go to the documentation of this file.
00001 <?php
00030 class ApiUndelete extends ApiBase {
00031 
00032         public function __construct( $main, $action ) {
00033                 parent::__construct( $main, $action );
00034         }
00035 
00036         public function execute() {
00037                 $params = $this->extractRequestParams();
00038 
00039                 if ( !$this->getUser()->isAllowed( 'undelete' ) ) {
00040                         $this->dieUsageMsg( 'permdenied-undelete' );
00041                 }
00042 
00043                 if ( $this->getUser()->isBlocked() ) {
00044                         $this->dieUsageMsg( 'blockedtext' );
00045                 }
00046 
00047                 $titleObj = Title::newFromText( $params['title'] );
00048                 if ( !$titleObj ) {
00049                         $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
00050                 }
00051 
00052                 // Convert timestamps
00053                 if ( !isset( $params['timestamps'] ) ) {
00054                         $params['timestamps'] = array();
00055                 }
00056                 if ( !is_array( $params['timestamps'] ) ) {
00057                         $params['timestamps'] = array( $params['timestamps'] );
00058                 }
00059                 foreach ( $params['timestamps'] as $i => $ts ) {
00060                         $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
00061                 }
00062 
00063                 $pa = new PageArchive( $titleObj );
00064                 $retval = $pa->undelete( ( isset( $params['timestamps'] ) ? $params['timestamps'] : array() ), $params['reason'] );
00065                 if ( !is_array( $retval ) ) {
00066                         $this->dieUsageMsg( 'cannotundelete' );
00067                 }
00068 
00069                 if ( $retval[1] ) {
00070                         wfRunHooks( 'FileUndeleteComplete',
00071                                 array( $titleObj, array(), $this->getUser(), $params['reason'] ) );
00072                 }
00073 
00074                 $this->setWatch( $params['watchlist'], $titleObj );
00075 
00076                 $info['title'] = $titleObj->getPrefixedText();
00077                 $info['revisions'] = intval( $retval[0] );
00078                 $info['fileversions'] = intval( $retval[1] );
00079                 $info['reason'] = intval( $retval[2] );
00080                 $this->getResult()->addValue( null, $this->getModuleName(), $info );
00081         }
00082 
00083         public function mustBePosted() {
00084                 return true;
00085         }
00086 
00087         public function isWriteMode() {
00088                 return true;
00089         }
00090 
00091         public function getAllowedParams() {
00092                 return array(
00093                         'title' => array(
00094                                 ApiBase::PARAM_TYPE => 'string',
00095                                 ApiBase::PARAM_REQUIRED => true
00096                         ),
00097                         'token' => null,
00098                         'reason' => '',
00099                         'timestamps' => array(
00100                                 ApiBase::PARAM_TYPE => 'timestamp',
00101                                 ApiBase::PARAM_ISMULTI => true,
00102                         ),
00103                         'watchlist' => array(
00104                                 ApiBase::PARAM_DFLT => 'preferences',
00105                                 ApiBase::PARAM_TYPE => array(
00106                                         'watch',
00107                                         'unwatch',
00108                                         'preferences',
00109                                         'nochange'
00110                                 ),
00111                         ),
00112                 );
00113         }
00114 
00115         public function getParamDescription() {
00116                 return array(
00117                         'title' => 'Title of the page you want to restore',
00118                         'token' => 'An undelete token previously retrieved through list=deletedrevs',
00119                         'reason' => 'Reason for restoring (optional)',
00120                         'timestamps' => 'Timestamps of the revisions to restore. If not set, all revisions will be restored.',
00121                         'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
00122                 );
00123         }
00124 
00125         public function getDescription() {
00126                 return array(
00127                         'Restore certain revisions of a deleted page. A list of deleted revisions (including timestamps) can be',
00128                         'retrieved through list=deletedrevs'
00129                 );
00130         }
00131 
00132         public function getPossibleErrors() {
00133                 return array_merge( parent::getPossibleErrors(), array(
00134                         array( 'permdenied-undelete' ),
00135                         array( 'blockedtext' ),
00136                         array( 'invalidtitle', 'title' ),
00137                         array( 'cannotundelete' ),
00138                 ) );
00139         }
00140 
00141         public function needsToken() {
00142                 return true;
00143         }
00144 
00145         public function getTokenSalt() {
00146                 return '';
00147         }
00148 
00149         public function getExamples() {
00150                 return array(
00151                         'api.php?action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page',
00152                         'api.php?action=undelete&title=Main%20Page&token=123ABC&timestamps=20070703220045|20070702194856'
00153                 );
00154         }
00155 
00156         public function getHelpUrls() {
00157                 return 'https://www.mediawiki.org/wiki/API:Undelete';
00158         }
00159 
00160         public function getVersion() {
00161                 return __CLASS__ . ': $Id$';
00162         }
00163 }