/**
  * Actually add the warning or error to the result
  * @param string $tag 'warning' or 'error'
  * @param string $moduleName
  * @param ApiMessage|ApiRawMessage $msg
  */
 protected function addWarningOrError($tag, $moduleName, $msg)
 {
     $value = ['code' => $msg->getApiCode()];
     switch ($this->format) {
         case 'wikitext':
             $value += ['text' => $msg->text(), ApiResult::META_CONTENT => 'text'];
             break;
         case 'html':
             $value += ['html' => $msg->parse(), ApiResult::META_CONTENT => 'html'];
             break;
         case 'raw':
             $value += ['message' => $msg->getKey(), 'params' => $msg->getParams()];
             ApiResult::setIndexedTagName($value['params'], 'param');
             break;
         case 'none':
             break;
     }
     $value += $msg->getApiData();
     $path = [$tag . 's', $moduleName];
     $existing = $this->result->getResultData($path);
     if ($existing === null || !in_array($value, $existing)) {
         $flags = ApiResult::NO_SIZE_CHECK;
         if ($existing === null) {
             $flags |= ApiResult::ADD_ON_TOP;
         }
         $this->result->addValue($path, null, $value, $flags);
         $this->result->addIndexedTagName($path, $tag);
     }
 }
Beispiel #2
0
 /**
  * @covers ApiResult
  */
 public function testDeprecatedFunctions()
 {
     // Ignore ApiResult deprecation warnings during this test
     set_error_handler(function ($errno, $errstr) use(&$warnings) {
         if (preg_match('/Use of ApiResult::\\S+ was deprecated in MediaWiki \\d+.\\d+\\./', $errstr)) {
             return true;
         }
         if (preg_match('/Use of ApiMain to ApiResult::__construct ' . 'was deprecated in MediaWiki \\d+.\\d+\\./', $errstr)) {
             return true;
         }
         return false;
     });
     $reset = new ScopedCallback('restore_error_handler');
     $context = new DerivativeContext(RequestContext::getMain());
     $context->setConfig(new HashConfig(array('APIModules' => array(), 'APIFormatModules' => array(), 'APIMaxResultSize' => 42)));
     $main = new ApiMain($context);
     $result = TestingAccessWrapper::newFromObject(new ApiResult($main));
     $this->assertSame(42, $result->maxSize);
     $this->assertSame($main->getErrorFormatter(), $result->errorFormatter);
     $this->assertSame($main, $result->mainForContinuation);
     $result = new ApiResult(8388608);
     $result->addContentValue(null, 'test', 'content');
     $result->addContentValue(array('foo', 'bar'), 'test', 'content');
     $result->addIndexedTagName(null, 'itn');
     $result->addSubelementsList(null, array('sub'));
     $this->assertSame(array('foo' => array('bar' => array('*' => 'content')), '*' => 'content'), $result->getData());
     $result->setRawMode();
     $this->assertSame(array('foo' => array('bar' => array('*' => 'content')), '*' => 'content', '_element' => 'itn', '_subelements' => array('sub')), $result->getData());
     $arr = array();
     ApiResult::setContent($arr, 'value');
     ApiResult::setContent($arr, 'value2', 'foobar');
     $this->assertSame(array(ApiResult::META_CONTENT => 'content', 'content' => 'value', 'foobar' => array(ApiResult::META_CONTENT => 'content', 'content' => 'value2')), $arr);
     $result = new ApiResult(3);
     $formatter = new ApiErrorFormatter_BackCompat($result);
     $result->setErrorFormatter($formatter);
     $result->disableSizeCheck();
     $this->assertTrue($result->addValue(null, 'foo', '1234567890'));
     $result->enableSizeCheck();
     $this->assertSame(0, $result->getSize());
     $this->assertFalse($result->addValue(null, 'foo', '1234567890'));
     $arr = array('foo' => array('bar' => 1));
     $result->setIndexedTagName_recursive($arr, 'itn');
     $this->assertSame(array('foo' => array('bar' => 1, ApiResult::META_INDEXED_TAG_NAME => 'itn')), $arr);
     $status = Status::newGood();
     $status->fatal('parentheses', '1');
     $status->fatal('parentheses', '2');
     $status->warning('parentheses', '3');
     $status->warning('parentheses', '4');
     $this->assertSame(array(array('type' => 'error', 'message' => 'parentheses', 'params' => array(0 => '1', ApiResult::META_INDEXED_TAG_NAME => 'param')), array('type' => 'error', 'message' => 'parentheses', 'params' => array(0 => '2', ApiResult::META_INDEXED_TAG_NAME => 'param')), ApiResult::META_INDEXED_TAG_NAME => 'error'), $result->convertStatusToArray($status, 'error'));
     $this->assertSame(array(array('type' => 'warning', 'message' => 'parentheses', 'params' => array(0 => '3', ApiResult::META_INDEXED_TAG_NAME => 'param')), array('type' => 'warning', 'message' => 'parentheses', 'params' => array(0 => '4', ApiResult::META_INDEXED_TAG_NAME => 'param')), ApiResult::META_INDEXED_TAG_NAME => 'warning'), $result->convertStatusToArray($status, 'warning'));
 }
