/[drupal]/contributions/modules/node_export/node_export.drush.inc
ViewVC logotype

Diff of /contributions/modules/node_export/node_export.drush.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1.2.1, Sat Oct 31 06:12:23 2009 UTC revision 1.1.2.2, Sat Oct 31 06:15:47 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id$  // $Id$
3    
4  /**  /**
5   * @file   * @file
6   *   Drush support for node_export.   *   Drush support for node_export.
7   */   */
8    
9  /**  /**
10   * Implementation of hook_drush_command().   * Implementation of hook_drush_command().
11   */   */
12  function node_export_drush_command() {  function node_export_drush_command() {
13    $items = array();    $items = array();
14    
15    $items['node_export'] = array(    $items['node_export'] = array(
16      'callback' => 'node_export_drush_callback_export',      'callback' => 'node_export_drush_callback_export',
17      'description' => "Export nodes.",      'description' => "Export nodes.",
18      'arguments' => array(      'arguments' => array(
19        'node ids' => 'A list of node ids to export.',        'node ids' => 'A list of node ids to export.',
20      ),      ),
21      'options' => array(      'options' => array(
22        '--file' => "A filename to write the node data to.",        '--file' => "A filename to write the node data to.",
23      ),      ),
24      'examples' => array(      'examples' => array(
25        'drush node_export 45 46 47 --file=filename' =>        'drush node_export 45 46 47 --file=filename' =>
26          'Export nodes 45, 46, and 47 to given filename.',          'Export nodes 45, 46, and 47 to given filename.',
27      ),      ),
28        'drush node_export 45 46 47 > filename' =>      'drush node_export 45 46 47 > filename' =>
29          'Export nodes 45, 46, and 47 to given filename. (Runs the risk of error messages going to that file.)',        'Export nodes 45, 46, and 47 to given filename. (Runs the risk of error messages going to that file.)',
30      ),    );
31    );    $items['node_export import'] = array(
32    $items['node_export import'] = array(      'callback' => 'node_export_drush_callback_import',
33      'callback' => 'node_export_drush_callback_import',      'description' => "Import node code from a previous export.",
34      'description' => "Import node code from a previous export.",      'options' => array(
35      'options' => array(        '--uid' => "Uid of user to save nodes as. If not given will use 1. You may specify 0 for the Anonymous user.",
36        '--uid' => "Uid of user to save nodes as. If not given will use 1. You may specify 0 for the Anonymous user.",      ),
37      ),      'examples' => array(
38      'examples' => array(        'drush node_export import < filename' =>
39        'drush node_export import < filename' =>          'Import nodes from given filename.',
40          'Import nodes from given filename.',        'drush node_export import --uid=2 < filename' =>
41        'drush node_export import --uid=2 < filename' =>          'Import nodes from given filename, saving them with uid 2 as author.',
42          'Import nodes from given filename, saving them with uid 2 as author.',      ),
43      ),    );
44    );  
45      return $items;
46    return $items;  }
47  }  
48    /**
49  /**   * Implementation of hook_drush_help().
50   * Implementation of hook_drush_help().   *
51   *   * This function is called whenever a drush user calls
52   * This function is called whenever a drush user calls   * 'drush help <name-of-your-command>'
53   * 'drush help <name-of-your-command>'   *
54   *   * @param
55   * @param   *   A string with the help section (prepend with 'drush:')
56   *   A string with the help section (prepend with 'drush:')   *
57   *   * @return
58   * @return   *   A string with the help text for your command.
59   *   A string with the help text for your command.   */
60   */  function node_export_drush_help($section) {
61  function node_export_drush_help($section) {    switch ($section) {
62    switch ($section) {      case 'drush:node_export':
63      case 'drush:node_export':        return dt("Exports nodes. Usage: 'drush node_export > filename'.");
64        return dt("Exports nodes. Usage: 'drush node_export > filename'.");      case 'drush:node_export import':
65      case 'drush:node_export import':        return dt("Imports nodes that have been exported. Usage: 'drush node_export import < filename'.");
66        return dt("Imports nodes that have been exported. Usage: 'drush node_export import < filename'.");    }
67    }  }
68  }  
69    /**
70  /**   * Drush command callback.
71   * Drush command callback.   *
72   *   * Export nodes.
73   * Export nodes.   */
74   */  function node_export_drush_callback_export() {
75  function node_export_drush_callback_export() {    $commands = func_get_args();
76    $commands = func_get_args();  
77      $nids = array_filter($commands, 'is_numeric');
78    $nids = array_filter($commands, 'is_numeric');  
79      $data = node_export_node_bulk($nids, TRUE);
80    $data = node_export_node_bulk($nids, TRUE);  
81      $filename = drush_get_option('file');
82    $filename = drush_get_option('file');  
83      if ($filename) {
84    if ($filename) {      // Output data to file. Note this only takes a flat filename for the current directory.
85      // Output data to file. Note this only takes a flat filename for the current directory.      // If file exists, ask for whether to overwrite
86      // If file exists, ask for whether to overwrite      if (file_exists($filename)) {
87      if (file_exists($filename)) {        if (!drush_confirm(dt('File ' . $filename . ' exists. Do you really want to overwrite?'))) {
88        if (!drush_confirm(dt('File ' . $filename . ' exists. Do you really want to overwrite?'))) {          return;
89          return;        }
90        }      }
91      }      // Write the file.
92      // Write the file.      file_put_contents($filename, $data);
93      file_put_contents($filename, $data);    }
94    }    else {
95    else {      // Print to terminal.
96      // Print to terminal.      drush_print_r($data);
97      drush_print_r($data);    }
98    }  }
99  }  
100    /**
101  /**   * Drush command callback.
102   * Drush command callback.   *
103   *   * Import nodes from data.
104   * Import nodes from data.   */
105   */  function node_export_drush_callback_import() {
106  function node_export_drush_callback_import() {    $commands = func_get_args();
107    $commands = func_get_args();  
108      // Switch to admin or the specified user so imported nodes are not anonymous.
109    // Switch to admin or the specified user so imported nodes are not anonymous.    $uid = drush_get_option('uid');
110    $uid = drush_get_option('uid');    // Test on NULL so uid may be given as 0.
111    // Test on NULL so uid may be given as 0.    if (is_null($uid)) {
112    if (is_null($uid)) {      $uid = 1;
113      $uid = 1;    }
114    }    // uid 0 is already loaded.
115    // uid 0 is already loaded.    if ($uid != 0) {
116    if ($uid != 0) {      global $user;
117      global $user;      $user = user_load($uid);
118      $user = user_load($uid);    }
119    }  
120      $node_code = file_get_contents("php://stdin", "r");
121    $node_code = file_get_contents("php://stdin", "r");  
122      if (!empty($node_code)) {
123    if (!empty($node_code)) {      $result = node_export_import($node_code, 'save-edit', FALSE, 'drush_print', 'dt');
124      $result = node_export_import($node_code, 'save-edit', FALSE, 'drush_print', 'dt');      if ($result === FALSE) {
125      if ($result === FALSE) {        // There was a problem with types?
126        // There was a problem with types?        drush_set_error('DRUSH_NOT_COMPLETED', 'Problem found with the import file. Check the node types exist.');
127        drush_set_error('DRUSH_NOT_COMPLETED', 'Problem found with the import file. Check the node types exist.');      }
128      }    }
129    }  
130    }
 }  

Legend:
Removed from v.1.1.2.1  
changed lines
  Added in v.1.1.2.2

  ViewVC Help
Powered by ViewVC 1.1.2