2.2 慢API替换

龶龲龯龫龩龥龣龟龞龜龛龙龕龒

龲龎龩龊龆龅龃龁齽齺齸齵齲齰齭齬齨齧齤齣齡齝齜API齒齎龒

齍齊user_save

1.鼱齎鼮鼬鼫鼩鼦

db_insert('users_roles')
    ->fields(array('uid', 'rid'));
    ->values(array(
        'uid' => $user->uid,
        'rid' => TEMP_EDITOR_RID,
      ));
    ->execute();

2.鼱齎鼮黤黣鼩鼦

db_delete('users_roles')
    ->condition('uid', $user->uid, 'rid' => TEMP_EDITOR_RID)
    ->execute();

3.黟點黜龁

$data = array(
  'uid' => '12',
  'pass' => '123456',
);

require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');

$data['pass'] = user_hash_password(trim($data['pass']));

drupal_write_record('users', $data, array('uid'));

4.麺麷麶齎鼮

/**
 * 保存用户基本信息
 *
 * @param $data
 *   users表字段信息   
 *
 * @return
 *   FALSE 失败,1 新增成功,2 更新成功
 */
function user_basic_save(&$data) {
  if (isset($data['uid'])) { // 更新
    if (isset($data['pass'])) { // 修改密码
      require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');

      $data['pass'] = user_hash_password(trim($data['pass']));
    }

    return drupal_write_record('users', $data, array('uid'));
  }
  else { // 新增
    require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');

    // 处理密码
    if (isset($data['pass'])) {
      $data['pass'] = user_hash_password(trim($data['pass']));
    }
    else {
      $data['pass'] = ''; // 生成随机密码
      $data['pass'] = user_hash_password(trim($data['pass']));
    }
    
    $data['uid'] = db_next_id(db_query('SELECT MAX(uid) FROM {users}')->fetchField());

    $data['created'] = REQUEST_TIME;
    $data['access'] = REQUEST_TIME;
    $data['login'] = REQUEST_TIME;

    if (!isset($data['mail'])) {
      $data['mail'] = $data['name'] . '@yoursite.com';
      $data['init'] = $data['name'] . '@yoursite.com';
    }

    $data['status'] = 1;

    return drupal_write_record('users', $data);
  }
}

5.黟點齎鼮麰麮麬麫

鹱黟點users麥鹰齲齽鹭齎鼮鹩齊鹦鹤

API鹢鹠龒

6.黟點齎鼮鹟鹞麬麫

鹭龩鹛鹩齊齨鹘鹔齽鹓鹏齵鹋鹉齡齝齜麥鹈龩{user_profile}

/**
 * 保存用户信息
 *
 * @param $user_profile
 *   array('uid', 'realname', 'gender', ...)
 *
 * @return
 *   FALSE 失败,1 新增成功,2 更新成功
 */
function user_profile_save($user_profile) {
  if (user_profile_load($user_profile['uid'])) { // 更新
    return drupal_write_record('user_profile', $user_profile, array('uid'));
  }
  else { // 新增
    return drupal_write_record('user_profile', $user_profile);
  }
}

7.鹁鹀鸿齎鼮鸽鸺鸸鸶鸵authmap齽drupal龲鸳龅鸱鸭鸬龩user_set_authmaps齽鸫鸬鸨鸧鸣$user鸡鸝鸚鼱鸙鸭齽鸗鸖鹈鸕鸸鸑鸧鸣user鸡鸝齽鸍齰齡齝齜鸉齍鸅鸱鸭鸄鸂uid龒點鸁鸀鷽齽齺齸齲齰齡鷹鸡鷶龊龒

/**
 * Save mappings of which external authentication module(s) authenticated
 * a user. Maps external usernames to user ids in the users table.
 *
 * @param $uid
 * @param $authmaps
 *   An associative array with a compound key and the username as the value.
 *   The key is made up of 'authname_' plus the name of the external authentication
 *   module.
 * @see user_external_login_register()
 */
function custom_user_set_authmaps($uid, $authmaps) {
  foreach ($authmaps as $key => $value) {
    $module = explode('_', $key, 2);
    if ($value) {
      db_merge('authmap')
        ->key(array(
          'uid' => $uid,
          'module' => $module[1],
        ))
        ->fields(array('authname' => $value))
        ->execute();
    }
    else {
      db_delete('authmap')
        ->condition('uid', $uid)
        ->condition('module', $module[1])
        ->execute();
    }
  }
}

