ApiParamInfo.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /*
  3. * Created on Dec 01, 2007
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2008 Roan Kattouw <Firstname>.<Lastname>@home.nl
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ("ApiBase.php");
  27. }
  28. /**
  29. * @ingroup API
  30. */
  31. class ApiParamInfo extends ApiBase {
  32. public function __construct($main, $action) {
  33. parent :: __construct($main, $action);
  34. }
  35. public function execute() {
  36. // Get parameters
  37. $params = $this->extractRequestParams();
  38. $result = $this->getResult();
  39. $queryObj = new ApiQuery($this->getMain(), 'query');
  40. $r = array();
  41. if(is_array($params['modules']))
  42. {
  43. $modArr = $this->getMain()->getModules();
  44. foreach($params['modules'] as $m)
  45. {
  46. if(!isset($modArr[$m]))
  47. {
  48. $r['modules'][] = array('name' => $m, 'missing' => '');
  49. continue;
  50. }
  51. $obj = new $modArr[$m]($this->getMain(), $m);
  52. $a = $this->getClassInfo($obj);
  53. $a['name'] = $m;
  54. $r['modules'][] = $a;
  55. }
  56. $result->setIndexedTagName($r['modules'], 'module');
  57. }
  58. if(is_array($params['querymodules']))
  59. {
  60. $qmodArr = $queryObj->getModules();
  61. foreach($params['querymodules'] as $qm)
  62. {
  63. if(!isset($qmodArr[$qm]))
  64. {
  65. $r['querymodules'][] = array('name' => $qm, 'missing' => '');
  66. continue;
  67. }
  68. $obj = new $qmodArr[$qm]($this, $qm);
  69. $a = $this->getClassInfo($obj);
  70. $a['name'] = $qm;
  71. $r['querymodules'][] = $a;
  72. }
  73. $result->setIndexedTagName($r['querymodules'], 'module');
  74. }
  75. if($params['mainmodule'])
  76. $r['mainmodule'] = $this->getClassInfo($this->getMain());
  77. if($params['pagesetmodule'])
  78. {
  79. $pageSet = new ApiPageSet($queryObj);
  80. $r['pagesetmodule'] = $this->getClassInfo($pageSet);
  81. }
  82. $result->addValue(null, $this->getModuleName(), $r);
  83. }
  84. function getClassInfo($obj)
  85. {
  86. $result = $this->getResult();
  87. $retval['classname'] = get_class($obj);
  88. $retval['description'] = (is_array($obj->getDescription()) ? implode("\n", $obj->getDescription()) : $obj->getDescription());
  89. $retval['prefix'] = $obj->getModulePrefix();
  90. if($obj->isReadMode())
  91. $retval['readrights'] = '';
  92. if($obj->isWriteMode())
  93. $retval['writerights'] = '';
  94. if($obj->mustBePosted())
  95. $retval['mustbeposted'] = '';
  96. $allowedParams = $obj->getFinalParams();
  97. if(!is_array($allowedParams))
  98. return $retval;
  99. $retval['parameters'] = array();
  100. $paramDesc = $obj->getFinalParamDescription();
  101. foreach($allowedParams as $n => $p)
  102. {
  103. $a = array('name' => $n);
  104. if(!is_array($p))
  105. {
  106. if(is_bool($p))
  107. {
  108. $a['type'] = 'bool';
  109. $a['default'] = ($p ? 'true' : 'false');
  110. }
  111. if(is_string($p))
  112. $a['default'] = $p;
  113. $retval['parameters'][] = $a;
  114. continue;
  115. }
  116. if(isset($p[ApiBase::PARAM_DFLT]))
  117. $a['default'] = $p[ApiBase::PARAM_DFLT];
  118. if(isset($p[ApiBase::PARAM_ISMULTI]))
  119. if($p[ApiBase::PARAM_ISMULTI])
  120. {
  121. $a['multi'] = '';
  122. $a['limit'] = $this->getMain()->canApiHighLimits() ?
  123. ApiBase::LIMIT_SML2 :
  124. ApiBase::LIMIT_SML1;
  125. }
  126. if(isset($p[ApiBase::PARAM_ALLOW_DUPLICATES]))
  127. if($p[ApiBase::PARAM_ALLOW_DUPLICATES])
  128. $a['allowsduplicates'] = '';
  129. if(isset($p[ApiBase::PARAM_TYPE]))
  130. {
  131. $a['type'] = $p[ApiBase::PARAM_TYPE];
  132. if(is_array($a['type']))
  133. $result->setIndexedTagName($a['type'], 't');
  134. }
  135. if(isset($p[ApiBase::PARAM_MAX]))
  136. $a['max'] = $p[ApiBase::PARAM_MAX];
  137. if(isset($p[ApiBase::PARAM_MAX2]))
  138. $a['highmax'] = $p[ApiBase::PARAM_MAX2];
  139. if(isset($p[ApiBase::PARAM_MIN]))
  140. $a['min'] = $p[ApiBase::PARAM_MIN];
  141. if(isset($paramDesc[$n]))
  142. $a['description'] = (is_array($paramDesc[$n]) ? implode("\n", $paramDesc[$n]) : $paramDesc[$n]);
  143. $retval['parameters'][] = $a;
  144. }
  145. $result->setIndexedTagName($retval['parameters'], 'param');
  146. return $retval;
  147. }
  148. public function isReadMode() {
  149. return false;
  150. }
  151. public function getAllowedParams() {
  152. return array (
  153. 'modules' => array(
  154. ApiBase :: PARAM_ISMULTI => true
  155. ),
  156. 'querymodules' => array(
  157. ApiBase :: PARAM_ISMULTI => true
  158. ),
  159. 'mainmodule' => false,
  160. 'pagesetmodule' => false,
  161. );
  162. }
  163. public function getParamDescription() {
  164. return array (
  165. 'modules' => 'List of module names (value of the action= parameter)',
  166. 'querymodules' => 'List of query module names (value of prop=, meta= or list= parameter)',
  167. 'mainmodule' => 'Get information about the main (top-level) module as well',
  168. 'pagesetmodule' => 'Get information about the pageset module (providing titles= and friends) as well',
  169. );
  170. }
  171. public function getDescription() {
  172. return 'Obtain information about certain API parameters';
  173. }
  174. protected function getExamples() {
  175. return array (
  176. 'api.php?action=paraminfo&modules=parse&querymodules=allpages|siteinfo'
  177. );
  178. }
  179. public function getVersion() {
  180. return __CLASS__ . ': $Id: ApiParamInfo.php 48091 2009-03-06 13:49:44Z catrope $';
  181. }
  182. }