Beispiel #3
0
 /**
  * @covers ApiResult
  */
 public function testMetadata()
 {
     $arr = ['foo' => ['bar' => []]];
     $result = new ApiResult(8388608);
     $result->addValue(null, 'foo', ['bar' => []]);
     $expect = ['foo' => ['bar' => [ApiResult::META_INDEXED_TAG_NAME => 'ritn', ApiResult::META_TYPE => 'default'], ApiResult::META_INDEXED_TAG_NAME => 'ritn', ApiResult::META_TYPE => 'default'], ApiResult::META_SUBELEMENTS => ['foo', 'bar'], ApiResult::META_INDEXED_TAG_NAME => 'itn', ApiResult::META_PRESERVE_KEYS => ['foo', 'bar'], ApiResult::META_TYPE => 'array'];
     ApiResult::setSubelementsList($arr, 'foo');
     ApiResult::setSubelementsList($arr, ['bar', 'baz']);
     ApiResult::unsetSubelementsList($arr, 'baz');
     ApiResult::setIndexedTagNameRecursive($arr, 'ritn');
     ApiResult::setIndexedTagName($arr, 'itn');
     ApiResult::setPreserveKeysList($arr, 'foo');
     ApiResult::setPreserveKeysList($arr, ['bar', 'baz']);
     ApiResult::unsetPreserveKeysList($arr, 'baz');
     ApiResult::setArrayTypeRecursive($arr, 'default');
     ApiResult::setArrayType($arr, 'array');
     $this->assertSame($expect, $arr);
     $result->addSubelementsList(null, 'foo');
     $result->addSubelementsList(null, ['bar', 'baz']);
     $result->removeSubelementsList(null, 'baz');
     $result->addIndexedTagNameRecursive(null, 'ritn');
     $result->addIndexedTagName(null, 'itn');
     $result->addPreserveKeysList(null, 'foo');
     $result->addPreserveKeysList(null, ['bar', 'baz']);
     $result->removePreserveKeysList(null, 'baz');
     $result->addArrayTypeRecursive(null, 'default');
     $result->addArrayType(null, 'array');
     $this->assertEquals($expect, $result->getResultData());
     $arr = ['foo' => ['bar' => []]];
     $expect = ['foo' => ['bar' => [ApiResult::META_TYPE => 'kvp', ApiResult::META_KVP_KEY_NAME => 'key'], ApiResult::META_TYPE => 'kvp', ApiResult::META_KVP_KEY_NAME => 'key'], ApiResult::META_TYPE => 'BCkvp', ApiResult::META_KVP_KEY_NAME => 'bc'];
     ApiResult::setArrayTypeRecursive($arr, 'kvp', 'key');
     ApiResult::setArrayType($arr, 'BCkvp', 'bc');
     $this->assertSame($expect, $arr);
 }