Completed
Push — master ( 53eae5...5a39da )
by Angus
03:43
created

Tracker_Admin_Model::handleUpdate()   C

Complexity

Conditions 10
Paths 10

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
nc 10
nop 1
dl 0
loc 62
ccs 0
cts 29
cp 0
crap 110
rs 6.9624
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 96
	public function __construct() {
5 96
		parent::__construct();
6 96
	}
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, tracker_titles.last_checked,
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() //region
34
				->group_start() //region
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() //region
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() //region
43
							->where('tracker_sites.use_custom', 'N')
44
							->where('last_checked < DATE_SUB(NOW(), INTERVAL 12 HOUR)')
45
						->group_end() //endregion
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() //region
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() //endregion
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() //endregion
59
				->group_end() //endregion
60
				->or_group_start() //region
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() //endregion
66
			->group_end() //endregion
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.last_checked', 'ASC');
76
		// endregion
77
		$query = $query->get();
78
79
		if($query->num_rows() > 0) {
80
			$hardRateLimit  = 500; //This is to avoid any possible IP bans by cache breaking again.
81
			$siteRateLimits = [];
82
			foreach ($query->result() as $row) {
83
				if(!array_key_exists($row->site_class,$siteRateLimits)) {
84
					$siteRateLimits[$row->site_class] = 0;
85
				}
86
87
				$siteRateLimits[$row->site_class]++;
88
				if($siteRateLimits[$row->site_class] > $hardRateLimit) continue;
89
90
				if($siteRateLimits[$row->site_class] > 25) {
91
					// We're doing lots of requests to a single site, so add delays.
92
					sleep(1);
93
				}
94
95
				$this->handleUpdate($row);
96
			}
97
		}
98
	}
99
100
	/**
101
	 * Intended to be only used as a quick way to update all series on a site after a bug.
102
	 *
103
	 * @param string      $site
104
	 * @param null|string $last_checked
105
	 */