鷱齊node_load

1.鷰鷯nid鷭鷩鷥鷤麰麮麬麫齽鷶鹭鷥鷤鷠鷟齽鷜鷛鷚鷗鷕鷩node麥鹰齲龒

鷶鹭齽鸗鷒齎龅鸬鷎鷍id鷭鷩name齽鸗鹱齝齜鸉鹭龊API龩

/**
 * 获取以英文逗号分隔的多个id字符串所对应的名字
 */
function get_names_from_ids($ids, $type='term') {
  $names = array();
  $ids = explode(',', $ids);

  switch ($type) {
    case 'term':
      foreach ($ids as $value) {
        $names[] = ; // 从taxonomy_term_data表里获取name
      }
      break;

    case 'node':
      foreach ($ids as $value) {
        $record = ; // 从node表获取title
        $names[] = $record['title'];
      }
      break;

    case 'users':
      foreach ($ids as $value) {
        $record = ''; // 从users表获取name
        $names[] = $record['name'];
      }
      break;

    default:
      # code...
      break;
  }

  return implode($names, ',');
}

2.鷕鷩鹀鸅鶫鹀鸅齰鹠麢麠麬麫齽鹉鶩齨鶥鶢鷟龅龶龲龊齽齲齰齎node_load + entity_metadata_wrapper齽鶝鸣鸬鼱鸉鸿鶚鶘鶗龒

$node = node_load($nid);
$node_wrapper = entity_metadata_wrapper('node', $node);
$field_1_value = $node_wrapper->field_field_1->value();
$field_2_value = $node_wrapper->field_field_2->value();

3.鷕鷩鹀鸅齰龊麢麠麬麫齽齲齰鷂鶖鶕鶔鶒鶏麢麠麥鶌鹭鶉鶅鶂鵾鷒齎齽齲齧齤API齽鹭龩鷭鷩鵺鵹鸍鵸鵴鵲龒鵯龩鷭鷩鵴鵲鵭鵪麬麫齽鹰齒鸕鶿麅鶒鶏齽鵧鸑鸣齧齤鸉齽鶩鵦齜鵢

/**
 * 获取项目所属机构id
 */
function get_project_institution($project_id) {
  $query = db_select('field_data_field_project_company', 'fpc');
  $query->condition('fpc.entity_id', $project_id);
  $query->fields('fpc', array('field_project_company_target_id'));
  
  return $query->execute()->fetchField();
}

4.鵜鵙鵘鶂齲齰鷎鷍龟龞龜龛鵗鹢鵕齍鸅齡齝齜麥齽鷕鷩麬麫鸸鹱鷕齡齝齜麥龒

鹭龩齝齜齍鸅龙鵟麬麫麥齽{institutions}齽鵒龙鵟鵜鵙鵘鶂龅鸍鸕麢麠麬麫鵧齵鼫鵏齣鸖鸅麥鹈齽鹉hook_node_insert, hook_node_update, hook_node_delete鸸齽鹢鵕麍麶鸨龒鸖鵌鹉鷕鷩龙鵟麬麫龅鵊鸿齽鵈鵅鸬app鷗鵁鹈齽鷚鷗鷕齡齝齜龅institutions麥齽鹱鶘鶿鸉鴿

鹀齊node_save

1.鴼鹭鸬鸄黟點鷥鷤鷠鷟齽鷥鷤鴺鴸鹔node麥鹈鸕龅麬麫齽齲齰鷚鷗鴴鸚node麥龒

2.鴼鹭鸄鸬黟點鷥鷤龅齍鸅鴰鴯鸅麢麠麬麫齽齲齰鷚鷗鴫鴴鸚麢麠麥齽麢麠version麥鹭鶉鴩鸕齎齣龅鴧鵧鸑齎鴣龒

鴡齊user_load

1.鷭鷩齎鼮麰麮麬麫齽鷕鷩users麥鹰齲

/**
 * 获取用户基本信息
 *
 * @return
 *   FALSE || 用户信息数组
 */
