| 1 |
<?php |
| 2 |
|
| 3 |
h |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use MediaWiki\Page\PageProps; |
| 26 |
use MediaWiki\Title\Title; |
| 27 |
use Wikimedia\ParamValidator\ParamValidator; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class ApiQueryPageProps extends ApiQueryBase { |
| 35 |
|
| 36 |
private PageProps $pageProps; |
| 37 |
|
| 38 |
public function __construct( |
| 39 |
ApiQuery $query, |
| 40 |
string $moduleName, |
| 41 |
PageProps $pageProps |
| 42 |
) { |
| 43 |
parent::__construct( $query, $moduleName, 'pp' ); |
| 44 |
$this->pageProps = $pageProps; |
| 45 |
} |
| 46 |
|
| 47 |
public function execute() { |
| 48 |
|
| 49 |
$pages = $this->getPageSet()->getGoodPages(); |
| 50 |
|
| 51 |
$params = $this->extractRequestParams(); |
| 52 |
if ( $params['continue'] ) { |
| 53 |
$cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int' ] ); |
| 54 |
$continueValue = $cont[0]; |
| 55 |
$filteredPages = []; |
| 56 |
foreach ( $pages as $id => $page ) { |
| 57 |
if ( $id >= $continueValue ) { |
| 58 |
$filteredPages[$id] = $page; |
| 59 |
} |
| 60 |
} |
| 61 |
$pages = $filteredPages; |
| 62 |
} |
| 63 |
|
| 64 |
if ( $pages === [] ) { |
| 65 |
|
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
if ( $params['prop'] ) { |
| 70 |
$properties = $this->pageProps->getProperties( $pages, $params['prop'] ); |
| 71 |
} else { |
| 72 |
$properties = $this->pageProps->getAllProperties( $pages ); |
| 73 |
} |
| 74 |
|
| 75 |
ksort( $properties ); |
| 76 |
|
| 77 |
$result = $this->getResult(); |
| 78 |
foreach ( $properties as $pageid => $props ) { |
| 79 |
if ( !$this->addPageProps( $result, $pageid, $props ) ) { |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
private function addPageProps( $result, $page, $props ) { |
| 95 |
ApiResult::setArrayType( $props, 'assoc' ); |
| 96 |
$fit = $result->addValue( [ 'query', 'pages', $page ], 'pageprops', $props ); |
| 97 |
|
| 98 |
if ( !$fit ) { |
| 99 |
$this->setContinueEnumParameter( 'continue', $page ); |
| 100 |
} |
| 101 |
|
| 102 |
return $fit; |
| 103 |
} |
| 104 |
|
| 105 |
public function getCacheMode( $params ) { |
| 106 |
return 'public'; |
| 107 |
} |
| 108 |
|
| 109 |
public function getAllowedParams() { |
| 110 |
return [ |
| 111 |
'continue' => [ |
| 112 |
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 113 |
], |
| 114 |
'prop' => [ |
| 115 |
ParamValidator::PARAM_ISMULTI => true, |
| 116 |
], |
| 117 |
]; |
| 118 |
} |
| 119 |
|
| 120 |
protected function getExamplesMessages() { |
| 121 |
$title = Title::newMainPage()->getPrefixedText(); |
| 122 |
$mp = rawurlencode( $title ); |
| 123 |
|
| 124 |
return [ |
| 125 |
"action=query&prop=pageprops&titles={$mp}|MediaWiki" |
| 126 |
=> 'apihelp-query+pageprops-example-simple', |
| 127 |
]; |
| 128 |
} |
| 129 |
|
| 130 |
public function getHelpUrls() { |
| 131 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageprops'; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
class_alias( ApiQueryPageProps::class, 'ApiQueryPageProps' ); |