Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different CURL context for logged in and logged out #4390

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, 'Connection: close'],
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_URL => API_ROOT,
CURLOPT_HTTPHEADER => ['Connection: close']
]);

$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 => ['Connection: close'],
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
Loading