Drush support would be really cool for this module, as it would allow use of this module in setting up sites with scripts :)

I've had a go at this, and attaching my drush inc file.
I've got stuck on node_export_node_decode -- it returns either an array or an object, and when the module calls this it then checks this, and I don't really follow what is going on here.
Perhaps you can take this the rest of the way? :)

Rename the attached file so it's an .inc; it can go anywhere in the module folder, eg a drush subfolder.
The command is drush node_import, so:

drush node_import < myfile.txt
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joachim’s picture

Status: Active » Needs review

Heh. Turns out the file you give to drush can't be a single node, it must be an array of nodes.
Code as posted here works, except for the node uid which gets set to Anonymous, since this is what drush runs as. This could probably be fixed with a user_load() of uid 1, or a change in the module.

joachim’s picture

FileSize
1.65 KB

Updated version.
The drush command switches the current user to uid 1 so nodes aren't anonymous. Added help too.

danielb’s picture

Status: Needs review » Postponed (maintainer needs more info)

I don't understand what you've done, and what this is for. You really need to explain what you're talking about.

danielb’s picture

I think perhaps more discussion is required on this before you commit yourself to more coding. We need to make a plan for what is possible with this module and what options should be available. What might someone intend to do with this? How will they use it? Will they still configure via the gui? Can they export as well as import? What sort of namespaces should this module be using? Can we create drush support in a separate module?

joachim’s picture

My use case is a client who wants a new site every few months with the same set of about a dozen pages to start with -- about, history, contact us, a webform and so on.
I've been using bones modules for this, which takes care of menu structure and paths, but the content still has to be copied over.
However, with this module I can create the content quickly.
And with drush support, I can make this another step in my drush install script:

drush dl (modules)
drush enable (modules)
drush node_import < datafile

Site done! :)

To answer your other questions:
- it probably makes sense to take the gui options. Drush can take commandline switches, but your module's code would need changing to accept them, probably.
- export would be useful too I think; however I just needed import for now. I'm sure if you committed this someone would provide the export ;)
- I don't think it makes sense to put drush support in a separate module. Most modules that support drush do so by adding the drush inc file to provide drush support in parallel to the gui.

danielb’s picture

Status: Postponed (maintainer needs more info) » Active

It would be good if the command was "node_export" since that is the name of this module. There is actually a module called "node_import" and we wouldn't want a clash if they decide to use drush too.
Here is what you might need to do

- Export a single node onto the screen
- Export a single node into a file
- Export bulk nodes onto the screen
- Export bulk nodes into a file
- Import from a file
- Import from input
- Set temporary settings for a particular operation above (??)
- Set the settings of the module permanently (??)

I have never used drush so forgive me if I don't know what I'm talking about.

I am willing to restructure the functions to make this possible. But I haven't found anything more than a basic guide on creating drush command - nothing that includes adding parameters and stuff.
Given what I've said - how could the command or commands be structured to make it obvious and usable for the majority of drush users?

something like this?

node_export export 45 46 47

node_export import < nodes.txt

I have no idea what's possible, and how to add flags for the settings, etc..

joachim’s picture

Good point about the namespace.
So:

drush node_export import < file
drush node_export export 45 46 47 > file

I don't think we need to distinguish between printout to terminal and to file -- that's what command line redirection is for:

drush node_export export              // prints to the terminal
drush node_export export > file    // prints to the file

As for settings, I don't think it's customary for drush module commands to let you change settings permanently. So I'd say skip that.
For setting import options at command time, drush takes options like --path, say.

The approach I took for module_builder was to restructure the module code so that instead of the function that does all the work doing variable_get() to find out settings, it has them passed to it as parameters. This means drush can check the command line options and pass them, and the GUI-based system can pass them in when it calls the workhorse.
This may not be the most elegant method!
The other option I can think of would be drush reading the existing variables, storing them, setting them to the values given by the command line, then restoring the originals after the command has run.
This is doable, again not hugely elegant, and if we were to implement this it might as well be in core drush for all modules to take advantage of -- say as a function drush_temp_variable_set() and drush_variable_restore_all(). Which might already be in drush, must go check ;)

I'll have a look at restructuring the drush inc to change the command names and give you a stub of a node export command.

joachim’s picture

On second thoughts,

drush node_export export 45 46 47 > file

means you don't see error messages, so we should add a file option too.

joachim’s picture

FileSize
3.64 KB

Updated version of the file with changes as outlined above.
I've left the calling of the exporting code for you to do, everything is in place, just turn the $nids into $data :)
Not looked at overriding settings yet: see #610372: system for setting temporary drupal variables.

joachim’s picture

Just found there's a stray closing bracket in the 'examples' key in the first hook.

danielb’s picture

This should appear in the dev version later today, let me know if I've got anything horribly wrong because I don't use drush!

danielb’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

joachim’s picture

Just to say I used the drush import on a site and it worked perfectly :)

danielb’s picture

sweet! thanks

avibrazil@gmail.com’s picture

Just installed the -dev version and I can confirm 'drush node_export import < editedfile.txt' works.
Export through drush also works.

Loving it.

arcane’s picture

How would one export all nodes of a site? What would the command be?

bshensky’s picture

Component: Code » Node export

I am attempting to export nodes using drush 4.6-dev on Drupal 5. The node_export functions are not nominally exposed via drush. In fact, the D5 module in use is simply named "export" (not "node_export").

Despite this, I was able to leverage the "execute PHP" capability of drush to bootstrap my D5 instance enough to get an export of a node. This is what I used:

$ drush ev 'include_once(drupal_get_path("module","export")."/export.pages.inc");echo export_node_export(node_load(25788),TRUE,1)."\n";' > node.25788.inc

Perhaps this may be of use to someone who is in a similar pickle?