62 error_reporting(E_ALL);
63 if (ini_get(
'memory_limit') !=
'-1') ini_set(
'memory_limit',
'-1');
65 if ((php_sapi_name() !=
'cli')) {
66 trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
69 $SYSTEM_ROOT = (isset($_SERVER[
'argv'][1])) ? $_SERVER[
'argv'][1] :
'';
70 if (empty($SYSTEM_ROOT)) {
71 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
75 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
76 echo
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
80 $import_home_dir = (isset($_SERVER[
'argv'][2])) ? $_SERVER[
'argv'][2] :
'';
81 if (empty($import_home_dir) || !is_dir($import_home_dir)) {
82 echo
"ERROR: You need to supply the path to the import directory as the second argument\n";
87 $matrix_root_assetid = 0;
88 $allow_unrestricted_access = FALSE;
90 if (isset($_SERVER[
'argv'][3]) && $_SERVER[
'argv'][3] !=
'--sort') {
91 $matrix_root_assetid = $_SERVER[
'argv'][3];
92 }
else if (isset($_SERVER[
'argv'][3]) && $_SERVER[
'argv'][3] ==
'--sort') {
96 if (isset($_SERVER[
'argv'][4]) && ($_SERVER[
'argv'][4] !=
'--sort') && ($_SERVER[
'argv'][4] == 1)) {
97 $allow_unrestricted_access = TRUE;
98 }
else if (isset($_SERVER[
'argv'][4]) && $_SERVER[
'argv'][4] ==
'--sort') {
102 if (isset($_SERVER[
'argv'][5]) && $_SERVER[
'argv'][5] ==
'--sort') {
104 }
else if (isset($_SERVER[
'argv'][5]) && $_SERVER[
'argv'][5] !=
'--sort') {
105 echo
"ERROR: Fifth argument passed can only be '--sort'";
109 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
110 require_once SQ_FUDGE_PATH.
'/general/file_system.inc';
112 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'file');
113 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'image');
114 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'pdf_file');
115 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'word_doc');
116 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'excel_doc');
117 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'powerpoint_doc');
118 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'rtf_file');
119 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'text_file');
121 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
133 function createFolder($folder_destination_id, $new_folder_name)
135 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
136 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
138 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'folder');
140 $folder_destination =& $GLOBALS[
'SQ_SYSTEM']->am->getAsset($folder_destination_id);
141 if (empty($folder_destination)) {
142 trigger_error(
"could not get the asset $folder_destination_id\n", E_USER_ERROR);
146 $folder->setAttrValue(
'name', $new_folder_name);
148 $folder_link = Array(
'asset' => &$folder_destination,
'link_type' => SQ_LINK_TYPE_2,
'',
'is_dependant' => 0,
'is_exclusive' => 0);
150 if (!$folder->create($folder_link)) {
151 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
152 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
153 trigger_error(
"could not create the folder $new_folder_name under the asset $folder_destination_id\n", E_USER_ERROR);
155 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
156 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
175 function getMatrixFolderId($folder_path, &$matrix_ids)
177 if (empty($matrix_ids[$folder_path])) {
178 $folder_to_create = Array();
179 array_push($folder_to_create, $folder_path);
181 while (!empty($folder_to_create)) {
185 $current_folder = array_pop($folder_to_create);
188 if (empty($current_folder)) {
189 trigger_error(
"We could not found the asset id for the folder $folder_path", E_USER_ERROR);
191 $parent_folder = dirname($current_folder);
192 if (!empty($matrix_ids[$parent_folder])) {
193 $matrix_ids[$current_folder] = createFolder($matrix_ids[$parent_folder], basename($current_folder));
195 array_push($folder_to_create, $parent_folder);
199 return $matrix_ids[$folder_path];
206 $matrix_ids = Array();
208 if (empty($matrix_root_assetid)) {
209 $import_dirs = list_dirs($import_home_dir, FALSE);
211 $import_dirs = Array();
214 $import_dirs = list_dirs($import_home_dir, TRUE, Array(), TRUE);
217 foreach ($import_dirs as $value) {
218 $matrix_ids[$value] = 0;
222 $import_dirs = array_merge(Array($import_home_dir), list_dirs($import_home_dir, TRUE, Array(), TRUE));
223 $matrix_ids[$import_home_dir] = $matrix_root_assetid;
227 foreach ($import_dirs as $import_dir) {
229 $import_path = $import_home_dir.
'/'.$import_dir;
231 if (empty($matrix_root_assetid)) {
232 $parent_asset =& $GLOBALS[
'SQ_SYSTEM']->am->getAsset(trim($import_dir));
234 $parent_asset =& $GLOBALS[
'SQ_SYSTEM']->am->getAsset(getMatrixFolderId(trim($import_dir), $matrix_ids));
236 $import_path = $import_dir;
238 if (is_null($parent_asset)) {
239 trigger_error(
"New parent asset #$parent_assetid does not exist\n", E_USER_ERROR);
242 $import_link = Array(
'asset' => $parent_asset,
'link_type' => SQ_LINK_TYPE_1);
245 $files = list_files($import_path);
246 if ($SORTING) sort($files);
248 foreach ($files as $filename) {
249 switch (get_file_type($filename)) {
252 $new_asset_type =
'word_doc';
255 $new_asset_type =
'pdf_file';
261 $new_asset_type =
'image';
264 $new_asset_type =
'excel_doc';
267 $new_asset_type =
'powerpoint_doc';
270 $new_asset_type =
'rtf_file';
273 $new_asset_type =
'text_file';
276 $new_asset_type =
'file';
281 $temp_info = Array(
'name' => $filename,
'tmp_name' => $import_path.
'/'.$filename,
'non_uploaded_file' => TRUE);
283 $new_file =
new $new_asset_type();
284 $new_file->_tmp[
'uploading_file'] = TRUE;
285 $new_file->setAttrValue(
'name', $filename);
286 $new_file->setAttrValue(
'allow_unrestricted', $allow_unrestricted_access);
288 if (!$new_file->create($import_link, $temp_info)) {
289 trigger_error(
'Failed to import '.$new_asset_type.
' '.$filename, E_USER_WARNING);
291 echo
'New '.$new_file->type().
' asset created for file '.$filename.
' - asset ID #'.$new_file->id.
"\n";
296 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();