/** * @param ApiResult $result * @param array $vals * @param string $params * @param string $type * @param string $action * @param string $ts * @param bool $legacy * @return array */ public static function addLogParams($result, &$vals, $params, $type, $action, $ts, $legacy = false) { switch ($type) { case 'move': if ($legacy) { $targetKey = 0; $noredirKey = 1; } else { $targetKey = '4::target'; $noredirKey = '5::noredir'; } if (isset($params[$targetKey])) { $title = Title::newFromText($params[$targetKey]); if ($title) { $vals2 = array(); ApiQueryBase::addTitleInfo($vals2, $title, 'new_'); $vals[$type] = $vals2; } } if (isset($params[$noredirKey]) && $params[$noredirKey]) { $vals[$type]['suppressedredirect'] = ''; } $params = null; break; case 'patrol': if ($legacy) { $cur = 0; $prev = 1; $auto = 2; } else { $cur = '4::curid'; $prev = '5::previd'; $auto = '6::auto'; } $vals2 = array(); $vals2['cur'] = $params[$cur]; $vals2['prev'] = $params[$prev]; $vals2['auto'] = $params[$auto]; $vals[$type] = $vals2; $params = null; break; case 'rights': $vals2 = array(); if ($legacy) { list($vals2['old'], $vals2['new']) = $params; } else { $vals2['new'] = implode(', ', $params['5::newgroups']); $vals2['old'] = implode(', ', $params['4::oldgroups']); } $vals[$type] = $vals2; $params = null; break; case 'block': if ($action == 'unblock') { break; } $vals2 = array(); list($vals2['duration'], $vals2['flags']) = $params; // Indefinite blocks have no expiry time if (SpecialBlock::parseExpiryInput($params[0]) !== wfGetDB(DB_SLAVE)->getInfinity()) { $vals2['expiry'] = wfTimestamp(TS_ISO_8601, strtotime($params[0], wfTimestamp(TS_UNIX, $ts))); } $vals[$type] = $vals2; $params = null; break; case 'upload': if (isset($params['img_timestamp'])) { $params['img_timestamp'] = wfTimestamp(TS_ISO_8601, $params['img_timestamp']); } break; } if (!is_null($params)) { $logParams = array(); // Keys like "4::paramname" can't be used for output so we change them to "paramname" foreach ($params as $key => $value) { if (strpos($key, ':') === false) { $logParams[$key] = $value; continue; } $logParam = explode(':', $key, 3); $logParams[$logParam[2]] = $value; } $result->setIndexedTagName($logParams, 'param'); $result->setIndexedTagName_recursive($logParams, 'param'); $vals = array_merge($vals, $logParams); } return $vals; }
/** * @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')); }