| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Extension\Gadgets\Api; |
| 22 |
|
| 23 |
use MediaWiki\Api\ApiBase; |
| 24 |
use MediaWiki\Api\ApiQuery; |
| 25 |
use MediaWiki\Api\ApiQueryBase; |
| 26 |
use MediaWiki\Api\ApiResult; |
| 27 |
use MediaWiki\Extension\Gadgets\GadgetRepo; |
| 28 |
use Wikimedia\ParamValidator\ParamValidator; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class ApiQueryGadgetCategories extends ApiQueryBase { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private $props; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private $neededNames; |
| 43 |
|
| 44 |
private GadgetRepo $gadgetRepo; |
| 45 |
|
| 46 |
public function __construct( ApiQuery $queryModule, $moduleName, GadgetRepo $gadgetRepo ) { |
| 47 |
parent::__construct( $queryModule, $moduleName, 'gc' ); |
| 48 |
$this->gadgetRepo = $gadgetRepo; |
| 49 |
} |
| 50 |
|
| 51 |
public function execute() { |
| 52 |
$params = $this->extractRequestParams(); |
| 53 |
$this->props = array_flip( $params['prop'] ); |
| 54 |
$this->neededNames = isset( $params['names'] ) |
| 55 |
? array_flip( $params['names'] ) |
| 56 |
: false; |
| 57 |
|
| 58 |
$this->getMain()->setCacheMode( 'public' ); |
| 59 |
|
| 60 |
$this->getList(); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
private function getList() { |
| 67 |
$data = []; |
| 68 |
$result = $this->getResult(); |
| 69 |
$gadgets = $this->gadgetRepo->getStructuredList(); |
| 70 |
|
| 71 |
if ( $gadgets ) { |
| 72 |
foreach ( $gadgets as $category => $list ) { |
| 73 |
if ( $this->neededNames && !isset( $this->neededNames[$category] ) ) { |
| 74 |
continue; |
| 75 |
} |
| 76 |
$row = []; |
| 77 |
if ( isset( $this->props['name'] ) ) { |
| 78 |
$row['name'] = $category; |
| 79 |
} |
| 80 |
|
| 81 |
if ( ( $category !== "" ) && isset( $this->props['title'] ) ) { |
| 82 |
$row['desc'] = $this->msg( "gadget-section-$category" )->parse(); |
| 83 |
} |
| 84 |
|
| 85 |
if ( isset( $this->props['members'] ) ) { |
| 86 |
$row['members'] = count( $list ); |
| 87 |
} |
| 88 |
|
| 89 |
$data[] = $row; |
| 90 |
} |
| 91 |
} |
| 92 |
ApiResult::setIndexedTagName( $data, 'category' ); |
| 93 |
$result->addValue( 'query', $this->getModuleName(), $data ); |
| 94 |
} |
| 95 |
|
| 96 |
public function getAllowedParams() { |
| 97 |
return [ |
| 98 |
'prop' => [ |
| 99 |
ParamValidator::PARAM_DEFAULT => 'name', |
| 100 |
ParamValidator::PARAM_ISMULTI => true, |
| 101 |
ParamValidator::PARAM_TYPE => [ |
| 102 |
'name', |
| 103 |
'title', |
| 104 |
'members', |
| 105 |
], |
| 106 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
| 107 |
], |
| 108 |
'names' => [ |
| 109 |
ParamValidator::PARAM_TYPE => 'string', |
| 110 |
ParamValidator::PARAM_ISMULTI => true, |
| 111 |
], |
| 112 |
]; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
protected function getExamplesMessages() { |
| 120 |
return [ |
| 121 |
'action=query&list=gadgetcategories' |
| 122 |
=> 'apihelp-query+gadgetcategories-example-1', |
| 123 |
'action=query&list=gadgetcategories&gcnames=foo|bar&gcprop=name|title|members' |
| 124 |
=> 'apihelp-query+gadgetcategories-example-2', |
| 125 |
]; |
| 126 |
} |
| 127 |
} |