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