Index: trunk/extensions/Gadgets/ApiQueryGadgets.php |
— | — | @@ -0,0 +1,162 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Created on 15 April 2011 |
| 5 | + * API for Gadgets extension |
| 6 | + * |
| 7 | + * This program is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License along |
| 18 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | + * http://www.gnu.org/copyleft/gpl.html |
| 21 | + */ |
| 22 | + |
| 23 | +class ApiQueryGadgets extends ApiQueryBase { |
| 24 | + private $props; |
| 25 | + |
| 26 | + public function __construct( $query, $moduleName ) { |
| 27 | + parent::__construct( $query, $moduleName, 'ga' ); |
| 28 | + } |
| 29 | + |
| 30 | + public function execute() { |
| 31 | + $params = $this->extractRequestParams(); |
| 32 | + $this->props = array_flip( $params['prop'] ); |
| 33 | + |
| 34 | + $this->applyList( $this->getList() ); |
| 35 | + } |
| 36 | + |
| 37 | + private function getList() { |
| 38 | + $gadgets = Gadget::loadStructuredList(); |
| 39 | + |
| 40 | + $result = array(); |
| 41 | + foreach ( $gadgets as $category => $list ) { |
| 42 | + foreach ( $list as $g ) { |
| 43 | + if ( $this->isNeeded( $g ) ) { |
| 44 | + $result[] = $g; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + return $result; |
| 49 | + } |
| 50 | + |
| 51 | + private function applyList( $gadgets ) { |
| 52 | + $data = array(); |
| 53 | + $result = $this->getResult(); |
| 54 | + |
| 55 | + foreach ( $gadgets as $g ) { |
| 56 | + $row = array(); |
| 57 | + if ( isset( $this->props['name'] ) ) { |
| 58 | + $row['name'] = $g->getName(); |
| 59 | + } |
| 60 | + if ( isset( $this->props['desc'] ) ) { |
| 61 | + $row['desc'] = $g->getDescription(); |
| 62 | + } |
| 63 | + if ( isset( $this->props['desc-raw'] ) ) { |
| 64 | + $row['desc-raw'] = $g->getRawDescription(); |
| 65 | + } |
| 66 | + if ( isset( $this->props['category'] ) ) { |
| 67 | + $row['category'] = $g->getCategory(); |
| 68 | + } |
| 69 | + if ( isset( $this->props['resourceloader'] ) && $g->supportsResourceLoader() ) { |
| 70 | + $row['resourceloader'] = ''; |
| 71 | + } |
| 72 | + if ( isset( $this->props['scripts'] ) ) { |
| 73 | + $row['scripts'] = $g->getScripts(); |
| 74 | + $result->setIndexedTagName( $row['scripts'], 'script' ); |
| 75 | + } |
| 76 | + if ( isset( $this->props['styles'] ) ) { |
| 77 | + $row['styles'] = $g->getStyles(); |
| 78 | + $result->setIndexedTagName( $row['styles'], 'style' ); |
| 79 | + } |
| 80 | + if ( isset( $this->props['dependencies'] ) ) { |
| 81 | + $row['dependencies'] = $g->getDependencies(); |
| 82 | + $result->setIndexedTagName( $row['dependencies'], 'module' ); |
| 83 | + } |
| 84 | + if ( isset( $this->props['rights'] ) ) { |
| 85 | + $row['rights'] = $g->getRequiredRights(); |
| 86 | + $result->setIndexedTagName( $row['rights'], 'right' ); |
| 87 | + } |
| 88 | + if ( isset( $this->props['default'] ) && $g->isOnByDefault() ) { |
| 89 | + $row['default'] = ''; |
| 90 | + } |
| 91 | + if ( isset( $this->props['definition'] ) ) { |
| 92 | + $row['definition'] = $g->getDefinition(); |
| 93 | + } |
| 94 | + $data[] = $row; |
| 95 | + } |
| 96 | + $result->setIndexedTagName( $data, 'gadget' ); |
| 97 | + $result->addValue( 'query', $this->getModuleName(), $data ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * |
| 102 | + */ |
| 103 | + private function isNeeded( Gadget $gadget ) { |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + public function getAllowedParams() { |
| 108 | + return array( |
| 109 | + 'prop' => array( |
| 110 | + ApiBase::PARAM_DFLT => 'name', |
| 111 | + ApiBase::PARAM_ISMULTI => true, |
| 112 | + ApiBase::PARAM_TYPE => array( |
| 113 | + 'name', |
| 114 | + 'desc', |
| 115 | + 'desc-raw', |
| 116 | + 'category', |
| 117 | + 'resourceloader', |
| 118 | + 'scripts', |
| 119 | + 'styles', |
| 120 | + 'dependencies', |
| 121 | + 'rights', |
| 122 | + 'default', |
| 123 | + 'definition', |
| 124 | + ), |
| 125 | + ), |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + public function getDescription() { |
| 130 | + return 'Returns a list of gadgets used on this wiki'; |
| 131 | + } |
| 132 | + |
| 133 | + public function getParamDescription() { |
| 134 | + return array( |
| 135 | + 'prop' => array( |
| 136 | + 'What gadget information to get:', |
| 137 | + ' name - Internal gadget name', |
| 138 | + ' desc - Gadget description transformed into HTML', |
| 139 | + ' desc-raw - Gadget description in raw wikitext', |
| 140 | + ' category - Internal name of a category gadget belongs to (empty if top-level gadget)', |
| 141 | + ' resourceloader - Whether gadget supports ResourceLoader', |
| 142 | + " scripts - List of gadget's scripts", |
| 143 | + " styles - List of gadget's styles", |
| 144 | + ' dependencies - List of ResourceLoader modules gadget depends on', |
| 145 | + ' rights - List of rights required to use gadget, if any', |
| 146 | + ' default - Whether gadget is enabled by default', |
| 147 | + ' definition - Line from MediaWiki:Gadgets-definition used to define the gadget', |
| 148 | + ), |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + protected function getExamples() { |
| 153 | + return array( |
| 154 | + 'Get a list of gadgets along with their descriptions:', |
| 155 | + ' api.php?action=query&list=gadgets&gaprop=name|desc', |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + public function getVersion() { |
| 160 | + return __CLASS__ . ': $Id: $'; |
| 161 | + } |
| 162 | + |
| 163 | +} |
\ No newline at end of file |
Property changes on: trunk/extensions/Gadgets/ApiQueryGadgets.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 164 | + native |
Index: trunk/extensions/Gadgets/Gadgets.php |
— | — | @@ -39,6 +39,7 @@ |
40 | 40 | $wgExtensionMessagesFiles['Gadgets'] = $dir . 'Gadgets.i18n.php'; |
41 | 41 | $wgExtensionAliasesFiles['Gadgets'] = $dir . 'Gadgets.alias.php'; |
42 | 42 | |
| 43 | +$wgAutoloadClasses['ApiQueryGadgets'] = $dir . 'ApiQueryGadgets.php'; |
43 | 44 | $wgAutoloadClasses['Gadget'] = $dir . 'Gadgets_body.php'; |
44 | 45 | $wgAutoloadClasses['GadgetHooks'] = $dir . 'Gadgets_body.php'; |
45 | 46 | $wgAutoloadClasses['GadgetResourceLoaderModule'] = $dir . 'Gadgets_body.php'; |
— | — | @@ -46,3 +47,5 @@ |
47 | 48 | |
48 | 49 | $wgSpecialPages['Gadgets'] = 'SpecialGadgets'; |
49 | 50 | $wgSpecialPageGroups['Gadgets'] = 'wiki'; |
| 51 | + |
| 52 | +$wgAPIListModules['gadgets'] = 'ApiQueryGadgets'; |