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