MediaWiki  REL1_21
ApiPurge.php
Go to the documentation of this file.
00001 <?php
00002 
00032 class ApiPurge extends ApiBase {
00033 
00034         private $mPageSet;
00035 
00043         private static function addValues( array &$result, $values, $flag = null, $name = null ) {
00044                 foreach ( $values as $val ) {
00045                         if( $val instanceof Title ) {
00046                                 $v = array();
00047                                 ApiQueryBase::addTitleInfo( $v, $val );
00048                         } elseif( $name !== null ) {
00049                                 $v = array( $name => $val );
00050                         } else {
00051                                 $v = $val;
00052                         }
00053                         if( $flag !== null ) {
00054                                 $v[$flag] = '';
00055                         }
00056                         $result[] = $v;
00057                 }
00058         }
00059 
00063         public function execute() {
00064                 $params = $this->extractRequestParams();
00065 
00066                 $forceLinkUpdate = $params['forcelinkupdate'];
00067                 $pageSet = $this->getPageSet();
00068                 $pageSet->execute();
00069 
00070                 $result = array();
00071                 self::addValues( $result, $pageSet->getInvalidTitles(), 'invalid', 'title' );
00072                 self::addValues( $result, $pageSet->getSpecialTitles(), 'special', 'title' );
00073                 self::addValues( $result, $pageSet->getMissingPageIDs(), 'missing', 'pageid' );
00074                 self::addValues( $result, $pageSet->getMissingRevisionIDs(), 'missing', 'revid' );
00075                 self::addValues( $result, $pageSet->getMissingTitles(), 'missing' );
00076                 self::addValues( $result, $pageSet->getInterwikiTitlesAsResult() );
00077 
00078                 foreach ( $pageSet->getGoodTitles() as $title ) {
00079                         $r = array();
00080                         ApiQueryBase::addTitleInfo( $r, $title );
00081                         $page = WikiPage::factory( $title );
00082                         $page->doPurge(); // Directly purge and skip the UI part of purge().
00083                         $r['purged'] = '';
00084 
00085                         if ( $forceLinkUpdate ) {
00086                                 if ( !$this->getUser()->pingLimiter() ) {
00087                                         global $wgEnableParserCache;
00088 
00089                                         $popts = $page->makeParserOptions( 'canonical' );
00090 
00091                                         # Parse content; note that HTML generation is only needed if we want to cache the result.
00092                                         $content = $page->getContent( Revision::RAW );
00093                                         $p_result = $content->getParserOutput( $title, $page->getLatest(), $popts, $wgEnableParserCache );
00094 
00095                                         # Update the links tables
00096                                         $updates = $content->getSecondaryDataUpdates( $title, null, true, $p_result );
00097                                         DataUpdate::runUpdates( $updates );
00098 
00099                                         $r['linkupdate'] = '';
00100 
00101                                         if ( $wgEnableParserCache ) {
00102                                                 $pcache = ParserCache::singleton();
00103                                                 $pcache->save( $p_result, $page, $popts );
00104                                         }
00105                                 } else {
00106                                         $error = $this->parseMsg( array( 'actionthrottledtext' ) );
00107                                         $this->setWarning( $error['info'] );
00108                                         $forceLinkUpdate = false;
00109                                 }
00110                         }
00111 
00112                         $result[] = $r;
00113                 }
00114                 $apiResult = $this->getResult();
00115                 $apiResult->setIndexedTagName( $result, 'page' );
00116                 $apiResult->addValue( null, $this->getModuleName(), $result );
00117 
00118                 $values = $pageSet->getNormalizedTitlesAsResult( $apiResult );
00119                 if ( $values ) {
00120                         $apiResult->addValue( null, 'normalized', $values );
00121                 }
00122                 $values = $pageSet->getConvertedTitlesAsResult( $apiResult );
00123                 if ( $values ) {
00124                         $apiResult->addValue( null, 'converted', $values );
00125                 }
00126                 $values = $pageSet->getRedirectTitlesAsResult( $apiResult );
00127                 if ( $values ) {
00128                         $apiResult->addValue( null, 'redirects', $values );
00129                 }
00130         }
00131 
00136         private function getPageSet() {
00137                 if ( !isset( $this->mPageSet ) ) {
00138                         $this->mPageSet = new ApiPageSet( $this );
00139                 }
00140                 return $this->mPageSet;
00141         }
00142 
00143         public function isWriteMode() {
00144                 return true;
00145         }
00146 
00147         public function mustBePosted() {
00148                 // Anonymous users are not allowed a non-POST request
00149                 return !$this->getUser()->isAllowed( 'purge' );
00150         }
00151 
00152         public function getAllowedParams( $flags = 0 ) {
00153                 $result = array( 'forcelinkupdate' => false );
00154                 if ( $flags ) {
00155                         $result += $this->getPageSet()->getFinalParams( $flags );
00156                 }
00157                 return $result;
00158         }
00159 
00160         public function getParamDescription() {
00161                 return $this->getPageSet()->getParamDescription()
00162                         + array( 'forcelinkupdate' => 'Update the links tables' );
00163         }
00164 
00165         public function getResultProperties() {
00166                 return array(
00167                         ApiBase::PROP_LIST => true,
00168                         '' => array(
00169                                 'ns' => array(
00170                                         ApiBase::PROP_TYPE => 'namespace',
00171                                         ApiBase::PROP_NULLABLE => true
00172                                 ),
00173                                 'title' => array(
00174                                         ApiBase::PROP_TYPE => 'string',
00175                                         ApiBase::PROP_NULLABLE => true
00176                                 ),
00177                                 'pageid' => array(
00178                                         ApiBase::PROP_TYPE => 'integer',
00179                                         ApiBase::PROP_NULLABLE => true
00180                                 ),
00181                                 'revid' => array(
00182                                         ApiBase::PROP_TYPE => 'integer',
00183                                         ApiBase::PROP_NULLABLE => true
00184                                 ),
00185                                 'invalid' => 'boolean',
00186                                 'special' => 'boolean',
00187                                 'missing' => 'boolean',
00188                                 'purged' => 'boolean',
00189                                 'linkupdate' => 'boolean',
00190                                 'iw' => array(
00191                                         ApiBase::PROP_TYPE => 'string',
00192                                         ApiBase::PROP_NULLABLE => true
00193                                 ),
00194                         )
00195                 );
00196         }
00197 
00198         public function getDescription() {
00199                 return array( 'Purge the cache for the given titles.',
00200                         'Requires a POST request if the user is not logged in.'
00201                 );
00202         }
00203 
00204         public function getPossibleErrors() {
00205                 return array_merge(
00206                         parent::getPossibleErrors(),
00207                         $this->getPageSet()->getPossibleErrors()
00208                 );
00209         }
00210 
00211         public function getExamples() {
00212                 return array(
00213                         'api.php?action=purge&titles=Main_Page|API' => 'Purge the "Main Page" and the "API" page',
00214                 );
00215         }
00216 
00217         public function getHelpUrls() {
00218                 return 'https://www.mediawiki.org/wiki/API:Purge';
00219         }
00220 }