sacha chua :: living an awesome life


Subscribe!
E-mail Feed reader

Drupal shell: quickly evaluating PHP statements in a Drupal context

I often find myself needing to variable_set something temporarily, just to try things out. The drush module provides a command-line interface that solves this problem with a little more hacking. To minimize the effect on my source tree, I’ve unpacked it into the sites directory for my local installation and enabled it in my test database. After I enabled the main drush module and the related modules, I tweaked drush_tools to include an insecure-but-useful eval command. Here it is:

--- sites/local.example.com/modules/drush/drush_tools.module.orig    2008-08-05 17:18:48.000000000 -0400
+++ sites/local.example.com/modules/drush/drush_tools.module    2008-08-05 17:18:55.000000000 -0400
@@ -46,6 +46,10 @@
     'callback' => 'drush_tools_sync',
     'description' => 'Rsync the Drupal tree to/from another server using ssh'
   );
+  $items['eval'] = array(
+    'callback' => 'drush_tools_eval',
+    'description' => 'Evaluate a command',
+  );
   return $items;
 }

@@ -156,3 +160,6 @@
   }
 }

+function drush_tools_eval($command) {
+  eval($command);
+}

I also added an alias to my ~/.bashrc along the lines of:

alias drush='php ~/drupal/sites/local.example.com/modules/drush/drush.php -r ~/drupal -l http://local.example.com'

where ~/drupal is my multisite Drupal directory root.

After I loaded the alias with “source ~/.bashrc”, I can now execute PHP statements in my Drupal context with commands like:

drush eval "variable_set('hello', 'world');"

Good stuff!

So-soHmmGoodGreatAwesome! (No Ratings Yet)
Loading ... Loading ...
Save to - del.icio.us - Digg it - reddit - StumbleUpon -

4 Responses to “Drupal shell: quickly evaluating PHP statements in a Drupal context”

  1. nothing insecure about it. if you can run a drush command you have command line access and thus can do much nastiness. please submit a patch for drush toolbox. this is worthy.

  2. Patch posted at http://drupal.org/node/292732

  3. Thanks for posting that! =) It took me a while to get work permission to contribute to Drupal, but I now have it – hooray!

  4. [...] written about using drush to evaluate PHP statements in the Drupal context using the command line before, and it turns out that Drush is also quite useful for running Simpletest scripts. Drush [...]

Discussion Area - Leave a Comment

Please comment as you, not your organization.





On This Day...

  • 2009: In-jokes and shared experiences — W- helped me put together some wire shelves for plants. One of the rods was deformed, so he brought out [...]
  • 2009: Weekly review: Week ending August 2, 2009 — From last week’s plans: Do some Facebook strategy consulting Find ways to recognize the contributions people had made to the idea lab [...]
  • 2007: Weekly review — This week Send thesis draft to people, really. Done. Check paperwork requirements for post-graduate work permit and start compiling them. Started Revise CASCON paper. Done. Do one [...]
  • 2007: Moving back to PCFinancial — I think I’ll move back to PCFinancial for my banking. I don’t think I’ll need any financial statements for my future [...]
  • 2005: Changing patterns of computing — I managed to leave my laptop at home today! =) That forced me to be productive all day. See how many [...]
  • 2005: On mailing lists — I find it a bit strange that people here like broadcasting their messages by putting everyone in To: or Cc:. I [...]
  • 2004: Philippine comic book blog — http://filipinocomics.blogspot.com E-Mail from Julius Oliveros
  • 2003: Emacs macros — Keyboard macros in Emacs allow me to easily generate the gallery from the pipe-separated values I exported from Gnumeric. Viva open [...]
  • 2003: RSS feed — I think it should be relatively easy to publish an RSS feed of the links from my planner wiki…
  • 2003: Plans — I’m going to try to move my plans up by a year. I suspect I’ll learn more by going to graduate [...]
  • 2003: Experts — I think I’ve a way to deal with my large CS123 class: topic experts. Each group has experts in charge of [...]
  • 2003: Teaching — Holding up. Doing remarkably well, in fact – probably because I’m too busy to do anything else. The sheer magnitude of [...]
  • 2003: Communication — Considering that I tend to text long messages in bursts, it might actually make more sense for me to stay at [...]
  • 2003: Redesign — Using objects in the beginning - graphics right away; bounds and draw promotes object reuse - using objects - implementing methods - expressions - if - loops - [...]
  • 2003: http://www.geocities.com/fanny11sy/featured-projects
  • 2003: JPCS conference — end of august, one way of reviving contact with JPCS. Need a venue for a seminar for the gathering of all [...]
  • 2003: Java patterns — http://www.anupriyo.com/oopfm.shtml