/** * @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; }
/** * @param string $action * @param array $params * @param EditInfo|null $editInfo * * @return mixed */ public function postRequest($action, array $params, EditInfo $editInfo = null) { if ($editInfo !== null) { $params = array_merge($this->getEditInfoParams($editInfo), $params); } $params['token'] = $this->api->getToken(); return $this->api->postRequest(new SimpleRequest($action, $params)); }
/** * @since 0.5 * * @param Title|string $title * @param DeleteOptions|null $options * * @return bool */ public function deleteFromPageTitle($title, DeleteOptions $options = null) { if (is_string($title)) { $title = new Title($title); } $this->api->postRequest(new SimpleRequest('delete', $this->getDeleteParams(new PageIdentifier($title), $options))); return true; }
/** * @since 0.2 * * @param Revision $revision * @param EditInfo $editInfo * * @returns bool success */ public function save(Revision $revision, EditInfo $editInfo = null) { $result = $this->api->postRequest(new SimpleRequest('edit', $this->getEditParams($revision, $editInfo))); if ($result['edit']['result'] == 'Success') { return true; } return false; }
/** * @since 0.5 * * @param Title|string $title * @param array $extraParams * * @return bool */ public function deleteFromPageTitle($title, array $extraParams = []) { if (is_string($title)) { $title = new Title($title); } $this->api->postRequest(new SimpleRequest('delete', $this->getDeleteParams(new PageIdentifier($title), $extraParams))); return true; }
/** * @param Title $title * * @throws OutOfBoundsException * @returns string */ private function getUndeleteToken(Title $title) { $response = $this->api->postRequest(new SimpleRequest('query', ['list' => 'deletedrevs', 'titles' => $title->getTitle(), 'drprop' => 'token'])); if (array_key_exists('token', $response['query']['deletedrevs'][0])) { return $response['query']['deletedrevs'][0]['token']; } else { throw new OutOfBoundsException('Could not get page undelete token from list=deletedrevs query'); } }
/** * @param string $targetName * @param string $location Can be local path or remote url * * @return bool */ public function upload($targetName, $location) { $params = array('filename' => $targetName, 'token' => $this->api->getToken('edit')); $headers = array(); if (is_file($location)) { $params['file'] = fopen($location, 'r'); $headers['Content-Type'] = 'multipart/form-data'; } else { $params['url'] = $location; } $this->api->postRequest(new SimpleRequest('upload', $params, $headers)); return true; }
/** * @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; }
/** * @since 0.3 * * @param User|string $user * @param array $extraParams * * @throws InvalidArgumentException * @return bool */ public function block($user, array $extraParams = []) { if (!$user instanceof User && !is_string($user)) { throw new InvalidArgumentException('$user must be either a string or User object'); } if ($user instanceof User) { $user = $user->getName(); } $params = ['user' => $user, 'token' => $this->api->getToken('block')]; $params = array_merge($extraParams, $params); $this->api->postRequest(new SimpleRequest('block', $params)); return true; }
/** * @since 0.3 * * @param User $user * @param string[] $add * @param string[] $remove * @param array $extraParams * * @return bool */ public function change(User $user, $add = [], $remove = [], array $extraParams = []) { $result = $this->api->postRequest(new SimpleRequest('query', ['list' => 'users', 'ustoken' => 'userrights', 'ususers' => $user->getName()])); $params = ['user' => $user->getName(), 'token' => $result['query']['users'][0]['userrightstoken']]; if (!empty($add)) { $params['add'] = implode('|', $add); } if (!empty($remove)) { $params['remove'] = implode('|', $remove); } $this->api->postRequest(new SimpleRequest('userrights', array_merge($extraParams, $params))); return true; }
private function reallyGetToken($type) { // We suppress errors on this call so the user doesn't get get a warning that isn't their fault. $result = @$this->api->postRequest(new SimpleRequest('query', array('meta' => 'tokens', 'type' => $this->getNewTokenType($type), 'continue' => ''))); // If mw<1.25 (no new module) if (array_key_exists('warnings', $result) && array_key_exists('query', $result['warnings']) && strstr($result['warnings']['query']['*'], "Unrecognized value for parameter 'meta': tokens")) { $this->usePre125TokensModule = true; $this->logger->log(LogLevel::DEBUG, 'Falling back to pre 1.25 token system'); $this->tokens[$type] = $this->reallyGetPre125Token($type); } else { $this->tokens[$type] = array_pop($result['query']['tokens']); } return $this->tokens[$type]; }
/** * @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; }
/** * @since 0.3 * * @param User $user * @param string[] $add * @param string[] $remove * @param UserRightsOptions|null $options * * @return bool */ public function change(User $user, $add = array(), $remove = array(), UserRightsOptions $options = null) { $result = $this->api->postRequest(new SimpleRequest('query', array('list' => 'users', 'ustoken' => 'userrights', 'ususers' => $user->getName()))); $params = array('user' => $user->getName(), 'token' => $result['query']['users'][0]['userrightstoken']); $reason = $options->getReason(); if (!empty($reason)) { $params['reason'] = $reason; } if (!empty($add)) { $params['add'] = implode('|', $add); } if (!empty($remove)) { $params['remove'] = implode('|', $remove); } $this->api->postRequest(new SimpleRequest('userrights', $params)); return true; }
/** * NOTE: This service has not been fully tested * * @param File $file * @param int $rotation Degrees to rotate image clockwise, One value: 90, 180, 270 * * @throws UsageException * @return bool */ public function rotate(File $file, $rotation) { $params = array('rotation' => $rotation, 'token' => $this->api->getToken()); if (!is_null($file->getPageIdentifier()->getTitle())) { $params['titles'] = $file->getPageIdentifier()->getTitle()->getText(); } else { $params['pageids'] = $file->getPageIdentifier()->getId(); } $result = $this->api->postRequest(new SimpleRequest('imagerotate', $params)); // This module sometimes gives odd errors so deal with them.. if (array_key_exists('imagerotate', $result)) { $imageRotate = array_pop($result['imagerotate']); if (array_key_exists('result', $imageRotate) && $imageRotate['result'] == 'Failure') { throw new UsageException('imagerotate-Failure', $imageRotate['errormessage'], $result); } } return true; }
/** * @since 0.3 * * @param User|string $user * @param BlockOptions $options * * @throws InvalidArgumentException * @return bool */ public function block($user, BlockOptions $options = null) { if (!$user instanceof User && !is_string($user)) { throw new InvalidArgumentException('$user must be either a string or User object'); } if ($user instanceof User) { $user = $user->getName(); } $params = array('user' => $user, 'token' => $this->api->getToken('block')); if ($options->getExpiry() !== 'never') { $params['expiry'] = $options->getExpiry(); } $reason = $options->getReason(); if (!empty($reason)) { $params['reason'] = $reason; } if ($options->getAllowusertalk()) { $params['allowusertalk'] = ''; } if ($options->getAnononly()) { $params['allowusertalk'] = ''; } if ($options->getAutoblock()) { $params['allowusertalk'] = ''; } if ($options->getHidename()) { $params['allowusertalk'] = ''; } if ($options->getNocreate()) { $params['allowusertalk'] = ''; } if ($options->getNoemail()) { $params['allowusertalk'] = ''; } if ($options->getReblock()) { $params['allowusertalk'] = ''; } if ($options->getWatchuser()) { $params['allowusertalk'] = ''; } $this->api->postRequest(new SimpleRequest('block', $params)); return true; }
/** * @param string $username * @param string $password * @param string|null $email * * @return bool */ public function create($username, $password, $email = null) { if (!is_string($username)) { throw new InvalidArgumentException('$username should be a string'); } if (!is_string($password)) { throw new InvalidArgumentException('$password should be a string'); } if (!is_string($email) && !is_null($email)) { throw new InvalidArgumentException('$email should be a string or null'); } $params = array('name' => $username, 'password' => $password); if (!is_null($email)) { $params['email'] = $email; } $result = $this->api->postRequest(new SimpleRequest('createaccount', $params)); if ($result['createaccount']['result'] == 'NeedToken') { $result = $this->api->postRequest(new SimpleRequest('createaccount', array_merge(array('token' => $result['createaccount']['token']), $params))); } if ($result['createaccount']['result'] === 'Success') { return true; } return false; }
/** * @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; }
/** * @param Revision $revision * * @returns string */ private function getTokenForRevision(Revision $revision) { $result = $this->api->postRequest(new SimpleRequest('query', array('list' => 'recentchanges', 'rcstart' => $revision->getTimestamp(), 'rcend' => $revision->getTimestamp(), 'rctoken' => 'patrol'))); $result = array_shift($result['query']['recentchanges']); return $result['patroltoken']; }
/** * @param Revision $revision * * @return bool */ public function undo(Revision $revision) { $this->api->postRequest(new SimpleRequest('edit', $this->getParamsFromRevision($revision))); return true; }
private function purgePages(array $pagesToPurge, array $siteApis, CommandHelper $commandHelper, ApiUser $apiUser) { $allCount = 0; foreach ($siteApis as $siteId => $url) { if (!isset($pagesToPurge[$siteId])) { $commandHelper->writeln("\nNo pages found to purge for site {$siteId} ({$url})"); continue; } $count = count($pagesToPurge[$siteId]); $commandHelper->writeln("\nPurging {$count} pages for site {$siteId} ({$url})"); $api = new MediawikiApi($url); $api->login($apiUser); $progressBar = $commandHelper->getProgressBar($count); $progressBar->start(); foreach (array_chunk($pagesToPurge[$siteId], 10) as $pages) { $params = array('titles' => implode('|', $pages), 'forcelinkupdate' => true); $api->postRequest(new SimpleRequest('purge', $params)); $progressBar->advance(10); } $progressBar->finish(); $allCount += $count; } $commandHelper->writeln("\nFinished purging {$allCount} pages"); }
/** * @since 0.6 * * @param ApiGenerator $generator * * @return bool */ public function purgeGenerator(ApiGenerator $generator) { $this->api->postRequest(new SimpleRequest('purge', $generator->getParams())); return true; }
/** * @since 0.5 * * @param Revision $revision * * @return bool */ public function delete(Revision $revision) { $params = array('type' => 'revision', 'hide' => 'content', 'token' => $this->api->getToken('delete'), 'ids' => $revision->getId()); $this->api->postRequest(new SimpleRequest('revisiondelete', $params)); return true; }
/** * @dataProvider provideActionsParamsResults */ public function testPostActionReturnsResult($expectedResult, $action, $params = array()) { $client = $this->getMockClient(); $client->expects($this->once())->method('post')->with(null, $this->getExpectedOptsForPostParams(array_merge(array('action' => $action), $params)))->will($this->returnValue($this->getMockResponse($expectedResult))); $api = new MediawikiApi($client); $result = $api->postRequest(new SimpleRequest($action, $params)); $this->assertEquals($expectedResult, $result); }
public function testPostActionWithFileReturnsResult() { $dummyFile = fopen('/dev/null', 'r'); $params = ['filename' => 'foo.jpg', 'file' => $dummyFile]; $client = $this->getMockClient(); $client->expects($this->once())->method('request')->with('POST', null, array('multipart' => array(array('name' => 'action', 'contents' => 'upload'), array('name' => 'filename', 'contents' => 'foo.jpg'), array('name' => 'file', 'contents' => $dummyFile), array('name' => 'format', 'contents' => 'json')), 'headers' => array('User-Agent' => 'addwiki-mediawiki-client')))->will($this->returnValue($this->getMockResponse(array('success ' => 1)))); $api = new MediawikiApi('', $client); $result = $api->postRequest(new SimpleRequest('upload', $params)); $this->assertEquals(array('success ' => 1), $result); }
/** * @since 0.2 * * @param int $pageid * @param Title $target * @param MoveOptions|null $options * * @return bool */ public function moveFromPageId($pageid, Title $target, MoveOptions $options = null) { $this->api->postRequest(new SimpleRequest('move', $this->getMoveParams($pageid, $target, $options))); return true; }
/** * @param Revision $revision * * @returns string */ private function getTokenForRevision(Revision $revision) { $result = $this->api->postRequest(new SimpleRequest('query', array('prop' => 'revisions', 'revids' => $revision->getId(), 'rvtoken' => 'rollback'))); $result = array_shift($result['query']['pages']); return $result['revisions'][0]['rollbacktoken']; }
/** * @since 0.2 * * @param int $pageid * @param Title $target * @param array $extraParams * * @return bool */ public function moveFromPageId($pageid, Title $target, array $extraParams = []) { $this->api->postRequest(new SimpleRequest('move', $this->getMoveParams($pageid, $target, $extraParams))); return true; }