/** * @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; }
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); }
/** * @dataProvider provideValidConstruction */ public function testValidConstruction($input, $expected) { $pages = new Pages($input); $this->assertEquals($expected, $pages->toArray()); }