2011-04-16 16:29:33 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2013-09-11 19:48:15 +00:00
|
|
|
if ( $gadgets === false ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2011-04-16 16:29:33 +00:00
|
|
|
$result = array();
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @param $gadgets array
|
|
|
|
*/
|
2011-04-16 16:29:33 +00:00
|
|
|
private function applyList( $gadgets ) {
|
|
|
|
$data = array();
|
|
|
|
$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 ) {
|
|
|
|
$row = array();
|
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
|
|
|
|
2011-04-16 16:29:33 +00:00
|
|
|
$result->setIndexedTagName( $data, 'gadget' );
|
|
|
|
$result->addValue( 'query', $this->getModuleName(), $data );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-09-10 22:07:16 +00:00
|
|
|
* @param $gadget Gadget
|
|
|
|
*
|
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
|
|
|
/**
|
|
|
|
* @param $g Gadget
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-09-01 16:08:47 +00:00
|
|
|
private function fakeMetadata( Gadget $g ) {
|
|
|
|
return array(
|
|
|
|
'settings' => array(
|
|
|
|
'rights' => $g->getRequiredRights(),
|
2011-10-22 19:09:25 +00:00
|
|
|
'skins' => $g->getRequiredSkins(),
|
2011-09-01 16:08:47 +00:00
|
|
|
'default' => $g->isOnByDefault(),
|
2015-08-03 06:34:16 +00:00
|
|
|
'hidden' => $g->isHidden(),
|
|
|
|
'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(),
|
2011-09-01 16:08:47 +00:00
|
|
|
),
|
|
|
|
'module' => array(
|
|
|
|
'scripts' => $g->getScripts(),
|
|
|
|
'styles' => $g->getStyles(),
|
|
|
|
'dependencies' => $g->getDependencies(),
|
2015-08-03 06:34:16 +00:00
|
|
|
'messages' => $g->getMessages(),
|
2011-09-01 16:08:47 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setIndexedTagNameForMetadata( &$metadata ) {
|
|
|
|
static $tagNames = array(
|
|
|
|
'rights' => 'right',
|
2011-10-22 19:09:25 +00:00
|
|
|
'skins' => 'skin',
|
2011-09-01 16:08:47 +00:00
|
|
|
'scripts' => 'script',
|
|
|
|
'styles' => 'style',
|
|
|
|
'dependencies' => 'dependency',
|
|
|
|
'messages' => 'message',
|
|
|
|
);
|
2011-09-09 11:11:22 +00:00
|
|
|
|
2011-09-01 16:08:47 +00:00
|
|
|
$result = $this->getResult();
|
2011-09-09 11:11:22 +00:00
|
|
|
foreach ( $metadata as &$data ) {
|
2011-09-01 16:08:47 +00:00
|
|
|
foreach ( $data as $key => &$value ) {
|
|
|
|
if ( is_array( $value ) ) {
|
|
|
|
$tag = isset( $tagNames[$key] ) ? $tagNames[$key] : $key;
|
|
|
|
$result->setIndexedTagName( $value, $tag );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-16 16:29:33 +00:00
|
|
|
|
|
|
|
public function getAllowedParams() {
|
|
|
|
return array(
|
|
|
|
'prop' => array(
|
2011-09-12 11:17:44 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'id|metadata',
|
2011-04-16 16:29:33 +00:00
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
ApiBase::PARAM_TYPE => array(
|
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',
|
|
|
|
),
|
|
|
|
),
|
2011-04-16 18:39:36 +00:00
|
|
|
'categories' => array(
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
),
|
2011-09-12 11:17:44 +00:00
|
|
|
'ids' => array(
|
2011-04-16 18:39:36 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
),
|
2011-07-25 16:01:52 +00:00
|
|
|
'allowedonly' => false,
|
|
|
|
'enabledonly' => false,
|
2011-04-16 16:29:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-29 15:57:35 +00:00
|
|
|
/**
|
|
|
|
* @see ApiBase::getExamplesMessages()
|
|
|
|
*/
|
|
|
|
protected function getExamplesMessages() {
|
|
|
|
$params = $this->getAllowedParams();
|
|
|
|
$allProps = implode( '|', $params['prop'][ApiBase::PARAM_TYPE] );
|
|
|
|
return array(
|
|
|
|
'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',
|
|
|
|
);
|
2011-04-16 16:29:33 +00:00
|
|
|
}
|
2011-04-16 18:39:36 +00:00
|
|
|
}
|