| 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 |  |  | 				log_message('error', "{$row->site_class} | {$row->title} ({$row->title_url}) | Failed to update."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 				$this->Tracker->title->updateFailedChecksByID((int) $row->title_id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 				print " - FAILED TO PARSE\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 			// Rate limited, do nothing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  | 			print " - Rate Limited!\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 	 * Checks for any sites which support custom updating (usually via following lists) and updates them. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | 	 * This is run hourly. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  | 	public function updateCustom() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  | 		$query = $this->db->select('*') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  | 		                  ->from('tracker_sites') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  | 		                  ->where('tracker_sites.status', 'enabled') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  | 		                  ->where('tracker_sites.use_custom', 'Y') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  | 		                  ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  | 		$sites = $query->result_array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  | 		foreach ($sites as $site) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  | 			$siteClass = $this->sites->{$site['site_class']}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  | 			if($titleDataList = $siteClass->doCustomUpdate()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  | 				foreach ($titleDataList as $titleURL => $titleData) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  | 					$titleURL = (string) $titleURL; //Number only keys get converted to int for some reason, so we need to fix that. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  | 					print "> {$titleData['title']} <{$site['site_class']}>"; //Print this prior to doing anything so we can more easily find out if something went wrong | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  | 					if(is_array($titleData) && !is_null($titleData['latest_chapter'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  | 						if($dbTitleData = $this->Tracker->title->getID($titleURL, (int) $site['id'], FALSE, TRUE)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  | 							if($this->sites->{$site['site_class']}->doCustomCheck($dbTitleData['latest_chapter'], $titleData['latest_chapter'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  | 								$titleID = $dbTitleData['id']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  | 								if($this->Tracker->title->updateByID((int) $titleID, $titleData['latest_chapter'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  | 									//Make sure last_checked is always updated on successful run. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  | 									//CHECK: Is there a reason we aren't just doing this in updateByID? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  | 									$this->db->set('last_checked', 'CURRENT_TIMESTAMP', FALSE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  | 									         ->where('id', $titleID) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  | 									         ->update('tracker_titles'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  | 									print " - ({$titleData['latest_chapter']})\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  | 								} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  | 									print " - Title doesn't exist? ($titleID)\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  | 								} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  | 							} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  | 								print " - Failed Check (DB: '{$dbTitleData['latest_chapter']}' || UPDATE: '{$titleData['latest_chapter']}')\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  | 							} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  | 						} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  | 							if($siteClass->customType === 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  | 								//We only need to log if following page is missing title, not latest releases | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  | 								log_message('error', "CUSTOM: {$titleData['title']} - {$site['site_class']} || Title does not exist in DB??"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  | 								print " - Title doesn't currently exist in DB? Maybe different language or title stub change? ($titleURL)\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  | 							} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  | 								print " - Title isn't currently tracked.\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  | 							} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  | 						} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  | 					} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  | 						log_message('error', "CUSTOM: {$titleData['title']} - {$site['site_class']} failed to custom update successfully"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  | 						print " - FAILED TO PARSE\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  | 					} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  | 	public function refollowCustom() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  | 		$query = $this->db->select('tracker_titles.id, tracker_titles.title_url, tracker_sites.site_class') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  | 		                  ->from('tracker_titles') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  | 		                  ->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  | 		                  ->where('tracker_titles.followed','N') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  | 		                  ->where('tracker_titles !=', '255') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  | 		                  ->where('tracker_sites.status', 'enabled') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  | 		                  ->where('tracker_sites.use_custom', 'Y') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  | 		                  ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  | 		if($query->num_rows() > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  | 			foreach($query->result() as $row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  | 				$titleData = $this->Tracker->sites->{$row->site_class}->getTitleData($row->title_url, TRUE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  | 				if($titleData) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  | 					$titleData = array_intersect_key($titleData, array_flip(['followed'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  | 					if(!empty($titleData)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  | 						$this->db->set($titleData) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  | 						         ->where('id', $row->id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  | 						         ->update('tracker_titles'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  | 						print "> {$row->site_class}:{$row->id}:{$row->title_url} FOLLOWED\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  | 					} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  | 						print "> {$row->site_class}:{$row->id}:{$row->title_url} FAILED (NO FOLLOWED)\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  | 					} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  | 				} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  | 					log_message('error', "getTitleData failed for: {$row->site_class} | {$row->title_url}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  | 					print "> {$row->site_class}:{$row->id}:{$row->title_url} FAILED (NO TITLEDATA)\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  | 	public function incrementRequests() : void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  | 		$temp_now = new DateTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  | 		$temp_now->setTimezone(new DateTimeZone('America/New_York')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  | 		$date = $temp_now->format('Y-m-d'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  | 		$query = $this->db->select('1') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  | 		                  ->from('site_stats') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  | 		                  ->where('date', $date) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  | 		                  ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  | 		if($query->num_rows() > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  | 			$this->db->set('total_requests', 'total_requests+1', FALSE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  | 			         ->where('date', $date) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  | 			         ->update('site_stats'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  | 			$this->db->insert('site_stats', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  | 				'date'           => $date, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  | 				'total_requests' => 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  | 			]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  | 	public function getNextUpdateTime(string $format = "%H:%I:%S") : string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  | 		$temp_now = new DateTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  | 		$temp_now->setTimezone(new DateTimeZone('America/New_York')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  | 		$temp_now_formatted = $temp_now->format('Y-m-d H:i:s'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  | 		//NOTE: PHP Bug: DateTime:diff doesn't play nice with setTimezone, so we need to create another DT object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  | 		$now         = new DateTime($temp_now_formatted); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  | 		$future_date = new DateTime($temp_now_formatted); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  | 		$now_hour    = (int) $now->format('H'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  | 		if($now_hour < 4) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  | 			//Time until 4am | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  | 			$future_date->setTime(4, 00); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  | 		} elseif($now_hour < 8) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  | 			//Time until 8am | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  | 			$future_date->setTime(8, 00); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  | 		} elseif($now_hour < 12) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  | 			//Time until 12pm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  | 			$future_date->setTime(12, 00); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  | 		} elseif($now_hour < 16) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  | 			//Time until 4pm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  | 			$future_date->setTime(16, 00); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  | 		} elseif($now_hour < 20) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  | 			//Time until 8pm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  | 			$future_date->setTime(20, 00); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  | 			//Time until 12am | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  | 			$future_date->setTime(00, 00); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  | 			$future_date->add(new DateInterval('P1D')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  | 		$interval = $future_date->diff($now); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  | 		return $interval->format($format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 343 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 344 |  |  |  |