26 if (ini_get(
'memory_limit') !=
'-1') ini_set(
'memory_limit',
'-1');
27 error_reporting(E_ALL);
28 $metadata_schemas = Array();
30 if (php_sapi_name() !=
'cli') {
31 trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
35 $SYSTEM_ROOT = (isset($_SERVER[
'argv'][1])) ? $_SERVER[
'argv'][1] :
'';
36 if (empty($SYSTEM_ROOT)) {
37 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
38 echo
'Usage: '.basename($_SERVER[
'argv'][0]).
" <system root> [schema ID]...\n";
42 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
43 echo
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
44 echo
'Usage: '.basename($_SERVER[
'argv'][0]).
" <system root> [schema ID]...\n";
48 define(
'SQ_SYSTEM_ROOT', realpath($SYSTEM_ROOT));
49 define(
'BATCH_SIZE', 50);
50 define(
'MAX_CONCURRENCY', 3);
51 define(
'SYNCH_FILE', $SYSTEM_ROOT.
'/data/private/logs/regen_metadata_by_schema.assetid');
54 $pid_prepare = pcntl_fork();
55 switch ($pid_prepare) {
60 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
67 if (isset($_SERVER[
'argv'][2])) {
68 $metadata_schemas = $_SERVER[
'argv'];
71 array_shift($metadata_schemas);
72 array_shift($metadata_schemas);
76 $metadata_schemas = $GLOBALS[
'SQ_SYSTEM']->am->getTypeAssetids(
'metadata_schema', TRUE);
80 $mm = $GLOBALS[
'SQ_SYSTEM']->getMetadataManager();
81 $total_assets = Array();
82 foreach ($metadata_schemas as $schemaid) {
83 $schema = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($schemaid);
85 trigger_error(
'Schema asset #'.$schemaid.
' could not be found', E_USER_WARNING);
89 trigger_error(
'Asset #'.$schema->id.
' is not a metadata schema', E_USER_WARNING);
92 $total_assets = array_merge($total_assets, $mm->getSchemaAssets($schemaid, TRUE));
96 file_put_contents(SYNCH_FILE, implode(
',', $total_assets));
102 pcntl_waitpid(-1, $status);
107 pcntl_waitpid(-1, $status);
113 $total_assets = Array();
114 if (file_exists(SYNCH_FILE)) {
115 $total_assets_str = file_get_contents(SYNCH_FILE);
117 trigger_error (
"Unable to find Synch File, probably because the user executing this script does not have permission to write to this folder.", E_USER_WARNING);
121 $total_assets = explode(
',', $total_assets_str);
122 $total_assets = array_unique($total_assets);
124 $chunk_assets = array_chunk($total_assets, BATCH_SIZE);
125 $current_asset_list = Array();
128 printUpdateStatus (
"Total ".count($total_assets).
" found");
130 while (!empty($chunk_assets)) {
134 $current_asset_list = array_pop($chunk_assets);
139 trigger_error(
'Process failed to fork while regenerating metadata', E_USER_ERROR);
144 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
146 $mm = $GLOBALS[
'SQ_SYSTEM']->getMetadataManager();
148 foreach ($current_asset_list as $assetid) {
149 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
150 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
151 $contextids = array_keys($GLOBALS[
'SQ_SYSTEM']->getAllContexts());
152 foreach ($contextids as $contextid) {
153 if (!$mm->regenerateMetadata($assetid, NULL)) {
154 trigger_error(
'Asset failed to regenrate metedata #'.$assetid.
'', E_USER_WARNING);
158 printUpdateStatus(
'Regenerated Metadata for assetid '.$assetid);
162 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($asset);
167 unset($current_asset_list);
171 pcntl_waitpid(-1, $status);
176 if ($fork_num >= MAX_CONCURRENCY) {
178 pcntl_waitpid(-1, $status);
182 if (empty($chunk_assets)) {
184 while ($fork_num > 0) {
186 pcntl_waitpid(-1, $status);
197 if (file_exists(SYNCH_FILE)) {
201 printUpdateStatus(
"Done");
216 function printUpdateStatus($status)
218 echo
"[ $status ]\n";