Elgg  Version 7.1
ElggUpgrade.php
Go to the documentation of this file.
1 <?php
7 use Elgg\Exceptions\InvalidArgumentException as ElggInvalidArgumentException;
8 use Elgg\Exceptions\UnexpectedValueException as ElggUnexpectedValueException;
9 use Elgg\Traits\TimeUsing;
11 
26 class ElggUpgrade extends ElggObject {
27 
28  use TimeUsing;
29 
33  public function initializeAttributes() {
34  parent::initializeAttributes();
35 
37 
38  $this->attributes['subtype'] = 'elgg_upgrade';
39  $this->attributes['container_guid'] = $site->guid;
40  $this->attributes['owner_guid'] = $site->guid;
41 
42  $this->offset = 0;
43  $this->is_completed = false;
44  }
45 
49  public function getDisplayName(): string {
50  $component = $this->getComponent();
51  $version = $this->getVersion();
52 
53  return elgg_echo("{$component}:upgrade:{$version}:title");
54  }
55 
61  public function setCompleted(): void {
62  $this->setStartTime(); // to make sure a start time is present
63  $this->setCompletedTime();
64  $this->is_completed = true;
65 
66  elgg_trigger_event('complete', 'upgrade', $this);
67  }
68 
74  public function isCompleted(): bool {
75  return (bool) $this->is_completed;
76  }
77 
84  public function getID(): ?string {
85  return $this->id;
86  }
87 
93  public function getComponent(): ?string {
94  if (!isset($this->id)) {
95  return null;
96  }
97 
98  $parts = explode(':', $this->id);
99 
100  return elgg_extract(0, $parts);
101  }
102 
108  public function getVersion(): ?int {
109  if (!isset($this->id)) {
110  return null;
111  }
112 
113  $parts = explode(':', $this->id);
114 
115  return elgg_extract(1, $parts) ? (int) elgg_extract(1, $parts) : null;
116  }
117 
123  public function isAsynchronous(): bool {
124  return is_subclass_of($this->class, \Elgg\Upgrade\AsynchronousUpgrade::class);
125  }
126 
132  public function getBatch(): Batch|false {
133  try {
134  $batch = _elgg_services()->upgradeLocator->getBatch($this->class, $this);
135  } catch (ElggInvalidArgumentException $ex) {
136  // only report error if the upgrade still needs to run
137  $loglevel = $this->isCompleted() ? \Psr\Log\LogLevel::INFO : \Psr\Log\LogLevel::ERROR;
138  elgg_log($ex->getMessage(), $loglevel);
139 
140  return false;
141  }
142 
143  // check version before shouldBeSkipped() so authors can get immediate feedback on an invalid batch.
144  $version = $batch->getVersion();
145 
146  // Version must be in format yyyymmddnn
147  if (preg_match('/^[0-9]{10}$/', $version) === 0) {
148  elgg_log("Upgrade {$this->class} returned an invalid version: {$version}");
149  return false;
150  }
151 
152  return $batch;
153  }
154 
162  public function setCompletedTime(?int $time = null): void {
163  $this->completed_time = $time ?? $this->getCurrentTime()->getTimestamp();
164  }
165 
171  public function getCompletedTime(): int {
172  return (int) $this->completed_time;
173  }
174 
180  public function reset(): void {
181  $this->is_completed = false;
182  $this->offset = 0;
183 
184  unset($this->completed_time);
185  unset($this->processed);
186  unset($this->start_time);
187  }
188 
197  public function setStartTime(?int $time = null): void {
198  if (isset($this->start_time)) {
199  return;
200  }
201 
202  $this->start_time = $time ?? $this->getCurrentTime()->getTimestamp();
203  }
204 
210  public function getStartTime(): int {
211  return (int) $this->start_time;
212  }
213 }
$site
Definition: icons.php:5
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/clear'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin', 'controller'=> \Elgg\Diagnostics\DownloadController::class,], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:76
if(! $annotation instanceof ElggAnnotation) $time
Definition: time.php:20
Represents an upgrade that runs outside the upgrade.php script.
Definition: ElggUpgrade.php:26
getComponent()
Get the component where the upgrade originated ('core' or a plugin ID)
Definition: ElggUpgrade.php:93
getBatch()
Return instance of the class that processes the data.
getDisplayName()
{{Get the entity's display name.string The title or name of this entity.}}
Definition: ElggUpgrade.php:49
setCompleted()
Mark this upgrade as completed.
Definition: ElggUpgrade.php:61
initializeAttributes()
{{Initialize the attributes array.This is vital to distinguish between metadata and base parameters....
Definition: ElggUpgrade.php:33
getCompletedTime()
Gets the time when the upgrade completed.
getVersion()
Get the version of the upgrade in the format <yyymmddhh>
isCompleted()
Has this upgrade completed?
Definition: ElggUpgrade.php:74
setStartTime(?int $time=null)
Sets the timestamp for when the upgrade started.
setCompletedTime(?int $time=null)
Sets the timestamp for when the upgrade completed.
getStartTime()
Gets the time when the upgrade completed.
reset()
Resets the update in order to be able to run it again.
isAsynchronous()
Check if the upgrade should be run asynchronously.
getID()
Get the unique ID for the upgrade in the format <plugin_name>:<yyymmddhh>
Definition: ElggUpgrade.php:84
Exception thrown if an argument is not of the expected type.
Exception thrown if a value does not match with a set of values.
Long running upgrades should implement this interface.
Definition: Batch.php:10
$version
if(! $entity instanceof \ElggUpgrade) $component
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:94
_elgg_services()
Get the global service provider.
Definition: elgglib.php:347
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:246
elgg_echo(string $message_key, array $args=[], string $language='')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:99
elgg_trigger_event(string $event, string $type, $object=null)
Trigger an Elgg Event and attempt to run all handler callbacks registered to that event,...
Definition: events.php:102
try
Definition: login_as.php:33
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10