Exemple #1
0
 /**
  * Appends an element for each page in the current pageSet with the
  * most general information (id, title), plus any title normalizations
  * and missing or invalid title/pageids/revids.
  */
 private function outputGeneralPageInfo()
 {
     $pageSet = $this->getPageSet();
     $result = $this->getResult();
     // We can't really handle max-result-size failure here, but we need to
     // check anyway in case someone set the limit stupidly low.
     $fit = true;
     $values = $pageSet->getNormalizedTitlesAsResult($result);
     if ($values) {
         $fit = $fit && $result->addValue('query', 'normalized', $values);
     }
     $values = $pageSet->getConvertedTitlesAsResult($result);
     if ($values) {
         $fit = $fit && $result->addValue('query', 'converted', $values);
     }
     $values = $pageSet->getInterwikiTitlesAsResult($result, $this->mParams['iwurl']);
     if ($values) {
         $fit = $fit && $result->addValue('query', 'interwiki', $values);
     }
     $values = $pageSet->getRedirectTitlesAsResult($result);
     if ($values) {
         $fit = $fit && $result->addValue('query', 'redirects', $values);
     }
     $values = $pageSet->getMissingRevisionIDsAsResult($result);
     if ($values) {
         $fit = $fit && $result->addValue('query', 'badrevids', $values);
     }
     // Page elements
     $pages = array();
     // Report any missing titles
     foreach ($pageSet->getMissingTitles() as $fakeId => $title) {
         $vals = array();
         ApiQueryBase::addTitleInfo($vals, $title);
         $vals['missing'] = true;
         $pages[$fakeId] = $vals;
     }
     // Report any invalid titles
     foreach ($pageSet->getInvalidTitlesAndReasons() as $fakeId => $data) {
         $pages[$fakeId] = $data + array('invalid' => true);
     }
     // Report any missing page ids
     foreach ($pageSet->getMissingPageIDs() as $pageid) {
         $pages[$pageid] = array('pageid' => $pageid, 'missing' => true);
     }
     // Report special pages
     /** @var $title Title */
     foreach ($pageSet->getSpecialTitles() as $fakeId => $title) {
         $vals = array();
         ApiQueryBase::addTitleInfo($vals, $title);
         $vals['special'] = true;
         if ($title->isSpecialPage() && !SpecialPageFactory::exists($title->getDBkey())) {
             $vals['missing'] = true;
         } elseif ($title->getNamespace() == NS_MEDIA && !wfFindFile($title)) {
             $vals['missing'] = true;
         }
         $pages[$fakeId] = $vals;
     }
     // Output general page information for found titles
     foreach ($pageSet->getGoodTitles() as $pageid => $title) {
         $vals = array();
         $vals['pageid'] = $pageid;
         ApiQueryBase::addTitleInfo($vals, $title);
         $pages[$pageid] = $vals;
     }
     if (count($pages)) {
         $pageSet->populateGeneratorData($pages);
         ApiResult::setArrayType($pages, 'BCarray');
         if ($this->mParams['indexpageids']) {
             $pageIDs = array_keys(ApiResult::stripMetadataNonRecursive($pages));
             // json treats all map keys as strings - converting to match
             $pageIDs = array_map('strval', $pageIDs);
             ApiResult::setIndexedTagName($pageIDs, 'id');
             $fit = $fit && $result->addValue('query', 'pageids', $pageIDs);
         }
         ApiResult::setIndexedTagName($pages, 'page');
         $fit = $fit && $result->addValue('query', 'pages', $pages);
     }
     if (!$fit) {
         $this->dieUsage('The value of $wgAPIMaxResultSize on this wiki is ' . 'too small to hold basic result information', 'badconfig');
     }
     if ($this->mParams['export']) {
         $this->doExport($pageSet, $result);
     }
 }
 /**
  * @covers ApiResult
  */
 public function testUtilityFunctions()
 {
     $arr = array('foo' => array('bar' => array('_dummy' => 'foobaz'), 'bar2' => (object) array('_dummy' => 'foobaz'), 'x' => 'ok', '_dummy' => 'foobaz'), 'foo2' => (object) array('bar' => array('_dummy' => 'foobaz'), 'bar2' => (object) array('_dummy' => 'foobaz'), 'x' => 'ok', '_dummy' => 'foobaz'), ApiResult::META_SUBELEMENTS => array('foo', 'bar'), ApiResult::META_INDEXED_TAG_NAME => 'itn', ApiResult::META_PRESERVE_KEYS => array('foo', 'bar', '_dummy2', 0), ApiResult::META_TYPE => 'array', '_dummy' => 'foobaz', '_dummy2' => 'foobaz!');
     $this->assertEquals(array('foo' => array('bar' => array(), 'bar2' => (object) array(), 'x' => 'ok'), 'foo2' => (object) array('bar' => array(), 'bar2' => (object) array(), 'x' => 'ok'), '_dummy2' => 'foobaz!'), ApiResult::stripMetadata($arr), 'ApiResult::stripMetadata');
     $metadata = array();
     $data = ApiResult::stripMetadataNonRecursive($arr, $metadata);
     $this->assertEquals(array('foo' => array('bar' => array('_dummy' => 'foobaz'), 'bar2' => (object) array('_dummy' => 'foobaz'), 'x' => 'ok', '_dummy' => 'foobaz'), 'foo2' => (object) array('bar' => array('_dummy' => 'foobaz'), 'bar2' => (object) array('_dummy' => 'foobaz'), 'x' => 'ok', '_dummy' => 'foobaz'), '_dummy2' => 'foobaz!'), $data, 'ApiResult::stripMetadataNonRecursive ($data)');
     $this->assertEquals(array(ApiResult::META_SUBELEMENTS => array('foo', 'bar'), ApiResult::META_INDEXED_TAG_NAME => 'itn', ApiResult::META_PRESERVE_KEYS => array('foo', 'bar', '_dummy2', 0), ApiResult::META_TYPE => 'array', '_dummy' => 'foobaz'), $metadata, 'ApiResult::stripMetadataNonRecursive ($metadata)');
     $metadata = null;
     $data = ApiResult::stripMetadataNonRecursive((object) $arr, $metadata);
     $this->assertEquals((object) array('foo' => array('bar' => array('_dummy' => 'foobaz'), 'bar2' => (object) array('_dummy' => 'foobaz'), 'x' => 'ok', '_dummy' => 'foobaz'), 'foo2' => (object) array('bar' => array('_dummy' => 'foobaz'), 'bar2' => (object) array('_dummy' => 'foobaz'), 'x' => 'ok', '_dummy' => 'foobaz'), '_dummy2' => 'foobaz!'), $data, 'ApiResult::stripMetadataNonRecursive on object ($data)');
     $this->assertEquals(array(ApiResult::META_SUBELEMENTS => array('foo', 'bar'), ApiResult::META_INDEXED_TAG_NAME => 'itn', ApiResult::META_PRESERVE_KEYS => array('foo', 'bar', '_dummy2', 0), ApiResult::META_TYPE => 'array', '_dummy' => 'foobaz'), $metadata, 'ApiResult::stripMetadataNonRecursive on object ($metadata)');
 }
 /**
  * Gets the edit token and timestamps in some ugly array structure. Needs to
  * be cleaned up.
  * @throws MWException
  * @return \array
  */
 protected function getEditInfo()
 {
     $params = new FauxRequest(array('action' => 'query', 'prop' => 'info|revisions', 'intoken' => 'edit', 'titles' => $this->getTitle(), 'rvprop' => 'timestamp'));
     $api = new ApiMain($params);
     $api->execute();
     if (defined('ApiResult::META_CONTENT')) {
         $data = $api->getResult()->getResultData();
     } else {
         $data = $api->getResultData();
     }
     if (!isset($data['query']['pages'])) {
         throw new MWException('Api query failed');
     }
     $data = $data['query']['pages'];
     if (defined('ApiResult::META_CONTENT')) {
         $data = ApiResult::stripMetadataNonRecursive($data);
     }
     $data = array_shift($data);
     return $data;
 }