aboutsummaryrefslogtreecommitdiff
path: root/upgrades/2012022501.php
blob: 50102e87572a02718f3fa8d3973d40e348908b9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
 * Download the video thumbnail in the server and link it to video
 *
 * First determine if the upgrade is needed and then if needed, batch the update
 */

$items = elgg_get_entities(array(
	'type' => 'object',
	'subtype' => 'videolist_item',
	'limit' => 5,
	'order_by' => 'e.time_created asc',
));

// if not items, no upgrade required
if (!$items) {
	return;
}

// if all five of the items have empty thumbnails, we need to upgrade
foreach ($items as $item) {
	if ($item->thumbnail === true) {
		return;
	}
}


/**
 * Downloads the thumbnail and saves into data folder
 *
 * @param ElggObject $item
 * @return bool
 */
function videolist_2012022501($item) {

	// do not upgrade videos that have already been upgraded
	if ($item->thumbnail === true) {
		return true;
	}

	$thumbnail = file_get_contents($item->thumbnail);
	if (!$thumbnail) {
		return false;
	}
	
	$prefix = "videolist/" . $item->guid;
	$filehandler = new ElggFile();
	$filehandler->owner_guid = $item->owner_guid;
	$filehandler->setFilename($prefix . ".jpg");
	$filehandler->open("write");
	$filehandler->write($thumbnail);
	$filehandler->close();
	
	$item->thumbnail = true;
	return true;
}

$previous_access = elgg_set_ignore_access(true);
$options = array(
	'type' => 'object',
	'subtype' => 'videolist_item',
	'limit' => 0,
);
$batch = new ElggBatch('elgg_get_entities', $options, 'videolist_2012022501', 100);
elgg_set_ignore_access($previous_access);

if ($batch->callbackResult) {
	error_log("Elgg videolist upgrade (2012022501) succeeded");
} else {
	error_log("Elgg videolist upgrade (2012022501) failed");
}