Skip to content

Commit

Permalink
Different CURL context for logged in and logged out (#4390)
Browse files Browse the repository at this point in the history
  • Loading branch information
GlazerMann authored Jan 31, 2024
1 parent 01295f2 commit 7cc50e7
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions WikipediaBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ final class WikipediaBot {
private Consumer $user_consumer;
private Client $user_client;
private Token $user_token;
private static CurlHandle $ch; // All wikipedia connections share a session
private static CurlHandle $ch_login;
private static CurlHandle $ch_logout;
private string $the_user = '';
private static ?self $last_WikipediaBot; // For NonStandardMode()

public static function make_ch() : void {
static $init_done = FALSE;
if ($init_done) return;
$init_done = TRUE;
self::$ch = curl_init_array(1.0,
[CURLOPT_FAILONERROR => TRUE ]); // This is a little paranoid - see https://curl.se/libcurl/c/CURLOPT_FAILONERROR.html
// This is a little paranoid - see https://curl.se/libcurl/c/CURLOPT_FAILONERROR.html
self::$ch_login = curl_init_array(1.0, [CURLOPT_FAILONERROR => TRUE ]);
self::$ch_logout = curl_init_array(1.0, [CURLOPT_FAILONERROR => TRUE ]);
}

function __construct() {
Expand Down Expand Up @@ -113,14 +115,14 @@ private function fetch(array $params, int $depth = 1) : ?object {
$authenticationHeader = $request->toHeader();

try {
curl_setopt_array(self::$ch, [
curl_setopt_array(self::$ch_login, [
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => http_build_query($params),
CURLOPT_HTTPHEADER => [$authenticationHeader],
CURLOPT_URL => API_ROOT
]);

$data = (string) @curl_exec(self::$ch);
$data = (string) @curl_exec(self::$ch_login);
$ret = @json_decode($data);
if (($ret === NULL) || ($ret === FALSE) || (isset($ret->error) && ( // @codeCoverageIgnoreStart
(string) $ret->error->code === 'assertuserfailed' ||
Expand Down Expand Up @@ -363,17 +365,17 @@ static private function QueryAPI(array $params) : string {
try {
$params['format'] = 'json';

curl_setopt_array(self::$ch, [
curl_setopt_array(self::$ch_logout, [
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => http_build_query($params),
CURLOPT_HTTPHEADER => [],
CURLOPT_URL => API_ROOT,
]);

$data = (string) @curl_exec(self::$ch);
$data = (string) @curl_exec(self::$ch_logout);
if ($data === '') {
sleep(4); // @codeCoverageIgnore
$data = (string) @curl_exec(self::$ch); // @codeCoverageIgnore
$data = (string) @curl_exec(self::$ch_logout); // @codeCoverageIgnore
}
return (self::ret_okay(@json_decode($data))) ? $data : '';
// @codeCoverageIgnoreStart
Expand Down Expand Up @@ -401,11 +403,11 @@ static public function get_links(string $title) : string {
}

static public function GetAPage(string $title) : string {
curl_setopt_array(self::$ch,
curl_setopt_array(self::$ch_logout,
[CURLOPT_HTTPGET => TRUE,
CURLOPT_HTTPHEADER => [],
CURLOPT_URL => WIKI_ROOT . '?' . http_build_query(['title' => $title, 'action' =>'raw'])]);
$text = (string) @curl_exec(self::$ch);
$text = (string) @curl_exec(self::$ch_logout);
return $text;
}

Expand Down

0 comments on commit 7cc50e7

Please sign in to comment.