예제 #1
0
 public function getFinalDescription()
 {
     // A bit of a hack to append 'api-help-authmanager-general-usage'
     $msgs = parent::getFinalDescription();
     $msgs[] = ApiBase::makeMessage('api-help-authmanager-general-usage', $this->getContext(), [$this->getModulePrefix(), $this->getModuleName(), $this->getModulePath(), AuthManager::ACTION_LOGIN, self::needsToken()]);
     return $msgs;
 }
예제 #2
0
파일: ApiHelp.php 프로젝트: paladox/2
 /**
  * Recursively-called function to actually construct the help
  *
  * @param IContextSource $context
  * @param ApiBase[] $modules
  * @param array $options
  * @param array &$haveModules
  * @return string
  */
 private static function getHelpInternal(IContextSource $context, array $modules, array $options, &$haveModules)
 {
     $out = '';
     $level = empty($options['headerlevel']) ? 2 : $options['headerlevel'];
     if (empty($options['tocnumber'])) {
         $tocnumber = array(2 => 0);
     } else {
         $tocnumber =& $options['tocnumber'];
     }
     foreach ($modules as $module) {
         $tocnumber[$level]++;
         $path = $module->getModulePath();
         $module->setContext($context);
         $help = array('header' => '', 'flags' => '', 'description' => '', 'help-urls' => '', 'parameters' => '', 'examples' => '', 'submodules' => '');
         if (empty($options['noheader']) || !empty($options['toc'])) {
             $anchor = $path;
             $i = 1;
             while (isset($haveModules[$anchor])) {
                 $anchor = $path . '|' . ++$i;
             }
             if ($module->isMain()) {
                 $header = $context->msg('api-help-main-header')->parse();
             } else {
                 $name = $module->getModuleName();
                 $header = $module->getParent()->getModuleManager()->getModuleGroup($name) . "={$name}";
                 if ($module->getModulePrefix() !== '') {
                     $header .= ' ' . $context->msg('parentheses', $module->getModulePrefix())->parse();
                 }
             }
             $haveModules[$anchor] = array('toclevel' => count($tocnumber), 'level' => $level, 'anchor' => $anchor, 'line' => $header, 'number' => join('.', $tocnumber), 'index' => false);
             if (empty($options['noheader'])) {
                 $help['header'] .= Html::element('h' . min(6, $level), array('id' => $anchor, 'class' => 'apihelp-header'), $header);
             }
         } else {
             $haveModules[$path] = true;
         }
         $links = array();
         $any = false;
         for ($m = $module; $m !== null; $m = $m->getParent()) {
             $name = $m->getModuleName();
             if ($name === 'main_int') {
                 $name = 'main';
             }
             if (count($modules) === 1 && $m === $modules[0] && !(!empty($options['submodules']) && $m->getModuleManager())) {
                 $link = Html::element('b', null, $name);
             } else {
                 $link = SpecialPage::getTitleFor('ApiHelp', $m->getModulePath())->getLocalURL();
                 $link = Html::element('a', array('href' => $link, 'class' => 'apihelp-linktrail'), $name);
                 $any = true;
             }
             array_unshift($links, $link);
         }
         if ($any) {
             $help['header'] .= self::wrap($context->msg('parentheses')->rawParams($context->getLanguage()->pipeList($links)), 'apihelp-linktrail', 'div');
         }
         $flags = $module->getHelpFlags();
         $help['flags'] .= Html::openElement('div', array('class' => 'apihelp-block apihelp-flags'));
         $msg = $context->msg('api-help-flags');
         if (!$msg->isDisabled()) {
             $help['flags'] .= self::wrap($msg->numParams(count($flags)), 'apihelp-block-head', 'div');
         }
         $help['flags'] .= Html::openElement('ul');
         foreach ($flags as $flag) {
             $help['flags'] .= Html::rawElement('li', null, self::wrap($context->msg("api-help-flag-{$flag}"), "apihelp-flag-{$flag}"));
         }
         $sourceInfo = $module->getModuleSourceInfo();
         if ($sourceInfo) {
             if (isset($sourceInfo['namemsg'])) {
                 $extname = $context->msg($sourceInfo['namemsg'])->text();
             } else {
                 $extname = $sourceInfo['name'];
             }
             $help['flags'] .= Html::rawElement('li', null, self::wrap($context->msg('api-help-source', $extname, $sourceInfo['name']), 'apihelp-source'));
             $link = SpecialPage::getTitleFor('Version', 'License/' . $sourceInfo['name']);
             if (isset($sourceInfo['license-name'])) {
                 $msg = $context->msg('api-help-license', $link, $sourceInfo['license-name']);
             } elseif (SpecialVersion::getExtLicenseFileName(dirname($sourceInfo['path']))) {
                 $msg = $context->msg('api-help-license-noname', $link);
             } else {
                 $msg = $context->msg('api-help-license-unknown');
             }
             $help['flags'] .= Html::rawElement('li', null, self::wrap($msg, 'apihelp-license'));
         } else {
             $help['flags'] .= Html::rawElement('li', null, self::wrap($context->msg('api-help-source-unknown'), 'apihelp-source'));
             $help['flags'] .= Html::rawElement('li', null, self::wrap($context->msg('api-help-license-unknown'), 'apihelp-license'));
         }
         $help['flags'] .= Html::closeElement('ul');
         $help['flags'] .= Html::closeElement('div');
         foreach ($module->getFinalDescription() as $msg) {
             $msg->setContext($context);
             $help['description'] .= $msg->parseAsBlock();
         }
         $urls = $module->getHelpUrls();
         if ($urls) {
             $help['help-urls'] .= Html::openElement('div', array('class' => 'apihelp-block apihelp-help-urls'));
             $msg = $context->msg('api-help-help-urls');
             if (!$msg->isDisabled()) {
                 $help['help-urls'] .= self::wrap($msg->numParams(count($urls)), 'apihelp-block-head', 'div');
             }
             if (!is_array($urls)) {
                 $urls = array($urls);
             }
             $help['help-urls'] .= Html::openElement('ul');
             foreach ($urls as $url) {
                 $help['help-urls'] .= Html::rawElement('li', null, Html::element('a', array('href' => $url), $url));
             }
             $help['help-urls'] .= Html::closeElement('ul');
             $help['help-urls'] .= Html::closeElement('div');
         }
         $params = $module->getFinalParams(ApiBase::GET_VALUES_FOR_HELP);
         $dynamicParams = $module->dynamicParameterDocumentation();
         $groups = array();
         if ($params || $dynamicParams !== null) {
             $help['parameters'] .= Html::openElement('div', array('class' => 'apihelp-block apihelp-parameters'));
             $msg = $context->msg('api-help-parameters');
             if (!$msg->isDisabled()) {
                 $help['parameters'] .= self::wrap($msg->numParams(count($params)), 'apihelp-block-head', 'div');
             }
             $help['parameters'] .= Html::openElement('dl');
             $descriptions = $module->getFinalParamDescription();
             foreach ($params as $name => $settings) {
                 if (!is_array($settings)) {
                     $settings = array(ApiBase::PARAM_DFLT => $settings);
                 }
                 $help['parameters'] .= Html::element('dt', null, $module->encodeParamName($name));
                 // Add description
                 $description = array();
                 if (isset($descriptions[$name])) {
                     foreach ($descriptions[$name] as $msg) {
                         $msg->setContext($context);
                         $description[] = $msg->parseAsBlock();
                     }
                 }
                 // Add usage info
                 $info = array();
                 // Required?
                 if (!empty($settings[ApiBase::PARAM_REQUIRED])) {
                     $info[] = $context->msg('api-help-param-required')->parse();
                 }
                 // Custom info?
                 if (!empty($settings[ApiBase::PARAM_HELP_MSG_INFO])) {
                     foreach ($settings[ApiBase::PARAM_HELP_MSG_INFO] as $i) {
                         $tag = array_shift($i);
                         $info[] = $context->msg("apihelp-{$path}-paraminfo-{$tag}")->numParams(count($i))->params($context->getLanguage()->commaList($i))->params($module->getModulePrefix())->parse();
                     }
                 }
                 // Type documentation
                 if (!isset($settings[ApiBase::PARAM_TYPE])) {
                     $dflt = isset($settings[ApiBase::PARAM_DFLT]) ? $settings[ApiBase::PARAM_DFLT] : null;
                     if (is_bool($dflt)) {
                         $settings[ApiBase::PARAM_TYPE] = 'boolean';
                     } elseif (is_string($dflt) || is_null($dflt)) {
                         $settings[ApiBase::PARAM_TYPE] = 'string';
                     } elseif (is_int($dflt)) {
                         $settings[ApiBase::PARAM_TYPE] = 'integer';
                     }
                 }
                 if (isset($settings[ApiBase::PARAM_TYPE])) {
                     $type = $settings[ApiBase::PARAM_TYPE];
                     $multi = !empty($settings[ApiBase::PARAM_ISMULTI]);
                     $hintPipeSeparated = true;
                     $count = ApiBase::LIMIT_SML2 + 1;
                     if (is_array($type)) {
                         $count = count($type);
                         $links = isset($settings[ApiBase::PARAM_VALUE_LINKS]) ? $settings[ApiBase::PARAM_VALUE_LINKS] : array();
                         $type = array_map(function ($v) use($links) {
                             $ret = wfEscapeWikiText($v);
                             if (isset($links[$v])) {
                                 $ret = "[[{$links[$v]}|{$ret}]]";
                             }
                             return $ret;
                         }, $type);
                         $i = array_search('', $type, true);
                         if ($i === false) {
                             $type = $context->getLanguage()->commaList($type);
                         } else {
                             unset($type[$i]);
                             $type = $context->msg('api-help-param-list-can-be-empty')->numParams(count($type))->params($context->getLanguage()->commaList($type))->parse();
                         }
                         $info[] = $context->msg('api-help-param-list')->params($multi ? 2 : 1)->params($type)->parse();
                         $hintPipeSeparated = false;
                     } else {
                         switch ($type) {
                             case 'submodule':
                                 $groups[] = $name;
                                 if (isset($settings[ApiBase::PARAM_SUBMODULE_MAP])) {
                                     $map = $settings[ApiBase::PARAM_SUBMODULE_MAP];
                                     ksort($map);
                                     $submodules = array();
                                     foreach ($map as $v => $m) {
                                         $submodules[] = "[[Special:ApiHelp/{$m}|{$v}]]";
                                     }
                                 } else {
                                     $submodules = $module->getModuleManager()->getNames($name);
                                     sort($submodules);
                                     $prefix = $module->isMain() ? '' : $module->getModulePath() . '+';
                                     $submodules = array_map(function ($name) use($prefix) {
                                         return "[[Special:ApiHelp/{$prefix}{$name}|{$name}]]";
                                     }, $submodules);
                                 }
                                 $count = count($submodules);
                                 $info[] = $context->msg('api-help-param-list')->params($multi ? 2 : 1)->params($context->getLanguage()->commaList($submodules))->parse();
                                 $hintPipeSeparated = false;
                                 // No type message necessary, we have a list of values.
                                 $type = null;
                                 break;
                             case 'namespace':
                                 $namespaces = MWNamespace::getValidNamespaces();
                                 $count = count($namespaces);
                                 $info[] = $context->msg('api-help-param-list')->params($multi ? 2 : 1)->params($context->getLanguage()->commaList($namespaces))->parse();
                                 $hintPipeSeparated = false;
                                 // No type message necessary, we have a list of values.
                                 $type = null;
                                 break;
                             case 'limit':
                                 if (isset($settings[ApiBase::PARAM_MAX2])) {
                                     $info[] = $context->msg('api-help-param-limit2')->numParams($settings[ApiBase::PARAM_MAX])->numParams($settings[ApiBase::PARAM_MAX2])->parse();
                                 } else {
                                     $info[] = $context->msg('api-help-param-limit')->numParams($settings[ApiBase::PARAM_MAX])->parse();
                                 }
                                 break;
                             case 'integer':
                                 // Possible messages:
                                 // api-help-param-integer-min,
                                 // api-help-param-integer-max,
                                 // api-help-param-integer-minmax
                                 $suffix = '';
                                 $min = $max = 0;
                                 if (isset($settings[ApiBase::PARAM_MIN])) {
                                     $suffix .= 'min';
                                     $min = $settings[ApiBase::PARAM_MIN];
                                 }
                                 if (isset($settings[ApiBase::PARAM_MAX])) {
                                     $suffix .= 'max';
                                     $max = $settings[ApiBase::PARAM_MAX];
                                 }
                                 if ($suffix !== '') {
                                     $info[] = $context->msg("api-help-param-integer-{$suffix}")->params($multi ? 2 : 1)->numParams($min, $max)->parse();
                                 }
                                 break;
                             case 'upload':
                                 $info[] = $context->msg('api-help-param-upload')->parse();
                                 // No type message necessary, api-help-param-upload should handle it.
                                 $type = null;
                                 break;
                             case 'string':
                             case 'text':
                                 // Displaying a type message here would be useless.
                                 $type = null;
                                 break;
                         }
                     }
                     // Add type. Messages for grep: api-help-param-type-limit
                     // api-help-param-type-integer api-help-param-type-boolean
                     // api-help-param-type-timestamp api-help-param-type-user
                     // api-help-param-type-password
                     if (is_string($type)) {
                         $msg = $context->msg("api-help-param-type-{$type}");
                         if (!$msg->isDisabled()) {
                             $info[] = $msg->params($multi ? 2 : 1)->parse();
                         }
                     }
                     if ($multi) {
                         $extra = array();
                         if ($hintPipeSeparated) {
                             $extra[] = $context->msg('api-help-param-multi-separate')->parse();
                         }
                         if ($count > ApiBase::LIMIT_SML1) {
                             $extra[] = $context->msg('api-help-param-multi-max')->numParams(ApiBase::LIMIT_SML1, ApiBase::LIMIT_SML2)->parse();
                         }
                         if ($extra) {
                             $info[] = join(' ', $extra);
                         }
                     }
                 }
                 // Add default
                 $default = isset($settings[ApiBase::PARAM_DFLT]) ? $settings[ApiBase::PARAM_DFLT] : null;
                 if ($default === '') {
                     $info[] = $context->msg('api-help-param-default-empty')->parse();
                 } elseif ($default !== null && $default !== false) {
                     $info[] = $context->msg('api-help-param-default')->params(wfEscapeWikiText($default))->parse();
                 }
                 if (!array_filter($description)) {
                     $description = array(self::wrap($context->msg('api-help-param-no-description'), 'apihelp-empty'));
                 }
                 // Add "deprecated" flag
                 if (!empty($settings[ApiBase::PARAM_DEPRECATED])) {
                     $help['parameters'] .= Html::openElement('dd', array('class' => 'info'));
                     $help['parameters'] .= self::wrap($context->msg('api-help-param-deprecated'), 'apihelp-deprecated', 'strong');
                     $help['parameters'] .= Html::closeElement('dd');
                 }
                 if ($description) {
                     $description = join('', $description);
                     $description = preg_replace('!\\s*</([oud]l)>\\s*<\\1>\\s*!', "\n", $description);
                     $help['parameters'] .= Html::rawElement('dd', array('class' => 'description'), $description);
                 }
                 foreach ($info as $i) {
                     $help['parameters'] .= Html::rawElement('dd', array('class' => 'info'), $i);
                 }
             }
             if ($dynamicParams !== null) {
                 $dynamicParams = ApiBase::makeMessage($dynamicParams, $context, array($module->getModulePrefix(), $module->getModuleName(), $module->getModulePath()));
                 $help['parameters'] .= Html::element('dt', null, '*');
                 $help['parameters'] .= Html::rawElement('dd', array('class' => 'description'), $dynamicParams->parse());
             }
             $help['parameters'] .= Html::closeElement('dl');
             $help['parameters'] .= Html::closeElement('div');
         }
         $examples = $module->getExamplesMessages();
         if ($examples) {
             $help['examples'] .= Html::openElement('div', array('class' => 'apihelp-block apihelp-examples'));
             $msg = $context->msg('api-help-examples');
             if (!$msg->isDisabled()) {
                 $help['examples'] .= self::wrap($msg->numParams(count($examples)), 'apihelp-block-head', 'div');
             }
             $help['examples'] .= Html::openElement('dl');
             foreach ($examples as $qs => $msg) {
                 $msg = ApiBase::makeMessage($msg, $context, array($module->getModulePrefix(), $module->getModuleName(), $module->getModulePath()));
                 $link = wfAppendQuery(wfScript('api'), $qs);
                 $help['examples'] .= Html::rawElement('dt', null, $msg->parse());
                 $help['examples'] .= Html::rawElement('dd', null, Html::element('a', array('href' => $link), "api.php?{$qs}"));
             }
             $help['examples'] .= Html::closeElement('dl');
             $help['examples'] .= Html::closeElement('div');
         }
         $subtocnumber = $tocnumber;
         $subtocnumber[$level + 1] = 0;
         $suboptions = array('submodules' => $options['recursivesubmodules'], 'headerlevel' => $level + 1, 'tocnumber' => &$subtocnumber, 'noheader' => false) + $options;
         if ($options['submodules'] && $module->getModuleManager()) {
             $manager = $module->getModuleManager();
             $submodules = array();
             foreach ($groups as $group) {
                 $names = $manager->getNames($group);
                 sort($names);
                 foreach ($names as $name) {
                     $submodules[] = $manager->getModule($name);
                 }
             }
             $help['submodules'] .= self::getHelpInternal($context, $submodules, $suboptions, $haveModules);
         }
         $module->modifyHelp($help, $suboptions, $haveModules);
         Hooks::run('APIHelpModifyOutput', array($module, &$help, $suboptions, &$haveModules));
         $out .= join("\n", $help);
     }
     return $out;
 }
