Elgg  Version master
MigrateEntityIconCroppingCoordinates.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Upgrades;
4 
6 
8 
12  public function getVersion(): int {
13  return 2024020101;
14  }
15 
19  public function shouldBeSkipped(): bool {
20  return empty($this->countItems());
21  }
22 
26  public function needsIncrementOffset(): bool {
27  return false;
28  }
29 
33  public function countItems(): int {
34  return elgg_count_entities($this->getOptions());
35  }
36 
40  public function run(Result $result, $offset): Result {
41  /* @var $entities \ElggBatch */
42  $entities = elgg_get_entities($this->getOptions([
43  'offset' => $offset,
44  ]));
45  /* @var $entity \ElggEntity */
46  foreach ($entities as $entity) {
47  $coords = [
48  'x1' => (int) $entity->x1,
49  'x2' => (int) $entity->x2,
50  'y1' => (int) $entity->y1,
51  'y2' => (int) $entity->y2,
52  ];
53 
54  try {
55  $entity->saveIconCoordinates($coords, 'icon');
56  } catch (\Elgg\Exceptions\ExceptionInterface $e) {
57  // something went wrong with the coords, probably broken
58  }
59 
60  unset($entity->x1);
61  unset($entity->x2);
62  unset($entity->y1);
63  unset($entity->y2);
64 
65  $result->addSuccesses();
66  }
67 
68  return $result;
69  }
70 
79  protected function getOptions(array $options = []): array {
80  $defaults = [
81  'limit' => 50,
82  'batch' => true,
83  'batch_inc_offset' => $this->needsIncrementOffset(),
84  'metadata_names' => [
85  'x1',
86  'x2',
87  'y1',
88  'y2',
89  ],
90  ];
91 
92  return array_merge($defaults, $options);
93  }
94 }
$defaults
Generic entity header upload helper.
Definition: header.php:6
if(empty($count)) $offset
Definition: pagination.php:26
addSuccesses(int $num=1)
Set an item (or items) as successfully upgraded.
Definition: Result.php:73
Generic interface which allows catching of all exceptions thrown in Elgg.
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
$entity
Definition: reset.php:8
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:518
Result of a single BatchUpgrade run.
Definition: Result.php:10
getOptions(array $options=[])
Get options for the upgrade.
System upgrades are executed synchronously at system upgrade.