Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
import_files.php
1 <?php
62 error_reporting(E_ALL);
63 if (ini_get('memory_limit') != '-1') ini_set('memory_limit', '-1');
64 
65 if ((php_sapi_name() != 'cli')) {
66  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
67 }
68 
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";
72  exit();
73 }
74 
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";
77  exit();
78 }
79 
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";
83  exit();
84 }
85 
86 $SORTING = FALSE;
87 $matrix_root_assetid = 0;
88 $allow_unrestricted_access = FALSE;
89 
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') {
93  $SORTING = TRUE;
94 }
95 
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') {
99  $SORTING = TRUE;
100 }
101 
102 if (isset($_SERVER['argv'][5]) && $_SERVER['argv'][5] == '--sort') {
103  $SORTING = TRUE;
104 } else if (isset($_SERVER['argv'][5]) && $_SERVER['argv'][5] != '--sort') {
105  echo "ERROR: Fifth argument passed can only be '--sort'";
106  exit();
107 }
108 
109 require_once $SYSTEM_ROOT.'/core/include/init.inc';
110 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
111 
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');
120 
121 $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
122 
123 
133 function createFolder($folder_destination_id, $new_folder_name)
134 {
135  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
136  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
137 
138  $GLOBALS['SQ_SYSTEM']->am->includeAsset('folder');
139 
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);
143  }
144 
145  $folder = new Folder();
146  $folder->setAttrValue('name', $new_folder_name);
147 
148  $folder_link = Array('asset' => &$folder_destination, 'link_type' => SQ_LINK_TYPE_2, '', 'is_dependant' => 0, 'is_exclusive' => 0);
149 
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);
154  } else {
155  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
156  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
157  }
158 
159  return $folder->id;
160 
161 }//end createFolder()
162 
163 
175 function getMatrixFolderId($folder_path, &$matrix_ids)
176 {
177  if (empty($matrix_ids[$folder_path])) {
178  $folder_to_create = Array();
179  array_push($folder_to_create, $folder_path);
180 
181  while (!empty($folder_to_create)) {
182  // take current folder and get its parent
183  // if the parent folder has a matrix id then create the folder
184  // if the parent folder doesnt add the parent folder to the $folder_to_create list
185  $current_folder = array_pop($folder_to_create);
186 
187  // if the current folder is empty means that we went too far
188  if (empty($current_folder)) {
189  trigger_error("We could not found the asset id for the folder $folder_path", E_USER_ERROR);
190  }
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));
194  } else {
195  array_push($folder_to_create, $parent_folder);
196  }
197  }
198  }
199  return $matrix_ids[$folder_path];
200 
201 }//end getMatrixFolderId()
202 
203 
204 // hash table that contains the name of the folder and its corresponding matrix asset id
205 // if the matrix asset id is empty (equals to zero) means that the folder doesnt exist yet
206 $matrix_ids = Array();
207 
208 if (empty($matrix_root_assetid)) {
209  $import_dirs = list_dirs($import_home_dir, FALSE);
210 } else {
211  $import_dirs = Array();
212 
213  // get the list of all the subfolders
214  $import_dirs = list_dirs($import_home_dir, TRUE, Array(), TRUE);
215 
216  // initialise the matrix_ids
217  foreach ($import_dirs as $value) {
218  $matrix_ids[$value] = 0;
219  }
220 
221  // add the import_home_dir to the $import_dirs array and matrix_ids
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;
224 
225 }
226 
227 foreach ($import_dirs as $import_dir) {
228 
229  $import_path = $import_home_dir.'/'.$import_dir;
230 
231  if (empty($matrix_root_assetid)) {
232  $parent_asset =& $GLOBALS['SQ_SYSTEM']->am->getAsset(trim($import_dir));
233  } else {
234  $parent_asset =& $GLOBALS['SQ_SYSTEM']->am->getAsset(getMatrixFolderId(trim($import_dir), $matrix_ids));
235  // overwrite the import path because we are using fullpath
236  $import_path = $import_dir;
237  }
238  if (is_null($parent_asset)) {
239  trigger_error("New parent asset #$parent_assetid does not exist\n", E_USER_ERROR);
240  }
241 
242  $import_link = Array('asset' => $parent_asset, 'link_type' => SQ_LINK_TYPE_1);
243 
244  // get a list of all files in the import directory
245  $files = list_files($import_path);
246  if ($SORTING) sort($files);
247 
248  foreach ($files as $filename) {
249  switch (get_file_type($filename)) {
250  case 'doc' :
251  case 'dot' :
252  $new_asset_type = 'word_doc';
253  break;
254  case 'pdf' :
255  $new_asset_type = 'pdf_file';
256  break;
257  case 'gif' :
258  case 'jpg' :
259  case 'jpeg' :
260  case 'png' :
261  $new_asset_type = 'image';
262  break;
263  case 'xls' :
264  $new_asset_type = 'excel_doc';
265  break;
266  case 'ppt' :
267  $new_asset_type = 'powerpoint_doc';
268  break;
269  case 'rtf' :
270  $new_asset_type = 'rtf_file';
271  break;
272  case 'txt' :
273  $new_asset_type = 'text_file';
274  break;
275  default :
276  $new_asset_type = 'file';
277  break;
278  }
279 
280  // create an asset under the new parent of the correct type
281  $temp_info = Array('name' => $filename, 'tmp_name' => $import_path.'/'.$filename, 'non_uploaded_file' => TRUE);
282 
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);
287 
288  if (!$new_file->create($import_link, $temp_info)) {
289  trigger_error('Failed to import '.$new_asset_type.' '.$filename, E_USER_WARNING);
290  } else {
291  echo 'New '.$new_file->type().' asset created for file '.$filename.' - asset ID #'.$new_file->id."\n";
292  }
293  }//end foreach
294 }//end foreach
295 
296 $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
297 
298 ?>