예제 #3
0
 /**
  * Get final parameter descriptions, after hooks have had a chance to tweak it as
  * needed.
  *
  * @since 1.25, returns array of Message[] rather than array of string[]
  * @return array Keys are parameter names, values are arrays of Message objects
  */
 public function getFinalParamDescription()
 {
     $prefix = $this->getModulePrefix();
     $name = $this->getModuleName();
     $path = $this->getModulePath();
     $desc = $this->getParamDescription();
     Hooks::run('APIGetParamDescription', array(&$this, &$desc));
     if (!$desc) {
         $desc = array();
     }
     $desc = self::escapeWikiText($desc);
     $params = $this->getFinalParams(ApiBase::GET_VALUES_FOR_HELP);
     $msgs = array();
     foreach ($params as $param => $settings) {
         if (!is_array($settings)) {
             $settings = array();
         }
         $d = isset($desc[$param]) ? $desc[$param] : '';
         if (is_array($d)) {
             // Special handling for prop parameters
             $d = array_map(function ($line) {
                 if (preg_match('/^\\s+(\\S+)\\s+-\\s+(.+)$/', $line, $m)) {
                     $line = "\n;{$m[1]}:{$m[2]}";
                 }
                 return $line;
             }, $d);
             $d = join(' ', $d);
         }
         if (isset($settings[ApiBase::PARAM_HELP_MSG])) {
             $msg = $settings[ApiBase::PARAM_HELP_MSG];
         } else {
             $msg = $this->msg("apihelp-{$path}-param-{$param}");
             if (!$msg->exists()) {
                 $msg = $this->msg('api-help-fallback-parameter', $d);
             }
         }
         $msg = ApiBase::makeMessage($msg, $this->getContext(), array($prefix, $param, $name, $path));
         if (!$msg) {
             $this->dieDebug(__METHOD__, 'Value in ApiBase::PARAM_HELP_MSG is not valid');
         }
         $msgs[$param] = array($msg);
         if (isset($settings[ApiBase::PARAM_HELP_MSG_PER_VALUE])) {
             if (!is_array($settings[ApiBase::PARAM_HELP_MSG_PER_VALUE])) {
                 $this->dieDebug(__METHOD__, 'ApiBase::PARAM_HELP_MSG_PER_VALUE is not valid');
             }
             if (!is_array($settings[ApiBase::PARAM_TYPE])) {
                 $this->dieDebug(__METHOD__, 'ApiBase::PARAM_HELP_MSG_PER_VALUE may only be used when ' . 'ApiBase::PARAM_TYPE is an array');
             }
             $valueMsgs = $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE];
             foreach ($settings[ApiBase::PARAM_TYPE] as $value) {
                 if (isset($valueMsgs[$value])) {
                     $msg = $valueMsgs[$value];
                 } else {
                     $msg = "apihelp-{$path}-paramvalue-{$param}-{$value}";
                 }
                 $m = ApiBase::makeMessage($msg, $this->getContext(), array($prefix, $param, $name, $path, $value));
                 if ($m) {
                     $m = new ApiHelpParamValueMessage($value, array($m->getKey(), 'api-help-param-no-description'), $m->getParams());
                     $msgs[$param][] = $m->setContext($this->getContext());
                 } else {
                     $this->dieDebug(__METHOD__, "Value in ApiBase::PARAM_HELP_MSG_PER_VALUE for {$value} is not valid");
                 }
             }
         }
         if (isset($settings[ApiBase::PARAM_HELP_MSG_APPEND])) {
             if (!is_array($settings[ApiBase::PARAM_HELP_MSG_APPEND])) {
                 $this->dieDebug(__METHOD__, 'Value for ApiBase::PARAM_HELP_MSG_APPEND is not an array');
             }
             foreach ($settings[ApiBase::PARAM_HELP_MSG_APPEND] as $m) {
                 $m = ApiBase::makeMessage($m, $this->getContext(), array($prefix, $param, $name, $path));
                 if ($m) {
                     $msgs[$param][] = $m;
                 } else {
                     $this->dieDebug(__METHOD__, 'Value in ApiBase::PARAM_HELP_MSG_APPEND is not valid');
                 }
             }
         }
     }
     Hooks::run('APIGetParamDescriptionMessages', array($this, &$msgs));
     return $msgs;
 }
