Archive for December, 2009

Making database changes to Drupal multi-sites

by Matt Danger on Dec.21, 2009, under Uncategorized

The company I work for hosts over 150 Drupal sites in a multi-site configuration and every so often I need to publish a change to the code that requires a database change. The large number of sites we host means that performing manual changes would require too much time and carries a chance of error. I’ve come up with a simple solution.

I first create a PHP script that bootstraps Drupal. Here is an example of a script that will rebuild the menu router tables.

// Bootstrap
require_once('includes/bootstrap.inc');
require_once('includes/common.inc');
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
 
// Rebuild menus
menu_rebuild();
print 'Menu rebuild complete\n';

I name this script rebuild_menus.php and upload it to my Drupal directory. Then, I create a text file on my desktop that contains a list of all the Drupal websites. This is an abbreviated example:

http://example1.com
http://example2.com
http://example3.com
http://example4.com

Finally, I execute the script on all the sites by running the following command:

for url in `cat list.txt`; do echo "Running script on $url"; curl $url/rebuild_menus.php; echo; done

3 Comments :, , , , more...

Programmatically create users in Drupal 6.x

by Matt Danger on Dec.16, 2009, under Drupal

You can create a Drupal user by sending user_save() a null parameter and an array of user information. You can also assign a role by setting an array that is keyed with the role ID (rid). This can be obtained by calling user_roles() as shown in the example below.

// Get an array of roles
$roles = user_roles();
 
// Basic account information
$user = array(
  'name' => 'Some User',
  'pass' => 'some_password',
  'mail' => 'user@example.com',
  'status' => 1,
  'init' => 'user@example.com',
  'roles' => array(array_search('some_role', $roles) => 1),
);
 
// See if the user exists by calling Drupal's user_load()
$existing_user = user_load(array('name' => $user['name']));
 
if (!$existing_user->uid) {
 
  // Save the user
  $user = user_save(NULL, $user);
 
}
2 Comments :, more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Send me a message on AIM!

Friends

Read what I read...