本文整理汇总了PHP中ApiResult::addMetadataToResultVars方法的典型用法代码示例。如果您正苦于以下问题:PHP ApiResult::addMetadataToResultVars方法的具体用法?PHP ApiResult::addMetadataToResultVars怎么用?PHP ApiResult::addMetadataToResultVars使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ApiResult
的用法示例。
在下文中一共展示了ApiResult::addMetadataToResultVars方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addMetadataToResultVars
/**
* Add the correct metadata to an array of vars we want to export through
* the API.
*
* @param array $vars
* @param boolean $forceHash
* @return array
*/
public static function addMetadataToResultVars($vars, $forceHash = true)
{
// Process subarrays and determine if this is a JS [] or {}
$hash = $forceHash;
$maxKey = -1;
$bools = array();
foreach ($vars as $k => $v) {
if (is_array($v) || is_object($v)) {
$vars[$k] = ApiResult::addMetadataToResultVars((array) $v, is_object($v));
} elseif (is_bool($v)) {
// Better here to use real bools even in BC formats
$bools[] = $k;
}
if (is_string($k)) {
$hash = true;
} elseif ($k > $maxKey) {
$maxKey = $k;
}
}
if (!$hash && $maxKey !== count($vars) - 1) {
$hash = true;
}
// Set metadata appropriately
if ($hash) {
// Get the list of keys we actually care about. Unfortunately, we can't support
// certain keys that conflict with ApiResult metadata.
$keys = array_diff(array_keys($vars), array(ApiResult::META_TYPE, ApiResult::META_PRESERVE_KEYS, ApiResult::META_KVP_KEY_NAME, ApiResult::META_INDEXED_TAG_NAME, ApiResult::META_BC_BOOLS));
return array(ApiResult::META_TYPE => 'kvp', ApiResult::META_KVP_KEY_NAME => 'key', ApiResult::META_PRESERVE_KEYS => $keys, ApiResult::META_BC_BOOLS => $bools, ApiResult::META_INDEXED_TAG_NAME => 'var') + $vars;
} else {
return array(ApiResult::META_TYPE => 'array', ApiResult::META_BC_BOOLS => $bools, ApiResult::META_INDEXED_TAG_NAME => 'value') + $vars;
}
}
示例2: testAddMetadataToResultVars
/**
* @covers ApiResult
*/
public function testAddMetadataToResultVars()
{
$arr = array('a' => "foo", 'b' => false, 'c' => 10, 'sequential_numeric_keys' => array('a', 'b', 'c'), 'non_sequential_numeric_keys' => array('a', 'b', 4 => 'c'), 'string_keys' => array('one' => 1, 'two' => 2), 'object_sequential_keys' => (object) array('a', 'b', 'c'), '_type' => "should be overwritten in result");
$this->assertSame(array(ApiResult::META_TYPE => 'kvp', ApiResult::META_KVP_KEY_NAME => 'key', ApiResult::META_PRESERVE_KEYS => array('a', 'b', 'c', 'sequential_numeric_keys', 'non_sequential_numeric_keys', 'string_keys', 'object_sequential_keys'), ApiResult::META_BC_BOOLS => array('b'), ApiResult::META_INDEXED_TAG_NAME => 'var', 'a' => "foo", 'b' => false, 'c' => 10, 'sequential_numeric_keys' => array(ApiResult::META_TYPE => 'array', ApiResult::META_BC_BOOLS => array(), ApiResult::META_INDEXED_TAG_NAME => 'value', 0 => 'a', 1 => 'b', 2 => 'c'), 'non_sequential_numeric_keys' => array(ApiResult::META_TYPE => 'kvp', ApiResult::META_KVP_KEY_NAME => 'key', ApiResult::META_PRESERVE_KEYS => array(0, 1, 4), ApiResult::META_BC_BOOLS => array(), ApiResult::META_INDEXED_TAG_NAME => 'var', 0 => 'a', 1 => 'b', 4 => 'c'), 'string_keys' => array(ApiResult::META_TYPE => 'kvp', ApiResult::META_KVP_KEY_NAME => 'key', ApiResult::META_PRESERVE_KEYS => array('one', 'two'), ApiResult::META_BC_BOOLS => array(), ApiResult::META_INDEXED_TAG_NAME => 'var', 'one' => 1, 'two' => 2), 'object_sequential_keys' => array(ApiResult::META_TYPE => 'kvp', ApiResult::META_KVP_KEY_NAME => 'key', ApiResult::META_PRESERVE_KEYS => array(0, 1, 2), ApiResult::META_BC_BOOLS => array(), ApiResult::META_INDEXED_TAG_NAME => 'var', 0 => 'a', 1 => 'b', 2 => 'c')), ApiResult::addMetadataToResultVars($arr));
}
示例3: execute
//.........这里部分代码省略.........
}
if (isset($prop['categorieshtml'])) {
$result_array['categorieshtml'] = $this->categoriesHtml($p_result->getCategories());
$result_array[ApiResult::META_BC_SUBELEMENTS][] = 'categorieshtml';
}
if (isset($prop['links'])) {
$result_array['links'] = $this->formatLinks($p_result->getLinks());
}
if (isset($prop['templates'])) {
$result_array['templates'] = $this->formatLinks($p_result->getTemplates());
}
if (isset($prop['images'])) {
$result_array['images'] = array_keys($p_result->getImages());
}
if (isset($prop['externallinks'])) {
$result_array['externallinks'] = array_keys($p_result->getExternalLinks());
}
if (isset($prop['sections'])) {
$result_array['sections'] = $p_result->getSections();
}
if (isset($prop['displaytitle'])) {
$result_array['displaytitle'] = $p_result->getDisplayTitle() ? $p_result->getDisplayTitle() : $titleObj->getPrefixedText();
}
if (isset($prop['headitems']) || isset($prop['headhtml'])) {
$context = $this->getContext();
$context->setTitle($titleObj);
$context->getOutput()->addParserOutputMetadata($p_result);
if (isset($prop['headitems'])) {
$headItems = $this->formatHeadItems($p_result->getHeadItems());
$css = $this->formatCss($context->getOutput()->buildCssLinksArray());
$scripts = array($context->getOutput()->getHeadScripts());
$result_array['headitems'] = array_merge($headItems, $css, $scripts);
}
if (isset($prop['headhtml'])) {
$result_array['headhtml'] = $context->getOutput()->headElement($context->getSkin());
$result_array[ApiResult::META_BC_SUBELEMENTS][] = 'headhtml';
}
}
if (isset($prop['modules'])) {
$result_array['modules'] = array_values(array_unique($p_result->getModules()));
$result_array['modulescripts'] = array_values(array_unique($p_result->getModuleScripts()));
$result_array['modulestyles'] = array_values(array_unique($p_result->getModuleStyles()));
// To be removed in 1.27
$result_array['modulemessages'] = array();
$this->setWarning('modulemessages is deprecated since MediaWiki 1.26');
}
if (isset($prop['jsconfigvars'])) {
$result_array['jsconfigvars'] = ApiResult::addMetadataToResultVars($p_result->getJsConfigVars());
}
if (isset($prop['encodedjsconfigvars'])) {
$result_array['encodedjsconfigvars'] = FormatJson::encode($p_result->getJsConfigVars(), false, FormatJson::ALL_OK);
$result_array[ApiResult::META_SUBELEMENTS][] = 'encodedjsconfigvars';
}
if (isset($prop['modules']) && !isset($prop['jsconfigvars']) && !isset($prop['encodedjsconfigvars'])) {
$this->setWarning("Property 'modules' was set but not 'jsconfigvars' " . "or 'encodedjsconfigvars'. Configuration variables are necessary " . "for proper module usage.");
}
if (isset($prop['indicators'])) {
$result_array['indicators'] = (array) $p_result->getIndicators();
ApiResult::setArrayType($result_array['indicators'], 'BCkvp', 'name');
}
if (isset($prop['iwlinks'])) {
$result_array['iwlinks'] = $this->formatIWLinks($p_result->getInterwikiLinks());
}
if (isset($prop['wikitext'])) {
$result_array['wikitext'] = $this->content->serialize($format);
$result_array[ApiResult::META_BC_SUBELEMENTS][] = 'wikitext';
if (!is_null($this->pstContent)) {
$result_array['psttext'] = $this->pstContent->serialize($format);
$result_array[ApiResult::META_BC_SUBELEMENTS][] = 'psttext';
}
}
if (isset($prop['properties'])) {
$result_array['properties'] = (array) $p_result->getProperties();
ApiResult::setArrayType($result_array['properties'], 'BCkvp', 'name');
}
if (isset($prop['limitreportdata'])) {
$result_array['limitreportdata'] = $this->formatLimitReportData($p_result->getLimitReportData());
}
if (isset($prop['limitreporthtml'])) {
$result_array['limitreporthtml'] = EditPage::getPreviewLimitReport($p_result);
$result_array[ApiResult::META_BC_SUBELEMENTS][] = 'limitreporthtml';
}
if (isset($prop['parsetree']) || $params['generatexml']) {
if ($this->content->getModel() != CONTENT_MODEL_WIKITEXT) {
$this->dieUsage("parsetree is only supported for wikitext content", "notwikitext");
}
$wgParser->startExternalParse($titleObj, $popts, Parser::OT_PREPROCESS);
$dom = $wgParser->preprocessToDom($this->content->getNativeData());
if (is_callable(array($dom, 'saveXML'))) {
$xml = $dom->saveXML();
} else {
$xml = $dom->__toString();
}
$result_array['parsetree'] = $xml;
$result_array[ApiResult::META_BC_SUBELEMENTS][] = 'parsetree';
}
$result_mapping = array('redirects' => 'r', 'langlinks' => 'll', 'categories' => 'cl', 'links' => 'pl', 'templates' => 'tl', 'images' => 'img', 'externallinks' => 'el', 'iwlinks' => 'iw', 'sections' => 's', 'headitems' => 'hi', 'modules' => 'm', 'indicators' => 'ind', 'modulescripts' => 'm', 'modulestyles' => 'm', 'modulemessages' => 'm', 'properties' => 'pp', 'limitreportdata' => 'lr');
$this->setIndexedTagNames($result_array, $result_mapping);
$result->addValue(null, $this->getModuleName(), $result_array);
}
示例4: execute
//.........这里部分代码省略.........
$revid = $params['revid'];
if ($revid !== null) {
$rev = Revision::newFromId($revid);
if (!$rev) {
$this->dieUsage("There is no revision ID {$revid}", 'missingrev');
}
$title_obj = $rev->getTitle();
} else {
$title_obj = Title::newFromText($params['title']);
if (!$title_obj || $title_obj->isExternal()) {
$this->dieUsageMsg(array('invalidtitle', $params['title']));
}
}
$result = $this->getResult();
// Parse text
global $wgParser;
$options = ParserOptions::newFromContext($this->getContext());
if ($params['includecomments']) {
$options->setRemoveComments(false);
}
$retval = array();
if (isset($prop['parsetree']) || $params['generatexml']) {
$wgParser->startExternalParse($title_obj, $options, Parser::OT_PREPROCESS);
$dom = $wgParser->preprocessToDom($params['text']);
if (is_callable(array($dom, 'saveXML'))) {
$xml = $dom->saveXML();
} else {
$xml = $dom->__toString();
}
if (isset($prop['parsetree'])) {
unset($prop['parsetree']);
$retval['parsetree'] = $xml;
} else {
// the old way
$result->addValue(null, 'parsetree', $xml);
$result->addValue(null, ApiResult::META_BC_SUBELEMENTS, array('parsetree'));
}
}
// if they didn't want any output except (probably) the parse tree,
// then don't bother actually fully expanding it
if ($prop || $params['prop'] === null) {
$wgParser->startExternalParse($title_obj, $options, Parser::OT_PREPROCESS);
$frame = $wgParser->getPreprocessor()->newFrame();
$wikitext = $wgParser->preprocess($params['text'], $title_obj, $options, $revid, $frame);
if ($params['prop'] === null) {
// the old way
ApiResult::setContentValue($retval, 'wikitext', $wikitext);
} else {
$p_output = $wgParser->getOutput();
if (isset($prop['categories'])) {
$categories = $p_output->getCategories();
if ($categories) {
$categories_result = array();
foreach ($categories as $category => $sortkey) {
$entry = array();
$entry['sortkey'] = $sortkey;
ApiResult::setContentValue($entry, 'category', $category);
$categories_result[] = $entry;
}
ApiResult::setIndexedTagName($categories_result, 'category');
$retval['categories'] = $categories_result;
}
}
if (isset($prop['properties'])) {
$properties = $p_output->getProperties();
if ($properties) {
ApiResult::setArrayType($properties, 'BCkvp', 'name');
ApiResult::setIndexedTagName($properties, 'property');
$retval['properties'] = $properties;
}
}
if (isset($prop['volatile'])) {
$retval['volatile'] = $frame->isVolatile();
}
if (isset($prop['ttl']) && $frame->getTTL() !== null) {
$retval['ttl'] = $frame->getTTL();
}
if (isset($prop['wikitext'])) {
$retval['wikitext'] = $wikitext;
}
if (isset($prop['modules'])) {
$retval['modules'] = array_values(array_unique($p_output->getModules()));
$retval['modulescripts'] = array_values(array_unique($p_output->getModuleScripts()));
$retval['modulestyles'] = array_values(array_unique($p_output->getModuleStyles()));
}
if (isset($prop['jsconfigvars'])) {
$retval['jsconfigvars'] = ApiResult::addMetadataToResultVars($p_output->getJsConfigVars());
}
if (isset($prop['encodedjsconfigvars'])) {
$retval['encodedjsconfigvars'] = FormatJson::encode($p_output->getJsConfigVars(), false, FormatJson::ALL_OK);
$retval[ApiResult::META_SUBELEMENTS][] = 'encodedjsconfigvars';
}
if (isset($prop['modules']) && !isset($prop['jsconfigvars']) && !isset($prop['encodedjsconfigvars'])) {
$this->setWarning("Property 'modules' was set but not 'jsconfigvars' " . "or 'encodedjsconfigvars'. Configuration variables are necessary " . "for proper module usage.");
}
}
}
ApiResult::setSubelementsList($retval, array('wikitext', 'parsetree'));
$result->addValue(null, $this->getModuleName(), $retval);
}