예제 #4
0
 /**
  * Test a message
  * @param Message $msg
  * @param string $what Which message is being checked
  */
 private function checkMessage($msg, $what)
 {
     $msg = ApiBase::makeMessage($msg, self::getMain()->getContext());
     $this->assertInstanceOf('Message', $msg, "{$what} message");
     $this->assertTrue($msg->exists(), "{$what} message {$msg->getKey()} exists");
 }
예제 #5
0
 /**
  * @param ApiBase $module
  * @return ApiResult
  */
 private function getModuleInfo($module)
 {
     $ret = [];
     $path = $module->getModulePath();
     $ret['name'] = $module->getModuleName();
     $ret['classname'] = get_class($module);
     $ret['path'] = $path;
     if (!$module->isMain()) {
         $ret['group'] = $module->getParent()->getModuleManager()->getModuleGroup($module->getModuleName());
     }
     $ret['prefix'] = $module->getModulePrefix();
     $sourceInfo = $module->getModuleSourceInfo();
     if ($sourceInfo) {
         $ret['source'] = $sourceInfo['name'];
         if (isset($sourceInfo['namemsg'])) {
             $ret['sourcename'] = $this->context->msg($sourceInfo['namemsg'])->text();
         } else {
             $ret['sourcename'] = $ret['source'];
         }
         $link = SpecialPage::getTitleFor('Version', 'License/' . $sourceInfo['name'])->getFullURL();
         if (isset($sourceInfo['license-name'])) {
             $ret['licensetag'] = $sourceInfo['license-name'];
             $ret['licenselink'] = (string) $link;
         } elseif (SpecialVersion::getExtLicenseFileName(dirname($sourceInfo['path']))) {
             $ret['licenselink'] = (string) $link;
         }
     }
     $this->formatHelpMessages($ret, 'description', $module->getFinalDescription());
     foreach ($module->getHelpFlags() as $flag) {
         $ret[$flag] = true;
     }
     $ret['helpurls'] = (array) $module->getHelpUrls();
     if (isset($ret['helpurls'][0]) && $ret['helpurls'][0] === false) {
         $ret['helpurls'] = [];
     }
     ApiResult::setIndexedTagName($ret['helpurls'], 'helpurl');
     if ($this->helpFormat !== 'none') {
         $ret['examples'] = [];
         $examples = $module->getExamplesMessages();
         foreach ($examples as $qs => $msg) {
             $item = ['query' => $qs];
             $msg = ApiBase::makeMessage($msg, $this->context, [$module->getModulePrefix(), $module->getModuleName(), $module->getModulePath()]);
             $this->formatHelpMessages($item, 'description', [$msg]);
             if (isset($item['description'])) {
                 if (is_array($item['description'])) {
                     $item['description'] = $item['description'][0];
                 } else {
                     ApiResult::setSubelementsList($item, 'description');
                 }
             }
             $ret['examples'][] = $item;
         }
         ApiResult::setIndexedTagName($ret['examples'], 'example');
     }
     $ret['parameters'] = [];
     $params = $module->getFinalParams(ApiBase::GET_VALUES_FOR_HELP);
     $paramDesc = $module->getFinalParamDescription();
     foreach ($params as $name => $settings) {
         if (!is_array($settings)) {
             $settings = [ApiBase::PARAM_DFLT => $settings];
         }
         $item = ['name' => $name];
         if (isset($paramDesc[$name])) {
             $this->formatHelpMessages($item, 'description', $paramDesc[$name], true);
         }
         $item['required'] = !empty($settings[ApiBase::PARAM_REQUIRED]);
         if (!empty($settings[ApiBase::PARAM_DEPRECATED])) {
             $item['deprecated'] = true;
         }
         if ($name === 'token' && $module->needsToken()) {
             $item['tokentype'] = $module->needsToken();
         }
         if (!isset($settings[ApiBase::PARAM_TYPE])) {
             $dflt = isset($settings[ApiBase::PARAM_DFLT]) ? $settings[ApiBase::PARAM_DFLT] : null;
             if (is_bool($dflt)) {
                 $settings[ApiBase::PARAM_TYPE] = 'boolean';
             } elseif (is_string($dflt) || is_null($dflt)) {
                 $settings[ApiBase::PARAM_TYPE] = 'string';
             } elseif (is_int($dflt)) {
                 $settings[ApiBase::PARAM_TYPE] = 'integer';
             }
         }
         if (isset($settings[ApiBase::PARAM_DFLT])) {
             switch ($settings[ApiBase::PARAM_TYPE]) {
                 case 'boolean':
                     $item['default'] = (bool) $settings[ApiBase::PARAM_DFLT];
                     break;
                 case 'string':
                 case 'text':
                 case 'password':
                     $item['default'] = strval($settings[ApiBase::PARAM_DFLT]);
                     break;
                 case 'integer':
                 case 'limit':
                     $item['default'] = intval($settings[ApiBase::PARAM_DFLT]);
                     break;
                 case 'timestamp':
                     $item['default'] = wfTimestamp(TS_ISO_8601, $settings[ApiBase::PARAM_DFLT]);
                     break;
                 default:
                     $item['default'] = $settings[ApiBase::PARAM_DFLT];
                     break;
             }
         }
         $item['multi'] = !empty($settings[ApiBase::PARAM_ISMULTI]);
         if ($item['multi']) {
             $item['limit'] = $this->getMain()->canApiHighLimits() ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_SML1;
             $item['lowlimit'] = ApiBase::LIMIT_SML1;
             $item['highlimit'] = ApiBase::LIMIT_SML2;
         }
         if (!empty($settings[ApiBase::PARAM_ALLOW_DUPLICATES])) {
             $item['allowsduplicates'] = true;
         }
         if (isset($settings[ApiBase::PARAM_TYPE])) {
             if ($settings[ApiBase::PARAM_TYPE] === 'submodule') {
                 if (isset($settings[ApiBase::PARAM_SUBMODULE_MAP])) {
                     ksort($settings[ApiBase::PARAM_SUBMODULE_MAP]);
                     $item['type'] = array_keys($settings[ApiBase::PARAM_SUBMODULE_MAP]);
                     $item['submodules'] = $settings[ApiBase::PARAM_SUBMODULE_MAP];
                 } else {
                     $item['type'] = $module->getModuleManager()->getNames($name);
                     sort($item['type']);
                     $prefix = $module->isMain() ? '' : $module->getModulePath() . '+';
                     $item['submodules'] = [];
                     foreach ($item['type'] as $v) {
                         $item['submodules'][$v] = $prefix . $v;
                     }
                 }
                 if (isset($settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX])) {
                     $item['submoduleparamprefix'] = $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX];
                 }
             } elseif ($settings[ApiBase::PARAM_TYPE] === 'tags') {
                 $item['type'] = ChangeTags::listExplicitlyDefinedTags();
             } else {
                 $item['type'] = $settings[ApiBase::PARAM_TYPE];
             }
             if (is_array($item['type'])) {
                 // To prevent sparse arrays from being serialized to JSON as objects
                 $item['type'] = array_values($item['type']);
                 ApiResult::setIndexedTagName($item['type'], 't');
             }
         }
         if (isset($settings[ApiBase::PARAM_MAX])) {
             $item['max'] = $settings[ApiBase::PARAM_MAX];
         }
         if (isset($settings[ApiBase::PARAM_MAX2])) {
             $item['highmax'] = $settings[ApiBase::PARAM_MAX2];
         }
         if (isset($settings[ApiBase::PARAM_MIN])) {
             $item['min'] = $settings[ApiBase::PARAM_MIN];
         }
         if (!empty($settings[ApiBase::PARAM_RANGE_ENFORCE])) {
             $item['enforcerange'] = true;
         }
         if (!empty($settings[ApiBase::PARAM_HELP_MSG_INFO])) {
             $item['info'] = [];
             foreach ($settings[ApiBase::PARAM_HELP_MSG_INFO] as $i) {
                 $tag = array_shift($i);
                 $info = ['name' => $tag];
                 if (count($i)) {
                     $info['values'] = $i;
                     ApiResult::setIndexedTagName($info['values'], 'v');
                 }
                 $this->formatHelpMessages($info, 'text', [$this->context->msg("apihelp-{$path}-paraminfo-{$tag}")->numParams(count($i))->params($this->context->getLanguage()->commaList($i))->params($module->getModulePrefix())]);
                 ApiResult::setSubelementsList($info, 'text');
                 $item['info'][] = $info;
             }
             ApiResult::setIndexedTagName($item['info'], 'i');
         }
         $ret['parameters'][] = $item;
     }
     ApiResult::setIndexedTagName($ret['parameters'], 'param');
     $dynamicParams = $module->dynamicParameterDocumentation();
     if ($dynamicParams !== null) {
         if ($this->helpFormat === 'none') {
             $ret['dynamicparameters'] = true;
         } else {
             $dynamicParams = ApiBase::makeMessage($dynamicParams, $this->context, [$module->getModulePrefix(), $module->getModuleName(), $module->getModulePath()]);
             $this->formatHelpMessages($ret, 'dynamicparameters', [$dynamicParams]);
         }
     }
     return $ret;
 }
