Advertisement
Guest User

Untitled

a guest
Nov 20th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.49 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL);
  3.  
  4. /*Подключение Drupal*/
  5. define('DRUPAL_ROOT', '/var/www/html/');
  6. require_once DRUPAL_ROOT.'includes/bootstrap.inc';
  7. require_once DRUPAL_ROOT.'includes/database/database.inc';
  8. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  9.  
  10. class CVSLoader
  11. {
  12.     const VID = 5;
  13.    
  14.     private $_dirName;
  15.     private $_csvFile;
  16.     private $_categories = [];
  17.  
  18.     public function __construct($dir)
  19.     {
  20.         if (!is_dir($dir))
  21.             die('Not a dir: ' . $dir);
  22.         $this->_dirName = $dir;
  23.         $file = $this->_dirName . 'category_info.csv';
  24.         if (file_exists($file)) {
  25.             $this->_csvFile = fopen($file, 'r');
  26.         }else{
  27.             die('Not found: ' . $file);
  28.         }
  29.  
  30.         /*Создание массива категорий*/
  31.         while (($record = fgetcsv($this->_csvFile, 0, ';')) !== false) {
  32.             array_push($this->_categories, $record);
  33.         }
  34.     }
  35.  
  36.     public function loadCsv()
  37.     {
  38.         /*Загрузка нод по категориям*/
  39.         $count = 0;
  40.         foreach ($this->_categories as $category) {
  41.             $categoryFile = $category[2];
  42.             $file = $this->_dirName . $categoryFile;
  43.             if (!file_exists($file))
  44.                 die('Not found: ' . $file);
  45.  
  46.             /*Создание ноды, и присвоение ей текрмина таксономии*/
  47.             //$termName = $category[1];
  48.             $csv = fopen($file, 'r');
  49.             while (($record = fgetcsv($csv, 0, ';')) !== false) {
  50.                 /*Создание корневого термина таксономии*/
  51.                 $termName = $category[1];
  52.                 $category_term = taxonomy_get_term_by_name($termName);
  53.                 if (empty($category_term)){
  54.                     $term = new stdClass();
  55.                     $term->vid = self::VID;
  56.                     $term->name = $termName;
  57.                     taxonomy_term_save($term);
  58.                 }
  59.                 $category_term_id = reset($category_term)->tid;
  60.  
  61.                 $node = new stdClass();
  62.                 $node->type = 'torrent';
  63.                 node_object_prepare($node);
  64.                 $termName = $record[1];
  65.                 $term = taxonomy_get_term_by_name($termName);
  66.                 if (empty($term)) {
  67.                     $term = new stdClass();
  68.                     $term->vid = self::VID;
  69.                     $term->name = $termName;
  70.                     $term->parent = $category_term_id;
  71.                     taxonomy_term_save($term);
  72.                     array_push($terms, $termName);
  73.                     $node->field_category['und'][0]['tid'] = $term->tid;
  74.                 }else{
  75.                     $term_id = reset($term)->tid;
  76.                     $node->field_category['und'][0]['tid'] = $term_id;
  77.                 }
  78.                 $node->status = 1;
  79.                 $node->title = $record[4];
  80.                 //$node->field_forum_name['und'][0]['value'] = $record[1];
  81.                 $node->field_info_hash['und'][0]['value'] = $record[3];
  82.                 $node->field_size['und'][0]['value'] = $record[5];
  83.                 $node = node_submit($node);
  84.                 node_save($node);
  85.                 $count++;
  86.                 print "Loaded record: {$count}\n";
  87.             }
  88.             fclose($csv);
  89.         }
  90.     }
  91.  
  92.     public function __destruct()
  93.     {
  94.         fclose($this->_csvFile);
  95.     }
  96. }
  97.  
  98. $loader = new CVSLoader('/srv/20140915/');
  99. $loader->loadCsv();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement