Exemplo n.º 1
0
 function testIncorrectPurgePages()
 {
     $api = $this->getMockApi();
     $api->expects($this->once())->method('postRequest')->with($this->isInstanceOf('\\Mediawiki\\Api\\SimpleRequest'))->will($this->returnValue(["batchcomplete" => "", "purge" => [["ns" => 0, "title" => "Foo", "purged" => ""], ["ns" => 0, "title" => "Bar", "purged" => ""], ["ns" => 0, "title" => "This page really does not exist", "missing" => ""]]]));
     $service = new PagePurger($api);
     $pages = new Pages([new Page(new PageIdentifier(new Title('Foo', 0), 100)), new Page(new PageIdentifier(new Title('Bar', 1), 101)), new Page(new PageIdentifier(new Title('MissingPage', 1), 103))]);
     // MissingPage is not in the pages that are returned by purgePages
     $pagesArray = $pages->toArray();
     array_pop($pagesArray);
     $result = new Pages($pagesArray);
     $this->assertEquals($service->purgePages($pages), $result);
 }
Exemplo n.º 2
0
 /**
  * @since 0.7
  *
  * @brief Purge multiple pages
  *
  * Purges all the pages of the Pages object
  * by submitting a 'purge' action to the mediawiki
  * api with the parameter 'pageids' set to be the
  * pages ids in multiple-value seperation.
  *
  * @param Pages $pages the pages that are going to be purged
  *
  * @return Pages the pages that have been purged successfully
  */
 public function purgePages(Pages $pages)
 {
     $pagesArray = $pages->toArray();
     $pagesIds = [];
     foreach ($pagesArray as $page) {
         array_push($pagesIds, $page->getId());
     }
     // convert an array to multiple-value format
     // because the mediawiki api require multiple
     // values to be seperated like the example
     // ex: [111, 222, 333] => "111|222|333"
     $pageIdsMultiple = implode('|', $pagesIds);
     $responseArray = $this->api->postRequest(new SimpleRequest('purge', ['pageids' => $pageIdsMultiple]));
     // array that will hold the successfully purged pages
     $purgedPages = new Pages();
     // for every purge result
     foreach ($responseArray['purge'] as $purgeResponse) {
         // if the purge for the page was successful
         if (array_key_exists('purged', $purgeResponse)) {
             // we iterate all the input pages
             foreach ($pagesArray as $page) {
                 // and if the page from the input was successfully purged
                 if ($purgeResponse['title'] === $page->getTitle()->getText()) {
                     // add it in the purgedPages object
                     $purgedPages->addPage($page);
                     break;
                 }
             }
         }
     }
     return $purgedPages;
 }
Exemplo n.º 3
0
 /**
  * Run a query to completion.
  *
  * @param string[] $params Query parameters
  * @param string $contName Result subelement name for continue details
  * @param string $resName Result element name for main results array
  * @param string $pageIdName Result element name for page ID
  * @param boolean $cont Whether to continue the query, using multiple requests
  * @return Pages
  */
 protected function runQuery($params, $contName, $resName, $pageIdName = 'pageid', $cont = true)
 {
     $pages = new Pages();
     do {
         // Set up continue parameter if it's been set already.
         if (isset($result['continue'][$contName])) {
             $params[$contName] = $result['continue'][$contName];
         }
         // Run the actual query.
         $result = $this->api->getRequest(new SimpleRequest('query', $params));
         if (!array_key_exists('query', $result)) {
             return $pages;
         }
         // Add the results to the output page list.
         foreach ($result['query'][$resName] as $member) {
             $pageTitle = new Title($member['title'], $member['ns']);
             $page = new Page(new PageIdentifier($pageTitle, $member[$pageIdName]));
             $pages->addPage($page);
         }
     } while ($cont && isset($result['continue']));
     return $pages;
 }
Exemplo n.º 4
0
 /**
  * @dataProvider provideValidConstruction
  */
 public function testValidConstruction($input, $expected)
 {
     $pages = new Pages($input);
     $this->assertEquals($expected, $pages->toArray());
 }
