You are here

function protected_node_update_6000 in Protected Node 6

Implementation of hook_update_N().

File

./protected_node.install, line 198

Code

function protected_node_update_6000() {
  global $db_type;
  $ret = array();

  // we want an is_protected field so we can load it easily and
  // we can keep the other parameters even when we unprotect
  // a node (although it's not that important right now.)
  $field = array(
    'type' => 'int',
    'size' => 'small',
    'not null' => TRUE,
    'default' => 0,
  );
  db_add_field($ret, 'protected_nodes', 'protected_node_is_protected', $field);
  db_add_index($ret, 'protected_nodes', 'protected_is_protected', array(
    'protected_node_is_protected',
  ));

  // at this time all the protected node entries meant that the
  // nodes were protected; yet, we don't want the default to be 1
  $sql = "UPDATE {protected_nodes} SET protected_node_is_protected = 1";
  $ret[] = update_sql($sql);

  // rename the password so we can directly append to the $node object
  // without too much potential for clashes

  //$field = array(

  //  'description' => t('The sha1 hashed password for the given node.'),
  //  'type' => 'char',
  //  'length' => 40,
  //  'not null' => TRUE,
  //  'default' => '',

  //);

  //db_change_field($ret, 'protected_nodes', 'passwd', 'protected_node_passwd', $field);

  //
  // Core destroys the data in this case because it uses a CAST(passwd AS char) instead
  // of CHAR(40). (at least that breaks under PostgreSQL.)
  // And of course MySQL does not support the RENAME COLUMN feature...
  switch ($db_type) {
    case 'mysql':
    case 'mysqli':
      $sql = "ALTER TABLE {protected_nodes} CHANGE passwd protected_node_passwd CHAR(40) NOT NULL";
      break;
    case 'pgsql':

      // also works in Oracle9i+
      $sql = "ALTER TABLE {protected_nodes} RENAME COLUMN passwd TO protected_node_passwd";
      break;
  }
  $ret[] = update_sql($sql);

  // create a column for the show title flag
  // then copy the flag status from the {variable} table
  $field = array(
    'description' => t('Whether the title of the node should also be protected.'),
    'type' => 'int',
    'size' => 'small',
    'not null' => TRUE,
    'default' => 0,
  );
  db_add_field($ret, 'protected_nodes', 'protected_node_show_title', $field);

  // the variable value is TRUE, no need to read it
  $sql_var = "SELECT name FROM {variable} WHERE name LIKE 'protected_node-%-show_title'";
  $sql_update = "UPDATE {protected_nodes} SET protected_node_show_title = 1 WHERE nid = %d";
  $result = db_query($sql_var);
  $ret[] = array(
    'success' => $result !== FALSE,
    'query' => check_plain($sql_var),
  );
  while ($row = db_fetch_array($result)) {

    // get the node identifier
    preg_match('/protected_node-([0-9]+)-show_title/', $row['name'], $match);

    // the update may do nothing if the node was deleted
    $r = db_query($sql_update, $match[1]);
    if ($r === FALSE) {

      // only record failures otherwise we could get thousands of entries...
      $ret[] = array(
        'success' => FALSE,
        'query' => check_plain($sql_update),
      );
    }

    // remove the variable
    variable_del($row['name']);
  }
  $field = array(
    'description' => t('Date when the password was last changed'),
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  );
  db_add_field($ret, 'protected_nodes', 'protected_node_passwd_changed', $field);

  // request for a rebuild of the node access table

  //node_access_needs_rebuild(TRUE);
  return $ret;
}