Elgg  Version 1.11
Helper2013022000.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Upgrades;
3 
10  const RELATIONSHIP_SUCCESS = '2013022000';
11  const RELATIONSHIP_FAILURE = '2013022000_fail';
12 
16  protected $siteGuid;
17 
21  protected $dbPrefix;
22 
27  public function __construct($siteGuid, $dbPrefix) {
28  $this->siteGuid = $siteGuid;
29  $this->dbPrefix = $dbPrefix;
30  }
31 
37  public function getBatchOptions() {
38  $relationship1 = sanitise_string(self::RELATIONSHIP_SUCCESS);
39  $relationship2 = sanitise_string(self::RELATIONSHIP_FAILURE);
40  // find users without either relationship
41  return array(
42  'type' => 'user',
43  'callback' => '',
44  'order_by' => 'e.guid',
45  'joins' => array(
46  "LEFT JOIN {$this->dbPrefix}entity_relationships er1
47  ON (e.guid = er1.guid_one
48  AND er1.guid_two = {$this->siteGuid}
49  AND er1.relationship = '$relationship1')
50  ",
51  "LEFT JOIN {$this->dbPrefix}entity_relationships er2
52  ON (e.guid = er2.guid_one
53  AND er2.guid_two = {$this->siteGuid}
54  AND er2.relationship = '$relationship2')
55  ",
56  ),
57  'wheres' => array("er1.guid_one IS NULL AND er2.guid_one IS NULL"),
58  'limit' => false,
59  );
60  }
61 
67  public function countUnmigratedUsers() {
68  $opts = $this->getBatchOptions();
69  $opts['count'] = true;
70  return elgg_get_entities($opts);
71  }
72 
79  public function makeMatrix($user_row) {
80  $time_created = date('Y/m/d', $user_row->time_created);
81  return "$time_created/$user_row->guid/";
82  }
83 
90  public function removeDirIfEmpty($dir) {
91  $files = scandir($dir);
92 
93  foreach ($files as $file) {
94  if ($file == '..' || $file == '.') {
95  continue;
96  }
97 
98  // not empty.
99  if (is_file("$dir/$file")) {
100  return false;
101  }
102 
103  // subdir not empty
104  if (is_dir("$dir/$file") && !$this->removeDirIfEmpty("$dir/$file")) {
105  return false;
106  }
107  }
108 
109  // only contains empty subdirs
110  return rmdir($dir);
111  }
112 
119  public function getLowerBucketBound($guid) {
121  if ($guid < 1) {
122  return false;
123  }
124  return (int) max(floor($guid / $bucket_size) * $bucket_size, 1);
125  }
126 
132  public function markSuccess($guid) {
133  add_entity_relationship($guid, self::RELATIONSHIP_SUCCESS, $this->siteGuid);
134  }
135 
141  public function markFailure($guid) {
142  add_entity_relationship($guid, self::RELATIONSHIP_FAILURE, $this->siteGuid);
143  }
144 
148  public function forgetFailures() {
149  $relationship = sanitise_string(self::RELATIONSHIP_FAILURE);
150  _elgg_services()->db->updateData("
151  DELETE FROM {$this->dbPrefix}entity_relationships
152  WHERE relationship = '$relationship'
153  AND guid_two = {$this->siteGuid}
154  ");
155  }
156 
160  public function forgetSuccesses() {
161  $relationship = sanitise_string(self::RELATIONSHIP_SUCCESS);
162  _elgg_services()->db->updateData("
163  DELETE FROM {$this->dbPrefix}entity_relationships
164  WHERE relationship = '$relationship'
165  AND guid_two = {$this->siteGuid}
166  ");
167  }
168 
174  public function hasFailures() {
175  $relationship = sanitise_string(self::RELATIONSHIP_FAILURE);
176  $sql = "
177  SELECT COUNT(*) AS cnt FROM {$this->dbPrefix}entity_relationships
178  WHERE relationship = '$relationship'
179  AND guid_two = {$this->siteGuid}
180  ";
181  $row = _elgg_services()->db->getDataRow($sql);
182  return ($row->cnt > 0);
183  }
184 }
185 
add_entity_relationship($guid_one, $relationship, $guid_two)
Create a relationship between two entities.
$files
Definition: crop.php:36
$guid
Removes an admin notice.
markSuccess($guid)
Mark the user as a successful data migration.
forgetSuccesses()
Remove the records for successful migrations.
removeDirIfEmpty($dir)
Remove directory if all users moved out of it.
hasFailures()
Are there any failures on record?
_elgg_services()
Definition: autoloader.php:14
sanitise_string($string)
Wrapper function for alternate English spelling (.
Definition: database.php:150
$time_created
Definition: online.php:16
elgg_get_entities(array $options=array())
Returns an array of entities with optional filtering.
Definition: entities.php:490
countUnmigratedUsers()
Get number of users who need data migration.
const BUCKET_SIZE
Number of entries per matrix dir.
getLowerBucketBound($guid)
Get the base directory name as int.
getBatchOptions()
Get elgg_get_entities() options for fetching users who need data migration.
Helper for data directory upgrade.
markFailure($guid)
Mark the user as having failed data migration.
makeMatrix($user_row)
Get the old directory location.
$row
forgetFailures()
Remove the records for failed migrations.
__construct($siteGuid, $dbPrefix)