Projet

Général

Profil

Paste
Télécharger (9,67 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / node_export / node_export.drush.inc @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 *   Drush support for node_export.
6
 */
7

    
8
/**
9
 * Implements hook_drush_command().
10
 */
11
function node_export_drush_command() {
12
  $items = array();
13

    
14
  $items['node-export-export'] = array(
15
    'callback' => 'drupal_node_export_callback_export',
16
    'description' => "Export nodes using Node export.",
17
    'arguments' => array(
18
      'nids' => "A list of space-separated node IDs to export.",
19
    ),
20
    'options' => array(
21
      'file' => "The filename of the output file.  If supplied, the node code will be exported to that file, otherwise it will export to stdout.",
22
      'format' => "If supplied, node code will be output using a particular export format, if available. (e.g. serialize)",
23
      'status' => "Filter for 'status'; A boolean value (0 or 1) indicating whether the node is published (visible to non-administrators).",
24
      'promote' => "Filter for 'promote'; A boolean value (0 or 1) indicating whether the node should be displayed on the front page.",
25
      'sticky' => "Filter for 'sticky'; A boolean value (0 or 1) indicating whether the node should be displayed at the top of lists in which it appears.",
26
      'translate' => "Filter for 'translate'; A boolean value (0 or 1) indicating whether the node translation needs to be updated.",
27
      'language' => "Filter for 'language'; The language code (e.g. de or en-US) of this node.",
28
      'type' => "Filter for 'type'; The machine-readable name (e.g. story or page) of the type of this node.",
29
      'sql' => "Filter by SQL (EXPERIMENTAL); An SQL query string that returns nids (e.g. \"SELECT nid FROM nodes WHERE nid < 10\").",
30
      'code' => "Filter by PHP code (EXPERIMENTAL); PHP code that prints or returns, an array or CSV string of nids (e.g. \"custom_get_my_nids();\"). Don't include PHP tags.",
31
    ),
32
    'examples' => array(
33
      'drush node-export-export 45 46 47 --file=filename' =>
34
        "export nodes with node IDs 45, 46, and 47 to the file with the supplied filename.",
35
      'drush node-export-export --type=story,page --file=filename' =>
36
        "export nodes of type story and page to the file with the supplied filename.",
37
    ),
38
  );
39
  $items['node-export-import'] = array(
40
    'callback' => 'drush_node_export_callback_import',
41
    'description' => "Import nodes previously exported with Node export.",
42
    'options' => array(
43
      'uid' => "User ID of user to save nodes as. If not given will use the user with an ID of 1. You may specify 0 for the Anonymous user.",
44
      'file' => "The filename of the input file.  If supplied, the node code will be imported from that file, otherwise it will import to stdin.",
45
    ),
46
    'examples' => array(
47
      'drush node-export-import --file=filename' =>
48
        'Import nodes from the file with the given filename.',
49
      'drush node-export-import --uid=2 --file=filename' =>
50
        "Import nodes from the file with the given filename.  The author of the nodes will be set to the user that has the user ID of 2.",
51
    ),
52
  );
53

    
54
  // Add aliases for usability.
55
  node_export_drush_command_add_alias($items, 'node-export-export', 'node-export');
56
  node_export_drush_command_add_alias($items, 'node-export-export', 'ne-export');
57
  node_export_drush_command_add_alias($items, 'node-export-import', 'ne-import');
58

    
59
  return $items;
60
}
61

    
62
/**
63
 * A function to help alias commands as other commands.
64
 */
65
function node_export_drush_command_add_alias(&$items, $command, $alias) {
66
  // Create a property on the command for adding aliases, if not there.
67
  if (!isset($items[$command]['node_export command aliases'])) {
68
    $items[$command]['node_export command aliases'] = array();
69
  }
70
  // Record the alias into that property.
71
  $items[$command]['node_export command aliases'][] = $alias;
72
  // Create the alias as a new command.
73
  $items[$alias] = $items[$command];
74
  // Indicate what this new command is an alias for.
75
  $items[$alias]['node_export alias for'] = $command;
76
}
77

    
78
/**
79
 * Implements hook_drush_help().
80
 *
81
 * This function is called whenever a drush user calls
82
 * 'drush help <name-of-your-command>'
83
 *
84
 * @param
85
 *   A string with the help section (prepend with 'drush:')
86
 *
87
 * @return
88
 *   A string with the help text for your command.
89
 */
90
function node_export_drush_help($section) {
91
  // This is to prevent duplication of information from hook_drush_command().
92
  $commands = node_export_drush_command();
93
  foreach ($commands as $command => $command_info) {
94
    if ($section == 'drush:' . $command) {
95
      $out = $command_info['description'];
96
      if (isset($command_info['node_export alias for'])) {
97
        $output .= "\nThis command is an alias for ";
98
        $output .= $command_info['node_export alias for'] . ".";
99
      }
100
      if (isset($command_info['node_export command aliases'])) {
101
        if (count($command_info['node_export command aliases']) == 1) {
102
          $output .= "\nThis command can be called by it's alias; ";
103
          $output .= $command_info['node_export command aliases'] . ".";
104
        }
105
        else {
106
          $last_alias = array_pop($command_info['node_export command aliases']);
107
          $output .= "\nThis command can be called by it's aliases; ";
108
          $output .= implode(", ", $command_info['node_export command aliases']);
109
          $output .= ", or " . $last_alias . ".";
110
        }
111
      }
112
      $out .= "\n\nArguments:";
113
      if (isset($command_info['arguments'])) {
114
        foreach ($command_info['arguments'] as $k => $v) {
115
          $out .= "\n  " . $k . " : " . $v;
116
        }
117
      }
118
      $out .= "\n\nOptions:";
119
      if (isset($command_info['options'])) {
120
        foreach ($command_info['options'] as $k => $v) {
121
          $out .= "\n  " . $k . " : " . $v;
122
        }
123
      }
124
      $out .= "\n\nExamples:";
125
      if (isset($command_info['examples'])) {
126
        foreach ($command_info['examples'] as $k => $v) {
127
          $out .= "\n  \'" . $k . "\' : " . $v;
128
        }
129
      }
130
      return dt($out);
131
    }
132
  }
133
}
134

    
135
/**
136
 * Drush command callback.
137
 *
138
 * export nodes.
139
 */
140
function drupal_node_export_callback_export() {
141

    
142
  // Set up an array of nid_filters.
143
  $nid_filters = array();
144

    
145
  // The base nids.
146
  $args = array_filter(func_get_args(), 'is_numeric');
147
  if ($args) {
148
    $nid_filters['base'] = $args;
149
  }
150

    
151
  // Filter for values in the node table (except for nids).
152
  $filters = array(
153
    'status',
154
    'promote',
155
    'sticky',
156
    'translate',
157
    'language',
158
    'type',
159
  );
160

    
161
  $query = db_select('node', 'n')->fields('n', array('nid'));
162
  $execute = FALSE;
163
  foreach ($filters as $filter) {
164
    $filter_option = drush_get_option($filter);
165
    if ($filter_option) {
166
      $query->condition($filter, explode(',', $filter_option), 'IN');
167
      // Only execute if conditions are set.
168
      $execute = TRUE;
169
    }
170
  }
171
  if ($execute) {
172
    $result = $query->execute();
173
    foreach ($result as $row) {
174
      $nid_filters['filters'][] = $row->nid;
175
    }
176
  }
177

    
178
  // Handle SQL option.
179
  $sql = drush_get_option('sql');
180
  if ($sql) {
181
    $result = db_query($sql);
182
    foreach ($result as $row) {
183
      $nid_filters['sql'][] = $row->nid;
184
    }
185
  }
186

    
187
  // Handle PHP option.
188
  $code = drush_get_option('code');
189
  if ($code) {
190
    ob_start();
191
    print eval("?><?php " . $code . " ?>");
192
    $result = ob_get_contents();
193
    ob_end_clean();
194
    if (is_array($result)) {
195
      $nid_filters['code'] = $result;
196
    }
197
    else {
198
      $nid_filters['code'] = explode(
199
        ",",
200
        str_replace(array("\n", "\r", "\t", " "), '', $result)
201
      );
202
    }
203
  }
204

    
205
  if (count($nid_filters) > 1) {
206
    // Compute the intersect of all $nid_filters if there are more than one.
207
    $nids = call_user_func_array('array_intersect', $nid_filters);
208
  }
209
  elseif (count($nid_filters) == 1) {
210
    // Use the only filter if there is only one.
211
    $nids = reset($nid_filters);
212
  }
213
  else {
214
    // Is there are no filters at all, do a query to get all nids.
215
    $result = db_select('node', 'n')->fields('n', array('nid'))->execute();
216
    $nids = array();
217
    foreach ($result as $row) {
218
      $nids[] = $row->nid;
219
    }
220
  }
221

    
222
  // Handle format option.
223
  $format = drush_get_option('format');
224

    
225
  if (empty($nids)) {
226
    drush_set_error('DRUSH_NOT_COMPLETED', "No nodes found.");
227
  }
228

    
229
  $result = node_export($nids, $format, 'dt');
230

    
231
  if ($result['success']) {
232
    $filename = drush_get_option('file');
233

    
234
    if ($filename) {
235
      // Output data to file. Note this only takes a flat filename for the current directory.
236
      // If file exists, ask for whether to overwrite.
237
      if (file_exists($filename)) {
238
        if (!drush_confirm(dt("File $filename exists. Do you really want to overwrite?"))) {
239
          return;
240
        }
241
      }
242
      // Write the file.
243
      file_put_contents($filename, $result['output']);
244
    }
245
    else {
246
      // stdout.
247
      drush_print_r($result['output']);
248
    }
249

    
250
  }
251
  else {
252
    // We have received an error message.
253
    drush_set_error('DRUSH_NOT_COMPLETED', strip_tags(implode("\n", $result['output'])));
254
  }
255

    
256
}
257

    
258
/**
259
 * Drush command callback.
260
 *
261
 * Import nodes from data.
262
 */
263
function drush_node_export_callback_import() {
264
  // Switch to site maintenance account or the specified user so imported nodes are not anonymous.
265
  $uid = drush_get_option('uid');
266
  // Test on NULL so uid may be given as 0.
267
  if (is_null($uid)) {
268
    $uid = 1;
269
  }
270
  // User 0 is already loaded.
271
  if ($uid != 0) {
272
    global $user;
273
    $user = user_load($uid);
274
  }
275

    
276
  $filename = drush_get_option('file');
277

    
278
  if ($filename) {
279
    $node_code = file_get_contents($filename, "r");
280
  }
281
  else {
282
    $node_code = file_get_contents("php://stdin", "r");
283
  }
284

    
285
  if (!empty($node_code)) {
286
    $result = node_export_import($node_code, 'dt');
287
    if (!$result['success']) {
288
      // We have received an error message.
289
      drush_set_error('DRUSH_NOT_COMPLETED', strip_tags(implode("\n", $result['output'])));
290
    }
291
    else {
292
      drush_print(strip_tags(implode("\n", $result['output'])));
293
    }
294
  }
295

    
296
}
297