/** * @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 Revision $revision * @param EditInfo $editInfo * * @throws RuntimeException * @returns array */ private function getEditParams(Revision $revision, EditInfo $editInfo = null) { if (!$revision->getPageIdentifier()->identifiesPage()) { throw new RuntimeException('$revision PageIdentifier does not identify a page'); } $params = []; $content = $revision->getContent(); $data = $content->getData(); if (!is_string($data)) { throw new RuntimeException('Dont know how to save content of this model.'); } $params['text'] = $content->getData(); $params['md5'] = md5($content->getData()); $timestamp = $revision->getTimestamp(); if (!is_null($timestamp)) { $params['basetimestamp'] = $timestamp; } if (!is_null($revision->getPageIdentifier()->getId())) { $params['pageid'] = $revision->getPageIdentifier()->getId(); } else { $params['title'] = $revision->getPageIdentifier()->getTitle()->getTitle(); } $params['token'] = $this->api->getToken(); if ($this->api->isLoggedin()) { $params['assert'] = 'user'; } $this->addEditInfoParams($editInfo, $params); return $params; }
/** * @param int $pageid * @param Title $target * @param array $extraParams * * @return array */ private function getMoveParams($pageid, $target, $extraParams) { $params = []; $params['fromid'] = $pageid; $params['to'] = $target->getTitle(); $params['token'] = $this->api->getToken('move'); return array_merge($extraParams, $params); }
/** * @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)); }
/** * @param Revision $revision * * @return array */ private function getParamsFromRevision(Revision $revision) { $params = array('undo' => $revision->getId(), 'token' => $this->api->getToken()); if (!is_null($revision->getPageIdentifier()->getId())) { $params['pageid'] = $revision->getPageIdentifier()->getId(); } else { $params['title'] = $revision->getPageIdentifier()->getTitle()->getTitle(); } return $params; }
/** * @param int $pageid * @param Title $target * @param MoveOptions|null $options * * @return array */ private function getMoveParams($pageid, $target, $options) { $params = array(); $params['fromid'] = $pageid; $params['to'] = $target->getTitle(); $reason = $options->getReason(); if (!empty($reason)) { $params['reason'] = $reason; } $params['token'] = $this->api->getToken('move'); return $params; }
/** * @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 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; }
/** * @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 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; }
/** * 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 PageIdentifier $identifier * @param array $extraParams * * @return array */ private function getDeleteParams(PageIdentifier $identifier, $extraParams) { $params = []; if (!is_null($identifier->getId())) { $params['pageid'] = $identifier->getId(); } else { $params['title'] = $identifier->getTitle()->getTitle(); } $params['token'] = $this->api->getToken('delete'); return array_merge($extraParams, $params); }
/** * @param PageIdentifier $identifier * @param DeleteOptions|null $options * * @return array */ private function getDeleteParams(PageIdentifier $identifier, $options) { $params = array(); if (!is_null($options)) { $reason = $options->getReason(); if (!empty($reason)) { $params['reason'] = $reason; } } if (!is_null($identifier->getId())) { $params['pageid'] = $identifier->getId(); } else { $params['title'] = $identifier->getTitle()->getTitle(); } $params['token'] = $this->api->getToken('delete'); return $params; }
/** * @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; }