Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
replace_csv_data_source_file.php
1 <?php
2 error_reporting(E_ALL);
3 if ((php_sapi_name() != 'cli')) {
4  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
5 }
6 
7 function print_usage() {
8  echo $_SERVER['argv'][0]." - Import a CSV File to a csv data source";
9  echo "Usage:\n";
10  echo "\n";
11  echo $_SERVER['argv'][0]." <SYSTEM_ROOT> <ASSETID> <CSV_PATH> <FORCE_UPDATE_TIME>\n";
12  echo "\n";
13  echo "SYSTEM_ROOT: Path to the Squiz Matrix system.\n";
14  echo "ASSETID: Assetid of a CSV Data Source asset.\n";
15  echo "CSV_PATH: Path to the CSV file to import\n";
16  echo "FORCE_UPDATE_TIME: Forcibly refresh the asset's updated time, otherwise asset will not refresh updated time for setting same content.(y/n)\n";
17 }
18 
19 
20 
21 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
22 if (empty($SYSTEM_ROOT)) {
23  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
24  print_usage();
25  exit();
26 }
27 
28 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
29  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
30  print_usage();
31  exit();
32 }
33 
34 require_once $SYSTEM_ROOT.'/core/include/init.inc';
35 
36 $GLOBALS['SQ_SYSTEM']->setCurrentUser($GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user'));
37 
38 $assetid = (isset($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : 0;
39 $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, 'data_source_csv');
40 if (empty($asset)) {
41  echo "You need to supply a CSV Data Source assetid as the second argument\n";
42  print_usage();
43  exit();
44 }
45 
46 if ($_SERVER['argc'] < 4) {
47  echo "You need to supply the path to CSV file as the third argument\n";
48  print_usage();
49  exit();
50 }
51 $filepath = $_SERVER['argv'][3];
52 require_once SQ_FUDGE_PATH.'/csv/csv.inc';
53 
54 $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
55 
56 $csv = new CSV($filepath);
57 $csv->import();
58 
59 $asset->setAttrValue('cached_content', $csv->values);
60 
61 // overwrite the cache with the new content
62 $asset->setResultSet(Array(), $asset->name);
63 $asset->getResultSet($asset->name);
64 
65 $asset->saveAttributes();
66 
67 // set last update time if forcibly required
68 $FORCE_UPDATE = (isset($_SERVER['argv'][4])) ? $_SERVER['argv'][4] : 'n';
69 if(strtolower($FORCE_UPDATE) == 'y') {
70  $asset->_updated();
71 }
72 
73 $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
74 ?>
75