By alfish


2011-08-23 12:50:43 8 Comments

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 comments

@kiamlaluno 2011-08-23 13:09:17

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>.

@AlxVallejo 2014-06-24 20:35:19

Are PHP tags not needed?

@kiamlaluno 2014-06-25 21:28:15

As far as I recall, you don't use PHP tags in Drush scripts.

@rashidkhan 2015-03-23 00:01:05

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;

@Francisco Luz 2015-06-20 18:36:11

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.

@tanius 2015-01-05 02:25:36

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.

@Mike Gifford 2012-02-29 20:14:27

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

@Bhanu Prakash Ryaga 2014-01-22 07:36:40

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

@heshanlk 2012-10-23 13:05:17

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

Related Questions

Sponsored Content

1 Answered Questions

Run a drush script from php system()

  • 2015-04-30 09:41:03
  • Pascal Liu
  • 376 View
  • 0 Score
  • 1 Answer
  • Tags:   drush

4 Answered Questions

[SOLVED] execute drush command from devel/php drupal

  • 2014-08-23 10:59:34
  • kalidasan
  • 8627 View
  • 9 Score
  • 4 Answer
  • Tags:   7 drush debugging

1 Answered Questions

[SOLVED] How to execute a VBO using Drush

0 Answered Questions

Drush on windows xp not working, stuck with no output at all

  • 2014-10-31 14:54:26
  • SilverXXX
  • 126 View
  • 1 Score
  • 0 Answer
  • Tags:   drush

2 Answered Questions

[SOLVED] How to run drush make on remote using relative path?

  • 2015-02-04 18:59:54
  • kenorb
  • 265 View
  • 1 Score
  • 2 Answer
  • Tags:   drush

1 Answered Questions

[SOLVED] Importing existing users into a new Drupal site

  • 2015-01-07 17:10:20
  • Cameron Kilgore
  • 766 View
  • 1 Score
  • 1 Answer
  • Tags:   7 users drush

2 Answered Questions

[SOLVED] drush partially working

  • 2013-11-17 23:34:34
  • Baba
  • 3062 View
  • 1 Score
  • 2 Answer
  • Tags:   drush

1 Answered Questions

[SOLVED] How to install PHP library using Drush

  • 2014-03-05 00:06:31
  • claws
  • 1379 View
  • 0 Score
  • 1 Answer
  • Tags:   drush

2 Answered Questions

[SOLVED] Drush isn't bootstrapping drush script

  • 2013-06-20 06:16:19
  • Mark Horgan
  • 864 View
  • 3 Score
  • 2 Answer
  • Tags:   drush

1 Answered Questions

[SOLVED] How do I delete all the user's posts, using drush?

  • 2011-08-23 14:04:33
  • alfish
  • 318 View
  • 2 Score
  • 1 Answer
  • Tags:   drush comments

Sponsored Content