]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - extensions/Gadgets/api/ApiQueryGadgets.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / extensions / Gadgets / api / ApiQueryGadgets.php
1 <?php
2 /**
3  * Created on 15 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
22 class ApiQueryGadgets extends ApiQueryBase {
23         /**
24          * @var array
25          */
26         private $props;
27
28         /**
29          * @var array|bool
30          */
31         private $categories;
32
33         /**
34          * @var array|bool
35          */
36         private $neededIds;
37
38         /**
39          * @var bool
40          */
41         private $listAllowed;
42
43         /**
44          * @var bool
45          */
46         private $listEnabled;
47
48         public function __construct( ApiQuery $queryModule, $moduleName ) {
49                 parent::__construct( $queryModule, $moduleName, 'ga' );
50         }
51
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'] )
57                         : false;
58                 $this->neededIds = isset( $params['ids'] )
59                         ? array_flip( $params['ids'] )
60                         : false;
61                 $this->listAllowed = isset( $params['allowedonly'] ) && $params['allowedonly'];
62                 $this->listEnabled = isset( $params['enabledonly'] ) && $params['enabledonly'];
63
64                 $this->getMain()->setCacheMode( $this->listAllowed || $this->listEnabled
65                         ? 'anon-public-user-private' : 'public' );
66
67                 $this->applyList( $this->getList() );
68         }
69
70         /**
71          * @return array
72          */
73         private function getList() {
74                 $gadgets = GadgetRepo::singleton()->getStructuredList();
75
76                 if ( $gadgets === false ) {
77                         return [];
78                 }
79
80                 $result = [];
81                 foreach ( $gadgets as $category => $list ) {
82                         if ( $this->categories && !isset( $this->categories[$category] ) ) {
83                                 continue;
84                         }
85
86                         foreach ( $list as $g ) {
87                                 if ( $this->isNeeded( $g ) ) {
88                                         $result[] = $g;
89                                 }
90                         }
91                 }
92                 return $result;
93         }
94
95         /**
96          * @param array $gadgets
97          */
98         private function applyList( $gadgets ) {
99                 $data = [];
100                 $result = $this->getResult();
101
102                 /**
103                  * @var $g Gadget
104                  */
105                 foreach ( $gadgets as $g ) {
106                         $row = [];
107                         if ( isset( $this->props['id'] ) ) {
108                                 $row['id'] = $g->getName();
109                         }
110
111                         if ( isset( $this->props['metadata'] ) ) {
112                                 $row['metadata'] = $this->fakeMetadata( $g );
113                                 $this->setIndexedTagNameForMetadata( $row['metadata'] );
114                         }
115
116                         if ( isset( $this->props['desc'] ) ) {
117                                 $row['desc'] = $g->getDescription();
118                         }
119
120                         $data[] = $row;
121                 }
122
123                 $result->setIndexedTagName( $data, 'gadget' );
124                 $result->addValue( 'query', $this->getModuleName(), $data );
125         }
126
127         /**
128          * @param Gadget $gadget
129          *
130          * @return bool
131          */
132         private function isNeeded( Gadget $gadget ) {
133                 $user = $this->getUser();
134
135                 return ( $this->neededIds === false || isset( $this->neededIds[$gadget->getName()] ) )
136                         && ( !$this->listAllowed || $gadget->isAllowed( $user ) )
137                         && ( !$this->listEnabled || $gadget->isEnabled( $user ) );
138         }
139
140         /**
141          * @param Gadget $g
142          * @return array
143          */
144         private function fakeMetadata( Gadget $g ) {
145                 return [
146                         'settings' => [
147                                 'rights' => $g->getRequiredRights(),
148                                 'skins' => $g->getRequiredSkins(),
149                                 'default' => $g->isOnByDefault(),
150                                 'hidden' => $g->isHidden(),
151                                 'shared' => false,
152                                 'category' => $g->getCategory(),
153                                 'legacyscripts' => (bool)$g->getLegacyScripts(),
154                         ],
155                         'module' => [
156                                 'scripts' => $g->getScripts(),
157                                 'styles' => $g->getStyles(),
158                                 'dependencies' => $g->getDependencies(),
159                                 'peers' => $g->getPeers(),
160                                 'messages' => $g->getMessages(),
161                         ]
162                 ];
163         }
164
165         private function setIndexedTagNameForMetadata( &$metadata ) {
166                 static $tagNames = [
167                         'rights' => 'right',
168                         'skins' => 'skin',
169                         'scripts' => 'script',
170                         'styles' => 'style',
171                         'dependencies' => 'dependency',
172                         'peers' => 'peer',
173                         'messages' => 'message',
174                 ];
175
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 );
182                                 }
183                         }
184                 }
185         }
186
187         public function getAllowedParams() {
188                 return [
189                         'prop' => [
190                                 ApiBase::PARAM_DFLT => 'id|metadata',
191                                 ApiBase::PARAM_ISMULTI => true,
192                                 ApiBase::PARAM_TYPE => [
193                                         'id',
194                                         'metadata',
195                                         'desc',
196                                 ],
197                         ],
198                         'categories' => [
199                                 ApiBase::PARAM_ISMULTI => true,
200                                 ApiBase::PARAM_TYPE => 'string',
201                         ],
202                         'ids' => [
203                                 ApiBase::PARAM_TYPE => 'string',
204                                 ApiBase::PARAM_ISMULTI => true,
205                         ],
206                         'allowedonly' => false,
207                         'enabledonly' => false,
208                 ];
209         }
210
211         /**
212          * @see ApiBase::getExamplesMessages()
213          * @return array
214          */
215         protected function getExamplesMessages() {
216                 $params = $this->getAllowedParams();
217                 $allProps = implode( '|', $params['prop'][ApiBase::PARAM_TYPE] );
218                 return [
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',
229                 ];
230         }
231 }