You are here

function _search_by_page_indexing_users in Search by Page 8

Same name and namespace in other branches
  1. 6 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.

Parameters

int $role: (optional) If this is passed in, instead of reading the paths table, only this role is assumed to be needed. Pass in the role ID.

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.

2 calls to _search_by_page_indexing_users()
search_by_page_attach_search_by_page_paths in search_by_page_attach/search_by_page_attach.module
Implements Search by Page hook_search_by_page_paths().
search_by_page_update_index in ./search_by_page.module
Implements hook_update_index().

File

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

Code

function _search_by_page_indexing_users($role = NULL) {

  // Figure out which roles are currently in our paths table, or passed in.
  $allroles = user_roles();
  if ($role) {
    $roles_needed = [
      $role,
    ];
  }
  else {
    $roles_needed = \Drupal::database()
      ->query('SELECT role FROM {search_by_page_path} GROUP BY role')
      ->fetchCol();
  }

  // Figure out which users we already have.
  $accounts_have = \Drupal::database()
    ->query('SELECT rid, uid FROM {search_by_page_index_users}')
    ->fetchAllKeyed();

  // Create or load needed users.
  $accounts = [];
  foreach ($roles_needed as $rid) {
    if (!isset($allroles[$rid])) {
      \Drupal::logger('search_by_page')
        ->error('Role :rid requested for search indexing, does not seem to exist', [
        ':rid' => $rid,
      ]);
      continue;
    }
    $rolename = $allroles[$rid];
    $account = FALSE;

    // Attempt to load the user from the UID we stored a previous time.
    if (isset($accounts_have[$rid])) {
      $account = Drupal\user\Entity\User::load($accounts_have[$rid]);
    }
    if (!$account) {

      // That didn't work. Delete previous entry in index_users table,
      // attempt to create a new user, and save this user ID.
      \Drupal::database()
        ->delete('search_by_page_index_users')
        ->condition('rid', $rid)
        ->execute();
      if ($rid == AccountInterface::ANONYMOUS_ROLE) {
        $account = Drupal\user\Entity\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 = [
          'pass' => user_password(),
          'name' => 'sbp indexing ' . $rolename . ' ' . user_password(),
          'mail' => user_password() . "@" . user_password() . ".com",
          'roles' => [
            $rid => $rolename,
          ],
          'status' => 0,
        ];
        $temp = \Drupal\user\Entity\User::create($new);
        $temp
          ->save();
        $account = Drupal\user\Entity\User::load($temp
          ->get('uid')->value);
        if ($account && $account
          ->get('uid')->value) {
          \Drupal::logger('search_by_page')
            ->notice('Created indexing user %uid (%uname) for role %rid (%rname)', [
            '%uid' => $account
              ->get('uid')->value,
            '%uname' => $account->name,
            '%rid' => $rid,
            '%rname' => $rolename,
          ]);
        }
        else {
          $account = FALSE;
        }
      }
      if ($account) {
        \Drupal::database()
          ->insert('search_by_page_index_users')
          ->fields([
          'rid' => $rid,
          'uid' => $account
            ->get('uid')->value,
        ])
          ->execute();
      }
    }

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