Completed
Push — 3.1 ( d59679...4b8741 )
by Jeroen
62:38 queued 13s
created

classes/ElggPlugin/Profile/AnnotationMigration.php (1 issue)

1
<?php
2
3
namespace ElggPlugin\Profile;
4
5
use Elgg\Upgrade\AsynchronousUpgrade;
6
use Elgg\Upgrade\Result;
7
8
/**
9
 * Copy all profile field metadata to annotations, with each name prefixed with "profile:"
10
 */
11
class AnnotationMigration implements AsynchronousUpgrade {
12
13
	/**
14
	 * {@inheritdoc}
15
	 */
16 3
	public function getVersion() {
17 3
		return 2017040700;
18
	}
19
20
	/**
21
	 * {@inheritdoc}
22
	 */
23
	public function shouldBeSkipped() {
24
		return false;
25
	}
26
27
	/**
28
	 * {@inheritdoc}
29
	 */
30
	public function needsIncrementOffset() {
31
		return true;
32
	}
33
34
	/**
35
	 * {@inheritdoc}
36
	 */
37
	public function countItems() {
38
		return count($this->getFieldNames());
39
	}
40
41
	/**
42
	 * {@inheritdoc}
43
	 */
44
	public function run(Result $result, $offset) {
45
		$name = $this->getFieldNames()[$offset];
46
47
		$db = elgg()->db;
48
		$sql = "
49
			INSERT INTO {$db->prefix}annotations
50
				  (entity_guid, `name`,    `value`, value_type, owner_guid, access_id, time_created, enabled)
51
			SELECT entity_guid, :new_name, `value`, value_type, owner_guid, access_id, time_created, enabled
52
			FROM {$db->prefix}metadata
53
			WHERE `name` = :old_name
54
			AND entity_guid IN (
55
				SELECT guid FROM {$db->prefix}entities WHERE type = 'user'
56
			)
57
		";
58
59
		try {
60
			$db->updateData($sql, false, [
61
				':old_name' => $name,
62
				':new_name' => "profile:$name",
63
			]);
64
			$result->addSuccesses(1);
65
		} catch (\DatabaseException $e) {
66
			$result->addError("Profile field '$name' could not be migrated: " . $e->getMessage());
67
			$result->addFailures(1);
68
		}
69
	}
70
71
	/**
72
	 * Get the profile field names
73
	 *
74
	 * @return string[]
75
	 */
76
	private function getFieldNames() {
0 ignored issues
show
Private method name "AnnotationMigration::getFieldNames" must be prefixed with an underscore
Loading history...
77
		return array_keys(elgg_get_config('profile_fields'));
78
	}
79
}
80