| 1 |
<?php |
| 2 |
|
| 3 |
9 |
| 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 ChangeTags; |
| 26 |
use MediaWiki\ChangeTags\ChangeTagsStore; |
| 27 |
use Wikimedia\ParamValidator\ParamValidator; |
| 28 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class ApiQueryTags extends ApiQueryBase { |
| 36 |
|
| 37 |
private ChangeTagsStore $changeTagsStore; |
| 38 |
|
| 39 |
public function __construct( ApiQuery $query, string $moduleName, ChangeTagsStore $changeTagsStore ) { |
| 40 |
parent::__construct( $query, $moduleName, 'tg' ); |
| 41 |
$this->changeTagsStore = $changeTagsStore; |
| 42 |
} |
| 43 |
|
| 44 |
public function execute() { |
| 45 |
$params = $this->extractRequestParams(); |
| 46 |
|
| 47 |
$prop = array_fill_keys( $params['prop'], true ); |
| 48 |
|
| 49 |
$fld_displayname = isset( $prop['displayname'] ); |
| 50 |
$fld_description = isset( $prop['description'] ); |
| 51 |
$fld_hitcount = isset( $prop['hitcount'] ); |
| 52 |
$fld_defined = isset( $prop['defined'] ); |
| 53 |
$fld_source = isset( $prop['source'] ); |
| 54 |
$fld_active = isset( $prop['active'] ); |
| 55 |
|
| 56 |
$limit = $params['limit']; |
| 57 |
$result = $this->getResult(); |
| 58 |
|
| 59 |
$softwareDefinedTags = array_fill_keys( $this->changeTagsStore->listSoftwareDefinedTags(), 0 ); |
| 60 |
$explicitlyDefinedTags = array_fill_keys( $this->changeTagsStore->listExplicitlyDefinedTags(), 0 ); |
| 61 |
$softwareActivatedTags = array_fill_keys( $this->changeTagsStore->listSoftwareActivatedTags(), 0 ); |
| 62 |
|
| 63 |
$tagHitcounts = array_merge( |
| 64 |
$softwareDefinedTags, |
| 65 |
$explicitlyDefinedTags, |
| 66 |
$this->changeTagsStore->tagUsageStatistics() |
| 67 |
); |
| 68 |
$tags = array_keys( $tagHitcounts ); |
| 69 |
|
| 70 |
|
| 71 |
if ( $params['continue'] !== null ) { |
| 72 |
$cont = $params['continue']; |
| 73 |
$tags = array_filter( $tags, static function ( $v ) use ( $cont ) { |
| 74 |
return $v >= $cont; |
| 75 |
} ); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
sort( $tags ); |
| 80 |
|
| 81 |
$count = 0; |
| 82 |
foreach ( $tags as $tagName ) { |
| 83 |
if ( ++$count > $limit ) { |
| 84 |
$this->setContinueEnumParameter( 'continue', $tagName ); |
| 85 |
break; |
| 86 |
} |
| 87 |
|
| 88 |
$tag = []; |
| 89 |
$tag['name'] = $tagName; |
| 90 |
|
| 91 |
if ( $fld_displayname ) { |
| 92 |
$tag['displayname'] = ChangeTags::tagDescription( $tagName, $this ); |
| 93 |
} |
| 94 |
|
| 95 |
if ( $fld_description ) { |
| 96 |
$msg = $this->msg( "tag-$tagName-description" ); |
| 97 |
$tag['description'] = $msg->exists() ? $msg->text() : ''; |
| 98 |
} |
| 99 |
|
| 100 |
if ( $fld_hitcount ) { |
| 101 |
$tag['hitcount'] = (int)$tagHitcounts[$tagName]; |
| 102 |
} |
| 103 |
|
| 104 |
$isSoftware = isset( $softwareDefinedTags[$tagName] ); |
| 105 |
$isExplicit = isset( $explicitlyDefinedTags[$tagName] ); |
| 106 |
|
| 107 |
if ( $fld_defined ) { |
| 108 |
$tag['defined'] = $isSoftware || $isExplicit; |
| 109 |
} |
| 110 |
|
| 111 |
if ( $fld_source ) { |
| 112 |
$tag['source'] = []; |
| 113 |
if ( $isSoftware ) { |
| 114 |
$tag['source'][] = 'software'; |
| 115 |
|
| 116 |
$tag['source'][] = 'extension'; |
| 117 |
} |
| 118 |
if ( $isExplicit ) { |
| 119 |
$tag['source'][] = 'manual'; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
if ( $fld_active ) { |
| 124 |
$tag['active'] = $isExplicit || isset( $softwareActivatedTags[$tagName] ); |
| 125 |
} |
| 126 |
|
| 127 |
$fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $tag ); |
| 128 |
if ( !$fit ) { |
| 129 |
$this->setContinueEnumParameter( 'continue', $tagName ); |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
$result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'tag' ); |
| 135 |
} |
| 136 |
|
| 137 |
public function getCacheMode( $params ) { |
| 138 |
return 'public'; |
| 139 |
} |
| 140 |
|
| 141 |
public function getAllowedParams() { |
| 142 |
return [ |
| 143 |
'continue' => [ |
| 144 |
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 145 |
], |
| 146 |
'limit' => [ |
| 147 |
ParamValidator::PARAM_DEFAULT => 10, |
| 148 |
ParamValidator::PARAM_TYPE => 'limit', |
| 149 |
IntegerDef::PARAM_MIN => 1, |
| 150 |
IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
| 151 |
IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
| 152 |
], |
| 153 |
'prop' => [ |
| 154 |
ParamValidator::PARAM_DEFAULT => '', |
| 155 |
ParamValidator::PARAM_TYPE => [ |
| 156 |
'displayname', |
| 157 |
'description', |
| 158 |
'hitcount', |
| 159 |
'defined', |
| 160 |
'source', |
| 161 |
'active', |
| 162 |
], |
| 163 |
ParamValidator::PARAM_ISMULTI => true, |
| 164 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
| 165 |
] |
| 166 |
]; |
| 167 |
} |
| 168 |
|
| 169 |
protected function getExamplesMessages() { |
| 170 |
return [ |
| 171 |
'action=query&list=tags&tgprop=displayname|description|hitcount|defined' |
| 172 |
=> 'apihelp-query+tags-example-simple', |
| 173 |
]; |
| 174 |
} |
| 175 |
|
| 176 |
public function getHelpUrls() { |
| 177 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tags'; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
class_alias( ApiQueryTags::class, 'ApiQueryTags' ); |