Created
June 20, 2014 19:31
-
-
Save nitriques/aac750d2d37c38ca178f to your computer and use it in GitHub Desktop.
Importing data into Symphony-CMS from the command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// You run it with php-cli | |
// i.e. php-cli import.php | |
// I usually run in on the server via ssh. | |
// I also usually put my scripts into manifest (to protect them from http access) | |
// This scripts imports data from a csv... | |
// BOOTSTRAP SYMPHONY | |
define('DOCROOT', str_replace('/manifest/pays', '', rtrim(dirname(__FILE__), '\\/') )); | |
define('VERBOSE', 0); | |
define('COUNTRIES_FILE', DOCROOT . '/manifest/pays/countries.csv'); | |
define('MUTEX_KEY', 'mutex'); | |
// section id | |
define('SOURCE', 169); | |
require_once(DOCROOT . '/symphony/lib/boot/bundle.php'); | |
require_once(DOCROOT . '/symphony/lib/core/class.cacheable.php'); | |
require_once(DOCROOT . '/symphony/lib/core/class.symphony.php'); | |
require_once(DOCROOT . '/symphony/lib/core/class.administration.php'); | |
require_once(DOCROOT . '/symphony/lib/toolkit/class.general.php'); | |
require_once(TOOLKIT . '/class.sectionmanager.php'); | |
require_once(TOOLKIT . '/class.entrymanager.php'); | |
// creates the DB | |
Administration::instance(); | |
$handle = fopen(COUNTRIES_FILE, 'r'); | |
$x = 1; | |
while ( ($csv = fgetcsv($handle) ) != NULL ) { | |
if (saveEntry($csv, $x)) { | |
$x++; | |
} | |
} | |
fclose($handle); | |
echo 'Done. Inserted ' . ($x-1) . ' countries.' . PHP_EOL; | |
function saveEntry($data, $x) { | |
$entry = EntryManager::create(); | |
$entry->set('section_id', SOURCE); | |
if ($data[0] == 'CA' || $data[0] == 'US') { | |
return false; | |
} | |
$entry->setData(1262, array('value' => $data[0], 'handle' => $data[0])); | |
$entry->setData(1261, array( | |
'value-en' => $data[1], 'handle-en' => Lang::createHandle($data[1]), | |
'value-fr' => $data[2], 'handle-fr' => Lang::createHandle($data[2]), | |
'value' => $data[2], 'handle' => Lang::createHandle($data[2]) | |
)); | |
$entry->setData(1266, array('value' => $x)); | |
$res = $entry->commit(); | |
if(!$res) { | |
throw new Exception('Could not create entry'); | |
} | |
return $res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment