You are here

function _search_by_page_indexing_users in Search by Page 6

Same name and namespace in other branches
  1. 8 search_by_page.module \_search_by_page_indexing_users()
  2. 7 search_by_page.module \_search_by_page_indexing_users()

Internal function: Returns a list of internal users to use for indexing.

Reads the paths table, and finds all roles modules said to use for indexing. Maintains a set of blocked users for each of these roles, and adds/removes users from this set as they appear/disappear from the list of needed users.

Return value

Array indexed by role name. Each element is a loaded user object having that role, with the status bit temporarily (in-memory) set to 1. Includes an entry for the anonymous role.

1 call to _search_by_page_indexing_users()
search_by_page_update_index in ./search_by_page.module
Implementation of hook_update_index().

File

./search_by_page.module, line 1521
Main module file for Drupal module Search by Page.

Code

function _search_by_page_indexing_users() {

  // Figure out which roles are currently in our paths table.
  $allroles = user_roles();
  $result = db_query('SELECT role FROM {sbp_path} GROUP BY role');
  $roles_needed = array();
  while ($item = db_fetch_object($result)) {
    $roles_needed[$item->role] = $allroles[$item->role];
  }

  // Figure out which users we already have.
  $result = db_query('SELECT rid, uid FROM {sbp_index_users}');
  $accounts_have = array();
  while ($item = db_fetch_object($result)) {
    $rid = $item->rid;
    if (isset($allroles[$rid])) {
      $rolename = $allroles[$rid];
      $accounts_have[$rolename] = $item->uid;
    }
  }

  // Create or load needed users.
  $accounts = array();
  foreach ($roles_needed as $rid => $rolename) {
    $account = FALSE;

    // Attempt to load the user from the UID we stored a previous time.
    if (isset($accounts_have[$rolename])) {
      $account = user_load($accounts_have[$rolename]);
    }
    if (!$account) {

      // That didn't work. Delete previous entry in index_users table,
      // attempt to create a new user, and save this user ID.
      db_query('DELETE FROM {sbp_index_users} WHERE rid = %d', $rid);
      if ($rid == DRUPAL_ANONYMOUS_RID) {
        $account = user_load(0);
      }
      else {

        // Create a blocked user with random password and email, and a random
        // suffix on the user name to prevent blocking problems. See issue
        // http://drupal.org/node/716342.
        $new = array(
          'pass' => user_password(),
          'name' => 'sbp indexing ' . $rolename . ' ' . user_password(),
          'mail' => user_password() . "@" . user_password() . ".com",
          'roles' => array(
            $rid => $rolename,
          ),
          'status' => 0,
        );
        $tmp = user_save((object) array(), $new);
        $account = user_load($tmp->uid);
        if ($account && $account->uid) {
          watchdog('search_by_page', 'Created indexing user %uid (%uname) for role %rid (%rname)', array(
            '%uid' => $account->uid,
            '%uname' => $account->name,
            '%rid' => $rid,
            '%rname' => $rolename,
          ), WATCHDOG_NOTICE);
        }
        else {
          $account = FALSE;
        }
      }
      if ($account) {
        db_query('INSERT INTO {sbp_index_users} (rid, uid) VALUES (%d, %d)', $rid, $account->uid);
      }
    }

    // Add to return value, setting status bit temporarily to 1 (in-memory).
    if ($account) {
      $account->status = 1;
      $accounts[$rolename] = $account;
    }
    else {
      watchdog('search_by_page', 'Unable to set up an indexing user for role %rid (%rname)', array(
        '%rid' => $rid,
        '%rname' => $rolename,
      ), WATCHDOG_ERROR);
    }
  }
  return $accounts;
}