33

I am new to Drush. How can I execute this script to remove comments of a specific user?

$uid = xx // the spam users id;
$query = db_query("SELECT cid FROM {comments} WHERE uid = %d", $uid);
while($cid = db_result($query)) {
  comment_delete($cid);
}

Also, that would be great if you could tell me how to complete the script so that it takes username instead of $uid.

Thanks

7 Answers 7

31

You can convert the script in a Drush shell script.

A drush shell script is any Unix shell script file that has its "execute" bit set (i.e., via chmod +x myscript.drush) and that begins with a specific line:

    #!/usr/bin/env drush

or

    #!/full/path/to/drush

Drush scripts are better than Bash scripts for the following reasons:

  • They are written in PHP
  • Drush can bootstrap the site before running the script

The following is the example you can find on helloword.script.

#!/usr/bin/env drush

//
// This example demonstrates how to write a drush
// "shebang" script.  These scripts start with the
// line "#!/usr/bin/env drush" or "#!/full/path/to/drush".
//
// See `drush topic docs-scripts` for more information.
//
drush_print("Hello world!");
drush_print();
drush_print("The arguments to this command were:");

//
// If called with --everything, use drush_get_arguments
// to print the commandline arguments.  Note that this
// call will include 'php-script' (the drush command)
// and the path to this script.
//
if (drush_get_option('everything')) {
  drush_print("  " . implode("\n  ", drush_get_arguments()));
}
//
// If --everything is not included, then use
// drush_shift to pull off the arguments one at
// a time.  drush_shift only returns the user
// commandline arguments, and does not include
// the drush command or the path to this script.
//
else {
  while ($arg = drush_shift()) {
    drush_print('  ' . $arg);
  }
}

drush_print();

 

You can made the script executable, so you can execute it with <script file> <parameters> where <script name> is the script name, and <parameters> are the parameters passed to the script; if the script is not executable, you call it with drush <script name> <parameters>.

2
  • Are PHP tags not needed?
    – AlxVallejo
    Commented Jun 24, 2014 at 20:35
  • As far as I recall, you don't use PHP tags in Drush scripts.
    – avpaderno
    Commented Jun 25, 2014 at 21:28
31

With drush php-eval, you can run your script without having to save it to a file first:

drush php-eval '
  $uid = 1234; 
  $query = db_query("SELECT cid FROM {comments} WHERE uid = %d", $uid);
  while($cid = db_result($query)) {
    comment_delete($cid);
  }
'

This uses nested quotation marks, so to prevent a mess I recommend to use only double quotes " within the PHP code.

16

I think you're looking at drush -d scr --uri=example.org sample_script.php to execute sample_script.php.

11

We can use drush php-script script_name to excute php file in Drush.

For help related to Drush for executing php files Type Drush php-script --help will list you the commands

Note: I have placed php scirpt in root folder of Drupal

0
6

It is simple you can run a php script by drush scr ~/sample.php.

4

On your command line, from anywhere, run:

$ drush --root=/path/to/drupal-installation --uri=youdomain.com scr /path/to/your/script.php

In case you are already at /path/to/drupal-installation just run:

$ drush --uri=youdomain.com scr /path/to/your/script.php

In case you are even further ahead at /path/to/drupal-installation/sites/youdomain.com than just run:

$ drush scr /path/to/your/script.php

your script.php file:

<?php
// Not always needed but sometimes you might have to first login as an administrator.
$admin_uid = 1;
$form_state = array('uid' => $admin_uid);
user_login_submit(array(), $form_state);

// Now the logged in user global $user object become available.
global $user;
print_r($user);

// Do whatever you want here.
0

Please note that the db_result has been removed in Drupal 7. The above code can be changed to:

$result = db_query($query);
foreach($result as $cid) {
  comment_delete($cid);
}

If you want to use username instead of the uid then you can get the username using this:

$username = user_load($uid)->name;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.