Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 94 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| ApiQueryQueryPage | |
0.00% |
0 / 93 |
|
0.00% |
0 / 9 |
812 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| executeGenerator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSpecialPage | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| run | |
0.00% |
0 / 47 |
|
0.00% |
0 / 1 |
306 | |||
| getCacheMode | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getAllowedParams | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
2 | |||
| getExamplesMessages | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright © 2010 Roan Kattouw <roan.kattouw@gmail.com> |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | */ |
| 8 | |
| 9 | namespace MediaWiki\Api; |
| 10 | |
| 11 | use MediaWiki\MainConfigNames; |
| 12 | use MediaWiki\SpecialPage\QueryPage; |
| 13 | use MediaWiki\SpecialPage\SpecialPageFactory; |
| 14 | use MediaWiki\Title\Title; |
| 15 | use Wikimedia\ParamValidator\ParamValidator; |
| 16 | use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 17 | use Wikimedia\Timestamp\TimestampFormat as TS; |
| 18 | |
| 19 | /** |
| 20 | * Query module to get the results of a QueryPage-based special page |
| 21 | * |
| 22 | * @ingroup API |
| 23 | */ |
| 24 | class ApiQueryQueryPage extends ApiQueryGeneratorBase { |
| 25 | |
| 26 | /** |
| 27 | * @var string[] list of special page names |
| 28 | */ |
| 29 | private $queryPages; |
| 30 | |
| 31 | private SpecialPageFactory $specialPageFactory; |
| 32 | |
| 33 | public function __construct( |
| 34 | ApiQuery $query, |
| 35 | string $moduleName, |
| 36 | SpecialPageFactory $specialPageFactory |
| 37 | ) { |
| 38 | parent::__construct( $query, $moduleName, 'qp' ); |
| 39 | $this->queryPages = array_values( array_diff( |
| 40 | array_column( QueryPage::getPages(), 1 ), // [ class, name ] |
| 41 | $this->getConfig()->get( MainConfigNames::APIUselessQueryPages ) |
| 42 | ) ); |
| 43 | $this->specialPageFactory = $specialPageFactory; |
| 44 | } |
| 45 | |
| 46 | public function execute() { |
| 47 | $this->run(); |
| 48 | } |
| 49 | |
| 50 | /** @inheritDoc */ |
| 51 | public function executeGenerator( $resultPageSet ) { |
| 52 | $this->run( $resultPageSet ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param string $name |
| 57 | * @return QueryPage |
| 58 | */ |
| 59 | private function getSpecialPage( $name ): QueryPage { |
| 60 | $qp = $this->specialPageFactory->getPage( $name ); |
| 61 | if ( !$qp ) { |
| 62 | self::dieDebug( |
| 63 | __METHOD__, |
| 64 | 'SpecialPageFactory failed to create special page ' . $name |
| 65 | ); |
| 66 | } |
| 67 | if ( !( $qp instanceof QueryPage ) ) { |
| 68 | self::dieDebug( |
| 69 | __METHOD__, |
| 70 | 'Special page ' . $name . ' is not a QueryPage' |
| 71 | ); |
| 72 | } |
| 73 | // @phan-suppress-next-line PhanTypeMismatchReturnNullable T240141 |
| 74 | return $qp; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @param ApiPageSet|null $resultPageSet Set when used as a generator, null otherwise |
| 79 | */ |
| 80 | public function run( $resultPageSet = null ) { |
| 81 | $params = $this->extractRequestParams(); |
| 82 | $result = $this->getResult(); |
| 83 | |
| 84 | $qp = $this->getSpecialPage( $params['page'] ); |
| 85 | if ( !$qp->userCanExecute( $this->getUser() ) ) { |
| 86 | $this->dieWithError( 'apierror-specialpage-cantexecute' ); |
| 87 | } |
| 88 | |
| 89 | if ( $resultPageSet === null ) { |
| 90 | $r = [ 'name' => $params['page'] ]; |
| 91 | if ( $qp->isCached() ) { |
| 92 | if ( !$qp->isCacheable() ) { |
| 93 | $r['disabled'] = true; |
| 94 | } else { |
| 95 | $r['cached'] = true; |
| 96 | $ts = $qp->getCachedTimestamp(); |
| 97 | if ( $ts ) { |
| 98 | $r['cachedtimestamp'] = wfTimestamp( TS::ISO_8601, $ts ); |
| 99 | } |
| 100 | $r['maxresults'] = $this->getConfig()->get( MainConfigNames::QueryCacheLimit ); |
| 101 | } |
| 102 | } |
| 103 | $result->addValue( [ 'query' ], $this->getModuleName(), $r ); |
| 104 | } |
| 105 | |
| 106 | if ( $qp->isCached() && !$qp->isCacheable() ) { |
| 107 | // Disabled query page, don't run the query |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 ); |
| 112 | $count = 0; |
| 113 | $titles = []; |
| 114 | foreach ( $res as $row ) { |
| 115 | if ( ++$count > $params['limit'] ) { |
| 116 | // We've had enough |
| 117 | $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] ); |
| 118 | break; |
| 119 | } |
| 120 | |
| 121 | $title = Title::makeTitle( $row->namespace, $row->title ); |
| 122 | if ( $resultPageSet === null ) { |
| 123 | $data = []; |
| 124 | if ( isset( $row->value ) ) { |
| 125 | $data['value'] = $row->value; |
| 126 | if ( $qp->usesTimestamps() ) { |
| 127 | $data['timestamp'] = wfTimestamp( TS::ISO_8601, $row->value ); |
| 128 | } |
| 129 | } |
| 130 | self::addTitleInfo( $data, $title ); |
| 131 | |
| 132 | foreach ( $row as $field => $value ) { |
| 133 | if ( !in_array( $field, [ 'namespace', 'title', 'value', 'qc_type' ] ) ) { |
| 134 | $data['databaseResult'][$field] = $value; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | $fit = $result->addValue( [ 'query', $this->getModuleName(), 'results' ], null, $data ); |
| 139 | if ( !$fit ) { |
| 140 | $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 ); |
| 141 | break; |
| 142 | } |
| 143 | } else { |
| 144 | $titles[] = $title; |
| 145 | } |
| 146 | } |
| 147 | if ( $resultPageSet === null ) { |
| 148 | $result->addIndexedTagName( |
| 149 | [ 'query', $this->getModuleName(), 'results' ], |
| 150 | 'page' |
| 151 | ); |
| 152 | } else { |
| 153 | $resultPageSet->populateFromTitles( $titles ); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** @inheritDoc */ |
| 158 | public function getCacheMode( $params ) { |
| 159 | $qp = $this->getSpecialPage( $params['page'] ); |
| 160 | if ( $qp->getRestriction() != '' ) { |
| 161 | return 'private'; |
| 162 | } |
| 163 | |
| 164 | return 'public'; |
| 165 | } |
| 166 | |
| 167 | /** @inheritDoc */ |
| 168 | public function getAllowedParams() { |
| 169 | return [ |
| 170 | 'page' => [ |
| 171 | ParamValidator::PARAM_TYPE => $this->queryPages, |
| 172 | ParamValidator::PARAM_REQUIRED => true |
| 173 | ], |
| 174 | 'offset' => [ |
| 175 | ParamValidator::PARAM_DEFAULT => 0, |
| 176 | ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 177 | ], |
| 178 | 'limit' => [ |
| 179 | ParamValidator::PARAM_DEFAULT => 10, |
| 180 | ParamValidator::PARAM_TYPE => 'limit', |
| 181 | IntegerDef::PARAM_MIN => 1, |
| 182 | IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
| 183 | IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
| 184 | ], |
| 185 | ]; |
| 186 | } |
| 187 | |
| 188 | /** @inheritDoc */ |
| 189 | protected function getExamplesMessages() { |
| 190 | return [ |
| 191 | 'action=query&list=querypage&qppage=Ancientpages' |
| 192 | => 'apihelp-query+querypage-example-ancientpages', |
| 193 | ]; |
| 194 | } |
| 195 | |
| 196 | /** @inheritDoc */ |
| 197 | public function getHelpUrls() { |
| 198 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Querypage'; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** @deprecated class alias since 1.43 */ |
| 203 | class_alias( ApiQueryQueryPage::class, 'ApiQueryQueryPage' ); |