Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
create_pages.php
1 <?php
28 error_reporting(E_ALL);
29 if (ini_get('memory_limit') != '-1') ini_set('memory_limit', '-1');
30 
31 if ((php_sapi_name() != 'cli')) trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
32 
33 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
34 if (empty($SYSTEM_ROOT)) {
35  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
36  exit();
37 }
38 
39 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
40  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
41  exit();
42 }
43 
44 $import_file = (isset($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : '';
45 if (empty($import_file) || !is_file($import_file)) {
46  echo "You need to supply the path to the import file as the second argument\n";
47  exit();
48 }
49 
50 require_once $SYSTEM_ROOT.'/core/include/init.inc';
51 
52 // get the import file
53 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
54 
55 $pages = file($import_file);
56 $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
57 $csv_fd = fopen($import_file, 'r');
58 $line_number = 1;
59 while (($data = fgetcsv($csv_fd, 1024, ',')) !== FALSE) {
60 
61  if (count($data) != 4) {
62  echo "Wrong number of arguments passed on line #$line_number : ".implode(', ', $data)."\n";
63  $line_number++;
64  continue;
65  }
66 
67  $GLOBALS['SQ_SYSTEM']->am->includeAsset(trim($data[1]));
68 
69  $parent_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset(trim($data[2]), '', TRUE);
70  if (is_null($parent_asset)) {
71  echo "New parent asset #{$data[2]} does not exist on line #$line_number\n";
72  $line_number++;
73  continue;
74  }
75 
76  $import_link = Array('asset' => &$parent_asset, 'link_type' => $data[3]);
77 
78  $new_asset_type = trim($data[1]);
79 
80  $new_page = new $new_asset_type();
81  $new_page->setAttrValue('name', trim($data[0]));
82 
83  if (!$new_page->create($import_link)) {
84  echo 'Failed to import '.$new_asset_type.' '.trim($data[0]);
85  $line_number++;
86  continue;
87  } else {
88  bam('New '.$new_page->type().' asset created for '.trim($data[0]).' - asset ID #'.$new_page->id);
89  }
90  $line_number++;
91 }
92 
93 $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
94 
95 ?>