function user_basic_load($uid) {
  $params = array(
    'table' => 'users',
    'conditions' => array('uid' => $uid),
    'single' => TRUE,
  );

  return get_table_record_value2($params);
}

鴘齲齰鴔鴓鴒龩user_basic_load_by_name, user_basic_load_by_mail鹔齡齝齜API龒

2.鷭鷩齎鼮鼩鼦麬麫齽鷕鷩users_roles麥鹰齲龒鴎鸿API鸬user_has_role齽鴰鴌齺齸齍鴊鷕鷩user鸡鸝鹈龅roles麬麫齽齵鸧鸣鸕user鸡鸝齽龃鴉鸑鴈齽鴄鼱鸗鴁鸕鸸鳿鳽鳹龅鸑鸬鳵龶鳲鳰齎鼮龅鼩鼦齽鸍齰鸑鸧鸣齍鷚鸂$user龒 龊龆鳬鸅鳫龛齽鸗鸬鵒鳧鸅鼩鼦龅鳽鳹齧齤鸉鶕鶔龅API齽鹭鳽鳹齎鼮鸬鳦鶖鳣鴌鼩鼦龩

/**
 * 判断用户是否是开发者角色
 */
function user_is_developer($uid = NULL) {
  if (!$uid) {
    global $user;
    $uid = $user->uid;
  }
  elseif (!is_numeric($uid)) {
    $uid = $uid->uid;
  }

  $developer_role = variable_get('user_admin_role', '');

  // 查询users_roles表
  // 查到了 return TRUE

  return FALSE;
}

3.鷭鷩齎鼮鴔鴓鹟鹞麬麫齽鳟鳜鸑齎齎鼮麢麠齽鷚鷗齒齎齡齝齜麥齽鹭龩user_profile

/**
 * 获取用户信息
 *
 * @return
 *   FALSE || 用户信息数组
 */
function user_profile_load($uid) {
  // 查询user_profile表
}

鴘齲齰鴔鴓鴒user_profile_load_by_phone鹔API龒

4.鹁鹀鸿鳲鳰鸸齽鸧鸣鶲authmap鹈鳙鴒齎鼮麬麫齽drupal龲鸳龅鸱鸭鸬龩user_external_load齽鸨麕麑鸸鳘鳖鸉user鸡鸝齽鸕鸸鳿鸑鸧鸣齽鸍齰齡齝齜齍鸅鸱鸭齽鸄鳘鳖uid龒

/**
 * Fetches a user object based on an external authentication source.
 *
 * @param string $authname
 *   The external authentication username.
 *
 * @return
 *   UID if the user is found or FALSE if not found.
 */
function custom_user_external_load($authname) {
  return db_query("SELECT uid FROM {authmap} WHERE authname = :authname", array(':authname' => $authname))->fetchField();
}

鳌齊file_load

1.鵅鳋麰麮鹠鸄鸧鸣鷕鷩file_managed麥鹱鶥鳈齣鷒齎龅鷊龫麬麫龒

/**
 * 获取文件信息
 */
function get_file_info($fid) {
  $params = array(
    'table' => 'file_managed',
    'single' => TRUE,
    'conditions' => array('fid' => $fid),
  );

  return get_table_record_value2($params);
}

鳄齊file_save

1.鵅鳋麰麮鹠鸄鸧鸣麺麷齣file_managed麥鹱齲齰鸉龒

/**
 *  更新文件基本信息
 */
function file_basic_save(&$file) {
  if (isset($file['fid'])) {
    return drupal_write_record('file_managed', $file, 'fid');
  }
  else {
    return drupal_write_record('file_managed', $file);
  }
}

鳃齊file_copy

1.鹭鶉鸑鲿鹿麕麶龅鷊龫鲼鳰齽齎file_unmanaged_copy龃鲸

鲴齊file_save_data

1.齲齰齡齝齜鸅custom_file_save_data齽鲰鹘鹱鸬鸑齎鸂file鸡鸝龒

