Drupal7: SAR Drush command

David G - DrupalRecently I mentioned the Search and Replace Scanner module for Drupal. It allows you to easily search and replace content from the Admin UI of your Drupal website. That’s great! But, sometimes you want to complete the same task but without a User Interface and simply perform this type of task as apart of a larger site change. So, in doing some research I stumbled across the SAR project, which is a drush Search and Replace command.

The reason I had to use this module is that a client requested the text “Computer Science” be changed, wherever it appears in a website, to “Computing”. Well on this website “Computer Science” probably appears in the following places:

  1. Node based textual content and fields found in content types such as Pages, and a custom content type representing university courses called ccs_course.
  2. There is a taxonomy tree of supported majors, every courses is associated with at least 1 major. Also News Items on the site can be associated with a major.
  3. Page URL paths likely use “computer science” as a pattern from the node-title (most likely).
  4. Courses are inserted from an external system via CSV files per quarter and processed by the Migrate module. Every course belongs to at least 1 major, now we must expect “computing” in the Migrate code and CSV file scanning process.
  5. Various views and page logic on the site is built using custom logic in custom modules, there is likely some URL scanning for “computer science”.
  6. The Workbench Moderation module is in-use on the website, it’s configured for URL based moderation — again we should check that “computer-science” is or isn’t in the list of URLs.

So off the top of my head those are allll the possible locations where “computer science” may appear in the code or within the website content (JOY!). So the SAR module will allow me to attempt to create an update script for the site content and infrastructure to move it all to “Computing”.

Example Usage of SAR:

You may install SAR like any other module using Drush:

dgurba at aw-dgurba in .../all/modules
$ drush dl sar --select
Choose one of the available releases for sar:
 [0]  :  Cancel                                                 
 [1]  :  7.x-2.x-dev  -  2015-Jun-18  -  Supported, Development 
 [2]  :  7.x-1.x-dev  -  2014-Dec-16  -  Development            
 [3]  :  7.x-1.0      -  2013-Sep-16  -  Supported, Recommended
1
Project sar (7.x-2.x-dev) downloaded to /home/dgurba/.drush/sar.

Note that while i’m in a Drupal ../sites/all/modules folder for a particular project, by default the SAR project installs to your $HOME/.drush directory. This is because this is a Drush command-set and not a Drupal module. I can see the merit of defaulting the install location to this folder — but if you want you may always pass the –destination option to the download command of Drush and for example place the SAR command-set within your project. Drush scans many locations for assets and commands, the $HOME folder is simply the most convenient place to put the folder. But your system configuration may differ from standard conventions.

Once installed take a quick look at the SAR usage options:

$ drush help sar
Replace strings in text fields in all content.

Examples:
 simple       drush sar devel.example.com www.example.com                                   
 blocks       drush sar --blocks devel.example.com www.example.com                          
 bundles      drush sar --bundles=page,article devel.example.com www.example.com            
 fields       drush sar --fields=field_name,field_other devel.example.com www.example.com   
 complex      drush sar --bundles=article --fields=field_name,field_other devel.example.com 
              www.example.com                                                               
 dry run      drush sar --dry-run devel.example.com www.example.com                         
 show ids     drush sar --show-ids devel.example.com www.example.com
            
Arguments:  
 search       Existing text.    
 replace      Replacement text.
            
Options:    
 --blocks     Only perform the search and replace on blocks.                         
 --bundles    Only perform the search and replace on these named bundles.            
 --dry-run    Only show which changes would be made, do not modify any data.         
 --fields     Only perform the search and replace on these named fields.             
 --show-ids   Show IDs of the entities that would be changed (Forces dry-run to ON).

Aliases: sar

So awesome! It will search through the Body fields of most anything (Content Types, Blocks, etc). And if needed you can supply a list of text fields to additionally search through. Another nice option this command supports in a dry-run, meaning simulate the command and show what it would do — but don’t really do it. Just an aside, Drush also supports a similar option for most commands with the –simulate flag.

So if I want to try this and change the text Computer Science to Computing wherever it is found I can do so with a dry-run — to not break everything!

drush sar --dry-run "computer science" computing
Going to fake a search & replace on all text fields in all bundles and all custom blocks on UCSB College of Creative Studies
  computer science => computing
 
Doing a dry run                                                                                                [warning]
Processing field body (page, article, book, ccs_course, faculty_simple, ccs_event, ccs_news, webform,          [ok]
distinguished_alumni, ccs_quarterly_course_directions)
  Updated 146 bundles.                                                                                         [ok]
  Updated 348 bundle revisions.                                                                                [ok]
Processing field comment_body (comment_node_page, comment_node_article, comment_node_book,                     [ok]
comment_node_ccs_course, comment_node_faculty_simple)
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_coursedb_course_number (ccs_course)                                                     [ok]
  Updated 149 bundles.                                                                                         [ok]
  Updated 149 bundle revisions.                                                                                [ok]
Processing field field_coursedb_days_and_times (ccs_course)                                                    [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_coursedb_enroll_code (ccs_course)                                                       [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_coursedb_notes (ccs_course)                                                             [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_coursedb_optional_texts (ccs_course)                                                    [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_coursedb_required_texts (ccs_course)                                                    [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_coursedb_rooms (ccs_course)                                                             [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_faculty_email (faculty_simple)                                                          [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_faculty_exit_date (faculty_simple)                                                      [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_faculty_first_name (faculty_simple, distinguished_alumni)                               [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_faculty_last_name (faculty_simple, distinguished_alumni)                                [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_faculty_office (faculty_simple)                                                         [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_faculty_office_Shours (faculty_simple)                                                   [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_faculty_other_emphasis (faculty_simple)                                                 [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_faculty_phone_number (faculty_simple)                                                   [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_faculty_webpage_url (faculty_simple)                                                    [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_slideshow_link (slideshow_entries)                                                      [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_emeritus_by_line (distinguished_alumni)                                                 [ok]
  Updated 6 bundles.                                                                                           [ok]
  Updated 7 bundle revisions.                                                                                  [ok]
Processing field field_fileSS_image_alt_text (image)                                                             [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_file_image_title_text (image)                                                           [ok]
  Updated 1 bundle.                                                                                            [ok]
  Updated 1 bundle revision.                                                                                   [ok]
Processing field field_ccs_event_location (ccs_event)                                                          [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
Processing field field_alumni_job_tltle (distinguished_alumni)                                                 [ok]
  Updated 0 bundles.                                                                                           [ok]
  Updated 0 bundle revisions.                                                                                  [ok]
  Updated 0 custom blocks.

When you’re comfortable you’ve matched all the content possible in  your search and replace run the command without the dry-run option to do all the changes permanently. NOTE: YOU SHOULD ALWAYS BACKUP YOUR DATA FIRST!!

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , | Posted under Drupal, Drush | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *