Skip to content

Commit

Permalink
Merge pull request #7 from cluebotng/refactor-login-debugging
Browse files Browse the repository at this point in the history
Refactor login & debugging
  • Loading branch information
DamianZaremba authored Oct 18, 2021
2 parents 92d7073 + f5a4e22 commit 29939be
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 35 deletions.
54 changes: 19 additions & 35 deletions src/Wikipedia/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -515,6 +511,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' .
'&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.
*
Expand All @@ -527,36 +538,14 @@ 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) {
$this->logger->addDebug($x);
}

$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';
}

/**
Expand Down Expand Up @@ -587,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
}

Expand Down
4 changes: 4 additions & 0 deletions src/Wikipedia/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 29939be

Please sign in to comment.