User Tools

Site Tools


drupal

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
drupal [2021/11/02 13:19] ssm2017drupal [2022/02/07 13:29] (current) – external edit 127.0.0.1
Line 129: Line 129:
       break;       break;
   }   }
 +}
 +
 +</sxh>
 +
 +=== Batch by drush ===
 +== Sources ==
 +  * http://www.metaltoad.com/blog/using-drupal-batch-api
 +  * http://victorquinn.com/blog/2012/08/11/programmatically-creating-fielded-d7-nodes/
 +== How ? ==
 +I needed to import some content from a Joomla! website. I have exported the content using phpmyadmin and asking to get the data as a php array from the table 'jos_content' and get only the fields named :
 +  * title
 +  * introtext
 +  * fulltext
 +  * state
 +  * created
 +  * created_by
 +  * modified
 +Then, i have used a drush script to import them. It tooks about 30 seconds for 280 nodes. The exported file was 1.2MB
 +== Code ==
 +<sxh bash>
 +~/.drush/bash_import.drush.inc
 +</sxh>
 +<sxh php>
 +<?php
 +
 +function drush_import_drush_command() {
 +  $items  = array();
 +  $items['drush_import'] = array(
 +    'callback'    =&gt; 'drush_import_setup_batch',
 +    'description' =&gt; dt('Import some nodes'),
 +    'arguments'   =&gt; array(
 +      'start'     =&gt; "start",
 +      'stop'      =&gt; "stop",
 +    ),
 +  );
 +  return $items;
 +}
 + 
 +function drush_import_drush_help($section) {
 +  switch ($section) {
 +    case 'drush:drush_import':
 +      return dt("blablabla.");
 +  }
 +}
 +
 +function drush_import_setup_batch($start=1, $stop=100000) {
 +  //  ...
 +  //  Populate $lots_of_data from record $start to record $stop.
 +  //  ...
 + include '/path/jos_content.php';
 +
 +  //Break up all of our data so each process does not time out.
 +  $chunks = array_chunk($jos_content, 20);
 +  $operations = array();
 +  $count_chunks = count($chunks);
 + 
 +  //for every chunk, assign some method to run on that chunk of data
 +  foreach ($chunks as $chunk) {
 +    $i++;
 +    $operations[] = array(
 +      "drush_import_some_nodes",
 +      array(
 +        $chunk ,
 +        'details'=&gt; t('(Importing chunk @chunk  of  @count)', array('@chunk '=&gt;$i, '@count'=&gt;$count_chunks))
 +      )
 +    );
 +  }
 + 
 +  //put all that information into our batch array
 +  $batch = array(
 +    'operations' =&gt; $operations,
 +    'title' =&gt; t('Import batch'),
 +    'init_message' =&gt; t('Initializing'),
 +    'error_message' =&gt; t('An error occurred'),
 +    'finished' =&gt; 'drush_import_finished_method'
 +  );
 + 
 +  //Get the batch process all ready!
 +  batch_set($batch);
 +  $batch =&amp; batch_get();
 + 
 +  //Because we are doing this on the back-end, we set progressive to false.
 +  $batch['progressive'] = FALSE;
 + 
 +  //Start processing the batch operations.
 +  drush_backend_batch_process();
 +}
 +
 +function drush_import_some_nodes($chunk, $operation_details, &amp;$context) {
 +  print("chunk size : ". count($chunk). "\n");
 +  foreach ($chunk as $content) {
 +    print("importing : ". $content['title']. "\n");
 +    drush_import_create_node($content);
 +  }
 +  $context['message'] = $operation_details; //Will show what chunk we're on.
 +}
 +
 +function drush_import_finished_method($success, $results, $operations) {
 +  //Let the user know we have finished!
 +  print t("Finished importing!\n");
 +}
 +
 +function drush_import_create_node($content) {
 +  // title
 +  // introtext
 +  // fulltext
 +  // state
 +  // created
 +  // created_by
 +  // modified
 +  $node = new stdClass();
 +  $node-&gt;type = 'blog';
 +  node_object_prepare($node);
 +  $node-&gt;uid = '1';
 +  $node-&gt;name = 'admin';
 +  $node-&gt;language = LANGUAGE_NONE;
 +  $node-&gt;title = $content['title'];
 +  $node-&gt;status = $content['state'];
 +  $node-&gt;created = strtotime($content['created']);
 +  $node-&gt;changed = strtotime($content['modified']);
 +  $node-&gt;body[LANGUAGE_NONE][0]['summary'] = $content['introtext'];
 +  $node-&gt;body[LANGUAGE_NONE][0]['value'] = $content['fulltext'];
 +  $node-&gt;body[LANGUAGE_NONE][0]['format'] = 'full_html';
 +  node_save($node);
 } }
  
drupal.1635855551.txt.gz · Last modified: (external edit)