3 * Created on 15 April 2011
4 * API for Gadgets extension
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.
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.
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
22 class ApiQueryGadgets extends ApiQueryBase {
48 public function __construct( ApiQuery $queryModule, $moduleName ) {
49 parent::__construct( $queryModule, $moduleName, 'ga' );
52 public function execute() {
53 $params = $this->extractRequestParams();
54 $this->props = array_flip( $params['prop'] );
55 $this->categories = isset( $params['categories'] )
56 ? array_flip( $params['categories'] )
58 $this->neededIds = isset( $params['ids'] )
59 ? array_flip( $params['ids'] )
61 $this->listAllowed = isset( $params['allowedonly'] ) && $params['allowedonly'];
62 $this->listEnabled = isset( $params['enabledonly'] ) && $params['enabledonly'];
64 $this->getMain()->setCacheMode( $this->listAllowed || $this->listEnabled
65 ? 'anon-public-user-private' : 'public' );
67 $this->applyList( $this->getList() );
73 private function getList() {
74 $gadgets = GadgetRepo::singleton()->getStructuredList();
76 if ( $gadgets === false ) {
81 foreach ( $gadgets as $category => $list ) {
82 if ( $this->categories && !isset( $this->categories[$category] ) ) {
86 foreach ( $list as $g ) {
87 if ( $this->isNeeded( $g ) ) {
96 * @param array $gadgets
98 private function applyList( $gadgets ) {
100 $result = $this->getResult();
105 foreach ( $gadgets as $g ) {
107 if ( isset( $this->props['id'] ) ) {
108 $row['id'] = $g->getName();
111 if ( isset( $this->props['metadata'] ) ) {
112 $row['metadata'] = $this->fakeMetadata( $g );
113 $this->setIndexedTagNameForMetadata( $row['metadata'] );
116 if ( isset( $this->props['desc'] ) ) {
117 $row['desc'] = $g->getDescription();
123 $result->setIndexedTagName( $data, 'gadget' );
124 $result->addValue( 'query', $this->getModuleName(), $data );
128 * @param Gadget $gadget
132 private function isNeeded( Gadget $gadget ) {
133 $user = $this->getUser();
135 return ( $this->neededIds === false || isset( $this->neededIds[$gadget->getName()] ) )
136 && ( !$this->listAllowed || $gadget->isAllowed( $user ) )
137 && ( !$this->listEnabled || $gadget->isEnabled( $user ) );
144 private function fakeMetadata( Gadget $g ) {
147 'rights' => $g->getRequiredRights(),
148 'skins' => $g->getRequiredSkins(),
149 'default' => $g->isOnByDefault(),
150 'hidden' => $g->isHidden(),
152 'category' => $g->getCategory(),
153 'legacyscripts' => (bool)$g->getLegacyScripts(),
156 'scripts' => $g->getScripts(),
157 'styles' => $g->getStyles(),
158 'dependencies' => $g->getDependencies(),
159 'peers' => $g->getPeers(),
160 'messages' => $g->getMessages(),
165 private function setIndexedTagNameForMetadata( &$metadata ) {
169 'scripts' => 'script',
171 'dependencies' => 'dependency',
173 'messages' => 'message',
176 $result = $this->getResult();
177 foreach ( $metadata as &$data ) {
178 foreach ( $data as $key => &$value ) {
179 if ( is_array( $value ) ) {
180 $tag = isset( $tagNames[$key] ) ? $tagNames[$key] : $key;
181 $result->setIndexedTagName( $value, $tag );
187 public function getAllowedParams() {
190 ApiBase::PARAM_DFLT => 'id|metadata',
191 ApiBase::PARAM_ISMULTI => true,
192 ApiBase::PARAM_TYPE => [
199 ApiBase::PARAM_ISMULTI => true,
200 ApiBase::PARAM_TYPE => 'string',
203 ApiBase::PARAM_TYPE => 'string',
204 ApiBase::PARAM_ISMULTI => true,
206 'allowedonly' => false,
207 'enabledonly' => false,
212 * @see ApiBase::getExamplesMessages()
215 protected function getExamplesMessages() {
216 $params = $this->getAllowedParams();
217 $allProps = implode( '|', $params['prop'][ApiBase::PARAM_TYPE] );
219 'action=query&list=gadgets&gaprop=id|desc'
220 => 'apihelp-query+gadgets-example-1',
221 "action=query&list=gadgets&gaprop=$allProps"
222 => 'apihelp-query+gadgets-example-2',
223 'action=query&list=gadgets&gacategories=foo'
224 => 'apihelp-query+gadgets-example-3',
225 'action=query&list=gadgets&gaids=foo|bar&gaprop=id|desc|metadata'
226 => 'apihelp-query+gadgets-example-4',
227 'action=query&list=gadgets&gaenabledonly'
228 => 'apihelp-query+gadgets-example-5',