Completed
Push — master ( 1dfd21...36ade4 )
by Angus
03:51
created
application/models/Tracker_Model.php 1 patch
Indentation   +99 added lines, -99 removed lines patch added patch discarded remove patch
@@ -157,9 +157,9 @@  discard block
 block discarded – undo
157 157
 
158 158
 	public function getSiteDataFromURL(string $site_url) {
159 159
 		$query = $this->db->select('id, site_class')
160
-		                  ->from('tracker_sites')
161
-		                  ->where('site', $site_url)
162
-		                  ->get();
160
+						  ->from('tracker_sites')
161
+						  ->where('site', $site_url)
162
+						  ->get();
163 163
 
164 164
 		if($query->num_rows() > 0) {
165 165
 			$siteData = $query->row();
@@ -170,11 +170,11 @@  discard block
 block discarded – undo
170 170
 
171 171
 	public function getTitleID(string $titleURL, int $siteID, bool $create = TRUE, bool $returnData = FALSE) {
172 172
 		$query = $this->db->select('tracker_titles.id, tracker_titles.title, tracker_titles.title_url, tracker_titles.latest_chapter, tracker_titles.status, tracker_sites.site_class, (tracker_titles.last_checked > DATE_SUB(NOW(), INTERVAL 3 DAY)) AS active', FALSE)
173
-		                  ->from('tracker_titles')
174
-		                  ->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
175
-		                  ->where('tracker_titles.title_url', $titleURL)
176
-		                  ->where('tracker_titles.site_id', $siteID)
177
-		                  ->get();
173
+						  ->from('tracker_titles')
174
+						  ->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
175
+						  ->where('tracker_titles.title_url', $titleURL)
176
+						  ->where('tracker_titles.site_id', $siteID)
177
+						  ->get();
178 178
 
179 179
 		if($query->num_rows() > 0) {
180 180
 			$id = (int) $query->row('id');
@@ -187,8 +187,8 @@  discard block
 block discarded – undo
187 187
 						//Make sure last_checked is always updated on successful run.
188 188
 						//CHECK: Is there a reason we aren't just doing this in updateTitleById?
189 189
 						$this->db->set('last_checked', 'CURRENT_TIMESTAMP', FALSE)
190
-						         ->where('id', $id)
191
-						         ->update('tracker_titles');
190
+								 ->where('id', $id)
191
+								 ->update('tracker_titles');
192 192
 					}
193 193
 				} else {
194 194
 					log_message('error', "{$query->row('title')} failed to update successfully");
@@ -227,14 +227,14 @@  discard block
 block discarded – undo
227 227
 			}
228 228
 
229 229
 			$idQuery = $this->db->select('id')
230
-			                    ->where('user_id', $userID)
231
-			                    ->where('title_id', $titleID)
232
-			                    ->get('tracker_chapters');
230
+								->where('user_id', $userID)
231
+								->where('title_id', $titleID)
232
+								->get('tracker_chapters');
233 233
 			if($idQuery->num_rows() > 0) {
234 234
 				$success = (bool) $this->db->set(['current_chapter' => $chapter, 'active' => 'Y', 'last_updated' => NULL])
235
-				                    ->where('user_id', $userID)
236
-				                    ->where('title_id', $titleID)
237
-				                    ->update('tracker_chapters');
235
+									->where('user_id', $userID)
236
+									->where('title_id', $titleID)
237
+									->update('tracker_chapters');
238 238
 
239 239
 				if($success) {
240 240
 					$idQueryRow = $idQuery->row();
@@ -259,9 +259,9 @@  discard block
 block discarded – undo
259 259
 
260 260
 	public function updateTrackerByID(int $userID, int $chapterID, string $chapter) : bool {
261 261
 		$success = (bool) $this->db->set(['current_chapter' => $chapter, 'active' => 'Y', 'last_updated' => NULL])
262
-		                    ->where('user_id', $userID)
263
-		                    ->where('id', $chapterID)
264
-		                    ->update('tracker_chapters');
262
+							->where('user_id', $userID)
263
+							->where('id', $chapterID)
264
+							->update('tracker_chapters');
265 265
 
266 266
 		if($success) {
267 267
 			$this->History->userUpdateTitle($chapterID, $chapter);
@@ -274,23 +274,23 @@  discard block
 block discarded – undo
274 274
 		//This is to allow user history to function properly.
275 275
 
276 276
 		$success = $this->db->set(['active' => 'N', 'last_updated' => NULL])
277
-		                    ->where('user_id', $userID)
278
-		                    ->where('id', $chapterID)
279
-		                    ->update('tracker_chapters');
277
+							->where('user_id', $userID)
278
+							->where('id', $chapterID)
279
+							->update('tracker_chapters');
280 280
 
281 281
 		return (bool) $success;
282 282
 	}
283 283
 	private function updateTitleById(int $id, string $latestChapter) {
284 284
 		//FIXME: Really not too happy with how we're doing history stuff here, it just feels messy.
285 285
 		$query = $this->db->select('latest_chapter AS current_chapter')
286
-		                  ->from('tracker_titles')
287
-		                  ->where('id', $id)
288
-		                  ->get();
286
+						  ->from('tracker_titles')
287
+						  ->where('id', $id)
288
+						  ->get();
289 289
 		$row = $query->row();
290 290
 
291 291
 		$success = $this->db->set(['latest_chapter' => $latestChapter]) //last_updated gets updated via a trigger if something changes
292
-		                    ->where('id', $id)
293
-		                    ->update('tracker_titles');
292
+							->where('id', $id)
293
+							->update('tracker_titles');
294 294
 
295 295
 		//Update History
296 296
 		//NOTE: To avoid doing another query to grab the last_updated time, we just use time() which <should> get the same thing.
@@ -301,16 +301,16 @@  discard block
 block discarded – undo
301 301
 	}
302 302
 	private function updateTitleDataById(int $id, array $titleData) {
303 303
 		$success = $this->db->set($titleData)
304
-		                    ->where('id', $id)
305
-		                    ->update('tracker_titles');
304
+							->where('id', $id)
305
+							->update('tracker_titles');
306 306
 
307 307
 		return (bool) $success;
308 308
 	}
309 309
 	private function addTitle(string $titleURL, int $siteID) {
310 310
 		$query = $this->db->select('site, site_class')
311
-		                  ->from('tracker_sites')
312
-		                  ->where('id', $siteID)
313
-		                  ->get();
311
+						  ->from('tracker_sites')
312
+						  ->where('id', $siteID)
313
+						  ->get();
314 314
 
315 315
 		$titleData = $this->sites->{$query->row()->site_class}->getTitleData($titleURL, TRUE);
316 316
 
@@ -371,8 +371,8 @@  discard block
 block discarded – undo
371 371
 						//Make sure last_checked is always updated on successful run.
372 372
 						//CHECK: Is there a reason we aren't just doing this in updateTitleById?
373 373
 						$this->db->set('last_checked', 'CURRENT_TIMESTAMP', FALSE)
374
-						         ->where('id', $row->id)
375
-						         ->update('tracker_titles');
374
+								 ->where('id', $row->id)
375
+								 ->update('tracker_titles');
376 376
 
377 377
 						print " - ({$titleData['latest_chapter']})\n";
378 378
 					}
@@ -398,8 +398,8 @@  discard block
 block discarded – undo
398 398
 									//Make sure last_checked is always updated on successful run.
399 399
 									//CHECK: Is there a reason we aren't just doing this in updateTitleById?
400 400
 									$this->db->set('last_checked', 'CURRENT_TIMESTAMP', FALSE)
401
-									         ->where('id', $titleID)
402
-									         ->update('tracker_titles');
401
+											 ->where('id', $titleID)
402
+											 ->update('tracker_titles');
403 403
 
404 404
 									print " - ({$titleData['latest_chapter']})\n";
405 405
 								} else {
@@ -526,9 +526,9 @@  discard block
 block discarded – undo
526 526
 	}
527 527
 	public function setCategoryTrackerByID(int $userID, int $chapterID, string $category) : bool {
528 528
 		$success = $this->db->set(['category' => $category, 'active' => 'Y', 'last_updated' => NULL])
529
-		                    ->where('user_id', $userID)
530
-		                    ->where('id', $chapterID)
531
-		                    ->update('tracker_chapters');
529
+							->where('user_id', $userID)
530
+							->where('id', $chapterID)
531
+							->update('tracker_chapters');
532 532
 
533 533
 		return (bool) $success;
534 534
 	}
@@ -537,9 +537,9 @@  discard block
 block discarded – undo
537 537
 		$success = FALSE;
538 538
 		if(preg_match("/^[a-z0-9\\-_,:]{0,255}$/", $tag_string)) {
539 539
 			$success = (bool) $this->db->set(['tags' => $tag_string, 'active' => 'Y', 'last_updated' => NULL])
540
-			                           ->where('user_id', $userID)
541
-			                           ->where('id', $chapterID)
542
-			                           ->update('tracker_chapters');
540
+									   ->where('user_id', $userID)
541
+									   ->where('id', $chapterID)
542
+									   ->update('tracker_chapters');
543 543
 		}
544 544
 
545 545
 		if($success) {
@@ -577,23 +577,23 @@  discard block
 block discarded – undo
577 577
 
578 578
 			//We need the series to be tracked
579 579
 			$idCQuery = $this->db->select('id')
580
-			                    ->where('user_id', $userID)
581
-			                    ->where('title_id', $titleID)
582
-			                    ->get('tracker_chapters');
580
+								->where('user_id', $userID)
581
+								->where('title_id', $titleID)
582
+								->get('tracker_chapters');
583 583
 			if($idCQuery->num_rows() > 0) {
584 584
 				$idCQueryRow = $idCQuery->row();
585 585
 
586 586
 				//Check if it is already favourited
587 587
 				$idFQuery = $this->db->select('id')
588
-				                    ->where('chapter_id', $idCQueryRow->id)
589
-				                    ->where('chapter', $chapter)
590
-				                    ->get('tracker_favourites');
588
+									->where('chapter_id', $idCQueryRow->id)
589
+									->where('chapter', $chapter)
590
+									->get('tracker_favourites');
591 591
 				if($idFQuery->num_rows() > 0) {
592 592
 					//Chapter is already favourited, so remove it from DB
593 593
 					$idFQueryRow = $idFQuery->row();
594 594
 
595 595
 					$isSuccess = (bool) $this->db->where('id', $idFQueryRow->id)
596
-					                           ->delete('tracker_favourites');
596
+											   ->delete('tracker_favourites');
597 597
 
598 598
 					if($isSuccess) {
599 599
 						$success = array(
@@ -664,20 +664,20 @@  discard block
 block discarded – undo
664 664
 
665 665
 	public function getSites() : array {
666 666
 		$query = $this->db->select('*')
667
-		                  ->from('tracker_sites')
668
-		                  ->where('status', 'enabled')
669
-		                  ->get();
667
+						  ->from('tracker_sites')
668
+						  ->where('status', 'enabled')
669
+						  ->get();
670 670
 
671 671
 		return $query->result_array();
672 672
 	}
673 673
 
674 674
 	public function getUsedCategories(int $userID) : array {
675 675
 		$query = $this->db->distinct()
676
-		                  ->select('category')
677
-		                  ->from('tracker_chapters')
678
-		                  ->where('tracker_chapters.active', 'Y')
679
-		                  ->where('user_id', $userID)
680
-		                  ->get();
676
+						  ->select('category')
677
+						  ->from('tracker_chapters')
678
+						  ->where('tracker_chapters.active', 'Y')
679
+						  ->where('user_id', $userID)
680
+						  ->get();
681 681
 
682 682
 		return array_column($query->result_array(), 'category');
683 683
 	}
@@ -688,51 +688,51 @@  discard block
 block discarded – undo
688 688
 
689 689
 			//CHECK: Is it possible to merge some of these queries?
690 690
 			$queryUsers = $this->db->select([
691
-			                            'COUNT(*) AS total_users',
692
-			                            'SUM(CASE WHEN api_key IS NOT NULL THEN 1 ELSE 0 END) AS validated_users',
693
-			                            'SUM(CASE WHEN (api_key IS NOT NULL AND from_unixtime(last_login) > DATE_SUB(NOW(), INTERVAL 7 DAY)) THEN 1 ELSE 0 END) AS active_users'
694
-			                       ], FALSE)
695
-			                       ->from('auth_users')
696
-			                       ->get();
691
+										'COUNT(*) AS total_users',
692
+										'SUM(CASE WHEN api_key IS NOT NULL THEN 1 ELSE 0 END) AS validated_users',
693
+										'SUM(CASE WHEN (api_key IS NOT NULL AND from_unixtime(last_login) > DATE_SUB(NOW(), INTERVAL 7 DAY)) THEN 1 ELSE 0 END) AS active_users'
694
+								   ], FALSE)
695
+								   ->from('auth_users')
696
+								   ->get();
697 697
 			$stats = array_merge($stats, $queryUsers->result_array()[0]);
698 698
 
699 699
 			$queryCounts = $this->db->select([
700
-			                             'tracker_titles.title',
701
-			                             'COUNT(tracker_chapters.title_id) AS count'
702
-			                        ], FALSE)
703
-			                        ->from('tracker_chapters')
704
-			                        ->join('tracker_titles', 'tracker_titles.id = tracker_chapters.title_id', 'left')
705
-			                        ->group_by('tracker_chapters.title_id')
706
-			                        ->having('count > 1')
707
-			                        ->order_by('count DESC')
708
-			                        ->get();
700
+										 'tracker_titles.title',
701
+										 'COUNT(tracker_chapters.title_id) AS count'
702
+									], FALSE)
703
+									->from('tracker_chapters')
704
+									->join('tracker_titles', 'tracker_titles.id = tracker_chapters.title_id', 'left')
705
+									->group_by('tracker_chapters.title_id')
706
+									->having('count > 1')
707
+									->order_by('count DESC')
708
+									->get();
709 709
 			$stats['titles_tracked_more'] = count($queryCounts->result_array());
710 710
 			$stats['top_title_name']  = $queryCounts->result_array()[0]['title'] ?? 'N/A';
711 711
 			$stats['top_title_count'] = $queryCounts->result_array()[0]['count'] ?? 'N/A';
712 712
 
713 713
 			$queryTitles = $this->db->select([
714
-			                             'COUNT(DISTINCT tracker_titles.id) AS total_titles',
715
-			                             'COUNT(DISTINCT tracker_titles.site_id) AS total_sites',
716
-			                             'SUM(CASE WHEN from_unixtime(auth_users.last_login) > DATE_SUB(NOW(), INTERVAL 120 HOUR) IS NOT NULL THEN 0 ELSE 1 END) AS inactive_titles',
717
-			                             'SUM(CASE WHEN (tracker_titles.last_updated > DATE_SUB(NOW(), INTERVAL 24 HOUR)) THEN 1 ELSE 0 END) AS updated_titles'
718
-			                        ], FALSE)
719
-			                        ->from('tracker_titles')
720
-			                        ->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
721
-			                        ->join('tracker_chapters', 'tracker_titles.id = tracker_chapters.title_id', 'left')
722
-			                        ->join('auth_users', 'tracker_chapters.user_id = auth_users.id', 'left')
723
-			                        ->get();
714
+										 'COUNT(DISTINCT tracker_titles.id) AS total_titles',
715
+										 'COUNT(DISTINCT tracker_titles.site_id) AS total_sites',
716
+										 'SUM(CASE WHEN from_unixtime(auth_users.last_login) > DATE_SUB(NOW(), INTERVAL 120 HOUR) IS NOT NULL THEN 0 ELSE 1 END) AS inactive_titles',
717
+										 'SUM(CASE WHEN (tracker_titles.last_updated > DATE_SUB(NOW(), INTERVAL 24 HOUR)) THEN 1 ELSE 0 END) AS updated_titles'
718
+									], FALSE)
719
+									->from('tracker_titles')
720
+									->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
721
+									->join('tracker_chapters', 'tracker_titles.id = tracker_chapters.title_id', 'left')
722
+									->join('auth_users', 'tracker_chapters.user_id = auth_users.id', 'left')
723
+									->get();
724 724
 			$stats = array_merge($stats, $queryTitles->result_array()[0]);
725 725
 
726 726
 			$querySites = $this->db->select([
727
-			                           'tracker_sites.site',
728
-			                           'COUNT(*) AS count'
729
-			                       ], FALSE)
730
-			                       ->from('tracker_titles')
731
-			                       ->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
732
-			                       ->group_by('tracker_titles.site_id')
733
-			                       ->order_by('count DESC')
734
-			                       ->limit(3)
735
-			                       ->get();
727
+									   'tracker_sites.site',
728
+									   'COUNT(*) AS count'
729
+								   ], FALSE)
730
+								   ->from('tracker_titles')
731
+								   ->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
732
+								   ->group_by('tracker_titles.site_id')
733
+								   ->order_by('count DESC')
734
+								   ->limit(3)
735
+								   ->get();
736 736
 			$querySitesResult = $querySites->result_array();
737 737
 			$stats['rank1_site']       = $querySitesResult[0]['site'];
738 738
 			$stats['rank1_site_count'] = $querySitesResult[0]['count'];
@@ -742,17 +742,17 @@  discard block
 block discarded – undo
742 742
 			$stats['rank3_site_count'] = $querySitesResult[2]['count'];
743 743
 
744 744
 			$queryTitlesU = $this->db->select([
745
-			                             'COUNT(*) AS title_updated_count'
746
-			                         ], FALSE)
747
-			                         ->from('tracker_titles_history')
748
-			                         ->get();
745
+										 'COUNT(*) AS title_updated_count'
746
+									 ], FALSE)
747
+									 ->from('tracker_titles_history')
748
+									 ->get();
749 749
 			$stats = array_merge($stats, $queryTitlesU->result_array()[0]);
750 750
 
751 751
 			$queryUsersU = $this->db->select([
752
-			                            'COUNT(*) AS user_updated_count'
753
-			                        ], FALSE)
754
-			                        ->from('tracker_user_history')
755
-			                        ->get();
752
+										'COUNT(*) AS user_updated_count'
753
+									], FALSE)
754
+									->from('tracker_user_history')
755
+									->get();
756 756
 			$stats = array_merge($stats, $queryUsersU->result_array()[0]);
757 757
 
758 758
 			$stats['live_time'] = timespan(/*2016-09-10T03:17:19*/ 1473477439, time(), 2);
Please login to merge, or discard this patch.