Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

classes/Elgg/Pages/Upgrades/MigratePageTop.php (1 issue)

1
<?php
2
3
namespace Elgg\Pages\Upgrades;
4
5
use ElggObject;
6
use \Elgg\Upgrade\Batch;
7
use \Elgg\Upgrade\Result;
8
9
/**
10
 * Migrate 'object', 'page_top' to 'object', 'page'
11
 * also set metadata 'parent_guid' to 0
12
 *
13
 * @since 3.0
14
 */
15
class MigratePageTop implements Batch {
16
	
17
	/**
18
	 * {@inheritDoc}
19
	 */
20
	public function getVersion() {
21
		return 2017110700;
22
	}
23
	
24
	/**
25
	 * {@inheritDoc}
26
	 */
27
	public function shouldBeSkipped() {
28
		return empty($this->countItems());
29
	}
30
	
31
	/**
32
	 * {@inheritDoc}
33
	 */
34
	public function countItems() {
35
		return elgg_get_entities([
1 ignored issue
show
Bug Best Practice introduced by Jerôme Bakker
The expression return elgg_get_entities...top', 'count' => true)) also could return the type ElggEntity[]|false which is incompatible with the return type mandated by Elgg\Upgrade\Batch::countItems() of integer.
Loading history...
36
			'type' => 'object',
37
			'subtype' => 'page_top',
38
			'count' => true,
39
		]);
40
	}
41
	
42
	/**
43
	 * {@inheritDoc}
44
	 */
45
	public function needsIncrementOffset() {
46
		return false;
47
	}
48
	
49
	/**
50
	 * {@inheritDoc}
51
	 */
52
	public function run(Result $result, $offset) {
53
		
54
		/* @var \ElggBatch $page_tops */
55
		$page_tops = elgg_get_entities([
56
			'type' => 'object',
57
			'subtype' => 'page_top',
58
			'offset' => $offset,
59
			'limit' => 25,
60
			'batch' => true,
61
			'batch_inc_offset' => $this->needsIncrementOffset(),
62
		]);
63
		
64
		/* @var $page_top \ElggObject */
65
		foreach ($page_tops as $page_top) {
66
			if ($this->migrate($page_top)) {
67
				$result->addSuccesses();
68
			} else {
69
				$result->addError("Error migrating page_top: {$page_top->guid}");
70
				$result->addFailures();
71
			}
72
		}
73
		
74
		return $result;
75
	}
76
	
77
	/**
78
	 * Migrate one page_top to a page
79
	 *
80
	 * @param ElggObject $page_top the top page to migrate
81
	 *
82
	 * @return bool
83
	 */
84
	protected  function migrate(ElggObject $page_top) {
85
		
86
		$dbprefix = elgg_get_config('dbprefix');
87
		$query = "UPDATE {$dbprefix}entities
88
			SET subtype = 'page'
89
			WHERE guid = {$page_top->guid}
90
		";
91
		
92
		if (!update_data($query)) {
93
			return false;
94
		}
95
		
96
		$page_top->parent_guid = 0;
97
		
98
		return true;
99
	}
100
}
101