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