MediaWiki  1.34.0
ApiQueryGadgetCategories.php
Go to the documentation of this file.
1 <?php
26  private $props;
27 
31  private $neededNames;
32 
33  public function __construct( ApiQuery $queryModule, $moduleName ) {
34  parent::__construct( $queryModule, $moduleName, 'gc' );
35  }
36 
37  public function execute() {
38  $params = $this->extractRequestParams();
39  $this->props = array_flip( $params['prop'] );
40  $this->neededNames = isset( $params['names'] )
41  ? array_flip( $params['names'] )
42  : false;
43 
44  $this->getMain()->setCacheMode( 'public' );
45 
46  $this->getList();
47  }
48 
49  private function getList() {
50  $data = [];
51  $result = $this->getResult();
52  $gadgets = GadgetRepo::singleton()->getStructuredList();
53 
54  if ( $gadgets ) {
55  foreach ( $gadgets as $category => $list ) {
56  if ( !$this->neededNames || isset( $this->neededNames[$category] ) ) {
57  $row = [];
58  if ( isset( $this->props['name'] ) ) {
59  $row['name'] = $category;
60  }
61 
62  if ( $category !== "" ) {
63  if ( isset( $this->props['title'] ) ) {
64  $row['desc'] = $this->msg( "gadget-section-$category" )->parse();
65  }
66  }
67 
68  if ( isset( $this->props['members'] ) ) {
69  $row['members'] = count( $list );
70  }
71 
72  $data[] = $row;
73  }
74  }
75  }
76  $result->setIndexedTagName( $data, 'category' );
77  $result->addValue( 'query', $this->getModuleName(), $data );
78  }
79 
80  public function getAllowedParams() {
81  return [
82  'prop' => [
83  ApiBase::PARAM_DFLT => 'name',
84  ApiBase::PARAM_ISMULTI => true,
86  'name',
87  'title',
88  'members',
89  ],
90  ],
91  'names' => [
92  ApiBase::PARAM_TYPE => 'string',
93  ApiBase::PARAM_ISMULTI => true,
94  ],
95  ];
96  }
97 
102  protected function getExamplesMessages() {
103  return [
104  'action=query&list=gadgetcategories'
105  => 'apihelp-query+gadgetcategories-example-1',
106  'action=query&list=gadgetcategories&gcnames=foo|bar&gcprop=name|title|members'
107  => 'apihelp-query+gadgetcategories-example-2',
108  ];
109  }
110 }
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
ApiQueryGadgetCategories\__construct
__construct(ApiQuery $queryModule, $moduleName)
Definition: ApiQueryGadgetCategories.php:33
ApiQueryGadgetCategories\getList
getList()
Definition: ApiQueryGadgetCategories.php:49
ApiQueryGadgetCategories\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryGadgetCategories.php:80
ApiQueryGadgetCategories\getExamplesMessages
getExamplesMessages()
Definition: ApiQueryGadgetCategories.php:102
ApiQueryBase
This is a base class for all Query modules.
Definition: ApiQueryBase.php:34
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
GadgetRepo\singleton
static singleton()
Get the configured default GadgetRepo.
Definition: GadgetRepo.php:88
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:168
ApiQueryGadgetCategories
Created on 16 April 2011 API for Gadgets extension.
Definition: ApiQueryGadgetCategories.php:22
ApiQueryGadgetCategories\$neededNames
array bool $neededNames
Definition: ApiQueryGadgetCategories.php:31
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:58
ApiBase\getMain
getMain()
Get the main module.
Definition: ApiBase.php:536
ApiQueryGadgetCategories\$props
array $props
Definition: ApiQueryGadgetCategories.php:26
ApiQueryGadgetCategories\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryGadgetCategories.php:37