You are here

menu_node.install in Menu Node API 6

Same filename and directory in other branches
  1. 7 menu_node.install

Install file for Menu Node API.

File

menu_node.install
View source
<?php

/**
 * @file
 *   Install file for Menu Node API.
 */

/**
 * Implements hook_schema().
 */
function menu_node_schema() {
  $schema['menu_node'] = array(
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'mlid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'mlid',
    ),
    'indexes' => array(
      'nid' => array(
        'nid',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function menu_node_install() {
  drupal_install_schema('menu_node');
}

/**
 * Implements hook_uninstall()
 */
function menu_node_uninstall() {
  drupal_uninstall_schema('menu_node');
}

/**
 * Implements hook_enable().
 *
 * On module enable, populate the {menu_node} table
 * based on existing menu items.
 */
function menu_node_enable() {
  $items = array();
  $result = db_query("SELECT ml.mlid, ml.link_path FROM {menu_links} ml INNER JOIN {menu_custom} mc ON ml.menu_name = mc.menu_name WHERE ml.link_path LIKE 'node/%' AND ml.router_path = 'node/%%'");
  while ($data = db_fetch_object($result)) {
    $nid = str_replace('node/', '', $data->link_path);

    // Ensure that we did not grab any bad links accidentally.
    $check = (bool) db_result(db_query("SELECT COUNT(*) FROM {node} WHERE nid = %d", $nid));
    if ($check) {
      db_query("INSERT INTO {menu_node} (nid, mlid) VALUES (%d, %d)", $nid, $data->mlid);
    }
  }
}

/**
 * Implements hook_disable().
 *
 * On module disable, erase the {menu_node} table.
 */
function menu_node_disable() {
  db_query("DELETE FROM {menu_node}");
}

/**
 * Update function to remove book module entries.
 */
function menu_node_update_6000() {
  $ret = array();
  if (module_exists('book')) {
    $result = db_query("SELECT mlid, nid FROM {book}");
    while ($data = db_fetch_object($result)) {
      menu_node_delete($data->nid, $data->mlid);
    }
  }
  return $ret;
}

/**
 * Erase bad records from the {menu_node} table.
 */
function menu_node_update_6001() {
  $return = array();
  $result = db_query("SELECT mn.mlid, mn.nid FROM {menu_node} mn LEFT JOIN {node} n ON mn.nid = n.nid LEFT JOIN {menu_links} ml ON ml.mlid = mn.mlid WHERE n.nid IS NULL OR ml.mlid IS NULL");
  while ($data = db_fetch_object($result)) {
    $return[] = update_sql("DELETE FROM {menu_node} WHERE mlid = {$data->mlid} AND nid = {$data->nid}");
  }
  return $return;
}

/**
 * Update to the new schema with only mlid as primary key.
 */
function menu_node_update_6002() {
  $ret = array();
  db_drop_primary_key($ret, 'menu_node');
  db_drop_index($ret, 'menu_node', 'nid');
  db_add_primary_key($ret, 'menu_node', array(
    'mlid',
  ));
  db_add_index($ret, 'menu_node', 'nid', array(
    'nid',
  ));
  return $ret;
}

/**
 * Correct for duplicate mlids with unique nids.
 */
function menu_node_update_6003() {
  $return = array();

  // Find any instances where more than one nid is mapped to an mlid.
  $result = db_query("SELECT mn.mlid, mn.nid, ml.link_path FROM {menu_node} mn\n    LEFT JOIN (SELECT mlid, count(mlid) AS count FROM {menu_node} GROUP BY mlid) mcount ON mn.mlid = mcount.mlid\n    LEFT JOIN {menu_links} ml ON mn.mlid = ml.mlid WHERE mcount.count > 1 AND ml.router_path LIKE 'node/%%'");

  // Walk through every row and test them based on nid.
  while ($data = db_fetch_object($result)) {
    $nid = str_replace('node/', '', $data->link_path);

    // Does the stored nid match the menu_link path nid?
    $check = (bool) $nid == $data->nid;

    // If check failed we delete the row with this combination.
    if (!$check) {
      $return[] = update_sql("DELETE FROM {menu_node} WHERE mlid = {$data->mlid} AND nid = {$data->nid}");
    }
  }
  return $return;
}

/**
 * Repeat update_6002, which failed on some systems.
 */
function menu_node_update_6004() {
  return menu_node_update_6002();
}

Functions

Namesort descending Description
menu_node_disable Implements hook_disable().
menu_node_enable Implements hook_enable().
menu_node_install Implements hook_install().
menu_node_schema Implements hook_schema().
menu_node_uninstall Implements hook_uninstall()
menu_node_update_6000 Update function to remove book module entries.
menu_node_update_6001 Erase bad records from the {menu_node} table.
menu_node_update_6002 Update to the new schema with only mlid as primary key.
menu_node_update_6003 Correct for duplicate mlids with unique nids.
menu_node_update_6004 Repeat update_6002, which failed on some systems.