UpdateStatus   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 97
rs 10
c 0
b 0
f 0
ccs 0
cts 70
cp 0
wmc 5
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A index() 0 13 1
B _get_updates() 0 75 3
1
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class UpdateStatus extends MY_Controller {
4
	public function __construct() {
5
		parent::__construct();
6
7
		$this->load->library('table');
8
	}
9
10
	public function index() : void {
11
		$this->header_data['title'] = "Update Status";
12
		$this->header_data['page']  = "update_status";
13
14
		$this->body_data['updates'] = array_merge([['Site', 'Title']], $this->_get_updates());
15
16
		$template = array(
17
			'table_open' => '<table class="table table-striped">'
18
		);
19
20
		$this->table->set_template($template);
21
		$this->_render_page("UpdateStatus");
22
	}
23
24
	private function _get_updates() {
25
		$update_time = $this->Tracker->admin->getNextUpdateTime("%H:%i");
26
		$this->body_data['update_time'] = explode(':', $update_time);
27
		//FIXME: This is just a copy/paste of the query in the admin model. Maybe we should just have a way to grab this normally?
28
		// @formatter:off
29
		$query = $this->db
30
			->select('
31
				tracker_titles.title,
32
				tracker_titles.title_url,
33
				tracker_sites.site_class,
34
				from_unixtime(MAX(auth_users.last_login)) AS timestamp
35
			')
36
			->from('tracker_titles')
37
			->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
38
			->join('tracker_chapters', 'tracker_titles.id = tracker_chapters.title_id', 'left')
39
			->join('auth_users', 'tracker_chapters.user_id = auth_users.id', 'left')
40
			->where('tracker_sites.status', 'enabled')
41
			->group_start()
42
				//Check if title is marked as on-going...
43
				->where('tracker_titles.status', 0)
44
				//AND matches one of where queries below
45
				->group_start()
46
					//Then check if it's NULL (only occurs for new series)
47
					->where('latest_chapter', NULL)
48
					//OR if it hasn't updated within the past 12 hours AND isn't a custom update site
49
					->or_group_start()
50
						->where('tracker_sites.use_custom', 'N')
51
						->where("last_checked < DATE_SUB(DATE_ADD(NOW(), INTERVAL '{$update_time}' HOUR_MINUTE), INTERVAL 12 HOUR)")
52
					->group_end()
53
					//OR it is a custom update site, has more than one follower and hasn't updated within the past 72 hours.
54
					->or_group_start()
55
						->where('tracker_titles.id IN (
56
							SELECT title_id
57
							FROM tracker_chapters
58
							GROUP BY title_id
59
							HAVING COUNT(title_id) > 1
60
						)', NULL, FALSE)
61
62
						->where("last_checked < DATE_SUB(DATE_ADD(NOW(), INTERVAL '{$update_time}' HOUR_MINUTE), INTERVAL 72 HOUR)")
63
					->group_end()
64
					//OR it is a custom update site and hasn't updated within the past 120 hours (5 days)
65
					->or_where("last_checked < DATE_SUB(DATE_ADD(NOW(), INTERVAL '{$update_time}' HOUR_MINUTE), INTERVAL 120 HOUR)")
66
				->group_end()
67
			->group_end()
68
			->or_group_start()
69
				//Check if title is marked as complete...
70
				->where('tracker_titles.status', 1)
71
				//Then check if it hasn't updated within the past week
72
				->where("last_checked < DATE_SUB(DATE_ADD(NOW(), INTERVAL '{$update_time}' HOUR_MINUTE), INTERVAL 1 WEEK)")
73
			->group_end()
74
			//Status 2 (One-shot) & 255 (Ignore) are both not updated intentionally.
75
76
			->group_by('tracker_titles.id, tracker_chapters.active')
77
			//Check if the series is actually being tracked by someone
78
			->having('timestamp IS NOT NULL')
79
			//AND if it's currently marked as active by the user
80
			->having('tracker_chapters.active', 'Y')
81
			//AND if they have been active in the last 120 hours (5 days)
82
			->having("timestamp > DATE_SUB(DATE_ADD(NOW(), INTERVAL '{$update_time}' HOUR_MINUTE), INTERVAL 120 HOUR)")
83
			->order_by('tracker_titles.title', 'ASC')
84
			->get();
85
		// @formatter:on
86
87
		$resultArr = [];
88
		if($query->num_rows() > 0) {
89
			foreach($query->result() as $row) {
90
				$resultArr[] = [
91
					'site'           => $row->site_class,
92
					'full_title_url' => "{$row->title}"
93
				];
94
			}
95
		}
96
97
		return $resultArr;
98
	}
99
}
100