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

Use different CURL for each type #4393

Closed
wants to merge 4 commits into from
Closed
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
43 changes: 20 additions & 23 deletions WikipediaBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,26 @@ final class WikipediaBot {
private Consumer $user_consumer;
private Client $user_client;
private Token $user_token;
private static CurlHandle $ch_login;
private static CurlHandle $ch_logout;
private static CurlHandle $ch_write;
private static CurlHandle $ch_post;
private static CurlHandle $ch_get;
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;
// 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 ]);
self::$ch_write = curl_init_array(1.0,
[CURLOPT_FAILONERROR => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_URL => API_ROOT]);
self::$ch_post = curl_init_array(1.0,
[CURLOPT_FAILONERROR => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_URL => API_ROOT]);
self::$ch_get = curl_init_array(1.0,
[CURLOPT_FAILONERROR => TRUE]);
}

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

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

$data = (string) @curl_exec(self::$ch_login);
$data = (string) @curl_exec(self::$ch_write);
$ret = @json_decode($data);
if (($ret === NULL) || ($ret === FALSE) || (isset($ret->error) && ( // @codeCoverageIgnoreStart
(string) $ret->error->code === 'assertuserfailed' ||
Expand Down Expand Up @@ -364,18 +370,12 @@ public static function redirect_target(string $page) : ?string {
static private function QueryAPI(array $params) : string {
try {
$params['format'] = 'json';
curl_setopt(self::$ch_post, CURLOPT_POSTFIELDS, http_build_query($params));

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_logout);
$data = (string) @curl_exec(self::$ch_post);
if ($data === '') {
sleep(4); // @codeCoverageIgnore
$data = (string) @curl_exec(self::$ch_logout); // @codeCoverageIgnore
$data = (string) @curl_exec(self::$ch_post); // @codeCoverageIgnore
}
return (self::ret_okay(@json_decode($data))) ? $data : '';
// @codeCoverageIgnoreStart
Expand Down Expand Up @@ -403,11 +403,8 @@ static public function get_links(string $title) : string {
}

static public function GetAPage(string $title) : string {
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_logout);
curl_setopt(self::$ch_get, CURLOPT_URL, WIKI_ROOT . '?' . http_build_query(['title' => $title, 'action' =>'raw']));
$text = (string) @curl_exec(self::$ch_get);
return $text;
}

Expand Down
Loading