|
1
|
|
|
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed'); |
|
2
|
|
|
|
|
3
|
|
|
class Tracker_Title_Model extends Tracker_Base_Model { |
|
4
|
112 |
|
public function __construct() { |
|
5
|
112 |
|
parent::__construct(); |
|
6
|
112 |
|
} |
|
7
|
|
|
|
|
8
|
|
|
public function getID(string $titleURL, int $siteID, bool $create = TRUE, bool $returnData = FALSE) { |
|
9
|
|
|
$query = $this->db->select('tracker_titles.id, tracker_titles.title, tracker_titles.title_url, tracker_titles.latest_chapter, tracker_titles.status, tracker_sites.site_class, (tracker_titles.last_checked > DATE_SUB(NOW(), INTERVAL 3 DAY)) AS active', FALSE) |
|
10
|
|
|
->from('tracker_titles') |
|
11
|
|
|
->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left') |
|
12
|
|
|
->where('tracker_titles.title_url', $titleURL) |
|
13
|
|
|
->where('tracker_titles.site_id', $siteID) |
|
14
|
|
|
->get(); |
|
15
|
|
|
|
|
16
|
|
|
if($query->num_rows() > 0) { |
|
17
|
|
|
$id = (int) $query->row('id'); |
|
18
|
|
|
|
|
19
|
|
|
//This updates inactive series if they are newly added, as noted in https://github.com/DakuTree/manga-tracker/issues/5#issuecomment-247480804 |
|
20
|
|
|
if(((int) $query->row('active')) === 0 && $query->row('status') === 0) { |
|
21
|
|
|
$titleData = $this->sites->{$query->row('site_class')}->getTitleData($query->row('title_url')); |
|
22
|
|
|
if(!is_null($titleData['latest_chapter'])) { |
|
23
|
|
|
if($this->updateByID((int) $id, $titleData['latest_chapter'])) { |
|
24
|
|
|
//Make sure last_checked is always updated on successful run. |
|
25
|
|
|
//CHECK: Is there a reason we aren't just doing this in updateTitleById? |
|
26
|
|
|
$this->db->set('last_checked', 'CURRENT_TIMESTAMP', FALSE) |
|
27
|
|
|
->where('id', $id) |
|
28
|
|
|
->update('tracker_titles'); |
|
29
|
|
|
} |
|
30
|
|
|
} else { |
|
31
|
|
|
log_message('error', "{$query->row('title')} failed to update successfully"); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$titleID = $id; |
|
36
|
|
|
} else { |
|
37
|
|
|
//TODO: Check if title is valid URL! |
|
38
|
|
|
if($create) $titleID = $this->addTitle($titleURL, $siteID); |
|
39
|
|
|
} |
|
40
|
|
|
if(!isset($titleID) || !$titleID) $titleID = 0; |
|
41
|
|
|
|
|
42
|
|
|
return ($returnData && $titleID !== 0 ? $query->row_array() : $titleID); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getIDFromData(string $title, string $siteURL) { |
|
46
|
|
|
if(!($siteData = $this->getSiteDataFromURL($siteURL))) { |
|
47
|
|
|
throw new Exception("Site URL is invalid: {$siteURL}"); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $this->getID($title, $siteData->id); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function addTitle(string $titleURL, int $siteID) { |
|
54
|
|
|
$query = $this->db->select('site, site_class') |
|
55
|
|
|
->from('tracker_sites') |
|
56
|
|
|
->where('id', $siteID) |
|
57
|
|
|
->get(); |
|
58
|
|
|
|
|
59
|
|
|
$titleData = $this->sites->{$query->row()->site_class}->getTitleData($titleURL, TRUE); |
|
60
|
|
|
|
|
61
|
|
|
//FIXME: getTitleData can fail, which will in turn cause the below to fail aswell, we should try and account for that |
|
62
|
|
|
if($titleData) { |
|
63
|
|
|
$this->db->insert('tracker_titles', array_merge($titleData, ['title_url' => $titleURL, 'site_id' => $siteID])); |
|
64
|
|
|
$titleID = $this->db->insert_id(); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$this->History->updateTitleHistory((int) $titleID, NULL, $titleData['latest_chapter'], $titleData['last_updated']); |
|
67
|
|
|
} else { |
|
68
|
|
|
log_message('error', "getTitleData failed for: {$query->row()->site_class} | {$titleURL}"); |
|
69
|
|
|
} |
|
70
|
|
|
return $titleID ?? 0; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
public function updateByID(int $id, string $latestChapter) { |
|
75
|
|
|
//FIXME: Really not too happy with how we're doing history stuff here, it just feels messy. |
|
76
|
|
|
$query = $this->db->select('latest_chapter AS current_chapter') |
|
77
|
|
|
->from('tracker_titles') |
|
78
|
|
|
->where('id', $id) |
|
79
|
|
|
->get(); |
|
80
|
|
|
$row = $query->row(); |
|
81
|
|
|
|
|
82
|
|
|
$success = $this->db->set(['latest_chapter' => $latestChapter]) //last_updated gets updated via a trigger if something changes |
|
83
|
|
|
->where('id', $id) |
|
84
|
|
|
->update('tracker_titles'); |
|
85
|
|
|
|
|
86
|
|
|
//Update History |
|
87
|
|
|
//NOTE: To avoid doing another query to grab the last_updated time, we just use time() which <should> get the same thing. |
|
88
|
|
|
//FIXME: The <preferable> solution here is we'd just check against the last_updated time, but that can have a few issues. |
|
89
|
|
|
$this->History->updateTitleHistory($id, $row->current_chapter, $latestChapter, date('Y-m-d H:i:s')); |
|
90
|
|
|
|
|
91
|
|
|
return (bool) $success; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
public function getSiteDataFromURL(string $site_url) { |
|
96
|
|
|
$query = $this->db->select('id, site_class') |
|
97
|
|
|
->from('tracker_sites') |
|
98
|
|
|
->where('site', $site_url) |
|
99
|
|
|
->get(); |
|
100
|
|
|
|
|
101
|
|
|
if($query->num_rows() > 0) { |
|
102
|
|
|
$siteData = $query->row(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $siteData ?? FALSE; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.