Skip to content

Instantly share code, notes, and snippets.

@shrop
Last active December 30, 2015 04:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shrop/67720ffece47c18575ef to your computer and use it in GitHub Desktop.
Save shrop/67720ffece47c18575ef to your computer and use it in GitHub Desktop.
Rebuild permissions for running via Drush (no batch process)
<?php
/**
* @file
* Rebuild the node access database with no php time limits.
*/
/**
* Rebuilds the node access database (for use with drush php-script).
*
* This is code borrowed from Drupal core and slightly modified to run with
* drush.
*
* @see node_access_rebuild()
*/
function node_access_rebuild_drush() {
db_delete('node_access')->execute();
// Only recalculate if the site is using a node_access module.
if (count(module_implements('node_grants'))) {
// Try to allocate enough time to rebuild node grants.
drupal_set_time_limit(0);
$nids = db_query("SELECT nid FROM {node}")->fetchCol();
foreach ($nids as $nid) {
$node = node_load($nid, NULL, TRUE);
// To preserve database integrity, only acquire grants if the node
// loads successfully.
if (!empty($node)) {
node_access_acquire_grants($node);
}
}
}
else {
// Not using any node_access modules. Add the default grant.
db_insert('node_access')
->fields(array(
'nid' => 0,
'realm' => 'all',
'gid' => 0,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
))
->execute();
}
drupal_set_message(t('Content permissions have been rebuilt.'));
node_access_needs_rebuild(FALSE);
cache_clear_all();
}
// Start node access rebuild process.
node_access_rebuild_drush();
@deekayen
Copy link

deekayen commented Dec 3, 2014

shrop said to run it like this:

time drush php-script rebuild-perms.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment