MediaWiki  REL1_20
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'] = $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' => array(
00098                                 ApiBase::PARAM_TYPE => 'string',
00099                                 ApiBase::PARAM_REQUIRED => true
00100                         ),
00101                         'reason' => '',
00102                         'timestamps' => array(
00103                                 ApiBase::PARAM_TYPE => 'timestamp',
00104                                 ApiBase::PARAM_ISMULTI => true,
00105                         ),
00106                         'watchlist' => array(
00107                                 ApiBase::PARAM_DFLT => 'preferences',
00108                                 ApiBase::PARAM_TYPE => array(
00109                                         'watch',
00110                                         'unwatch',
00111                                         'preferences',
00112                                         'nochange'
00113                                 ),
00114                         ),
00115                 );
00116         }
00117 
00118         public function getParamDescription() {
00119                 return array(
00120                         'title' => 'Title of the page you want to restore',
00121                         'token' => 'An undelete token previously retrieved through list=deletedrevs',
00122                         'reason' => 'Reason for restoring',
00123                         'timestamps' => 'Timestamps of the revisions to restore. If not set, all revisions will be restored.',
00124                         'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
00125                 );
00126         }
00127 
00128         public function getResultProperties() {
00129                 return array(
00130                         '' => array(
00131                                 'title' => 'string',
00132                                 'revisions' => 'integer',
00133                                 'filerevisions' => 'integer',
00134                                 'reason' => 'string'
00135                         )
00136                 );
00137         }
00138 
00139         public function getDescription() {
00140                 return array(
00141                         'Restore certain revisions of a deleted page. A list of deleted revisions (including timestamps) can be',
00142                         'retrieved through list=deletedrevs'
00143                 );
00144         }
00145 
00146         public function getPossibleErrors() {
00147                 return array_merge( parent::getPossibleErrors(), array(
00148                         array( 'permdenied-undelete' ),
00149                         array( 'blockedtext' ),
00150                         array( 'invalidtitle', 'title' ),
00151                         array( 'cannotundelete' ),
00152                 ) );
00153         }
00154 
00155         public function needsToken() {
00156                 return true;
00157         }
00158 
00159         public function getTokenSalt() {
00160                 return '';
00161         }
00162 
00163         public function getExamples() {
00164                 return array(
00165                         'api.php?action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page',
00166                         'api.php?action=undelete&title=Main%20Page&token=123ABC&timestamps=20070703220045|20070702194856'
00167                 );
00168         }
00169 
00170         public function getHelpUrls() {
00171                 return 'https://www.mediawiki.org/wiki/API:Undelete';
00172         }
00173 
00174         public function getVersion() {
00175                 return __CLASS__ . ': $Id$';
00176         }
00177 }