59 printStdErr(
"Directory structure to Folder Asset Matrix importer\n\n");
60 printStdErr(
"Usage: import_folder_structure [system root] [folder dir] [parent id]\n");
61 printStdErr(
"system root : The Matrix System root directory\n");
62 printStdErr(
"folder dir : A filesystem directory containing the folders to be imported\n");
63 printStdErr(
"parent id : The asset ID of a Folder etc. under which the assets are to reside\n");
76 function printStdErr($string)
78 fwrite(STDERR,
"$string");
92 function formatCSVValue($string)
94 $string = trim($string);
96 if (!is_numeric($string)) {
98 $string = str_replace(
'"',
'""', $string);
99 $string =
'"'.$string.
'"';
116 function createLink(&$parent_asset, &$child_asset)
120 'asset' => &$parent_asset,
123 'sort_order' => NULL,
124 'is_dependant' => FALSE,
125 'is_exclusive' => FALSE,
128 $link_id = $child_asset->create($link);
144 function createFolder($name, &$parent_folder)
146 printStdErr(
'- Creating Folder '.$name);
152 $folder->setAttrValue(
'name', $name);
156 $link_id = createLink($parent_folder, $folder);
160 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($folder);
162 printStdErr(
' => asset ID '.$folder->id.
"\n");
164 echo formatCSVValue($name).
','.$folder->id;
173 if (ini_get(
'memory_limit') !=
'-1') ini_set(
'memory_limit',
'-1');
175 if ((php_sapi_name() !=
'cli')) {
176 trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
180 $argv = $_SERVER[
'argv'];
181 $GLOBALS[
'SYSTEM_ROOT'] = (isset($argv[1])) ? $argv[1] :
'';
182 if (empty($GLOBALS[
'SYSTEM_ROOT'])) {
184 printStdErr(
"* The Matrix system root directory must be specified as the first parameter\n\n");
188 require_once $GLOBALS[
'SYSTEM_ROOT'].
'/core/include/init.inc';
190 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'folder');
193 $filesystem_dir = $argv[2];
194 if (empty($filesystem_dir)) {
196 printStdErr(
"* A source filesystem directory must be specified as the second parameter\n\n");
199 if (!is_dir($filesystem_dir)) {
201 printStdErr(
"* The supplied filesystem directory is either not a directory or was not found\n\n");
207 $parent_id = (int)$argv[3];
208 if ($parent_id == 0) {
210 printStdErr(
"* A Parent asset ID must be specified as the third parameter\n\n");
215 printStdErr(
"** The directories on the filesystem can be renamed to the asset IDs of their respective Folder Assets.\n");
216 printStdErr(
" This will allow any files to be imported by using the import_files.php script.\n");
217 printStdErr(
" Would you like this to occur ** THIS CANNOT BE UNDONE **\n");
219 $valid_input = FALSE;
220 $valid_choices = Array(
'y',
'n');
221 while (!$valid_input) {
222 printStdErr(
' (y / n): ');
223 $user_input = rtrim(strtolower(fgets(STDIN, 1024)));
224 $valid_input = in_array($user_input, $valid_choices);
227 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
230 $parent_asset =& $GLOBALS[
'SQ_SYSTEM']->am->getAsset($parent_id);
233 if ($dh = opendir($filesystem_dir)) {
234 while (($file = readdir($dh)) !== FALSE) {
235 $is_directory = (filetype($filesystem_dir.
'/'.$file) ==
'dir');
238 if (($is_directory) && ($file !=
'.') && ($file !=
'..')) {
239 $asset_id = createFolder(trim($file), $parent_asset);
242 if ($user_input ==
'y') {
243 rename($filesystem_dir.
'/'.$file, $filesystem_dir.
'/'.$asset_id);
251 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();