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