MediaWiki  REL1_23
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 ' .
00126                 'revisions will be restored.',
00127             'watchlist' => 'Unconditionally add or remove the page from your ' .
00128                 'watchlist, use preferences or do not change watch',
00129         );
00130     }
00131 
00132     public function getResultProperties() {
00133         return array(
00134             '' => array(
00135                 'title' => 'string',
00136                 'revisions' => 'integer',
00137                 'filerevisions' => 'integer',
00138                 'reason' => 'string'
00139             )
00140         );
00141     }
00142 
00143     public function getDescription() {
00144         return array(
00145             'Restore certain revisions of a deleted page. A list of deleted revisions ',
00146             '(including timestamps) can be retrieved through list=deletedrevs.'
00147         );
00148     }
00149 
00150     public function getPossibleErrors() {
00151         return array_merge( parent::getPossibleErrors(), array(
00152             array( 'permdenied-undelete' ),
00153             array( 'blockedtext' ),
00154             array( 'invalidtitle', 'title' ),
00155             array( 'cannotundelete' ),
00156         ) );
00157     }
00158 
00159     public function needsToken() {
00160         return true;
00161     }
00162 
00163     public function getTokenSalt() {
00164         return '';
00165     }
00166 
00167     public function getExamples() {
00168         return array(
00169             'api.php?action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page',
00170             'api.php?action=undelete&title=Main%20Page&token=123ABC&timestamps=20070703220045|20070702194856'
00171         );
00172     }
00173 
00174     public function getHelpUrls() {
00175         return 'https://www.mediawiki.org/wiki/API:Undelete';
00176     }
00177 }