Skip to content

Commit ad4929e

Browse files
authored
Merge pull request #807 from OpenKnowledgeMaps/refactor/upgrade-php-to-8.2
refactor: upgrade php to 8.2
2 parents 6910bac + d5fee19 commit ad4929e

4 files changed

Lines changed: 46 additions & 19 deletions

File tree

local_dev/searchflow-container/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
FROM php:8.0-apache
1+
FROM php:8.2-apache
22

33
LABEL maintainer="Chris Kittel <christopher.kittel@openknowledgemaps.org>"
44

55
RUN a2enmod rewrite
66

77
RUN apt-get update && apt-get install -y \
8-
curl libsqlite3-dev php7.4-sqlite libonig-dev libxml2-dev \
8+
curl libsqlite3-dev libonig-dev libxml2-dev \
99
gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 \
1010
libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 \
1111
libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 \

server/classes/headstart/library/APIClient.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
<?php
22

33
namespace headstart\library;
4+
5+
use Exception;
6+
47
require_once dirname(__FILE__) . '/CommUtils.php';
58

69
class APIClient {
10+
private array $ini_array;
11+
private string $database;
12+
private string $base_route;
13+
private array $settings;
714

815
public function __construct($ini_array) {
9-
1016
$this->load_configs($ini_array);
1117
}
1218

13-
public function load_configs($ini_array) {
19+
public function load_configs(array $ini_array): void {
1420
$this->ini_array = $ini_array;
1521
$this->settings = $this->ini_array["general"];
1622
$this->database = $this->ini_array["connection"]["database"];
@@ -21,46 +27,44 @@ public function load_configs($ini_array) {
2127

2228
public function call_api($endpoint, $payload) {
2329
$route = $this->base_route . $endpoint;
30+
2431
try {
2532
$res = CommUtils::call_api($route, $payload);
33+
2634
if ($res["httpcode"] != 200) {
2735
$res["route"] = $route;
2836
$res = $this->handle_api_errors($res);
2937
}
38+
3039
return $res;
3140
}
3241
catch (Exception $e) {
3342
error_log("Error in APIClient: " . $e);
34-
$res = array("status"=>"error",
35-
"httpcode"=>500,
36-
"reason"=>array("unexpected data processing error"));
43+
$res = array("status"=>"error", "httpcode"=>500, "reason"=>array("unexpected data processing error"));
3744
return $res;
3845
}
39-
finally {
40-
}
4146
}
4247

4348
public function call_persistence($endpoint, $payload) {
4449
$route = $this->base_route . "persistence/" . $endpoint . "/" . $this->database;
50+
4551
try {
4652
$res = CommUtils::call_api($route, $payload);
53+
4754
if ($res["httpcode"] != 200) {
4855
$res["route"] = $route;
4956
$res = $this->handle_api_errors($res);
5057
}
58+
5159
return $res;
5260
}
5361
catch (Exception $e) {
5462
// what happens here is instead of bubbling the error up,
55-
// it is caught and we fake a response that looks like an error
63+
// fake a response that looks like an error
5664
// because of the hardcoded reason we loose the original error information
57-
$res = array("status"=>"error",
58-
"httpcode"=>500,
59-
"reason"=>array("unexpected data processing error"));
65+
$res = array("status"=>"error", "httpcode"=>500, "reason"=>array("unexpected data processing error"));
6066
return $res;
6167
}
62-
finally {
63-
}
6468
}
6569

6670
public function handle_api_errors($res) {
@@ -85,5 +89,4 @@ public function handle_api_errors($res) {
8589
error_log(("Trying to handle API errors: " . print_r($res, true)));
8690
return $res;
8791
}
88-
8992
}

server/services/getPDF.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function extractValidPdfUrls(array $revision_data, string $paper_id, string $vis
144144

145145
$inner_data = json_decode($revision_data["data"], true);
146146
$documents_raw = $inner_data["documents"] ?? null;
147-
$documents = json_decode($documents_raw, true);
147+
$documents = $documents_raw !== null ? json_decode($documents_raw, true) : null;
148148

149149
if (strtolower($vis_type) == 'timeline') {
150150
$inner_data = json_decode($inner_data["data"]);

server/services/search.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,35 @@ function packParamsJSON($params_array, $post_params)
2828
return json_encode($output_array);
2929
}
3030

31+
function encode_string(string $str, string $to_encoding = 'UTF-8'): string {
32+
// Defining constants with known encodings and default one
33+
$DEFAULT_ENCODING = 'ISO-8859-1';
34+
$ENCODINGS = [
35+
'UTF-8',
36+
'Windows-1251',
37+
'ISO-8859-1',
38+
'ASCII',
39+
'KOI8-R',
40+
'CP866'
41+
];
42+
43+
// Trying to define the received string encoding
44+
$str_encoding = mb_detect_encoding($str, $ENCODINGS, true);
45+
46+
// If the received string encoding is unknown the default one will be used
47+
if ($str_encoding === false) {
48+
$str_encoding = $DEFAULT_ENCODING;
49+
}
50+
51+
// Converting encoding and returning updated string back
52+
return mb_convert_encoding($str, $to_encoding, $str_encoding);
53+
}
54+
3155
function utf8_converter($array)
3256
{
3357
array_walk_recursive($array, function (&$item, $key) {
34-
if (!mb_detect_encoding($item, 'utf-8', true)) {
35-
$item = utf8_encode($item);
58+
if ($item !== null && !mb_detect_encoding($item, 'utf-8', true)) {
59+
$item = encode_string($item);
3660
}
3761
});
3862

0 commit comments

Comments
 (0)