Skip to content

Instantly share code, notes, and snippets.

@bcmiller
Created August 3, 2010 20:28
Show Gist options
  • Save bcmiller/507092 to your computer and use it in GitHub Desktop.
Save bcmiller/507092 to your computer and use it in GitHub Desktop.
<?php
# Load Drupal functions
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
# Check permissions to access this page
if (!empty($update_free_access) || $user->uid != 1) {
require './update.php';
exit;
}
# Retrieve node index in the nodes table and time started
$index = (!isset($_GET['index']) || !is_numeric($_GET['index']) || $_GET['index']<0) ? 0 : $_GET['index'];
$t = (!isset($_GET['t']) || !is_numeric($_GET['t']) || $_GET['t']<0) ? time() : $_GET['t'];
# First launch : flush node_access table and caches
if($index==0) {
node_access_needs_rebuild(TRUE);
db_query("DELETE FROM {node_access}");
}
# Fetch next node to rebuild grants
#$actual = db_result(db_query("SELECT nid FROM {node} WHERE nid > %d ORDER BY nid",$_GET['node']));
$actual = db_result(db_query_range("SELECT nid FROM {node} ORDER BY nid", $index, 1));
$total = db_result(db_query("SELECT COUNT(nid) FROM {node}"));
$percent = ($total > 0) ? ($index/$total*100) : 100;
$time = (floor($percent)) ? format_interval((time()-$t)/$percent*(100-$percent)) : t("caclulating...");
if ($actual>0) {
# Update actual node grants
$node=node_load($actual);
node_access_acquire_grants($node);
} else {
# Finishing : remove 'rebuild permissions' warning
node_access_needs_rebuild(FALSE);
drupal_flush_all_caches();
}
/*
# Old method
set_time_limit(0);
db_query("DELETE FROM {node_access}");
node_access_rebuild();
*/
?><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo t("Rebuilding nodes permissions !percent%", array('!percent' => floor($percent))); ?></title>
<script type="text/javascript">
<!--
function delayer(){
window.location = "<?php echo $_SERVER["SCRIPT_NAME"]."?t=$t&index=".($index+1); ?>"
}
//-->
</script>
</head>
<body <?php echo ($actual>0 && !isset($_GET['stop'])) ? " onLoad=\"setTimeout('delayer()', 0)\"" : ""; ?>>
<?php
if ($actual>0) echo nl2br(t("Rebuilding nodes access rights... !percent% (!index / !total)\nStarted on !started. Estimation of time remaining: %time", array('!percent' => floor($percent), '!index' => $index, '!total' => $total, '!started' => format_date($t), '%time' => $time, )));
else echo t("Nodes access rights rebuilding : finished.");
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment