24 error_reporting(E_ALL);
25 if ((php_sapi_name() !=
'cli')) {
26 trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
29 if (count($argv) < 3) {
30 echo
"Usage: php scripts/regen_metadata_by_root_nodes.php <SYSTEM_ROOT> <ASSETID[, ASSETID]> <MAX_THREAD_NUM> <BATCH_SIZE> <--skip-asset-update> <--direct-children-only> \n";
34 $SYSTEM_ROOT = (isset($_SERVER[
'argv'][1])) ? $_SERVER[
'argv'][1] :
'';
35 if (empty($SYSTEM_ROOT)) {
36 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
40 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
41 echo
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
45 $assetids = (isset($_SERVER[
'argv'][2])) ? $_SERVER[
'argv'][2] :
'';
46 if (empty($assetids)) {
47 echo
"ERROR: You need to specify the root nodes to regenerate metadata from as the second argument\n";
51 $max_thread_num = (isset($_SERVER[
'argv'][3])) ? $_SERVER[
'argv'][3] :
'';
52 if (empty($max_thread_num) || ($max_thread_num > 5)) $max_thread_num = 3;
54 $batch_size = (isset($_SERVER[
'argv'][4])) ? $_SERVER[
'argv'][4] :
'';
55 if (empty($batch_size)) $batch_size = 50;
57 $update_assets = TRUE;
58 $max_asset_depth = NULL;
59 if (isset($_SERVER[
'argv'][5])) {
60 $options = array_slice($_SERVER[
'argv'], 5);
61 foreach ($options as $option) {
62 if ($option ==
'--skip-asset-update') {
63 $update_assets = FALSE;
64 }
else if ($option ==
'--direct-children-only') {
71 define(
'LOG_FILE', $SYSTEM_ROOT.
'/data/private/logs/regen_metadata_by_root_nodes.log');
72 define(
'SYNCH_FILE', $SYSTEM_ROOT.
'/data/private/logs/regen_metadata_by_root_nodes.assetid');
73 define(
'BATCH_SIZE', $batch_size);
74 define(
'MAX_CONCURRENCY', $max_thread_num);
78 $assetids = preg_replace(
'/[\s]*/',
'', $assetids);
80 $pid_prepare = pcntl_fork();
81 switch ($pid_prepare) {
86 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
88 $root_user = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'root_user');
96 if (!$GLOBALS[
'SQ_SYSTEM']->setCurrentUser($root_user)) {
97 echo
"ERROR: Failed logging in as root user\n";
102 $rootnodes = getRootNodes($assetids);
105 foreach ($rootnodes as $rootnode_id) {
106 $children += array_merge($children, array_keys(($GLOBALS[
'SQ_SYSTEM']->am->getChildren($rootnode_id,
'', TRUE, NULL, NULL, NULL, TRUE, 1, $max_asset_depth))));
110 file_put_contents(SYNCH_FILE, implode(
',', $children));
116 pcntl_waitpid(-1, $status);
121 pcntl_waitpid(-1, $status);
126 if (file_exists(SYNCH_FILE)) {
127 $children_str = file_get_contents(SYNCH_FILE);
129 echo
"Unable to find Synch File, probably because the root user was not able to log in, or the user executing this script does not have permission to write to this folder.\n";
133 $children = explode(
',', $children_str);
134 $children = array_unique($children);
137 $chunk_children = array_chunk($children, BATCH_SIZE);
138 $current_child_list = Array();
140 log_to_file(
'======================= Start Regenerating Metadata '.date(
'd-m-Y h:i:s').
' =======================', LOG_FILE);
141 log_to_file(
"Regenerating for: " . var_export(count($children),TRUE) .
" assets \n", LOG_FILE);
144 while (!empty($chunk_children)) {
145 $current_child_list = array_pop($chunk_children);
150 trigger_error(
'Process failed to fork while regenerating metadata', E_USER_ERROR);
155 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
156 $GLOBALS[
'SQ_SYSTEM']->setCurrentUser($GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'root_user'));
158 $mm = $GLOBALS[
'SQ_SYSTEM']->getMetadataManager();
160 foreach ($current_child_list as $child_assetid) {
161 $child_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($child_assetid);
162 if (!$GLOBALS[
'SQ_SYSTEM']->am->acquireLock($child_assetid,
'metadata')) {
163 log_to_file(
'Unable to acquire metadata lock for assetid ' .$child_assetid.
'. Skipping this asset.', LOG_FILE);
167 if (!$child_asset->writeAccess(
'metadata')) {
168 log_to_file(
'Do not have write access for assetid ' .$child_assetid .
'. Skipping this asset.', LOG_FILE);
172 if (!$mm->regenerateMetadata($child_assetid, NULL, $update_assets)) {
173 log_to_file(
'Failed regenerating metadata for assetid ' .$child_assetid .
'.', LOG_FILE);
177 log_to_file(
'Regenerated Metadata for child assetid '.$child_assetid, LOG_FILE);
179 $GLOBALS[
'SQ_SYSTEM']->am->releaseLock($child_assetid,
'metadata');
180 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($child_asset);
186 $GLOBALS[
'SQ_SYSTEM']->restoreCurrentUser();
191 pcntl_waitpid(-1, $status);
196 if ($fork_num >= MAX_CONCURRENCY) {
198 pcntl_waitpid(-1, $status);
202 if (empty($chunk_children)) {
204 while ($fork_num > 0) {
206 pcntl_waitpid(-1, $status);
218 log_to_file(
'======================= Finished Regenerating Metadata '.date(
'd-m-Y h:i:s').
' =======================', LOG_FILE);
219 if (file_exists(SYNCH_FILE)) {
234 function get_line($prompt=
'')
238 return rtrim(fgets(STDIN, 4096));
247 function log_to_file($content, $file_name=
"regen_metadata_by_root_nodes.log")
249 file_put_contents($file_name,
'['.date(
'd-m-Y h:i:s').
'] '.$content.
"\n", FILE_APPEND);
254 function getRootNodes($action)
256 $rootnodes = explode(
',', $action);
259 $rootnodes_exists = $GLOBALS[
'SQ_SYSTEM']->am->assetExists($rootnodes);
261 $not_exists = array_diff($rootnodes, $rootnodes_exists);
262 if (!empty($not_exists)) {
263 $list_not_exists = implode(
', ', $not_exists);
264 echo
"These rootnode ids do not exists in the system: $list_not_exists \n";