/**
  * Call this method to initialize output data. See execute()
  * @param ApiResult $result
  * @param object $feed An instance of one of the $wgFeedClasses classes
  * @param array $feedItems Array of FeedItem objects
  */
 public static function setResult($result, $feed, $feedItems)
 {
     // Store output in the Result data.
     // This way we can check during execution if any error has occurred
     // Disable size checking for this because we can't continue
     // cleanly; size checking would cause more problems than it'd
     // solve
     $result->addValue(null, '_feed', $feed, ApiResult::NO_VALIDATE);
     $result->addValue(null, '_feeditems', $feedItems, ApiResult::NO_VALIDATE);
 }
 private function doTag($tagName, $hitcount)
 {
     static $count = 0;
     static $doneTags = array();
     if (in_array($tagName, $doneTags)) {
         return true;
     }
     if (++$count > $this->limit) {
         $this->setContinueEnumParameter('continue', $tagName);
         return false;
     }
     $tag = array();
     $tag['name'] = $tagName;
     if ($this->fld_displayname) {
         $tag['displayname'] = ChangeTags::tagDescription($tagName);
     }
     if ($this->fld_description) {
         $msg = wfMessage("tag-{$tagName}-description");
         $tag['description'] = $msg->exists() ? $msg->text() : '';
     }
     if ($this->fld_hitcount) {
         $tag['hitcount'] = $hitcount;
     }
     $doneTags[] = $tagName;
     $fit = $this->result->addValue(array('query', $this->getModuleName()), null, $tag);
     if (!$fit) {
         $this->setContinueEnumParameter('continue', $tagName);
         return false;
     }
     return true;
 }
 /**
  * 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);
     }
 }
Example #4
0
 /**
  * @dataProvider provideRedirectMergePolicy
  */
 public function testRedirectMergePolicyWithApiResult($mergePolicy, $expect)
 {
     list($target, $pageSet) = $this->createPageSetWithRedirect();
     $pageSet->setRedirectMergePolicy($mergePolicy);
     $result = new ApiResult(false);
     $result->addValue(null, 'pages', [$target->getArticleID() => []]);
     $pageSet->populateGeneratorData($result, ['pages']);
     $this->assertEquals($expect, $result->getResultData(['pages', $target->getArticleID()]));
 }
 /**
  * Add page properties to an ApiResult, adding a continue
  * parameter if it doesn't fit.
  *
  * @param ApiResult $result
  * @param int $page
  * @param array $props
  * @return bool True if it fits in the result
  */
 private function addPageProps($result, $page, $props)
 {
     ApiResult::setArrayType($props, 'assoc');
     $fit = $result->addValue(['query', 'pages', $page], 'pageprops', $props);
     if (!$fit) {
         $this->setContinueEnumParameter('continue', $page);
     }
     return $fit;
 }
Example #6
0
 /**
  * Append the debug info to given ApiResult
  *
  * @param $context IContextSource
  * @param $result ApiResult
  */
 public static function appendDebugInfoToApiResult(IContextSource $context, ApiResult $result)
 {
     if (!self::$enabled) {
         return;
     }
     // output errors as debug info, when display_errors is on
     // this is necessary for all non html output of the api, because that clears all errors first
     $obContents = ob_get_contents();
     if ($obContents) {
         $obContentArray = explode('<br />', $obContents);
         foreach ($obContentArray as $obContent) {
             if (trim($obContent)) {
                 self::debugMsg(Sanitizer::stripAllTags($obContent));
             }
         }
     }
     MWDebug::log('MWDebug output complete');
     $debugInfo = self::getDebugInfo($context);
     $result->setIndexedTagName($debugInfo, 'debuginfo');
     $result->setIndexedTagName($debugInfo['log'], 'line');
     $result->setIndexedTagName($debugInfo['debugLog'], 'msg');
     $result->setIndexedTagName($debugInfo['queries'], 'query');
     $result->setIndexedTagName($debugInfo['includes'], 'queries');
     $result->addValue(null, 'debuginfo', $debugInfo);
 }
Example #7
0
 /**
  * @param ApiPageSet $pageSet Pages to be exported
  * @param ApiResult $result Result to output to
  */
 private function doExport($pageSet, $result)
 {
     $exportTitles = array();
     $titles = $pageSet->getGoodTitles();
     if (count($titles)) {
         $user = $this->getUser();
         /** @var $title Title */
         foreach ($titles as $title) {
             if ($title->userCan('read', $user)) {
                 $exportTitles[] = $title;
             }
         }
     }
     $exporter = new WikiExporter($this->getDB());
     // WikiExporter writes to stdout, so catch its
     // output with an ob
     ob_start();
     $exporter->openStream();
     foreach ($exportTitles as $title) {
         $exporter->pageByTitle($title);
     }
     $exporter->closeStream();
     $exportxml = ob_get_contents();
     ob_end_clean();
     // Don't check the size of exported stuff
     // It's not continuable, so it would cause more
     // problems than it'd solve
     if ($this->mParams['exportnowrap']) {
         $result->reset();
         // Raw formatter will handle this
         $result->addValue(null, 'text', $exportxml, ApiResult::NO_SIZE_CHECK);
         $result->addValue(null, 'mime', 'text/xml', ApiResult::NO_SIZE_CHECK);
     } else {
         $r = array();
         ApiResult::setContent($r, $exportxml);
         $result->addValue('query', 'export', $r, ApiResult::NO_SIZE_CHECK);
     }
 }
Example #8
0
 /**
  * Add page properties to an ApiResult, adding a continue
  * parameter if it doesn't fit.
  *
  * @param ApiResult $result
  * @param int $page
  * @param array $props
  * @return bool True if it fits in the result
  */
 private function addPageProps($result, $page, $props)
 {
     $fit = $result->addValue(array('query', 'pages', $page), 'pageprops', $props);
     if (!$fit) {
         $this->setContinueEnumParameter('continue', $page);
     }
     return $fit;
 }
Example #9
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'));
 }
 private function getRedirectName(ApiResult $result)
 {
     $res =& $result->getData();
     if (isset($res['query']) && isset($res['query']['pages'])) {
         foreach ($this->getPageSet()->getGoodTitles() as $page_id => $oTitle) {
             $res['query']['pages'][$page_id]['redirectto'] = "";
             if ($oTitle->isRedirect()) {
                 $oArticle = new Article($oTitle);
                 $oRedirTitle = $oArticle->getRedirectTarget();
                 if ($oRedirTitle instanceof Title) {
                     $result->addValue(array("query", "pages", $page_id), "redirectto", Title::makeName($oRedirTitle->getNamespace(), $oRedirTitle->getDBkey()));
                 }
             }
         }
     }
 }
 /**
  * Store the continuation data into the result
  * @param ApiResult $result
  */
 public function setContinuationIntoResult(ApiResult $result)
 {
     list($data, $batchcomplete) = $this->getContinuation();
     if ($data) {
         $result->addValue(null, 'continue', $data, ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK);
     }
     if ($batchcomplete) {
         $result->addValue(null, 'batchcomplete', true, ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK);
     }
 }
Example #12
0
 /**
  * @covers ApiResult
  * @dataProvider provideTransformations
  * @param string $label
  * @param array $input
  * @param array $transforms
  * @param array|Exception $expect
  */
 public function testTransformations($label, $input, $transforms, $expect)
 {
     $result = new ApiResult(false);
     $result->addValue(null, 'test', $input);
     if ($expect instanceof Exception) {
         try {
             $output = $result->getResultData('test', $transforms);
             $this->fail('Expected exception not thrown', $label);
         } catch (Exception $ex) {
             $this->assertEquals($ex, $expect, $label);
         }
     } else {
         $output = $result->getResultData('test', $transforms);
         $this->assertEquals($expect, $output, $label);
     }
 }
Example #13
0
 /**
  * @param ApiPageSet $pageSet Pages to be exported
  * @param ApiResult $result Result to output to
  */
 private function doExport($pageSet, $result)
 {
     $exportTitles = [];
     $titles = $pageSet->getGoodTitles();
     if (count($titles)) {
         $user = $this->getUser();
         /** @var $title Title */
         foreach ($titles as $title) {
             if ($title->userCan('read', $user)) {
                 $exportTitles[] = $title;
             }
         }
     }
     $exporter = new WikiExporter($this->getDB());
     $sink = new DumpStringOutput();
     $exporter->setOutputSink($sink);
     $exporter->openStream();
     foreach ($exportTitles as $title) {
         $exporter->pageByTitle($title);
     }
     $exporter->closeStream();
     // Don't check the size of exported stuff
     // It's not continuable, so it would cause more
     // problems than it'd solve
     if ($this->mParams['exportnowrap']) {
         $result->reset();
         // Raw formatter will handle this
         $result->addValue(null, 'text', $sink, ApiResult::NO_SIZE_CHECK);
         $result->addValue(null, 'mime', 'text/xml', ApiResult::NO_SIZE_CHECK);
     } else {
         $result->addValue('query', 'export', $sink, ApiResult::NO_SIZE_CHECK);
         $result->addValue('query', ApiResult::META_BC_SUBELEMENTS, ['export']);
     }
 }