Completed
Push — master ( 00d59c...3370b3 )
by Angus
03:36
created

Tracker_Admin_Model   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 344
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 1.61%

Importance

Changes 0
Metric Value
dl 0
loc 344
ccs 3
cts 186
cp 0.0161
rs 8.2608
c 0
b 0
f 0
wmc 40
lcom 1
cbo 6

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A updateLatestChapters() 0 73 3
B updateAllTitlesBySite() 0 50 4
C handleUpdate() 0 59 9
C updateCustom() 0 49 10
B refollowCustom() 0 33 5
A incrementRequests() 0 21 2
B getNextUpdateTime() 0 33 6

How to fix   Complexity   

Complex Class

Complex classes like Tracker_Admin_Model often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Tracker_Admin_Model, and based on these observations, apply Extract Interface, too.

1
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class Tracker_Admin_Model extends Tracker_Base_Model {
4 127
	public function __construct() {
5 127
		parent::__construct();
6 127
	}
7
8
	/**
9
	 * Checks for any series that haven't updated in 16 hours and updates them.
10
	 * This is ran every 4 hours via a cron job.
11
	 */
12
	public function updateLatestChapters() {
13
		// region $query = Get all titles ready to update;
14
		// @formatter:off
15
		$query = $this->db
16
			->select('
17
				tracker_titles.id as title_id,
18
				tracker_titles.title,
19
				tracker_titles.title_url,
20
				tracker_titles.status,
21
				tracker_sites.site,
22
				tracker_sites.site_class,
23
				tracker_sites.status,
24
				tracker_titles.latest_chapter,
25
				tracker_titles.last_updated,
26
				from_unixtime(MAX(auth_users.last_login)) AS timestamp
27
			')
28
			->from('tracker_titles')
29
			->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
30
			->join('tracker_chapters', 'tracker_titles.id = tracker_chapters.title_id', 'left')
31
			->join('auth_users', 'tracker_chapters.user_id = auth_users.id', 'left')
32
			->where('tracker_sites.status', 'enabled')
33
			->group_start()
34
				->group_start()
35
					//Check if title is marked as on-going...
36
					->where('tracker_titles.status', 0)
37
					//AND matches one of where queries below
38
					->group_start()
39
						//Then check if it's NULL (only occurs for new series)
40
						//->where('latest_chapter', NULL) //NOTE: This isn't needed anymore??
41
						//OR if it hasn't updated within the past 12 hours AND isn't a custom update site
42
						->group_start()
43
							->where('tracker_sites.use_custom', 'N')
44
							->where('last_checked < DATE_SUB(NOW(), INTERVAL 12 HOUR)')
45
						->group_end()
46
						//OR it is a custom update site, has more than one follower and hasn't updated within the past 72 hours.
47
						->or_group_start()
48
							->where('tracker_titles.id IN (
49
								SELECT title_id
50
								FROM tracker_chapters
51
								GROUP BY title_id
52
								HAVING COUNT(title_id) > 1
53
							)', NULL, FALSE)
54
							->where('last_checked < DATE_SUB(NOW(), INTERVAL 72 HOUR)')
55
						->group_end()
56
						//OR it is a custom update site and hasn't updated within the past 120 hours (5 days)
57
						->or_where('last_checked < DATE_SUB(NOW(), INTERVAL 120 HOUR)')
58
					->group_end()
59
				->group_end()
60
				->or_group_start()
61
					//Check if title is marked as complete...
62
					->where('tracker_titles.status', 1)
63
					//Then check if it hasn't updated within the past week
64
					->where('last_checked < DATE_SUB(NOW(), INTERVAL 1 WEEK)')
65
				->group_end()
66
			->group_end()
67
			//Status 2 (One-shot) & 255 (Ignore) are both not updated intentionally.
68
			->group_by('tracker_titles.id, tracker_chapters.active')
69
			//Check if the series is actually being tracked by someone
70
			->having('timestamp IS NOT NULL')
71
			//AND if it's currently marked as active by the user
72
			->having('tracker_chapters.active', 'Y')
73
			//AND if they have been active in the last 120 hours (5 days)
74
			->having('timestamp > DATE_SUB(NOW(), INTERVAL 120 HOUR)')
75
			->order_by('tracker_titles.title', 'ASC');
76
		// endregion
77
		$query = $query->get();
78
79
		if($query->num_rows() > 0) {
80
			foreach ($query->result() as $row) {
81
				$this->handleUpdate($row);
82
			}
83
		}
84
	}
85
86
	/**
87
	 * Intended to be only used as a quick way to update all series on a site after a bug.
88
	 *
89
	 * @param string      $site
90
	 * @param null|string $last_checked
91
	 */
92
	public function updateAllTitlesBySite(string $site, ?string $last_checked = NULL) {
93
		// region $query = Get all titles by $site;
94
		// @formatter:off
95
		$query = $this->db
96
			->select('
97
				tracker_titles.id as title_id,
98
				tracker_titles.title,
99
				tracker_titles.title_url,
100
				tracker_titles.status,
101
				tracker_sites.site,
102
				tracker_sites.site_class,
103
				tracker_sites.status,
104
				tracker_titles.latest_chapter,
105
				tracker_titles.last_updated,
106
				from_unixtime(MAX(auth_users.last_login)) AS timestamp
107
			')
108
			->from('tracker_titles')
109
			->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
110
			->join('tracker_chapters', 'tracker_titles.id = tracker_chapters.title_id', 'left')
111
			->join('auth_users', 'tracker_chapters.user_id = auth_users.id', 'left')
112
			->where('tracker_sites.status', 'enabled')
113
			->where('tracker_sites.site_class', $site)
114
			->group_start()
115
				//Check if title is marked as on-going...
116
				->where('tracker_titles.status', 0)
117
				//Check if title is marked as complete...
118
				->or_where('tracker_titles.status', 1)
119
			->group_end()
120
			//Status 2 (One-shot) & 255 (Ignore) are both not updated intentionally.
121
			->group_by('tracker_titles.id, tracker_chapters.active')
122
			//Check if the series is actually being tracked by someone
123
			->having('timestamp IS NOT NULL')
124
			//AND if it's currently marked as active by the user
125
			->having('tracker_chapters.active', 'Y')
126
			//AND if they have been active in the last 120 hours (5 days)
127
			->having('timestamp > DATE_SUB(NOW(), INTERVAL 120 HOUR)')
128
			->order_by('tracker_titles.last_checked', 'ASC');
129
		// @formatter:on
130
		if(!is_null($last_checked)) {
131
			$query = $query->where('tracker_titles.last_checked >', $last_checked);
132
		}
133
		// endregion
134
		$query = $query->get();
135
136
		if($query->num_rows() > 0) {
137
			foreach ($query->result() as $row) {
138
				$this->handleUpdate($row);
139
			}
140
		}
141
	}
142
143
	protected function handleUpdate(object $row) : void {
144
		/** @var Base_Site_Model $site */
145
		$site = $this->sites->{$row->site_class};
146
147
		print "> {$row->title} <{$row->site_class} - {$row->title_url}> | <{$row->title_id}>"; //Print this prior to doing anything so we can more easily find out if something went wrong
148
149
		$updateData = $site->handleBatchUpdate($row->title_url);
150
		if(!$updateData['limited']) {
151
			$titleData = $updateData['titleData'];
152
			if(is_array($titleData) && (!is_null($titleData['latest_chapter']) || $site->canHaveNoChapters)) {
153
				if(count($titleData) >= 3) {
154
					// Normal update.
155
156
					//FIXME: "At the moment" we don't seem to be doing anything with TitleData['last_updated'].
157
					//       Should we even use this? Y/N
158
					if($this->Tracker->title->updateByID((int) $row->title_id, $titleData['latest_chapter'])) {
159
						//Make sure last_checked is always updated on successful run.
160
						//CHECK: Is there a reason we aren't just doing this in updateByID?
161
						$this->db->set('last_checked', 'CURRENT_TIMESTAMP', FALSE)
162
						         ->where('id', $row->title_id)
163
						         ->update('tracker_titles');
164
165
						print " - ({$titleData['latest_chapter']})\n";
166
					} else {
167
						log_message('error', "{$row->site_class} | {$row->title} ({$row->title_url}) | Failed to update.");
168
169
						print " - Something went wrong?\n";
170
					}
171
				} else {
172
					// No chapters were returned, but site allows this.
173
					if($this->Tracker->title->updateByID((int) $row->title_id, NULL)) {
174
						//Make sure last_checked is always updated on successful run.
175
						//CHECK: Is there a reason we aren't just doing this in updateByID?
176
						$this->db->set('last_checked', 'CURRENT_TIMESTAMP', FALSE)
177
						         ->where('id', $row->title_id)
178
						         ->update('tracker_titles');
179
180
						print " - (No chapters found?)\n";
181
					} else {
182
						log_message('error', "{$row->site_class} | {$row->title} ({$row->title_url}) | Failed to update.");
183
184
						print " - Something went wrong?\n";
185
					}
186
				}
187
			}
188
			else {
189
				//TODO: We should have some way to handle this in the site models.
190
				if($row->site_class !== 'MangaKakarot') {
191
					log_message('error', "{$row->site_class} | {$row->title} ({$row->title_url}) | Failed to update.");
192
				}
193
				$this->Tracker->title->updateFailedChecksByID((int) $row->title_id);
194
195
				print " - FAILED TO PARSE\n";
196
			}
197
		} else {
198
			// Rate limited, do nothing.
199
			print " - Rate Limited!\n";
200
		}
201
	}
202
203
	/**
204
	 * Checks for any sites which support custom updating (usually via following lists) and updates them.
205
	 * This is run hourly.
206
	 */
207
	public function updateCustom() {
208
		$query = $this->db->select('*')
209
		                  ->from('tracker_sites')
210
		                  ->where('tracker_sites.status', 'enabled')
211
		                  ->where('tracker_sites.use_custom', 'Y')
212
		                  ->get();
213
214
		$sites = $query->result_array();
215
		foreach ($sites as $site) {
216
			$siteClass = $this->sites->{$site['site_class']};
217
			if($titleDataList = $siteClass->doCustomUpdate()) {
218
				foreach ($titleDataList as $titleURL => $titleData) {
219
					$titleURL = (string) $titleURL; //Number only keys get converted to int for some reason, so we need to fix that.
220
					print "> {$titleData['title']} <{$site['site_class']}>"; //Print this prior to doing anything so we can more easily find out if something went wrong
221
					if(is_array($titleData) && !is_null($titleData['latest_chapter'])) {
222
						if($dbTitleData = $this->Tracker->title->getID($titleURL, (int) $site['id'], FALSE, TRUE)) {
223
							if($this->sites->{$site['site_class']}->doCustomCheck($dbTitleData['latest_chapter'], $titleData['latest_chapter'])) {
224
								$titleID = $dbTitleData['id'];
225
								if($this->Tracker->title->updateByID((int) $titleID, $titleData['latest_chapter'])) {
226
									//Make sure last_checked is always updated on successful run.
227
									//CHECK: Is there a reason we aren't just doing this in updateByID?
228
									$this->db->set('last_checked', 'CURRENT_TIMESTAMP', FALSE)
229
									         ->where('id', $titleID)
230
									         ->update('tracker_titles');
231
232
									print " - ({$titleData['latest_chapter']})\n";
233
								} else {
234
									print " - Title doesn't exist? ($titleID)\n";
235
								}
236
							} else {
237
								print " - Failed Check (DB: '{$dbTitleData['latest_chapter']}' || UPDATE: '{$titleData['latest_chapter']}')\n";
238
							}
239
						} else {
240
							if($siteClass->customType === 1) {
241
								//We only need to log if following page is missing title, not latest releases
242
								log_message('error', "CUSTOM: {$titleData['title']} - {$site['site_class']} || Title does not exist in DB??");
243
								print " - Title doesn't currently exist in DB? Maybe different language or title stub change? ($titleURL)\n";
244
							} else {
245
								print " - Title isn't currently tracked.\n";
246
							}
247
						}
248
					} else {
249
						log_message('error', "CUSTOM: {$titleData['title']} - {$site['site_class']} failed to custom update successfully");
250
						print " - FAILED TO PARSE\n";
251
					}
252
				}
253
			}
254
		}
255
	}
256
257
	public function refollowCustom() {
258
		$query = $this->db->select('tracker_titles.id, tracker_titles.title_url, tracker_sites.site_class')
259
		                  ->from('tracker_titles')
260
		                  ->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
261
		                  ->where('tracker_titles.followed','N')
262
		                  ->where('tracker_titles !=', '255')
263
		                  ->where('tracker_sites.status', 'enabled')
264
		                  ->where('tracker_sites.use_custom', 'Y')
265
		                  ->get();
266
267
		if($query->num_rows() > 0) {
268
			foreach($query->result() as $row) {
269
				$titleData = $this->Tracker->sites->{$row->site_class}->getTitleData($row->title_url, TRUE);
270
271
				if($titleData) {
272
					$titleData = array_intersect_key($titleData, array_flip(['followed']));
273
274
					if(!empty($titleData)) {
275
						$this->db->set($titleData)
276
						         ->where('id', $row->id)
277
						         ->update('tracker_titles');
278
279
						print "> {$row->site_class}:{$row->id}:{$row->title_url} FOLLOWED\n";
280
					} else {
281
						print "> {$row->site_class}:{$row->id}:{$row->title_url} FAILED (NO FOLLOWED)\n";
282
					}
283
				} else {
284
					log_message('error', "getTitleData failed for: {$row->site_class} | {$row->title_url}");
285
					print "> {$row->site_class}:{$row->id}:{$row->title_url} FAILED (NO TITLEDATA)\n";
286
				}
287
			}
288
		}
289
	}
290
291
	public function incrementRequests() : void {
292
		$temp_now = new DateTime();
293
		$temp_now->setTimezone(new DateTimeZone('America/New_York'));
294
		$date = $temp_now->format('Y-m-d');
295
296
		$query = $this->db->select('1')
297
		                  ->from('site_stats')
298
		                  ->where('date', $date)
299
		                  ->get();
300
301
		if($query->num_rows() > 0) {
302
			$this->db->set('total_requests', 'total_requests+1', FALSE)
303
			         ->where('date', $date)
304
			         ->update('site_stats');
305
		} else {
306
			$this->db->insert('site_stats', [
307
				'date'           => $date,
308
				'total_requests' => 1
309
			]);
310
		}
311
	}
312
313
	public function getNextUpdateTime(string $format = "%H:%I:%S") : string {
314
		$temp_now = new DateTime();
315
		$temp_now->setTimezone(new DateTimeZone('America/New_York'));
316
		$temp_now_formatted = $temp_now->format('Y-m-d H:i:s');
317
318
		//NOTE: PHP Bug: DateTime:diff doesn't play nice with setTimezone, so we need to create another DT object
319
		$now         = new DateTime($temp_now_formatted);
320
		$future_date = new DateTime($temp_now_formatted);
321
		$now_hour    = (int) $now->format('H');
322
		if($now_hour < 4) {
323
			//Time until 4am
324
			$future_date->setTime(4, 00);
325
		} elseif($now_hour < 8) {
326
			//Time until 8am
327
			$future_date->setTime(8, 00);
328
		} elseif($now_hour < 12) {
329
			//Time until 12pm
330
			$future_date->setTime(12, 00);
331
		} elseif($now_hour < 16) {
332
			//Time until 4pm
333
			$future_date->setTime(16, 00);
334
		} elseif($now_hour < 20) {
335
			//Time until 8pm
336
			$future_date->setTime(20, 00);
337
		} else {
338
			//Time until 12am
339
			$future_date->setTime(00, 00);
340
			$future_date->add(new DateInterval('P1D'));
341
		}
342
343
		$interval = $future_date->diff($now);
344
		return $interval->format($format);
345
	}
346
}
347