From a34056a5c339be48d4b5a4c18e02898d13deeffd Mon Sep 17 00:00:00 2001 From: Damian Zaremba Date: Mon, 18 Oct 2021 16:29:42 +0200 Subject: [PATCH 1/2] Api::login - Always specify a login token --- src/Wikipedia/Api.php | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/src/Wikipedia/Api.php b/src/Wikipedia/Api.php index b257c6d..7c459cd 100644 --- a/src/Wikipedia/Api.php +++ b/src/Wikipedia/Api.php @@ -515,6 +515,21 @@ public function gettoken($title) return $x['query']['tokens']['csrftoken']; } + /** + * This function returns a login token. + * + * @return A login token + **/ + public function getLoginToken() + { + $x = $this->http->get($this->apiurl . '?rawcontinue=1&format=php' . + '?action=query&meta=tokens&type=login'); + $x = $this->http->unserialize($x); + + return $x['query']['tokens']['logintoken']; + } + + /** * This function takes a username and password and logs you into wikipedia. * @@ -527,7 +542,9 @@ public function login($user, $pass) $this->pass = $pass; $x = $this->http->post( $this->apiurl . '?action=login&format=php', - array('lgname' => $user, 'lgpassword' => $pass) + array('lgname' => $user, + 'lgpassword' => $pass, + 'lgtoken' => $this->getLoginToken()) ); if ($this->logger !== null) { @@ -536,27 +553,7 @@ public function login($user, $pass) $x = $this->http->unserialize($x); - if ($x['login']['result'] == 'Success') { - return true; - } - if ($x['login']['result'] == 'NeedToken') { - $x = $this->http->post( - $this->apiurl . '?action=login&format=php', - array('lgname' => $user, 'lgpassword' => $pass, 'lgtoken' => $x['login']['token']) - ); - - if ($this->logger !== null) { - $this->logger->addDebug($x); - } - - $x = $this->http->unserialize($x); - - if ($x['login']['result'] == 'Success') { - return true; - } - } - - return false; + return $x['login']['result'] == 'Success'; } /** From f5a4e22fb8770e4bbac40c8f651cc3d3b6a34f8d Mon Sep 17 00:00:00 2001 From: Damian Zaremba Date: Mon, 18 Oct 2021 16:30:22 +0200 Subject: [PATCH 2/2] Http::unserialize - centralise debug logging of response --- src/Wikipedia/Api.php | 15 +-------------- src/Wikipedia/Http.php | 4 ++++ 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/Wikipedia/Api.php b/src/Wikipedia/Api.php index 7c459cd..a3fe301 100644 --- a/src/Wikipedia/Api.php +++ b/src/Wikipedia/Api.php @@ -476,10 +476,6 @@ public function edit( } $x = $this->http->post($this->apiurl, $params); - if ($this->logger !== null) { - $this->logger->addDebug($x); - } - $x = $this->http->unserialize($x); if ($x['edit']['result'] == 'Success') { @@ -523,7 +519,7 @@ public function gettoken($title) public function getLoginToken() { $x = $this->http->get($this->apiurl . '?rawcontinue=1&format=php' . - '?action=query&meta=tokens&type=login'); + '&meta=tokens&type=login'); $x = $this->http->unserialize($x); return $x['query']['tokens']['logintoken']; @@ -547,10 +543,6 @@ public function login($user, $pass) 'lgtoken' => $this->getLoginToken()) ); - if ($this->logger !== null) { - $this->logger->addDebug($x); - } - $x = $this->http->unserialize($x); return $x['login']['result'] == 'Success'; @@ -584,11 +576,6 @@ public function move($old, $new, $reason, $checkrun = true) ); $x = $this->http->post($this->apiurl, $params); - - if ($this->logger !== null) { - $this->logger->addDebug($x); - } - $this->http->unserialize($x); // this emits warnings if needed } diff --git a/src/Wikipedia/Http.php b/src/Wikipedia/Http.php index 0409dac..286a6d5 100644 --- a/src/Wikipedia/Http.php +++ b/src/Wikipedia/Http.php @@ -136,6 +136,10 @@ public function get($url) **/ public function unserialize($response) { + if ($this->logger !== null) { + $this->logger->addDebug('Decoding response: ' . $response); + } + $response = unserialize($response); if ($this->logger !== null) {