Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
import_folder_structure.php
1 <?php
57 function printUsage()
58 {
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");
64 
65 }//end printUsage()
66 
67 
76 function printStdErr($string)
77 {
78  fwrite(STDERR, "$string");
79 
80 }//end printStdErr()
81 
82 
92 function formatCSVValue($string)
93 {
94  $string = trim($string);
95 
96  if (!is_numeric($string)) {
97  // Double quote double quotes
98  $string = str_replace('"', '""', $string);
99  $string = '"'.$string.'"';
100  }
101 
102  return $string;
103 
104 }//end formatCSVValue()
105 
106 
116 function createLink(&$parent_asset, &$child_asset)
117 {
118  // Link the asset to the parent asset
119  $link = Array(
120  'asset' => &$parent_asset,
121  'link_type' => 1,
122  'value' => '',
123  'sort_order' => NULL,
124  'is_dependant' => FALSE,
125  'is_exclusive' => FALSE,
126  );
127 
128  $link_id = $child_asset->create($link);
129 
130  return $link_id;
131 
132 }//end createLink()
133 
134 
144 function createFolder($name, &$parent_folder)
145 {
146  printStdErr('- Creating Folder '.$name);
147 
148  $folder = new Folder();
149  printStdErr('.');
150 
151  // Set Folder name
152  $folder->setAttrValue('name', $name);
153  printStdErr('.');
154 
155  // Link the new asset under the parent folder
156  $link_id = createLink($parent_folder, $folder);
157  printStdErr('.');
158 
159  // Free memory
160  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($folder);
161 
162  printStdErr(' => asset ID '.$folder->id."\n");
163 
164  echo formatCSVValue($name).','.$folder->id;
165 
166  return $folder->id;
167 
168 }//end createFolder()
169 
170 
171 /************************** MAIN PROGRAM ****************************/
172 
173 if (ini_get('memory_limit') != '-1') ini_set('memory_limit', '-1');
174 
175 if ((php_sapi_name() != 'cli')) {
176  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
177 }
178 
179 // Matrix system root directory
180 $argv = $_SERVER['argv'];
181 $GLOBALS['SYSTEM_ROOT'] = (isset($argv[1])) ? $argv[1] : '';
182 if (empty($GLOBALS['SYSTEM_ROOT'])) {
183  printUsage();
184  printStdErr("* The Matrix system root directory must be specified as the first parameter\n\n");
185  exit(-99);
186 }
187 
188 require_once $GLOBALS['SYSTEM_ROOT'].'/core/include/init.inc';
189 
190 $GLOBALS['SQ_SYSTEM']->am->includeAsset('folder');
191 
192 // Has a directory name been supplied?
193 $filesystem_dir = $argv[2];
194 if (empty($filesystem_dir)) {
195  printUsage();
196  printStdErr("* A source filesystem directory must be specified as the second parameter\n\n");
197  exit(-2);
198 } else {
199  if (!is_dir($filesystem_dir)) {
200  printUsage();
201  printStdErr("* The supplied filesystem directory is either not a directory or was not found\n\n");
202  exit(-3);
203  }
204 }
205 
206 // Has a parent ID been supplied?
207 $parent_id = (int)$argv[3];
208 if ($parent_id == 0) {
209  printUsage();
210  printStdErr("* A Parent asset ID must be specified as the third parameter\n\n");
211  exit(-4);
212 }
213 
214 // Prompt the user regarding directory renaming
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");
218 
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);
225 }
226 
227 $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
228 
229 // Find the parent asset
230 $parent_asset =& $GLOBALS['SQ_SYSTEM']->am->getAsset($parent_id);
231 
232 // Find all directories and create Folder assets for each
233 if ($dh = opendir($filesystem_dir)) {
234  while (($file = readdir($dh)) !== FALSE) {
235  $is_directory = (filetype($filesystem_dir.'/'.$file) == 'dir');
236 
237  // Skip reserved "." and ".." directories
238  if (($is_directory) && ($file != '.') && ($file != '..')) {
239  $asset_id = createFolder(trim($file), $parent_asset);
240 
241  // Rename filesystem directories if requested
242  if ($user_input == 'y') {
243  rename($filesystem_dir.'/'.$file, $filesystem_dir.'/'.$asset_id);
244  }
245  }
246  }
247 
248  closedir($dh);
249 }
250 
251 $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
252 
253 ?>