| 1 |
<?php |
| 2 |
|
| 3 |
s |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Api; |
| 25 |
|
| 26 |
use MediaWiki\Title\Title; |
| 27 |
use Wikimedia\ParamValidator\ParamValidator; |
| 28 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class ApiQueryPagesWithProp extends ApiQueryGeneratorBase { |
| 37 |
|
| 38 |
public function __construct( ApiQuery $query, string $moduleName ) { |
| 39 |
parent::__construct( $query, $moduleName, 'pwp' ); |
| 40 |
} |
| 41 |
|
| 42 |
public function execute() { |
| 43 |
$this->run(); |
| 44 |
} |
| 45 |
|
| 46 |
public function getCacheMode( $params ) { |
| 47 |
return 'public'; |
| 48 |
} |
| 49 |
|
| 50 |
public function executeGenerator( $resultPageSet ) { |
| 51 |
$this->run( $resultPageSet ); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
private function run( $resultPageSet = null ) { |
| 59 |
$params = $this->extractRequestParams(); |
| 60 |
|
| 61 |
$prop = array_fill_keys( $params['prop'], true ); |
| 62 |
$fld_ids = isset( $prop['ids'] ); |
| 63 |
$fld_title = isset( $prop['title'] ); |
| 64 |
$fld_value = isset( $prop['value'] ); |
| 65 |
|
| 66 |
if ( $resultPageSet === null ) { |
| 67 |
$this->addFields( [ 'page_id' ] ); |
| 68 |
$this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title ); |
| 69 |
$this->addFieldsIf( 'pp_value', $fld_value ); |
| 70 |
} else { |
| 71 |
$this->addFields( $resultPageSet->getPageTableFields() ); |
| 72 |
} |
| 73 |
$this->addTables( [ 'page_props', 'page' ] ); |
| 74 |
$this->addWhere( 'pp_page=page_id' ); |
| 75 |
$this->addWhereFld( 'pp_propname', $params['propname'] ); |
| 76 |
|
| 77 |
$dir = ( $params['dir'] == 'ascending' ) ? 'newer' : 'older'; |
| 78 |
|
| 79 |
if ( $params['continue'] ) { |
| 80 |
$cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int' ] ); |
| 81 |
|
| 82 |
$this->addWhereRange( 'pp_page', $dir, $cont[0], null ); |
| 83 |
} |
| 84 |
|
| 85 |
$sort = ( $params['dir'] === 'descending' ? ' DESC' : '' ); |
| 86 |
$this->addOption( 'ORDER BY', 'pp_page' . $sort ); |
| 87 |
|
| 88 |
$limit = $params['limit']; |
| 89 |
$this->addOption( 'LIMIT', $limit + 1 ); |
| 90 |
|
| 91 |
$result = $this->getResult(); |
| 92 |
$count = 0; |
| 93 |
$res = $this->select( __METHOD__ ); |
| 94 |
|
| 95 |
if ( $fld_title && $resultPageSet === null ) { |
| 96 |
$this->executeGenderCacheFromResultWrapper( $res, __METHOD__ ); |
| 97 |
} |
| 98 |
|
| 99 |
foreach ( $res as $row ) { |
| 100 |
if ( ++$count > $limit ) { |
| 101 |
|
| 102 |
|
| 103 |
$this->setContinueEnumParameter( 'continue', $row->page_id ); |
| 104 |
break; |
| 105 |
} |
| 106 |
|
| 107 |
if ( $resultPageSet === null ) { |
| 108 |
$vals = [ |
| 109 |
ApiResult::META_TYPE => 'assoc', |
| 110 |
]; |
| 111 |
if ( $fld_ids ) { |
| 112 |
$vals['pageid'] = (int)$row->page_id; |
| 113 |
} |
| 114 |
if ( $fld_title ) { |
| 115 |
$title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
| 116 |
ApiQueryBase::addTitleInfo( $vals, $title ); |
| 117 |
} |
| 118 |
if ( $fld_value ) { |
| 119 |
$vals['value'] = $row->pp_value; |
| 120 |
} |
| 121 |
$fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals ); |
| 122 |
if ( !$fit ) { |
| 123 |
$this->setContinueEnumParameter( 'continue', $row->page_id ); |
| 124 |
break; |
| 125 |
} |
| 126 |
} else { |
| 127 |
$resultPageSet->processDbRow( $row ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if ( $resultPageSet === null ) { |
| 132 |
$result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' ); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
public function getAllowedParams() { |
| 137 |
return [ |
| 138 |
'propname' => [ |
| 139 |
ParamValidator::PARAM_TYPE => 'string', |
| 140 |
ParamValidator::PARAM_REQUIRED => true, |
| 141 |
], |
| 142 |
'prop' => [ |
| 143 |
ParamValidator::PARAM_DEFAULT => 'ids|title', |
| 144 |
ParamValidator::PARAM_ISMULTI => true, |
| 145 |
ParamValidator::PARAM_TYPE => [ |
| 146 |
'ids', |
| 147 |
'title', |
| 148 |
'value', |
| 149 |
], |
| 150 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
| 151 |
], |
| 152 |
'continue' => [ |
| 153 |
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 154 |
], |
| 155 |
'limit' => [ |
| 156 |
ParamValidator::PARAM_TYPE => 'limit', |
| 157 |
ParamValidator::PARAM_DEFAULT => 10, |
| 158 |
IntegerDef::PARAM_MIN => 1, |
| 159 |
IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
| 160 |
IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
| 161 |
], |
| 162 |
'dir' => [ |
| 163 |
ParamValidator::PARAM_DEFAULT => 'ascending', |
| 164 |
ParamValidator::PARAM_TYPE => [ |
| 165 |
'ascending', |
| 166 |
'descending', |
| 167 |
] |
| 168 |
], |
| 169 |
]; |
| 170 |
} |
| 171 |
|
| 172 |
protected function getExamplesMessages() { |
| 173 |
return [ |
| 174 |
'action=query&list=pageswithprop&pwppropname=displaytitle&pwpprop=ids|title|value' |
| 175 |
=> 'apihelp-query+pageswithprop-example-simple', |
| 176 |
'action=query&generator=pageswithprop&gpwppropname=notoc&prop=info' |
| 177 |
=> 'apihelp-query+pageswithprop-example-generator', |
| 178 |
]; |
| 179 |
} |
| 180 |
|
| 181 |
public function getHelpUrls() { |
| 182 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageswithprop'; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
class_alias( ApiQueryPagesWithProp::class, 'ApiQueryPagesWithProp' ); |