106
	public function updateAllTitlesBySite(string $site, ?string $last_checked = NULL) {
107
		// region $query = Get all titles by $site;
108
		// @formatter:off
109
		$query = $this->db
110
			->select('
111
				tracker_titles.id as title_id,
112
				tracker_titles.title,
113
				tracker_titles.title_url,
114
				tracker_titles.status,
115
				tracker_sites.site,
116
				tracker_sites.site_class,
117
				tracker_sites.status,
118
				tracker_titles.latest_chapter,
119
				tracker_titles.last_updated,
120
				from_unixtime(MAX(auth_users.last_login)) AS timestamp
121
			')
122
			->from('tracker_titles')
123
			->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
124
			->join('tracker_chapters', 'tracker_titles.id = tracker_chapters.title_id', 'left')
125
			->join('auth_users', 'tracker_chapters.user_id = auth_users.id', 'left')
126
			->where('tracker_sites.status', 'enabled')
127
			->where('tracker_sites.site_class', $site)
128
			->group_start() //region
129
				//Check if title is marked as on-going...
130
				->where('tracker_titles.status', 0)
131
				//Check if title is marked as complete...
132
				->or_where('tracker_titles.status', 1)
133
			->group_end() //endregion
134
			//Status 2 (One-shot) & 255 (Ignore) are both not updated intentionally.
135
			->group_by('tracker_titles.id, tracker_chapters.active')
136
			//Check if the series is actually being tracked by someone
137
			->having('timestamp IS NOT NULL')
138
			//AND if it's currently marked as active by the user
139
			->having('tracker_chapters.active', 'Y')
140
			//AND if they have been active in the last 120 hours (5 days)
141
			->having('timestamp > DATE_SUB(NOW(), INTERVAL 120 HOUR)')
142
			->order_by('tracker_titles.last_checked', 'ASC');
143
		// @formatter:on
144
		if(!is_null($last_checked)) {
145
			$query = $query->where('tracker_titles.last_checked >', $last_checked);
146
		}
147
		// endregion
148
		$query = $query->get();
149
150
		if($query->num_rows() > 0) {
151
			foreach ($query->result() as $row) {
152
				$this->handleUpdate($row);
153
			}
154
		}
155
	}
156
157
	protected function handleUpdate(object $row) : void {
158
		/** @var Base_Site_Model $site */
159
		$site = $this->sites->{$row->site_class};
160
161
		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
162
163
		$updateData = $site->handleBatchUpdate($row->title_url);
164
		if(!$updateData['limited']) {
165
			$titleData = $updateData['titleData'];
166
			if(is_array($titleData)) {
167
				if(array_keys_exist(['title', 'latest_chapter', 'last_updated'], $titleData)) {
168
					// Standard update.
169
170
					if($this->Tracker->title->updateTitleDataByID((int) $row->title_id, $titleData)) {
171
						print " - ({$titleData['latest_chapter']})\n";
172
					} else {
173
						log_message('error', "{$row->site_class} | {$row->title} ({$row->title_url}) | Failed to update.");
0 ignored issues
show
Unused Code introduced by
The call to the function log_message() seems unnecessary as the function has no side-effects.
Loading history...
174
175
						print " - Something went wrong?\n";
176
					}
177
				}
178
				else if(array_key_exists('status', $titleData)) {
179
					// Series has probably been deleted.
180
181
					if($this->Tracker->title->updateTitleDataByID((int) $row->title_id, $titleData)) {
182
						print " - Status has changed ({$titleData['status']})\n";
183
					} else {
184
						log_message('error', "{$row->site_class} | {$row->title} ({$row->title_url}) | Failed to update.");
0 ignored issues
show
Unused Code introduced by
The call to the function log_message() seems unnecessary as the function has no side-effects.
Loading history...
185
186
						print " - Something went wrong?\n";
187
					}
188
				}
189
				else if($site->canHaveNoChapters) {
190
					// Previous statements failed, however site can have no chapters.
191
					if($this->Tracker->title->updateTitleDataByID((int) $row->title_id, ['latest_chapter' => NULL])) {
192
						print " - (No chapters found?)\n";
193
					} else {
194
						log_message('error', "{$row->site_class} | {$row->title} ({$row->title_url}) | Failed to update.");
0 ignored issues
show
Unused Code introduced by
The call to the function log_message() seems unnecessary as the function has no side-effects.
Loading history...
195
196
						print " - Something went wrong?\n";
197
					}
198
				}
199
				else {
200
					log_message('error', 'handleUpdate failed due to invalid titleData info?');
0 ignored issues
show
Unused Code introduced by
The call to the function log_message() seems unnecessary as the function has no side-effects.
Loading history...
201
				}
202
			}
203
			else {
204
				// If TitleData does not exist, either something has broken, or we've set up the site wrong.
205
206
				//TODO: We should have some way to handle this in the site models.
207
				if($row->site_class !== 'MangaKakarot') {
208
					log_message('error', "{$row->site_class} | {$row->title} ({$row->title_url}) | Failed to update.");
0 ignored issues
show
Unused Code introduced by
The call to the function log_message() seems unnecessary as the function has no side-effects.
Loading history...
209
				}
210
				$this->Tracker->title->updateFailedChecksByID((int) $row->title_id);
211
212
				print " - FAILED TO PARSE\n";
213
			}
214
		} else {
215
			// Rate limited, do nothing.
216
			print " - Rate Limited!\n";
217
		}
218
	}
219
220
	/**
221
	 * Checks for any sites which support custom updating (usually via following lists) and updates them.
222
	 * This is run hourly.
223
	 */
224
	public function updateCustom() {
225
		$query = $this->db->select('*')
226
		                  ->from('tracker_sites')
227
		                  ->where('tracker_sites.status', 'enabled')
228
		                  ->where('tracker_sites.use_custom', 'Y')
229
		                  ->get();
230
231
		$sites = $query->result_array();
232
		foreach ($sites as $site) {
233
			$siteClass = $this->sites->{$site['site_class']};
234
			if($titleDataList = $siteClass->doCustomUpdate()) {
235
				foreach ($titleDataList as $titleURL => $titleData) {
236
					$titleURL = (string) $titleURL; //Number only keys get converted to int for some reason, so we need to fix that.
237
					print "> {$titleData['title']} <{$site['site_class']}>"; //Print this prior to doing anything so we can more easily find out if something went wrong
238
					if(is_array($titleData) && !is_null($titleData['latest_chapter'])) {
239
						if($dbTitleData = $this->Tracker->title->getID($titleURL, (int) $site['id'], FALSE, TRUE)) {
240
							if($this->sites->{$site['site_class']}->doCustomCheck($dbTitleData['latest_chapter'], $titleData['latest_chapter'])) {
241
								$titleID = $dbTitleData['id'];
242
								if($this->Tracker->title->updateByID((int) $titleID, $titleData['latest_chapter'])) {
243
									//Make sure last_checked is always updated on successful run.
244
									//CHECK: Is there a reason we aren't just doing this in updateByID?
245
									$this->db->set('last_checked', 'CURRENT_TIMESTAMP', FALSE)
246
									         ->where('id', $titleID)
247
									         ->update('tracker_titles');
248
249
									print " - ({$titleData['latest_chapter']})\n";
250
								} else {
251
									print " - Title doesn't exist? ($titleID)\n";
252
								}
253
							} else {
254
								print " - Failed Check (DB: '{$dbTitleData['latest_chapter']}' || UPDATE: '{$titleData['latest_chapter']}')\n";
255
							}
256
						} else {
257
							if($siteClass->customType === 1) {
258
								//We only need to log if following page is missing title, not latest releases
259
								log_message('error', "CUSTOM: {$titleData['title']} - {$site['site_class']} || Title does not exist in DB??");
0 ignored issues
show
Unused Code introduced by
The call to the function log_message() seems unnecessary as the function has no side-effects.
Loading history...
260
								print " - Title doesn't currently exist in DB? Maybe different language or title stub change? ($titleURL)\n";
261
							} else {
262
								print " - Title isn't currently tracked.\n";
263
							}
264
						}
265
					} else {
266
						log_message('error', "CUSTOM: {$titleData['title']} - {$site['site_class']} failed to custom update successfully");
0 ignored issues
show
Unused Code introduced by
The call to the function log_message() seems unnecessary as the function has no side-effects.
Loading history...
267
						print " - FAILED TO PARSE\n";
268
					}
269
				}
270
			}
271
		}
272
	}
273
274
	public function refollowCustom() {
275
		$query = $this->db->select('tracker_titles.id, tracker_titles.title_url, tracker_sites.site_class')
276
		                  ->from('tracker_titles')
277
		                  ->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
278
		                  ->where('tracker_titles.followed','N')
279
		                  ->where('tracker_titles !=', '255')
280
		                  ->where('tracker_sites.status', 'enabled')
281
		                  ->where('tracker_sites.use_custom', 'Y')
282
		                  ->get();
283
284
		if($query->num_rows() > 0) {
285
			foreach($query->result() as $row) {
286
				$titleData = $this->Tracker->sites->{$row->site_class}->getTitleData($row->title_url, TRUE);
287
288
				if($titleData) {
289
					$titleData = array_intersect_key($titleData, array_flip(['followed']));
290
291
					if(!empty($titleData)) {
292
						$this->db->set($titleData)
293
						         ->where('id', $row->id)
294
						         ->update('tracker_titles');
295
296
						print "> {$row->site_class}:{$row->id}:{$row->title_url} FOLLOWED\n";
297
					} else {
298
						print "> {$row->site_class}:{$row->id}:{$row->title_url} FAILED (NO FOLLOWED)\n";
299
					}
300
				} else {
301
					log_message('error', "getTitleData failed for: {$row->site_class} | {$row->title_url}");
0 ignored issues
show
Unused Code introduced by
The call to the function log_message() seems unnecessary as the function has no side-effects.
Loading history...
302
					print "> {$row->site_class}:{$row->id}:{$row->title_url} FAILED (NO TITLEDATA)\n";
303
				}
304
			}
305
		}
306
	}
307
308
	public function incrementRequests() : void {
309
		$temp_now = new DateTime();
310
		$temp_now->setTimezone(new DateTimeZone('America/New_York'));
311
		$date = $temp_now->format('Y-m-d');
312
313
		$query = $this->db->select('1')
314
		                  ->from('site_stats')
315
		                  ->where('date', $date)
316
		                  ->get();
317
318
		if($query->num_rows() > 0) {
319
			$this->db->set('total_requests', 'total_requests+1', FALSE)
320
			         ->where('date', $date)
321
			         ->update('site_stats');
322
		} else {
323
			$this->db->insert('site_stats', [
324
				'date'           => $date,
325
				'total_requests' => 1
326
			]);
327
		}
328
	}
329
330
	public function getNextUpdateTime(string $format = "%H:%I:%S") : string {
331
		$temp_now = new DateTime();
332
		$temp_now->setTimezone(new DateTimeZone('America/New_York'));
333
		$temp_now_formatted = $temp_now->format('Y-m-d H:i:s');
334
335
		//NOTE: PHP Bug: DateTime:diff doesn't play nice with setTimezone, so we need to create another DT object
336
		$now         = new DateTime($temp_now_formatted);
337
		$future_date = new DateTime($temp_now_formatted);
338
		$now_hour    = (int) $now->format('H');
339
		if($now_hour < 4) {
340
			//Time until 4am
341
			$future_date->setTime(4, 00);
342
		} elseif($now_hour < 8) {
343
			//Time until 8am
344
			$future_date->setTime(8, 00);
345
		} elseif($now_hour < 12) {
346
			//Time until 12pm
347
			$future_date->setTime(12, 00);
348
		} elseif($now_hour < 16) {
349
			//Time until 4pm
350
			$future_date->setTime(16, 00);
351
		} elseif($now_hour < 20) {
352
			//Time until 8pm
353
			$future_date->setTime(20, 00);
354
		} else {
355
			//Time until 12am
356
			$future_date->setTime(00, 00);
357
			$future_date->add(new DateInterval('P1D'));
358
		}
359
360
		$interval = $future_date->diff($now);
361
		return $interval->format($format);
362
	}
363
}
364