MediaWiki  REL1_24
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, $this->getConfig() );
00060         $retval = $pa->undelete(
00061             ( isset( $params['timestamps'] ) ? $params['timestamps'] : array() ),
00062             $params['reason'],
00063             $params['fileids'],
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, $params['fileids'], $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             'reason' => '',
00100             'timestamps' => array(
00101                 ApiBase::PARAM_TYPE => 'timestamp',
00102                 ApiBase::PARAM_ISMULTI => true,
00103             ),
00104             'fileids' => array(
00105                 ApiBase::PARAM_TYPE => 'integer',
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             'reason' => 'Reason for restoring',
00124             'timestamps' => array(
00125                 'Timestamps of the revisions to restore.',
00126                 'If both timestamps and fileids are empty, all will be restored.',
00127             ),
00128             'fileids' => array(
00129                 'IDs of the file revisions to restore.',
00130                 'If both timestamps and fileids are empty, all will be restored.',
00131             ),
00132             'watchlist' => 'Unconditionally add or remove the page from your ' .
00133                 'watchlist, use preferences or do not change watch',
00134         );
00135     }
00136 
00137     public function getDescription() {
00138         return array(
00139             'Restore certain revisions of a deleted page. A list of deleted revisions ',
00140             '(including timestamps) can be retrieved through list=deletedrevs, and a list',
00141             'of deleted file ids can be retrieved through list=filearchive.'
00142         );
00143     }
00144 
00145     public function needsToken() {
00146         return 'csrf';
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 }