MediaWiki  master
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(
00065                         ( isset( $params['timestamps'] ) ? $params['timestamps'] : array() ),
00066                         $params['reason'],
00067                         array(),
00068                         false,
00069                         $this->getUser()
00070                 );
00071                 if ( !is_array( $retval ) ) {
00072                         $this->dieUsageMsg( 'cannotundelete' );
00073                 }
00074 
00075                 if ( $retval[1] ) {
00076                         wfRunHooks( 'FileUndeleteComplete',
00077                                 array( $titleObj, array(), $this->getUser(), $params['reason'] ) );
00078                 }
00079 
00080                 $this->setWatch( $params['watchlist'], $titleObj );
00081 
00082                 $info['title'] = $titleObj->getPrefixedText();
00083                 $info['revisions'] = intval( $retval[0] );
00084                 $info['fileversions'] = intval( $retval[1] );
00085                 $info['reason'] = $retval[2];
00086                 $this->getResult()->addValue( null, $this->getModuleName(), $info );
00087         }
00088 
00089         public function mustBePosted() {
00090                 return true;
00091         }
00092 
00093         public function isWriteMode() {
00094                 return true;
00095         }
00096 
00097         public function getAllowedParams() {
00098                 return array(
00099                         'title' => array(
00100                                 ApiBase::PARAM_TYPE => 'string',
00101                                 ApiBase::PARAM_REQUIRED => true
00102                         ),
00103                         'token' => array(
00104                                 ApiBase::PARAM_TYPE => 'string',
00105                                 ApiBase::PARAM_REQUIRED => true
00106                         ),
00107                         'reason' => '',
00108                         'timestamps' => array(
00109                                 ApiBase::PARAM_TYPE => 'timestamp',
00110                                 ApiBase::PARAM_ISMULTI => true,
00111                         ),
00112                         'watchlist' => array(
00113                                 ApiBase::PARAM_DFLT => 'preferences',
00114                                 ApiBase::PARAM_TYPE => array(
00115                                         'watch',
00116                                         'unwatch',
00117                                         'preferences',
00118                                         'nochange'
00119                                 ),
00120                         ),
00121                 );
00122         }
00123 
00124         public function getParamDescription() {
00125                 return array(
00126                         'title' => 'Title of the page you want to restore',
00127                         'token' => 'An undelete token previously retrieved through list=deletedrevs',
00128                         'reason' => 'Reason for restoring',
00129                         'timestamps' => 'Timestamps of the revisions to restore. If not set, all revisions will be restored.',
00130                         'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
00131                 );
00132         }
00133 
00134         public function getResultProperties() {
00135                 return array(
00136                         '' => array(
00137                                 'title' => 'string',
00138                                 'revisions' => 'integer',
00139                                 'filerevisions' => 'integer',
00140                                 'reason' => 'string'
00141                         )
00142                 );
00143         }
00144 
00145         public function getDescription() {
00146                 return array(
00147                         'Restore certain revisions of a deleted page. A list of deleted revisions (including timestamps) can be',
00148                         'retrieved through list=deletedrevs'
00149                 );
00150         }
00151 
00152         public function getPossibleErrors() {
00153                 return array_merge( parent::getPossibleErrors(), array(
00154                         array( 'permdenied-undelete' ),
00155                         array( 'blockedtext' ),
00156                         array( 'invalidtitle', 'title' ),
00157                         array( 'cannotundelete' ),
00158                 ) );
00159         }
00160 
00161         public function needsToken() {
00162                 return true;
00163         }
00164 
00165         public function getTokenSalt() {
00166                 return '';
00167         }
00168 
00169         public function getExamples() {
00170                 return array(
00171                         'api.php?action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page',
00172                         'api.php?action=undelete&title=Main%20Page&token=123ABC&timestamps=20070703220045|20070702194856'
00173                 );
00174         }
00175 
00176         public function getHelpUrls() {
00177                 return 'https://www.mediawiki.org/wiki/API:Undelete';
00178         }
00179 
00180         public function getVersion() {
00181                 return __CLASS__ . ': $Id$';
00182         }
00183 }