/**
 * 保存文件
 *
 * @param $data
 *   文件数据
 *
 * @param $destination
 *   文件保存路径
 *
 * @param $author
 *   文件作者uid
 *
 * @param $replace
 *   Replace behavior when the destination file already exists:
 *   - FILE_EXISTS_REPLACE - Replace the existing file.
 *   - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is
 *                          unique.
 *   - FILE_EXISTS_ERROR - Do nothing and return FALSE.
 *
 * @return
 *   文件信息数组 || FALSE
 */
function custom_file_save_data($data, $destination, $author = NULL, $replace = FILE_EXISTS_RENAME) {
  if ($author === NULL) {
    global $user;

    $author = $user->uid;
  }

  // 验证路径是否合法
  if (!file_valid_uri($destination)) {
    return FALSE;
  }

  // 保存物理文件
  if ($uri = file_unmanaged_save_data($data, $destination, $replace)) {
    $file = array(
      'uid' => $author,
      'uri' => $uri,
      'filename' => drupal_basename($uri),
      'filemime' => file_get_mimetype($uri),
      'filesize' => filesize($uri),
      'status' => 1,
      'timestamp' => REQUEST_TIME,
    );

    if (file_basic_save($file)) {
      return $file;
    }
    else {
      return FALSE;
    }
  }

  return FALSE;
}

鲜齊taxonomy_vocabulary_load

1.鷕鷩鲛鲘麥麬麫齽鸄鸧鸣鴫鷕鷩taxonomy_vocabulary麥鹰齲

鲕齊taxonomy_vocabulary_machine_name_load

1.鷕鷩鲛鲘麥麬麫齽鸄鸧鸣鴫鷕鷩taxonomy_vocabulary麥鹰齲

/**
 * 根据词汇表的机读名获取词汇表id
 */
function get_voc_id_from_machine_name($machine_name) {
  // 查询taxonomy_vocabulary表
}

鲕齍齊taxonomy_term_load

1.鷕鷩鲓鲐麬麫齽鸄鸧鸣鴫鷕鷩taxonomy_term_data麥鹰齲

鷒齎龅鸿鲢龩

/**
 * 根据tid获取分类名字
 */
function get_term_name_from_tid($tid) {
  // 查询taxonomy_term_data表
}

/**
 * 根据分类名字获取tid
 */
function get_tid_from_term_name($term_name, $vid) {
  // 查询taxonomy_term_data表
}

鲕鷱齊taxonomy_term_save

1.麺麷鲓鲐齽鸄鸧鸣麺麷taxonomy_term_data鲎taxonomy_term_hierarchy鴯鸅麥鹰齲龒

/**
 * 保存术语
 */
function custom_term_save($name, $vid) {
  $term = array(
    'name' => $name,
    'vid' => $vid,
  );

  if (drupal_write_record('taxonomy_term_data', $term)) {
    // 保存术语等级记录
    $temp = array(
      'tid' => $term['tid'],
    );

    drupal_write_record('taxonomy_term_hierarchy', $temp);

    return $term['tid'];
  }

  return FALSE;
}

鲕鹀齊taxonomy_get_tree

1.鷕鷩鲛鲘麥龊龅鲓鲐麬麫鲆麥齽鶲taxonomy_term_data麥齡鲅鷕鷩鴚鲃鹰齲

/**
 * 获取词汇表一级术语数组
 * 
 * @param $voc_machine_name
 *   词汇表机读名
 *
 * @param $not_multiple
 *   若词汇表术语为多层及,则传值TRUE,否则默认为FALSE,即:单层级
 *
 * @return
 *   术语数组,array('tid' => 'name')
 */
function get_vocabulary_first_level_terms($voc_machine_name, $multiple = FALSE) {
  $vid = get_vid_from_machine_name($voc_machine_name);

  if (!$multiple) {
    $query = "SELECT tid, name FROM {taxonomy_term_data} WHERE vid = :vid ORDER BY weight DESC";
  }
  else {
    $query = "SELECT td.tid, td.name FROM {taxonomy_term_data} td INNER JOIN {taxonomy_term_hierarchy} th ON td.tid = th.tid WHERE td.vid = :vid AND th.parent = 0 ORDER BY weight DESC";
  }

  $result = db_query($query, array(':vid' => $vid));

  $return_array = array();
  foreach ($result as $value) {
    $return_array[$value->tid] = $value->name;
  }

  return $return_array;
}

评论 (写第一个评论)