[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
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 private $props, 24 $categories, 25 $neededIds, 26 $listAllowed, 27 $listEnabled; 28 29 public function __construct( $query, $moduleName ) { 30 parent::__construct( $query, $moduleName, 'ga' ); 31 } 32 33 public function execute() { 34 $params = $this->extractRequestParams(); 35 $this->props = array_flip( $params['prop'] ); 36 $this->categories = isset( $params['categories'] ) 37 ? array_flip( $params['categories'] ) 38 : false; 39 $this->neededIds = isset( $params['ids'] ) 40 ? array_flip( $params['ids'] ) 41 : false; 42 $this->listAllowed = isset( $params['allowedonly'] ) && $params['allowedonly']; 43 $this->listEnabled = isset( $params['enabledonly'] ) && $params['enabledonly']; 44 45 $this->getMain()->setCacheMode( $this->listAllowed || $this->listEnabled 46 ? 'anon-public-user-private' : 'public' ); 47 48 $this->applyList( $this->getList() ); 49 } 50 51 /** 52 * @return array 53 */ 54 private function getList() { 55 $gadgets = Gadget::loadStructuredList(); 56 57 if ( $gadgets === false ) { 58 return array(); 59 } 60 61 $result = array(); 62 foreach ( $gadgets as $category => $list ) { 63 if ( $this->categories && !isset( $this->categories[$category] ) ) { 64 continue; 65 } 66 67 foreach ( $list as $g ) { 68 if ( $this->isNeeded( $g ) ) { 69 $result[] = $g; 70 } 71 } 72 } 73 return $result; 74 } 75 76 /** 77 * @param $gadgets array 78 */ 79 private function applyList( $gadgets ) { 80 $data = array(); 81 $result = $this->getResult(); 82 83 /** 84 * @var $g Gadget 85 */ 86 foreach ( $gadgets as $g ) { 87 $row = array(); 88 if ( isset( $this->props['id'] ) ) { 89 $row['id'] = $g->getName(); 90 } 91 92 if ( isset( $this->props['metadata'] ) ) { 93 $row['metadata'] = $this->fakeMetadata( $g ); 94 $this->setIndexedTagNameForMetadata( $row['metadata'] ); 95 } 96 97 if ( isset( $this->props['desc'] ) ) { 98 $row['desc'] = $g->getDescription(); 99 } 100 101 $data[] = $row; 102 } 103 104 $result->setIndexedTagName( $data, 'gadget' ); 105 $result->addValue( 'query', $this->getModuleName(), $data ); 106 } 107 108 /** 109 * @param $gadget Gadget 110 * 111 * @return bool 112 */ 113 private function isNeeded( Gadget $gadget ) { 114 $user = $this->getUser(); 115 116 return ( $this->neededIds === false || isset( $this->neededIds[$gadget->getName()] ) ) 117 && ( !$this->listAllowed || $gadget->isAllowed( $user ) ) 118 && ( !$this->listEnabled || $gadget->isEnabled( $user ) ); 119 } 120 121 /** 122 * @param $g Gadget 123 * @return array 124 */ 125 private function fakeMetadata( Gadget $g ) { 126 return array( 127 'settings' => array( 128 'rights' => $g->getRequiredRights(), 129 'skins' => $g->getRequiredSkins(), 130 'default' => $g->isOnByDefault(), 131 'hidden' => false, // Only exists in RL2 branch 132 'shared' => false, // Only exists in RL2 branch 133 'category' => $g->getCategory(), 134 ), 135 'module' => array( 136 'scripts' => $g->getScripts(), 137 'styles' => $g->getStyles(), 138 'dependencies' => $g->getDependencies(), 139 'messages' => array(), // Only exists in RL2 branch 140 ) 141 ); 142 } 143 144 private function setIndexedTagNameForMetadata( &$metadata ) { 145 static $tagNames = array( 146 'rights' => 'right', 147 'skins' => 'skin', 148 'scripts' => 'script', 149 'styles' => 'style', 150 'dependencies' => 'dependency', 151 'messages' => 'message', 152 ); 153 154 $result = $this->getResult(); 155 foreach ( $metadata as &$data ) { 156 foreach ( $data as $key => &$value ) { 157 if ( is_array( $value ) ) { 158 $tag = isset( $tagNames[$key] ) ? $tagNames[$key] : $key; 159 $result->setIndexedTagName( $value, $tag ); 160 } 161 } 162 } 163 } 164 165 public function getAllowedParams() { 166 return array( 167 'prop' => array( 168 ApiBase::PARAM_DFLT => 'id|metadata', 169 ApiBase::PARAM_ISMULTI => true, 170 ApiBase::PARAM_TYPE => array( 171 'id', 172 'metadata', 173 'desc', 174 ), 175 ), 176 'categories' => array( 177 ApiBase::PARAM_ISMULTI => true, 178 ApiBase::PARAM_TYPE => 'string', 179 ), 180 'ids' => array( 181 ApiBase::PARAM_TYPE => 'string', 182 ApiBase::PARAM_ISMULTI => true, 183 ), 184 'allowedonly' => false, 185 'enabledonly' => false, 186 ); 187 } 188 189 public function getDescription() { 190 return 'Returns a list of gadgets used on this wiki'; 191 } 192 193 public function getParamDescription() { 194 return array( 195 'prop' => array( 196 'What gadget information to get:', 197 ' id - Internal gadget ID', 198 ' metadata - The gadget metadata', 199 ' desc - Gadget description transformed into HTML (can be slow, use only if really needed)', 200 ), 201 'categories' => 'Gadgets from what categories to retrieve', 202 'ids' => 'ID(s) of gadgets to retrieve', 203 'allowedonly' => 'List only gadgets allowed to current user', 204 'enabledonly' => 'List only gadgets enabled by current user', 205 ); 206 } 207 208 public function getExamples() { 209 $params = $this->getAllowedParams(); 210 $allProps = implode( '|', $params['prop'][ApiBase::PARAM_TYPE] ); 211 return array( 212 'Get a list of gadgets along with their descriptions:', 213 ' api.php?action=query&list=gadgets&gaprop=id|desc', 214 'Get a list of gadgets with all possible properties:', 215 " api.php?action=query&list=gadgets&gaprop=$allProps", 216 'Get a list of gadgets belonging to category "foo":', 217 ' api.php?action=query&list=gadgets&gacategories=foo', 218 'Get information about gadgets "foo" and "bar":', 219 ' api.php?action=query&list=gadgets&gaids=foo|bar&gaprop=id|desc|metadata', 220 'Get a list of gadgets enabled by current user:', 221 ' api.php?action=query&list=gadgets&gaenabledonly', 222 ); 223 } 224 225 public function getVersion() { 226 return __CLASS__ . ': $Id$'; 227 } 228 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |