2011-04-16 16:29:33 +00:00
|
|
|
<?php
|
2021-10-09 00:34:45 +00:00
|
|
|
|
2022-02-06 18:54:47 +00:00
|
|
|
namespace MediaWiki\Extension\Gadgets\Api;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use ApiQuery;
|
|
|
|
use ApiQueryBase;
|
|
|
|
use ApiResult;
|
|
|
|
use MediaWiki\Extension\Gadgets\Gadget;
|
|
|
|
use MediaWiki\Extension\Gadgets\GadgetRepo;
|
2021-10-09 00:34:45 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
|
|
|
2011-04-16 16:29:33 +00:00
|
|
|
/**
|
|
|
|
* Created on 15 April 2011
|
|
|
|
* API for Gadgets extension
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ApiQueryGadgets extends ApiQueryBase {
|
2015-06-17 18:21:27 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $props;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array|bool
|
|
|
|
*/
|
|
|
|
private $categories;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array|bool
|
|
|
|
*/
|
|
|
|
private $neededIds;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $listAllowed;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $listEnabled;
|
|
|
|
|
|
|
|
public function __construct( ApiQuery $queryModule, $moduleName ) {
|
|
|
|
parent::__construct( $queryModule, $moduleName, 'ga' );
|
2011-04-16 16:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$this->props = array_flip( $params['prop'] );
|
2011-04-16 18:39:36 +00:00
|
|
|
$this->categories = isset( $params['categories'] )
|
|
|
|
? array_flip( $params['categories'] )
|
|
|
|
: false;
|
2011-09-12 11:17:44 +00:00
|
|
|
$this->neededIds = isset( $params['ids'] )
|
|
|
|
? array_flip( $params['ids'] )
|
2011-04-16 18:39:36 +00:00
|
|
|
: false;
|
2011-07-25 16:01:52 +00:00
|
|
|
$this->listAllowed = isset( $params['allowedonly'] ) && $params['allowedonly'];
|
|
|
|
$this->listEnabled = isset( $params['enabledonly'] ) && $params['enabledonly'];
|
2011-04-16 18:39:36 +00:00
|
|
|
|
|
|
|
$this->getMain()->setCacheMode( $this->listAllowed || $this->listEnabled
|
|
|
|
? 'anon-public-user-private' : 'public' );
|
2011-04-16 16:29:33 +00:00
|
|
|
|
|
|
|
$this->applyList( $this->getList() );
|
|
|
|
}
|
|
|
|
|
2011-09-09 11:11:22 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-04-16 16:29:33 +00:00
|
|
|
private function getList() {
|
2015-07-18 22:40:42 +00:00
|
|
|
$gadgets = GadgetRepo::singleton()->getStructuredList();
|
2011-04-16 16:29:33 +00:00
|
|
|
|
2019-03-17 23:39:02 +00:00
|
|
|
if ( !$gadgets ) {
|
2016-12-28 10:25:47 +00:00
|
|
|
return [];
|
2013-09-11 19:48:15 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 10:25:47 +00:00
|
|
|
$result = [];
|
2011-04-16 16:29:33 +00:00
|
|
|
foreach ( $gadgets as $category => $list ) {
|
2011-04-16 18:39:36 +00:00
|
|
|
if ( $this->categories && !isset( $this->categories[$category] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-09-23 06:48:37 +00:00
|
|
|
|
2011-04-16 16:29:33 +00:00
|
|
|
foreach ( $list as $g ) {
|
|
|
|
if ( $this->isNeeded( $g ) ) {
|
|
|
|
$result[] = $g;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2011-09-10 22:07:16 +00:00
|
|
|
/**
|
2017-08-27 14:07:08 +00:00
|
|
|
* @param array $gadgets
|
2011-09-10 22:07:16 +00:00
|
|
|
*/
|
2011-04-16 16:29:33 +00:00
|
|
|
private function applyList( $gadgets ) {
|
2016-12-28 10:25:47 +00:00
|
|
|
$data = [];
|
2011-04-16 16:29:33 +00:00
|
|
|
$result = $this->getResult();
|
|
|
|
|
2012-02-17 21:23:46 +00:00
|
|
|
/**
|
|
|
|
* @var $g Gadget
|
|
|
|
*/
|
2011-04-16 16:29:33 +00:00
|
|
|
foreach ( $gadgets as $g ) {
|
2016-12-28 10:25:47 +00:00
|
|
|
$row = [];
|
2011-09-12 11:17:44 +00:00
|
|
|
if ( isset( $this->props['id'] ) ) {
|
|
|
|
$row['id'] = $g->getName();
|
2011-04-16 16:29:33 +00:00
|
|
|
}
|
2011-09-23 06:48:37 +00:00
|
|
|
|
2011-09-01 16:08:47 +00:00
|
|
|
if ( isset( $this->props['metadata'] ) ) {
|
|
|
|
$row['metadata'] = $this->fakeMetadata( $g );
|
|
|
|
$this->setIndexedTagNameForMetadata( $row['metadata'] );
|
|
|
|
}
|
2011-09-23 06:48:37 +00:00
|
|
|
|
2011-04-16 16:29:33 +00:00
|
|
|
if ( isset( $this->props['desc'] ) ) {
|
|
|
|
$row['desc'] = $g->getDescription();
|
|
|
|
}
|
2011-09-23 06:48:37 +00:00
|
|
|
|
2011-04-16 16:29:33 +00:00
|
|
|
$data[] = $row;
|
|
|
|
}
|
2011-09-23 06:48:37 +00:00
|
|
|
|
2021-10-09 00:34:45 +00:00
|
|
|
ApiResult::setIndexedTagName( $data, 'gadget' );
|
2011-04-16 16:29:33 +00:00
|
|
|
$result->addValue( 'query', $this->getModuleName(), $data );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-27 14:07:08 +00:00
|
|
|
* @param Gadget $gadget
|
2011-09-10 22:07:16 +00:00
|
|
|
*
|
2011-09-09 11:11:22 +00:00
|
|
|
* @return bool
|
2011-04-16 16:29:33 +00:00
|
|
|
*/
|
|
|
|
private function isNeeded( Gadget $gadget ) {
|
2012-08-17 09:28:09 +00:00
|
|
|
$user = $this->getUser();
|
2011-04-16 18:39:36 +00:00
|
|
|
|
2011-09-12 11:17:44 +00:00
|
|
|
return ( $this->neededIds === false || isset( $this->neededIds[$gadget->getName()] ) )
|
2012-08-17 09:28:09 +00:00
|
|
|
&& ( !$this->listAllowed || $gadget->isAllowed( $user ) )
|
|
|
|
&& ( !$this->listEnabled || $gadget->isEnabled( $user ) );
|
2011-04-16 16:29:33 +00:00
|
|
|
}
|
2011-09-09 11:11:22 +00:00
|
|
|
|
2011-09-10 22:07:16 +00:00
|
|
|
/**
|
2017-08-27 14:07:08 +00:00
|
|
|
* @param Gadget $g
|
2011-09-10 22:07:16 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2011-09-01 16:08:47 +00:00
|
|
|
private function fakeMetadata( Gadget $g ) {
|
2016-12-28 10:25:47 +00:00
|
|
|
return [
|
|
|
|
'settings' => [
|
2011-09-01 16:08:47 +00:00
|
|
|
'rights' => $g->getRequiredRights(),
|
2011-10-22 19:09:25 +00:00
|
|
|
'skins' => $g->getRequiredSkins(),
|
2021-12-14 13:10:22 +00:00
|
|
|
'actions' => $g->getRequiredActions(),
|
2011-09-01 16:08:47 +00:00
|
|
|
'default' => $g->isOnByDefault(),
|
2015-08-03 06:34:16 +00:00
|
|
|
'hidden' => $g->isHidden(),
|
2021-10-17 13:05:15 +00:00
|
|
|
'package' => $g->isPackaged(),
|
2015-08-03 06:34:16 +00:00
|
|
|
'shared' => false,
|
2011-09-01 16:08:47 +00:00
|
|
|
'category' => $g->getCategory(),
|
2015-06-13 02:33:35 +00:00
|
|
|
'legacyscripts' => (bool)$g->getLegacyScripts(),
|
2022-01-11 09:03:44 +00:00
|
|
|
'supportsUrlLoad' => $g->supportsUrlLoad(),
|
2016-12-28 10:25:47 +00:00
|
|
|
],
|
|
|
|
'module' => [
|
2011-09-01 16:08:47 +00:00
|
|
|
'scripts' => $g->getScripts(),
|
|
|
|
'styles' => $g->getStyles(),
|
2021-10-17 13:05:15 +00:00
|
|
|
'datas' => $g->getJSONs(),
|
2011-09-01 16:08:47 +00:00
|
|
|
'dependencies' => $g->getDependencies(),
|
2016-11-18 04:54:17 +00:00
|
|
|
'peers' => $g->getPeers(),
|
2015-08-03 06:34:16 +00:00
|
|
|
'messages' => $g->getMessages(),
|
2016-12-28 10:25:47 +00:00
|
|
|
]
|
|
|
|
];
|
2011-09-01 16:08:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 09:32:09 +00:00
|
|
|
/**
|
|
|
|
* @param array[] &$metadata
|
|
|
|
*/
|
2011-09-01 16:08:47 +00:00
|
|
|
private function setIndexedTagNameForMetadata( &$metadata ) {
|
2016-12-28 10:25:47 +00:00
|
|
|
static $tagNames = [
|
2011-09-01 16:08:47 +00:00
|
|
|
'rights' => 'right',
|
2011-10-22 19:09:25 +00:00
|
|
|
'skins' => 'skin',
|
2021-12-14 13:10:22 +00:00
|
|
|
'actions' => 'action',
|
2011-09-01 16:08:47 +00:00
|
|
|
'scripts' => 'script',
|
|
|
|
'styles' => 'style',
|
2021-10-17 13:05:15 +00:00
|
|
|
'datas' => 'data',
|
2011-09-01 16:08:47 +00:00
|
|
|
'dependencies' => 'dependency',
|
2016-11-18 04:54:17 +00:00
|
|
|
'peers' => 'peer',
|
2011-09-01 16:08:47 +00:00
|
|
|
'messages' => 'message',
|
2016-12-28 10:25:47 +00:00
|
|
|
];
|
2011-09-09 11:11:22 +00:00
|
|
|
|
2017-10-09 23:20:26 +00:00
|
|
|
foreach ( $metadata as $data ) {
|
|
|
|
foreach ( $data as $key => $value ) {
|
2011-09-01 16:08:47 +00:00
|
|
|
if ( is_array( $value ) ) {
|
2019-03-17 23:39:02 +00:00
|
|
|
$tag = $tagNames[$key] ?? $key;
|
2021-10-09 00:34:45 +00:00
|
|
|
ApiResult::setIndexedTagName( $value, $tag );
|
2011-09-01 16:08:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-16 16:29:33 +00:00
|
|
|
|
|
|
|
public function getAllowedParams() {
|
2016-12-28 10:25:47 +00:00
|
|
|
return [
|
|
|
|
'prop' => [
|
2021-10-09 00:34:45 +00:00
|
|
|
ParamValidator::PARAM_DEFAULT => 'id|metadata',
|
2022-04-03 23:27:06 +00:00
|
|
|
ParamValidator::PARAM_ISMULTI => true,
|
|
|
|
ParamValidator::PARAM_TYPE => [
|
2011-09-12 11:17:44 +00:00
|
|
|
'id',
|
2011-09-01 16:08:47 +00:00
|
|
|
'metadata',
|
2011-04-16 16:29:33 +00:00
|
|
|
'desc',
|
2016-12-28 10:25:47 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'categories' => [
|
2022-04-03 23:27:06 +00:00
|
|
|
ParamValidator::PARAM_ISMULTI => true,
|
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
2016-12-28 10:25:47 +00:00
|
|
|
],
|
|
|
|
'ids' => [
|
2022-04-03 23:27:06 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
ParamValidator::PARAM_ISMULTI => true,
|
2016-12-28 10:25:47 +00:00
|
|
|
],
|
2011-07-25 16:01:52 +00:00
|
|
|
'allowedonly' => false,
|
|
|
|
'enabledonly' => false,
|
2016-12-28 10:25:47 +00:00
|
|
|
];
|
2011-04-16 16:29:33 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 15:57:35 +00:00
|
|
|
/**
|
|
|
|
* @see ApiBase::getExamplesMessages()
|
2017-08-27 14:07:08 +00:00
|
|
|
* @return array
|
2014-10-29 15:57:35 +00:00
|
|
|
*/
|
|
|
|
protected function getExamplesMessages() {
|
|
|
|
$params = $this->getAllowedParams();
|
2022-04-03 23:27:06 +00:00
|
|
|
$allProps = implode( '|', $params['prop'][ParamValidator::PARAM_TYPE] );
|
2016-12-28 10:25:47 +00:00
|
|
|
return [
|
2014-10-29 15:57:35 +00:00
|
|
|
'action=query&list=gadgets&gaprop=id|desc'
|
|
|
|
=> 'apihelp-query+gadgets-example-1',
|
|
|
|
"action=query&list=gadgets&gaprop=$allProps"
|
|
|
|
=> 'apihelp-query+gadgets-example-2',
|
|
|
|
'action=query&list=gadgets&gacategories=foo'
|
|
|
|
=> 'apihelp-query+gadgets-example-3',
|
|
|
|
'action=query&list=gadgets&gaids=foo|bar&gaprop=id|desc|metadata'
|
|
|
|
=> 'apihelp-query+gadgets-example-4',
|
|
|
|
'action=query&list=gadgets&gaenabledonly'
|
|
|
|
=> 'apihelp-query+gadgets-example-5',
|
2016-12-28 10:25:47 +00:00
|
|
|
];
|
2011-04-16 16:29:33 +00:00
|
|
|
}
|
2011-04-16 18:39:36 +00:00
|
|
|
}
|