1<?php 2/** 3 * Created on 16 April 2011 4 * API for Gadgets extension 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License along 17 * with this program; if not, write to the Free Software Foundation, Inc., 18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 * http://www.gnu.org/copyleft/gpl.html 20 */ 21 22class ApiQueryGadgetCategories extends ApiQueryBase { 23 /** 24 * @var array 25 */ 26 private $props; 27 28 /** 29 * @var array|bool 30 */ 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 /** 50 * @return void 51 */ 52 private function getList() { 53 $data = []; 54 $result = $this->getResult(); 55 $gadgets = GadgetRepo::singleton()->getStructuredList(); 56 57 if ( $gadgets ) { 58 foreach ( $gadgets as $category => $list ) { 59 if ( !$this->neededNames || isset( $this->neededNames[$category] ) ) { 60 $row = []; 61 if ( isset( $this->props['name'] ) ) { 62 $row['name'] = $category; 63 } 64 65 if ( $category !== "" ) { 66 if ( isset( $this->props['title'] ) ) { 67 $row['desc'] = $this->msg( "gadget-section-$category" )->parse(); 68 } 69 } 70 71 if ( isset( $this->props['members'] ) ) { 72 $row['members'] = count( $list ); 73 } 74 75 $data[] = $row; 76 } 77 } 78 } 79 $result->setIndexedTagName( $data, 'category' ); 80 $result->addValue( 'query', $this->getModuleName(), $data ); 81 } 82 83 public function getAllowedParams() { 84 return [ 85 'prop' => [ 86 ApiBase::PARAM_DFLT => 'name', 87 ApiBase::PARAM_ISMULTI => true, 88 ApiBase::PARAM_TYPE => [ 89 'name', 90 'title', 91 'members', 92 ], 93 ], 94 'names' => [ 95 ApiBase::PARAM_TYPE => 'string', 96 ApiBase::PARAM_ISMULTI => true, 97 ], 98 ]; 99 } 100 101 /** 102 * @see ApiBase::getExamplesMessages() 103 * @return array 104 */ 105 protected function getExamplesMessages() { 106 return [ 107 'action=query&list=gadgetcategories' 108 => 'apihelp-query+gadgetcategories-example-1', 109 'action=query&list=gadgetcategories&gcnames=foo|bar&gcprop=name|title|members' 110 => 'apihelp-query+gadgetcategories-example-2', 111 ]; 112 } 113} 114