Esempio n. 1
0
 /**
  * @since 0.3
  *
  * @brief Purge a single page
  *
  * Purges a single page by submitting a
  * 'purge' action to the mediawiki api
  * with the parameter 'pageids' set to
  * the singe page id
  *
  * @param Page $page the page that is going to be purged
  *
  * @return bool return true if the purge was successful
  */
 public function purge(Page $page)
 {
     $responseArray = $this->api->postRequest(new SimpleRequest('purge', ['pageids' => $page->getId()]));
     // the purge response for the page
     $purgeResponse = $responseArray['purge'][0];
     return array_key_exists('purged', $purgeResponse);
 }
Esempio n. 2
0
 /**
  * @since 0.3
  *
  * @param Page $page
  * @param string[] $protections where the 'key' is the action and the 'value' is the group
  * @param ProtectOptions $options
  *
  * @return bool
  * @throws InvalidArgumentException
  */
 public function protect(Page $page, $protections, ProtectOptions $options = null)
 {
     if (!is_array($protections) || empty($protections)) {
         throw new InvalidArgumentException('$protections must be an array with keys and values');
     }
     $params = array('pageid' => $page->getId(), 'token' => $this->api->getToken('protect'));
     $protectionsString = '';
     foreach ($protections as $action => $value) {
         if (!is_string($action) || !is_string($value)) {
             throw new InvalidArgumentException('All keys and elements of $protections must be strings');
         }
         $protectionsString = $action . '=' . $value . '|';
     }
     $params['protections'] = rtrim($protectionsString, '|');
     if ($options->getExpiry() !== 'infinite') {
         $params['expiry'] = $options->getExpiry();
     }
     if ($options->getReason() !== '') {
         $params['reason'] = $options->getReason();
     }
     if ($options->getCascade()) {
         $params['cascade'] = '';
     }
     if ($options->getWatchlist() !== 'preferences') {
         $params['watchlist'] = $options->getWatchlist();
     }
     $this->api->postRequest(new SimpleRequest('protect', $params));
     return true;
 }
Esempio n. 3
0
 /**
  * @dataProvider provideValidConstruction
  */
 public function testValidConstruction($pageIdentifier, $revisions)
 {
     $page = new Page($pageIdentifier, $revisions);
     $this->assertEquals($pageIdentifier, $page->getPageIdentifier());
     if (is_null($revisions)) {
         $this->assertInstanceOf('Mediawiki\\DataModel\\Revisions', $page->getRevisions());
     } else {
         $this->assertEquals($revisions, $page->getRevisions());
     }
 }
Esempio n. 4
0
 /**
  * @param string $url
  * @param PageIdentifier $pageIdentifier
  * @param Revisions $revisions
  */
 public function __construct($url, PageIdentifier $pageIdentifier = null, Revisions $revisions = null)
 {
     parent::__construct($pageIdentifier, $revisions);
     if (!is_string($url)) {
         throw new InvalidArgumentException('$url must be a string');
     }
     $this->url = $url;
 }
Esempio n. 5
0
 /**
  * @since 0.3
  *
  * @param Page $page
  * @param string[] $protections where the 'key' is the action and the 'value' is the group
  * @param array $extraParams
  *
  * @return bool
  * @throws InvalidArgumentException
  */
 public function protect(Page $page, $protections, array $extraParams = [])
 {
     if (!is_array($protections) || empty($protections)) {
         throw new InvalidArgumentException('$protections must be an array with keys and values');
     }
     $params = ['pageid' => $page->getId(), 'token' => $this->api->getToken('protect')];
     $protectionsString = '';
     foreach ($protections as $action => $value) {
         if (!is_string($action) || !is_string($value)) {
             throw new InvalidArgumentException('All keys and elements of $protections must be strings');
         }
         $protectionsString = $action . '=' . $value . '|';
     }
     $params['protections'] = rtrim($protectionsString, '|');
     $this->api->postRequest(new SimpleRequest('protect', array_merge($extraParams, $params)));
     return true;
 }
 /**
  * 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;
 }
Esempio n. 7
0
 /**
  * @param Page $page
  *
  * @returns bool
  */
 public function watch(Page $page)
 {
     $params = ['token' => $this->api->getToken('watch')];
     if (!is_null($page->getPageIdentifier()->getId())) {
         $params['pageids'] = $page->getPageIdentifier()->getId();
     } elseif (!is_null($page->getPageIdentifier()->getTitle())) {
         $params['titles'] = $page->getPageIdentifier()->getTitle()->getTitle();
     } elseif (!is_null($page->getRevisions()->getLatest())) {
         $params['revids'] = $page->getRevisions()->getLatest()->getId();
     }
     $this->api->postRequest(new SimpleRequest('watch', $params));
     return true;
 }
 /**
  * @param Page $page
  *
  * @return Page
  *
  * @throws InvalidArgumentException
  */
 public function getFromPage(Page $page)
 {
     return $this->getFromPageIdentifier($page->getPageIdentifier());
 }
