drupal
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| drupal [2021/11/02 13:19] – ssm2017 | drupal [2022/02/07 13:29] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 129: | Line 129: | ||
| break; | break; | ||
| } | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | === Batch by drush === | ||
| + | == Sources == | ||
| + | * http:// | ||
| + | * http:// | ||
| + | == 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 ' | ||
| + | * 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> | ||
| + | ~/ | ||
| + | </ | ||
| + | <sxh php> | ||
| + | <?php | ||
| + | |||
| + | function drush_import_drush_command() { | ||
| + | $items | ||
| + | $items[' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ), | ||
| + | ); | ||
| + | return $items; | ||
| + | } | ||
| + | |||
| + | function drush_import_drush_help($section) { | ||
| + | switch ($section) { | ||
| + | case ' | ||
| + | return dt(" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | function drush_import_setup_batch($start=1, | ||
| + | // ... | ||
| + | // Populate $lots_of_data from record $start to record $stop. | ||
| + | // ... | ||
| + | | ||
| + | |||
| + | //Break up all of our data so each process does not time out. | ||
| + | $chunks = array_chunk($jos_content, | ||
| + | $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( | ||
| + | " | ||
| + | array( | ||
| + | $chunk , | ||
| + | ' | ||
| + | ) | ||
| + | ); | ||
| + | } | ||
| + | |||
| + | //put all that information into our batch array | ||
| + | $batch = array( | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ); | ||
| + | |||
| + | //Get the batch process all ready! | ||
| + | batch_set($batch); | ||
| + | $batch =& batch_get(); | ||
| + | |||
| + | //Because we are doing this on the back-end, we set progressive to false. | ||
| + | $batch[' | ||
| + | |||
| + | //Start processing the batch operations. | ||
| + | drush_backend_batch_process(); | ||
| + | } | ||
| + | |||
| + | function drush_import_some_nodes($chunk, | ||
| + | print(" | ||
| + | foreach ($chunk as $content) { | ||
| + | print(" | ||
| + | drush_import_create_node($content); | ||
| + | } | ||
| + | $context[' | ||
| + | } | ||
| + | |||
| + | function drush_import_finished_method($success, | ||
| + | //Let the user know we have finished! | ||
| + | print t(" | ||
| + | } | ||
| + | |||
| + | function drush_import_create_node($content) { | ||
| + | // title | ||
| + | // introtext | ||
| + | // fulltext | ||
| + | // state | ||
| + | // created | ||
| + | // created_by | ||
| + | // modified | ||
| + | $node = new stdClass(); | ||
| + | $node-& | ||
| + | node_object_prepare($node); | ||
| + | $node-& | ||
| + | $node-& | ||
| + | $node-& | ||
| + | $node-& | ||
| + | $node-& | ||
| + | $node-& | ||
| + | $node-& | ||
| + | $node-& | ||
| + | $node-& | ||
| + | $node-& | ||
| + | node_save($node); | ||
| } | } | ||
drupal.1635855551.txt.gz · Last modified: (external edit)
