node_access_rebuild

  1. drupal
    1. 5
    2. 6
    3. 7
    4. 8
Versions
5 node_access_rebuild()
6 – 8 node_access_rebuild($batch_mode = FALSE)

Rebuild the node access database. This is occasionally needed by modules that make system-wide changes to access levels.

When the rebuild is required by an admin-triggered action (e.g module settings form), calling node_access_needs_rebuild(TRUE) instead of node_access_rebuild() lets the user perform his changes and actually rebuild only once he is done.

Note : As of Drupal 6, node access modules are not required to (and actually should not) call node_access_rebuild() in hook_enable/disable anymore.

Parameters

$batch_mode Set to TRUE to process in 'batch' mode, spawning processing over several HTTP requests (thus avoiding the risk of PHP timeout if the site has a large number of nodes). hook_update_N and any form submit handler are safe contexts to use the 'batch mode'. Less decidable cases (such as calls from hook_user, hook_taxonomy, etc...) might consider using the non-batch mode.

See also

node_access_needs_rebuild()

Related topics

▾ 9 functions call node_access_rebuild()

CommentNodeAccessTest::setUp in modules/comment/comment.test
FilePrivateTestCase::setUp in modules/file/tests/file.test
Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…
module_disable in includes/module.inc
Disable a given set of modules.
NodeAccessBaseTableTestCase::setUp in modules/node/node.test
Enable modules and create user with specific permissions.
NodeAccessExampleTestCase::setUp in examples/node_access_example/node_access_example.test
Enable modules and create user with specific permissions.
NodeEntityFieldQueryAlter::setUp in modules/node/node.test
Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…
NodeQueryAlter::setUp in modules/node/node.test
Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…
node_configure_rebuild_confirm_submit in modules/node/node.admin.inc
Handler for wipe confirmation
SearchNodeAccessTest::setUp in modules/search/search.test
Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…

Code

modules/node/node.module, line 3447

<?php
function node_access_rebuild($batch_mode = FALSE) {
  db_delete('node_access')->execute();
  // Only recalculate if the site is using a node_access module.
  if (count(module_implements('node_grants'))) {
    if ($batch_mode) {
      $batch = array(
        'title' => t('Rebuilding content access permissions'), 
        'operations' => array(
          array('_node_access_rebuild_batch_operation', array()),
        ), 
        'finished' => '_node_access_rebuild_batch_finished',
      );
      batch_set($batch);
    }
    else {
      // Try to allocate enough time to rebuild node grants
      drupal_set_time_limit(240);

      $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();
  }

  if (!isset($batch)) {
    drupal_set_message(t('Content permissions have been rebuilt.'));
    node_access_needs_rebuild(FALSE);
    cache_clear_all();
  }
}
?>