CloudAssetsSyncDupsTask   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 48
c 0
b 0
f 0
wmc 7
lcom 0
cbo 3
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 42 7
1
<?php
2
/**
3
 * If there are any duplicate file records (2 records pointing to the same file)
4
 * This task will sync the cloud state between them. If any of them has a live
5
 * status, all of them will.
6
 *
7
 * NOTE: this scenario shouldn't ever happen but there are lots of ways that it
8
 * CAN happen (I've had this crop up on two different projects) and when it does
9
 * it causes strange artifacts like thumbnails disappearing.
10
 *
11
 * This is a quick fix for sites with duplicate records. I'd suggest writing a
12
 * script to actually combine duplicates as a long-term solution but that would
13
 * have to be site-specific.
14
 *
15
 * @author Mark Guinn <[email protected]>
16
 * @date 02.06.2014
17
 * @package cloudassets
18
 * @subpackage tasks
19
 */
20
class CloudAssetsSyncDupsTask extends BuildTask
21
{
22
	protected $title = "Cloud Assets: Sync Duplicate File Status";
23
	protected $description = "If there are any duplicate file records (2 records pointing to the same file) this task will sync the cloud state between them. If any of them has a live status, all of them will.";
24
25
	public function run($request) {
26
		$results = DB::query(<<<SQL
27
			SELECT "Filename", count("ID") as "Num",
28
				group_concat("ID" order by "ID" separator ',') as "IDs",
29
				group_concat("CloudStatus" order by "ID" separator ',') as "CloudStatus",
30
				group_concat("CloudSize" order by "ID" separator ',') as "CloudSize",
31
				group_concat("CloudMetaJson" order by "ID" separator '\n') as "CloudMetaJson"
32
			FROM "File"
33
			GROUP BY "Filename"
34
			HAVING "Num" > 1
35
SQL
36
		);
37
38
		$fixed = 0;
39
		foreach ($results as $row) {
40
			$status = explode(',', $row['CloudStatus']);
41
			$size   = explode(',', $row['CloudSize']);
42
			$meta   = explode("\n", $row['CloudMetaJson']);
43
			$pos    = array_search('Live', $status);
44
			if ($pos !== false && (in_array('Local', $status) || in_array('Error', $status))) {
45
				$myMeta = '';
46
				foreach ($meta as $m) {
47
					if (!empty($m)) {
48
						$myMeta = $m;
49
						break;
50
					}
51
				}
52
53
				$fixed++;
54
				DB::query(sprintf(<<<SQL
55
					UPDATE "File"
56
					SET "CloudStatus" = 'Live',
57
						"CloudSize" = '%d',
58
						"CloudMetaJson" = '%s'
59
					WHERE "ID" in (%s)
60
SQL
61
				, (int)$size[$pos], Convert::raw2sql($myMeta), $row['IDs']));
62
			}
63
		}
64
65
		echo "Fixed: $fixed\n\n";
66
	}
67
}