From 23188fae16b2cca412077bd518310729f6df9621 Mon Sep 17 00:00:00 2001 From: Tobias Zwick Date: Mon, 20 Jul 2020 16:20:22 +0200 Subject: [PATCH] wip refactor to use classes --- classes/OSMPhotoNote.class.php | 10 +++ classes/OSMPhotoNoteDao.class.php | 104 ++++++++++++++++++++++++++ classes/OSMPhotoNoteFetcher.class.php | 50 +++++++++++++ classes/OSMPhotoNoteParser.class.php | 38 ++++++++++ cleanup.php | 3 +- db_helper.php | 89 ---------------------- helper.php | 14 ---- osm_photo_note.php | 42 ----------- 8 files changed, 204 insertions(+), 146 deletions(-) create mode 100644 classes/OSMPhotoNote.class.php create mode 100644 classes/OSMPhotoNoteDao.class.php create mode 100644 classes/OSMPhotoNoteFetcher.class.php create mode 100644 classes/OSMPhotoNoteParser.class.php delete mode 100644 osm_photo_note.php diff --git a/classes/OSMPhotoNote.class.php b/classes/OSMPhotoNote.class.php new file mode 100644 index 0000000..a926106 --- /dev/null +++ b/classes/OSMPhotoNote.class.php @@ -0,0 +1,10 @@ +mysqli = $mysqli; + $this->createTable(); + } + + private function createTable() + { + $this->mysqli->query( + 'CREATE TABLE IF NOT EXISTS photos( + file_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, + file_ext VARCHAR(10) NOT NULL, + creation_time DATETIME NOT NULL, + note_id BIGINT UNSIGNED + )' + ); + } + + public function newPhoto($file_ext) + { + $stmt = $this->mysqli->prepare( + 'INSERT INTO photos(file_ext, creation_time) + VALUES (?, NOW())' + ); + $stmt->bind_param('s', $file_ext); + $stmt->execute(); + return $this->mysqli->insert_id; + } + + public function deletePhoto(int $file_id) + { + $stmt = $this->mysqli->prepare('DELETE FROM photos WHERE file_id=?'); + $stmt->bind_param('i', $file_id); + $stmt->execute(); + } + + public function getOldInactivePhotos(): array + { + $stmt = $this->mysqli->prepare( + 'SELECT file_id, file_ext, note_id FROM photos + WHERE note_id IS NULL + AND creation_time < ADDDATE(NOW(), INTERVAL -? HOUR)' + ); + $max_age = Config::MAX_TMP_LIFETIME_HOURS; + $stmt->bind_param('i', $max_age); + $stmt->execute(); + $result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC); + return $result; + } + + public function getOldestActivePhotos(int $num): array + { + $stmt = $this->mysqli->prepare( + 'SELECT file_id, file_ext, note_id FROM photos + WHERE note_id IS NOT NULL + ORDER BY creation_time + LIMIT ?' + ); + $stmt->bind_param('i', $num); + $stmt->execute(); + $result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC); + return $result; + } + + public function getActivePhotos(): array + { + $result = $this->mysqli->query( + 'SELECT file_id, file_ext, note_id FROM photos + WHERE note_id IS NOT NULL + ORDER BY creation_time DESC' + ); + return $result->fetch_all(MYSQLI_ASSOC); + } + + public function getInactivePhotos(array $photo_ids): array + { + $in = str_repeat('?,', count($photo_ids) - 1) . '?'; + $stmt = $this->mysqli->prepare( + "SELECT file_id, file_ext FROM photos + WHERE file_id IN ($in) + AND note_id IS NULL" + ); + $stmt->bind_param(str_repeat('i', count($photo_ids)), ...$photo_ids); + $stmt->execute(); + return $stmt->get_result()->fetch_all(MYSQLI_ASSOC); + } + + public function activatePhoto(int $photo_id, int $note_id) + { + $stmt = $this->mysqli->prepare( + "UPDATE photos SET note_id = ? + WHERE file_id = ?" + ); + $stmt->bind_param('ii', $note_id, $photo_id); + $stmt->execute(); + } +} \ No newline at end of file diff --git a/classes/OSMPhotoNoteFetcher.class.php b/classes/OSMPhotoNoteFetcher.class.php new file mode 100644 index 0000000..6ede615 --- /dev/null +++ b/classes/OSMPhotoNoteFetcher.class.php @@ -0,0 +1,50 @@ +user = $user; + $this->pass = $pass; + $this->parser = new OSMPhotoNoteParser($photos_url); + } + + public function fetch(int $note_id): ?OSMPhotoNote + { + $url = self::OSM_NOTES_API . strval($note_id) . '.json'; + $response = fetchUrl($url, $this->user, $this->pass); + if ($response->code == 404 || $response->code == 410) { + return null; + } + else if ($response->code != 200) { + throw new Exception('OSM API returned error code ' . $response->code); + } + return $this->parser->parse($response->body); + } + + function fetchUrl($url, $user = null, $pass = null) + { + $response = new stdClass(); + $curl = curl_init($url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_USERAGENT, 'StreetComplete Photo Service'); + if ($user !== null and $pass !== null) { + curl_setopt($curl, CURLOPT_USERPWD, $user . ":" . $pass); + } + $response->body = curl_exec($curl); + $response->code = curl_getinfo($curl, CURLINFO_RESPONSE_CODE); + curl_close($curl); + return $response; + } +} \ No newline at end of file diff --git a/classes/OSMPhotoNoteParser.class.php b/classes/OSMPhotoNoteParser.class.php new file mode 100644 index 0000000..15c49db --- /dev/null +++ b/classes/OSMPhotoNoteParser.class.php @@ -0,0 +1,38 @@ +photos_url = $photos_url; + } + + public function parse(string $json): OSMPhotoNote + { + $note = json_decode($response->body, true); + + $r = new OSMPhotoNote(); + $r->note_id = $note['properties']['id']; + $r->status = $note['properties']['status']; + if ($r->status == 'closed') { + $r->closed_at = $note['properties']['closed_at']; + } + + $relevant_comments = ""; + foreach ($note['properties']['comments'] as $comment) { + if (array_key_exists('uid', $comment)) { + $relevant_comments .= "\n" . $comment['text']; + } + } + $search_regex = '~(?photos_url, '/')) . '/(\d+)\.[a-z]+(?!\S)~i'; + preg_match_all($search_regex, $relevant_comments, $matches); + $r->photo_ids = array_unique(array_map('intval', $matches[1])); + + return $r; + } +} \ No newline at end of file diff --git a/cleanup.php b/cleanup.php index 261d2cd..d3d46bb 100755 --- a/cleanup.php +++ b/cleanup.php @@ -12,7 +12,8 @@ function deletePhoto($photo) { global $db_helper; $file_path = ($photo['note_id']==NULL ? Config::PHOTOS_TMP_DIR : Config::PHOTOS_SRV_DIR) . DIRECTORY_SEPARATOR . $photo['file_id'] . $photo['file_ext']; - $success = unlink($file_path); + echo "delete photo ".$photo['file_id']."\n"; + $success = unlink($file_path); if ($success) { $db_helper->deletePhoto($photo['file_id']); } else { diff --git a/db_helper.php b/db_helper.php index 9ac91be..964cf59 100644 --- a/db_helper.php +++ b/db_helper.php @@ -16,93 +16,4 @@ public function __destruct() $this->_connection->close(); } - public function createTable() - { - $this->_connection->query( - 'CREATE TABLE IF NOT EXISTS photos( - file_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, - file_ext VARCHAR(10) NOT NULL, - creation_time DATETIME NOT NULL, - note_id BIGINT UNSIGNED - )' - ); - } - - public function newPhoto($file_ext) - { - $stmt = $this->_connection->prepare( - 'INSERT INTO photos(file_ext, creation_time) - VALUES (?, NOW())' - ); - $stmt->bind_param('s', $file_ext); - $stmt->execute(); - return $this->_connection->insert_id; - } - - public function deletePhoto($file_id) - { - $stmt = $this->_connection->prepare('DELETE FROM photos WHERE file_id=?'); - $stmt->bind_param('i', $file_id); - $stmt->execute(); - } - - public function getOldInactivePhotos() - { - $stmt = $this->_connection->prepare( - 'SELECT file_id, file_ext, note_id FROM photos - WHERE note_id IS NULL - AND creation_time < ADDDATE(NOW(), INTERVAL -? HOUR)' - ); - $max_age = Config::MAX_TMP_LIFETIME_HOURS; - $stmt->bind_param('i', $max_age); - $stmt->execute(); - $result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC); - return $result; - } - - public function getOldestActivePhotos($num) - { - $stmt = $this->_connection->prepare( - 'SELECT file_id, file_ext, note_id FROM photos - WHERE note_id IS NOT NULL - ORDER BY creation_time - LIMIT ?' - ); - $stmt->bind_param('i', $num); - $stmt->execute(); - $result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC); - return $result; - } - - public function getActivePhotos() - { - $result = $this->_connection->query( - 'SELECT file_id, file_ext, note_id - FROM photos WHERE note_id IS NOT NULL' - ); - return $result->fetch_all(MYSQLI_ASSOC); - } - - public function getInactivePhotos($photo_ids) - { - $in = str_repeat('?,', count($photo_ids) - 1) . '?'; - $stmt = $this->_connection->prepare( - "SELECT file_id, file_ext FROM photos - WHERE file_id IN ($in) - AND note_id IS NULL" - ); - $stmt->bind_param(str_repeat('i', count($photo_ids)), ...$photo_ids); - $stmt->execute(); - return $stmt->get_result()->fetch_all(MYSQLI_ASSOC); - } - - public function activatePhoto($photo_id, $note_id) - { - $stmt = $this->_connection->prepare( - "UPDATE photos SET note_id = ? - WHERE file_id = ?" - ); - $stmt->bind_param('ii', $note_id, $photo_id); - $stmt->execute(); - } } diff --git a/helper.php b/helper.php index 1030567..1a2bede 100644 --- a/helper.php +++ b/helper.php @@ -6,20 +6,6 @@ function returnError($code, $message) exit(json_encode(array('error' => $message))); } -function fetchUrl($url, $user = null, $pass = null) -{ - $response = new stdClass(); - $curl = curl_init($url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - if ($user !== null and $pass !== null) { - curl_setopt($curl, CURLOPT_USERPWD, $user . ":" . $pass); - } - $response->body = curl_exec($curl); - $response->code = curl_getinfo($curl, CURLINFO_RESPONSE_CODE); - curl_close($curl); - return $response; -} - function directorySize($dir) { $size = 0; diff --git a/osm_photo_note.php b/osm_photo_note.php deleted file mode 100644 index 3e13591..0000000 --- a/osm_photo_note.php +++ /dev/null @@ -1,42 +0,0 @@ -note_id = $note_id; - - $note_fetch_url = self::OSM_NOTES_API . strval($note_id) . '.json'; - $response = fetchUrl($note_fetch_url, Config::OSM_API_USER, Config::OSM_API_PASS); - $this->http_code = $response->code; - if ($this->http_code !== 200) { - return; - } - - $note = json_decode($response->body, true); - $this->status = $note['properties']['status']; - if ($this->status === 'closed') { - $this->closed_at = $note['properties']['closed_at']; - } - - $relevant_comments = ""; - foreach ($note['properties']['comments'] as $comment) { - if (array_key_exists('uid', $comment)) { - $relevant_comments .= "\n" . $comment['text']; - } - } - $search_regex = '~(?photo_ids = array_unique(array_map('intval', $matches[1])); - } -}