Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
74.49% |
73 / 98 |
|
37.50% |
3 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ApiPurge | |
75.26% |
73 / 97 |
|
37.50% |
3 / 8 |
29.33 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
81.69% |
58 / 71 |
|
0.00% |
0 / 1 |
15.20 | |||
| getPageSet | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| isWriteMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| mustBePosted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAllowedParams | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
2.00 | |||
| getExamplesMessages | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Api; |
| 8 | |
| 9 | use MediaWiki\Deferred\DeferredUpdates; |
| 10 | use MediaWiki\Logger\LoggerFactory; |
| 11 | use MediaWiki\Page\WikiPageFactory; |
| 12 | use MediaWiki\Permissions\PermissionStatus; |
| 13 | use MediaWiki\Status\Status; |
| 14 | use MediaWiki\Title\Title; |
| 15 | use MediaWiki\Title\TitleFormatter; |
| 16 | |
| 17 | /** |
| 18 | * API interface for page purging |
| 19 | * @ingroup API |
| 20 | */ |
| 21 | class ApiPurge extends ApiBase { |
| 22 | /** @var ApiPageSet|null */ |
| 23 | private $mPageSet = null; |
| 24 | |
| 25 | private WikiPageFactory $wikiPageFactory; |
| 26 | private TitleFormatter $titleFormatter; |
| 27 | |
| 28 | public function __construct( |
| 29 | ApiMain $mainModule, |
| 30 | string $moduleName, |
| 31 | WikiPageFactory $wikiPageFactory, |
| 32 | TitleFormatter $titleFormatter |
| 33 | ) { |
| 34 | parent::__construct( $mainModule, $moduleName ); |
| 35 | $this->wikiPageFactory = $wikiPageFactory; |
| 36 | $this->titleFormatter = $titleFormatter; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Purges the cache of a page |
| 41 | */ |
| 42 | public function execute() { |
| 43 | $authority = $this->getAuthority(); |
| 44 | |
| 45 | // Fail early if the user is sitewide blocked. |
| 46 | $block = $authority->getBlock(); |
| 47 | if ( $block && $block->isSitewide() ) { |
| 48 | $this->dieBlocked( $block ); |
| 49 | } |
| 50 | |
| 51 | $params = $this->extractRequestParams(); |
| 52 | |
| 53 | $continuationManager = new ApiContinuationManager( $this, [], [] ); |
| 54 | $this->setContinuationManager( $continuationManager ); |
| 55 | |
| 56 | $forceLinkUpdate = $params['forcelinkupdate']; |
| 57 | $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate']; |
| 58 | $pageSet = $this->getPageSet(); |
| 59 | $pageSet->execute(); |
| 60 | |
| 61 | $result = $pageSet->getInvalidTitlesAndRevisions(); |
| 62 | $userName = $authority->getUser()->getName(); |
| 63 | $now = wfTimestampNow(); |
| 64 | |
| 65 | foreach ( $pageSet->getGoodPages() as $pageIdentity ) { |
| 66 | $title = $this->titleFormatter->getPrefixedText( $pageIdentity ); |
| 67 | $r = [ |
| 68 | 'ns' => $pageIdentity->getNamespace(), |
| 69 | 'title' => $title, |
| 70 | ]; |
| 71 | $page = $this->wikiPageFactory->newFromTitle( $pageIdentity ); |
| 72 | |
| 73 | $purgeAuthStatus = PermissionStatus::newEmpty(); |
| 74 | if ( $authority->authorizeAction( 'purge', $purgeAuthStatus ) ) { |
| 75 | // Directly purge and skip the UI part of purge() |
| 76 | $page->doPurge(); |
| 77 | $r['purged'] = true; |
| 78 | } else { |
| 79 | if ( $purgeAuthStatus->isRateLimitExceeded() ) { |
| 80 | $this->addWarning( 'apierror-ratelimited' ); |
| 81 | } else { |
| 82 | $this->addWarning( Status::wrap( $purgeAuthStatus )->getMessage() ); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) { |
| 87 | $linkpurgeAuthStatus = PermissionStatus::newEmpty(); |
| 88 | if ( $authority->authorizeAction( 'linkpurge', $linkpurgeAuthStatus ) ) { |
| 89 | # Logging to better see expensive usage patterns |
| 90 | if ( $forceRecursiveLinkUpdate ) { |
| 91 | LoggerFactory::getInstance( 'RecursiveLinkPurge' )->info( |
| 92 | "Recursive link purge enqueued for {title}", |
| 93 | [ |
| 94 | 'user' => $userName, |
| 95 | 'title' => $title |
| 96 | ] |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | $page->updateParserCache( [ |
| 101 | 'causeAction' => 'api-purge', |
| 102 | 'causeAgent' => $userName, |
| 103 | ] ); |
| 104 | $page->doSecondaryDataUpdates( [ |
| 105 | 'recursive' => $forceRecursiveLinkUpdate, |
| 106 | 'causeAction' => 'api-purge', |
| 107 | 'causeAgent' => $userName, |
| 108 | 'defer' => DeferredUpdates::PRESEND, |
| 109 | 'freshness' => $now, |
| 110 | ] ); |
| 111 | $r['linkupdate'] = true; |
| 112 | } else { |
| 113 | if ( $linkpurgeAuthStatus->isRateLimitExceeded() ) { |
| 114 | $this->addWarning( 'apierror-ratelimited' ); |
| 115 | $forceLinkUpdate = false; |
| 116 | $forceRecursiveLinkUpdate = false; |
| 117 | } else { |
| 118 | $this->addWarning( Status::wrap( $linkpurgeAuthStatus )->getMessage() ); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | $result[] = $r; |
| 124 | } |
| 125 | $apiResult = $this->getResult(); |
| 126 | ApiResult::setIndexedTagName( $result, 'page' ); |
| 127 | $apiResult->addValue( null, $this->getModuleName(), $result ); |
| 128 | |
| 129 | $values = $pageSet->getNormalizedTitlesAsResult( $apiResult ); |
| 130 | if ( $values ) { |
| 131 | $apiResult->addValue( null, 'normalized', $values ); |
| 132 | } |
| 133 | $values = $pageSet->getConvertedTitlesAsResult( $apiResult ); |
| 134 | if ( $values ) { |
| 135 | $apiResult->addValue( null, 'converted', $values ); |
| 136 | } |
| 137 | $values = $pageSet->getRedirectTitlesAsResult( $apiResult ); |
| 138 | if ( $values ) { |
| 139 | $apiResult->addValue( null, 'redirects', $values ); |
| 140 | } |
| 141 | |
| 142 | $this->setContinuationManager( null ); |
| 143 | $continuationManager->setContinuationIntoResult( $apiResult ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Get a cached instance of an ApiPageSet object |
| 148 | * @return ApiPageSet |
| 149 | */ |
| 150 | private function getPageSet() { |
| 151 | $this->mPageSet ??= new ApiPageSet( $this ); |
| 152 | |
| 153 | return $this->mPageSet; |
| 154 | } |
| 155 | |
| 156 | /** @inheritDoc */ |
| 157 | public function isWriteMode() { |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | /** @inheritDoc */ |
| 162 | public function mustBePosted() { |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | /** @inheritDoc */ |
| 167 | public function getAllowedParams( $flags = 0 ) { |
| 168 | $result = [ |
| 169 | 'forcelinkupdate' => false, |
| 170 | 'forcerecursivelinkupdate' => false, |
| 171 | 'continue' => [ |
| 172 | ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 173 | ], |
| 174 | ]; |
| 175 | if ( $flags ) { |
| 176 | $result += $this->getPageSet()->getFinalParams( $flags ); |
| 177 | } |
| 178 | |
| 179 | return $result; |
| 180 | } |
| 181 | |
| 182 | /** @inheritDoc */ |
| 183 | protected function getExamplesMessages() { |
| 184 | $title = Title::newMainPage()->getPrefixedText(); |
| 185 | $mp = rawurlencode( $title ); |
| 186 | |
| 187 | return [ |
| 188 | "action=purge&titles={$mp}|API" |
| 189 | => 'apihelp-purge-example-simple', |
| 190 | 'action=purge&generator=allpages&gapnamespace=0&gaplimit=10' |
| 191 | => 'apihelp-purge-example-generator', |
| 192 | ]; |
| 193 | } |
| 194 | |
| 195 | /** @inheritDoc */ |
| 196 | public function getHelpUrls() { |
| 197 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Purge'; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** @deprecated class alias since 1.43 */ |
| 202 | class_alias( ApiPurge::class, 'ApiPurge' ); |