Exemplo n.º 5
0
 /**
  * @param ListRandomOptions $options
  */
 public function getRandom(ListRandomOptions $options = null)
 {
     if (is_null($options)) {
         $options = new ListRandomOptions();
     }
     $continue = '';
     $limit = $options->getLimit();
     $pages = new Pages();
     while (true) {
         $params = array('list' => 'random', 'rnlimit' => $options->getLimit(), 'rnnamespace' => implode('|', $options->getNamespaces()));
         if ($options->getRedirectsOnly() === true) {
             $params['rnredirect'] = 1;
         }
         if (!empty($continue)) {
             $params['rncontinue'] = $continue;
         }
         if ($limit === null) {
             $params['rnlimit'] = 5000;
         } else {
             $params['rnlimit'] = $limit;
         }
         $result = $this->api->getRequest(new SimpleRequest('query', $params));
         $limit = $limit - count($result['query']['random']);
         foreach ($result['query']['random'] as $member) {
             $pages->addPage(new Page(new PageIdentifier(new Title($member['title'], $member['ns']), $member['pageid']), new Revisions()));
         }
         if ($limit !== null && $limit <= 0) {
             return $pages;
         }
         if (empty($result['query-continue']['random']['rncontinue'])) {
             return $pages;
         } else {
             $continue = $result['query-continue']['random']['rncontinue'];
         }
     }
 }
Exemplo n.º 6
0
 /**
  * Visit every descendant page of $rootCategoryName (which will be a Category
  * page, because there are no desecendants of any other pages).
  * @param Page $rootCat The full name of the page to start at.
  * @param Page[] $currentPath Used only when recursing into this method, to track each path
  * through the category hierarchy in case of loops.
  * @return Pages All descendants of the given category.
  * @throws CategoryLoopException If a category loop is detected.
  */
 public function descend(Page $rootCat, $currentPath = null)
 {
     // Make sure we know the namespace IDs.
     $this->retrieveNamespaces();
     $rootCatName = $rootCat->getPageIdentifier()->getTitle()->getText();
     if (is_null($currentPath)) {
         $this->alreadyVisited = [];
         $currentPath = new Pages();
     }
     $this->alreadyVisited[] = $rootCatName;
     $currentPath->addPage($rootCat);
     // Start a list of child pages.
     $descendants = new Pages();
     do {
         $pageListGetter = new PageListGetter($this->api);
         $members = $pageListGetter->getPageListFromCategoryName($rootCatName);
         foreach ($members->toArray() as $member) {
             /** @var Title */
             $memberTitle = $member->getPageIdentifier()->getTitle();
             // See if this page is a Category page.
             $isCat = false;
             if (isset($this->namespaces[$memberTitle->getNs()])) {
                 $ns = $this->namespaces[$memberTitle->getNs()];
                 $isCat = isset($ns['canonical']) && $ns['canonical'] === 'Category';
             }
             // If it's a category, descend into it.
             if ($isCat) {
                 // If this member has already been visited on this branch of the traversal,
                 // throw an Exception with information about which categories form the loop.
                 if ($currentPath->hasPage($member)) {
                     $currentPath->addPage($member);
                     $loop = new CategoryLoopException();
                     $loop->setCategoryPath($currentPath);
                     throw $loop;
                 }
                 // Don't go any further if we've already visited this member
                 // (does not indicate a loop, however; we've already caught that above).
                 if (in_array($memberTitle->getText(), $this->alreadyVisited)) {
                     continue;
                 }
                 // Call any registered callbacked, and carry on to the next branch.
                 $this->call(self::CALLBACK_CATEGORY, [$member, $rootCat]);
                 $newDescendants = $this->descend($member, $currentPath);
                 $descendants->addPages($newDescendants);
                 $currentPath = new Pages();
                 // Re-set the path.
             } else {
                 // If it's a page, add it to the list and carry on.
                 $descendants->addPage($member);
                 $this->call(self::CALLBACK_PAGE, [$member, $rootCat]);
             }
         }
     } while (isset($result['continue']));
     return $descendants;
 }