Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ALL);
- /*Подключение Drupal*/
- define('DRUPAL_ROOT', '/var/www/html/');
- require_once DRUPAL_ROOT.'includes/bootstrap.inc';
- require_once DRUPAL_ROOT.'includes/database/database.inc';
- drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
- class CVSLoader
- {
- const VID = 5;
- private $_dirName;
- private $_csvFile;
- private $_categories = [];
- public function __construct($dir)
- {
- if (!is_dir($dir))
- die('Not a dir: ' . $dir);
- $this->_dirName = $dir;
- $file = $this->_dirName . 'category_info.csv';
- if (file_exists($file)) {
- $this->_csvFile = fopen($file, 'r');
- }else{
- die('Not found: ' . $file);
- }
- /*Создание массива категорий*/
- while (($record = fgetcsv($this->_csvFile, 0, ';')) !== false) {
- array_push($this->_categories, $record);
- }
- }
- public function loadCsv()
- {
- /*Загрузка нод по категориям*/
- $count = 0;
- foreach ($this->_categories as $category) {
- $categoryFile = $category[2];
- $file = $this->_dirName . $categoryFile;
- if (!file_exists($file))
- die('Not found: ' . $file);
- /*Создание ноды, и присвоение ей текрмина таксономии*/
- //$termName = $category[1];
- $csv = fopen($file, 'r');
- while (($record = fgetcsv($csv, 0, ';')) !== false) {
- /*Создание корневого термина таксономии*/
- $termName = $category[1];
- $category_term = taxonomy_get_term_by_name($termName);
- if (empty($category_term)){
- $term = new stdClass();
- $term->vid = self::VID;
- $term->name = $termName;
- taxonomy_term_save($term);
- }
- $category_term_id = reset($category_term)->tid;
- $node = new stdClass();
- $node->type = 'torrent';
- node_object_prepare($node);
- $termName = $record[1];
- $term = taxonomy_get_term_by_name($termName);
- if (empty($term)) {
- $term = new stdClass();
- $term->vid = self::VID;
- $term->name = $termName;
- $term->parent = $category_term_id;
- taxonomy_term_save($term);
- array_push($terms, $termName);
- $node->field_category['und'][0]['tid'] = $term->tid;
- }else{
- $term_id = reset($term)->tid;
- $node->field_category['und'][0]['tid'] = $term_id;
- }
- $node->status = 1;
- $node->title = $record[4];
- //$node->field_forum_name['und'][0]['value'] = $record[1];
- $node->field_info_hash['und'][0]['value'] = $record[3];
- $node->field_size['und'][0]['value'] = $record[5];
- $node = node_submit($node);
- node_save($node);
- $count++;
- print "Loaded record: {$count}\n";
- }
- fclose($csv);
- }
- }
- public function __destruct()
- {
- fclose($this->_csvFile);
- }
- }
- $loader = new CVSLoader('/srv/20140915/');
- $loader->loadCsv();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement