1<?php 2 3namespace Elgg\Pages\Upgrades; 4 5use Elgg\Upgrade\AsynchronousUpgrade; 6use Elgg\Upgrade\Result; 7use ElggObject; 8 9/** 10 * Migrate 'object', 'page_top' to 'object', 'page' 11 * also set metadata 'parent_guid' to 0 12 * 13 * @since 3.0 14 */ 15class MigratePageTop implements AsynchronousUpgrade { 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_count_entities([ 36 'type' => 'object', 37 'subtype' => 'page_top', 38 ]); 39 } 40 41 /** 42 * {@inheritDoc} 43 */ 44 public function needsIncrementOffset() { 45 return false; 46 } 47 48 /** 49 * {@inheritDoc} 50 */ 51 public function run(Result $result, $offset) { 52 53 /* @var \ElggBatch $page_tops */ 54 $page_tops = elgg_get_entities([ 55 'type' => 'object', 56 'subtype' => 'page_top', 57 'offset' => $offset, 58 'limit' => 25, 59 'batch' => true, 60 'batch_inc_offset' => $this->needsIncrementOffset(), 61 ]); 62 63 /* @var $page_top \ElggObject */ 64 foreach ($page_tops as $page_top) { 65 if ($this->migrate($page_top)) { 66 $result->addSuccesses(); 67 } else { 68 $result->addError("Error migrating page_top: {$page_top->guid}"); 69 $result->addFailures(); 70 } 71 } 72 } 73 74 /** 75 * Migrate one page_top to a page 76 * 77 * @param ElggObject $page_top the top page to migrate 78 * 79 * @return bool 80 */ 81 protected function migrate(ElggObject $page_top) { 82 83 $dbprefix = elgg_get_config('dbprefix'); 84 $query = "UPDATE {$dbprefix}entities 85 SET subtype = 'page' 86 WHERE guid = {$page_top->guid} 87 "; 88 89 if (!elgg()->db->updateData($query)) { 90 return false; 91 } 92 93 $page_top->parent_guid = 0; 94 95 return true; 96 } 97} 98