Esempio n. 9
0
 /**
  * @since 0.3
  *
  * @param Page $page
  *
  * @return bool
  */
 public function purge(Page $page)
 {
     $this->api->postRequest(new SimpleRequest('purge', array('pageids' => $page->getId())));
     return true;
 }
Esempio n. 10
0
 /**
  * @since 0.2
  *
  * @param Page $page
  * @param QueryOptions|null $options
  *
  * @return Page
  */
 public function getFromPage(Page $page, QueryOptions $options = null)
 {
     if (is_null($options)) {
         $options = new QueryOptions();
     }
     $result = $this->api->getRequest(new SimpleRequest('query', $this->getQuery(array('pageids' => $page->getId()), $options)));
     $revisions = $this->getRevisionsFromResult(array_shift($result['query']['pages']));
     $revisions->addRevisions($page->getRevisions());
     return new Page($page->getPageIdentifier(), $revisions);
 }
Esempio n. 11
0
 /**
  * @since 0.2
  *
  * @param Page $page
  * @param Title $target
  * @param MoveOptions|null $options
  *
  * @return bool
  */
 public function move(Page $page, Title $target, MoveOptions $options = null)
 {
     $this->api->postRequest(new SimpleRequest('move', $this->getMoveParams($page->getId(), $target, $options)));
     return true;
 }
Esempio n. 12
0
 /**
  * @param Page $page
  *
  * @return bool
  */
 public function hasPage(Page $page)
 {
     return array_key_exists($page->getId(), $this->pages);
 }
Esempio n. 13
0
 /**
  * @since 0.3
  *
  * @param Page $page
  * @param array $extraParams
  *
  * @return bool
  */
 public function restore(Page $page, array $extraParams = [])
 {
     $this->api->postRequest(new SimpleRequest('undelete', $this->getUndeleteParams($page->getTitle(), $extraParams)));
     return true;
 }
Esempio n. 14
0
 /**
  * @since 0.2
  *
  * @param Page $page
  * @param array $extraParams
  *
  * @return bool
  */
 public function delete(Page $page, array $extraParams = [])
 {
     $this->api->postRequest(new SimpleRequest('delete', $this->getDeleteParams($page->getPageIdentifier(), $extraParams)));
     return true;
 }
Esempio n. 15
0
 /**
  * @since 0.2
  *
  * @param Page $page
  * @param array $extraParams
  *
  * @return Page
  */
 public function getFromPage(Page $page, array $extraParams = [])
 {
     $result = $this->api->getRequest(new SimpleRequest('query', $this->getQuery(['pageids' => $page->getId()], $extraParams)));
     $revisions = $this->getRevisionsFromResult(array_shift($result['query']['pages']));
     $revisions->addRevisions($page->getRevisions());
     return new Page($page->getPageIdentifier(), $revisions);
 }
Esempio n. 16
0
 /**
  * @since 0.2
  *
  * @param Page $page
  * @param DeleteOptions|null $options
  *
  * @return bool
  */
 public function delete(Page $page, DeleteOptions $options = null)
 {
     $this->api->postRequest(new SimpleRequest('delete', $this->getDeleteParams($page->getPageIdentifier(), $options)));
     return true;
 }
Esempio n. 17
0
 /**
  * @since 0.3
  *
  * @param Page $page
  * @param UndeleteOptions|null $options
  *
  * @return bool
  */
 public function restore(Page $page, UndeleteOptions $options = null)
 {
     $this->api->postRequest(new SimpleRequest('undelete', $this->getUndeleteParams($page->getTitle(), $options)));
     return true;
 }
Esempio n. 18
0
 /**
  * @since 0.2
  *
  * @param Page $page
  * @param Title $target
  * @param array $extraParams
  *
  * @return bool
  */
 public function move(Page $page, Title $target, array $extraParams = [])
 {
     $this->api->postRequest(new SimpleRequest('move', $this->getMoveParams($page->getId(), $target, $extraParams)));
     return true;
 }
Esempio n. 19
0
 /**
  * @param Page $page
  * @param DumpQuery $query
  *
  * @return bool
  */
 private function matchPage(Page $page, DumpQuery $query)
 {
     //Check namespaces
     if (count($query->getNamespaceFilters()) !== 0 && !in_array($page->getTitle()->getNs(), $query->getNamespaceFilters())) {
         return false;
     }
     //Check Title
     foreach ($query->getTitleFilters(DumpQuery::TYPE_CONTAINS) as $regex) {
         if (!preg_match($regex, $page->getTitle()->getTitle())) {
             return false;
         }
     }
     foreach ($query->getTitleFilters(DumpQuery::TYPE_MISSING) as $regex) {
         if (preg_match($regex, $page->getTitle()->getTitle())) {
             return false;
         }
     }
     //Check Text
     foreach ($query->getTextFilters(DumpQuery::TYPE_CONTAINS) as $regex) {
         if (!preg_match($regex, $page->getRevisions()->getLatest()->getContent())) {
             return false;
         }
     }
     foreach ($query->getTextFilters(DumpQuery::TYPE_MISSING) as $regex) {
         if (preg_match($regex, $page->getRevisions()->getLatest()->getContent())) {
             return false;
         }
     }
     return true;
 }