예제 #6
0
 /**
  * @param ApiBase $module
  * @return ApiResult
  */
 private function getModuleInfo($module)
 {
     $result = $this->getResult();
     $ret = array();
     $path = $module->getModulePath();
     $ret['name'] = $module->getModuleName();
     $ret['classname'] = get_class($module);
     $ret['path'] = $path;
     if (!$module->isMain()) {
         $ret['group'] = $module->getParent()->getModuleManager()->getModuleGroup($module->getModuleName());
     }
     $ret['prefix'] = $module->getModulePrefix();
     $this->formatHelpMessages($ret, 'description', $module->getFinalDescription());
     foreach ($module->getHelpFlags() as $flag) {
         $ret[$flag] = true;
     }
     $ret['helpurls'] = (array) $module->getHelpUrls();
     if (isset($ret['helpurls'][0]) && $ret['helpurls'][0] === false) {
         $ret['helpurls'] = array();
     }
     ApiResult::setIndexedTagName($ret['helpurls'], 'helpurl');
     if ($this->helpFormat !== 'none') {
         $ret['examples'] = array();
         $examples = $module->getExamplesMessages();
         foreach ($examples as $qs => $msg) {
             $item = array('query' => $qs);
             $msg = ApiBase::makeMessage($msg, $this->context, array($module->getModulePrefix(), $module->getModuleName(), $module->getModulePath()));
             $this->formatHelpMessages($item, 'description', array($msg));
             if (isset($item['description'])) {
                 if (is_array($item['description'])) {
                     $item['description'] = $item['description'][0];
                 } else {
                     ApiResult::setSubelementsList($item, 'description');
                 }
             }
             $ret['examples'][] = $item;
         }
         ApiResult::setIndexedTagName($ret['examples'], 'example');
     }
     $ret['parameters'] = array();
     $params = $module->getFinalParams(ApiBase::GET_VALUES_FOR_HELP);
     $paramDesc = $module->getFinalParamDescription();
     foreach ($params as $name => $settings) {
         if (!is_array($settings)) {
             $settings = array(ApiBase::PARAM_DFLT => $settings);
         }
         $item = array('name' => $name);
         if (isset($paramDesc[$name])) {
             $this->formatHelpMessages($item, 'description', $paramDesc[$name], true);
         }
         $item['required'] = !empty($settings[ApiBase::PARAM_REQUIRED]);
         if (!empty($settings[ApiBase::PARAM_DEPRECATED])) {
             $item['deprecated'] = true;
         }
         if ($name === 'token' && $module->needsToken()) {
             $item['tokentype'] = $module->needsToken();
         }
         if (!isset($settings[ApiBase::PARAM_TYPE])) {
             $dflt = isset($settings[ApiBase::PARAM_DFLT]) ? $settings[ApiBase::PARAM_DFLT] : null;
             if (is_bool($dflt)) {
                 $settings[ApiBase::PARAM_TYPE] = 'boolean';
             } elseif (is_string($dflt) || is_null($dflt)) {
                 $settings[ApiBase::PARAM_TYPE] = 'string';
             } elseif (is_int($dflt)) {
                 $settings[ApiBase::PARAM_TYPE] = 'integer';
             }
         }
         if (isset($settings[ApiBase::PARAM_DFLT])) {
             switch ($settings[ApiBase::PARAM_TYPE]) {
                 case 'boolean':
                     $item['default'] = $settings[ApiBase::PARAM_DFLT] ? 'true' : 'false';
                     break;
                 case 'string':
                     $item['default'] = strval($settings[ApiBase::PARAM_DFLT]);
                     break;
                 case 'integer':
                     $item['default'] = intval($settings[ApiBase::PARAM_DFLT]);
                     break;
                 default:
                     $item['default'] = $settings[ApiBase::PARAM_DFLT];
                     break;
             }
         }
         $item['multi'] = !empty($settings[ApiBase::PARAM_ISMULTI]);
         if ($item['multi']) {
             $item['limit'] = $this->getMain()->canApiHighLimits() ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_SML1;
             $item['lowlimit'] = ApiBase::LIMIT_SML1;
             $item['highlimit'] = ApiBase::LIMIT_SML2;
         }
         if (!empty($settings[ApiBase::PARAM_ALLOW_DUPLICATES])) {
             $item['allowsduplicates'] = true;
         }
         if (isset($settings[ApiBase::PARAM_TYPE])) {
             if ($settings[ApiBase::PARAM_TYPE] === 'submodule') {
                 $item['type'] = $module->getModuleManager()->getNames($name);
                 sort($item['type']);
                 $item['submodules'] = true;
             } else {
                 $item['type'] = $settings[ApiBase::PARAM_TYPE];
             }
             if (is_array($item['type'])) {
                 // To prevent sparse arrays from being serialized to JSON as objects
                 $item['type'] = array_values($item['type']);
                 ApiResult::setIndexedTagName($item['type'], 't');
             }
         }
         if (isset($settings[ApiBase::PARAM_MAX])) {
             $item['max'] = $settings[ApiBase::PARAM_MAX];
         }
         if (isset($settings[ApiBase::PARAM_MAX2])) {
             $item['highmax'] = $settings[ApiBase::PARAM_MAX2];
         }
         if (isset($settings[ApiBase::PARAM_MIN])) {
             $item['min'] = $settings[ApiBase::PARAM_MIN];
         }
         if (!empty($settings[ApiBase::PARAM_HELP_MSG_INFO])) {
             $item['info'] = array();
             foreach ($settings[ApiBase::PARAM_HELP_MSG_INFO] as $i) {
                 $tag = array_shift($i);
                 $info = array('name' => $tag);
                 if (count($i)) {
                     $info['values'] = $i;
                     ApiResult::setIndexedTagName($info['values'], 'v');
                 }
                 $this->formatHelpMessages($info, 'text', array($this->context->msg("apihelp-{$path}-paraminfo-{$tag}")->numParams(count($i))->params($this->context->getLanguage()->commaList($i))->params($module->getModulePrefix())));
                 ApiResult::setSubelementsList($info, 'text');
                 $item['info'][] = $info;
             }
             ApiResult::setIndexedTagName($item['info'], 'i');
         }
         $ret['parameters'][] = $item;
     }
     ApiResult::setIndexedTagName($ret['parameters'], 'param');
     return $ret;
 }