/** * {@inheritdoc} */ public function find($ip, $url, $limit, $method, $start = NULL, $end = NULL) { $select = $this->database->select('webprofiler', 'wp', ['fetch' => \PDO::FETCH_ASSOC]); if (NULL === $start) { $start = 0; } if (NULL === $end) { $end = time(); } if ($ip = preg_replace('/[^\\d\\.]/', '', $ip)) { $select->condition('ip', '%' . $this->database->escapeLike($ip) . '%', 'LIKE'); } if ($url) { $select->condition('url', '%' . $this->database->escapeLike(addcslashes($url, '%_\\')) . '%', 'LIKE'); } if ($method) { $select->condition('method', $method); } if (!empty($start)) { $select->condition('time', $start, '>='); } if (!empty($end)) { $select->condition('time', $end, '<='); } $select->fields('wp', ['token', 'ip', 'method', 'url', 'time', 'parent']); $select->orderBy('time', 'DESC'); $select->range(0, $limit); return $select->execute()->fetchAllAssoc('token'); }
/** * {@inheritdoc} */ public function execute() { $results = array(); if (!$this->isSearchExecutable()) { return $results; } $keys = $this->keywords; // Replace wildcards with MySQL/PostgreSQL wildcards. $keys = preg_replace('!\\*+!', '%', $keys); $query = $this->database->select('users')->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender'); $query->fields('users', array('uid')); if ($this->currentUser->hasPermission('administer users')) { // Administrators can also search in the otherwise private email field, and // they don't need to be restricted to only active users. $query->fields('users', array('mail')); $query->condition($query->orConditionGroup()->condition('name', '%' . $this->database->escapeLike($keys) . '%', 'LIKE')->condition('mail', '%' . $this->database->escapeLike($keys) . '%', 'LIKE')); } else { // Regular users can only search via usernames, and we do not show them // blocked accounts. $query->condition('name', '%' . $this->database->escapeLike($keys) . '%', 'LIKE')->condition('status', 1); } $uids = $query->limit(15)->execute()->fetchCol(); $accounts = $this->entityManager->getStorage('user')->loadMultiple($uids); foreach ($accounts as $account) { $result = array('title' => $account->getUsername(), 'link' => url('user/' . $account->id(), array('absolute' => TRUE))); if ($this->currentUser->hasPermission('administer users')) { $result['title'] .= ' (' . $account->getEmail() . ')'; } $results[] = $result; } return $results; }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $uid = $input->getArgument('uid'); $account = User::load($uid); if (!$account) { // Error loading User entity. $io->error(sprintf($this->trans('commands.user.login.clear.attempts.errors.invalid-user'), $uid)); return 1; } // Define event name and identifier. $event = 'user.failed_login_user'; // Identifier is created by uid and IP address, // Then we defined a generic identifier. $identifier = "{$account->id()}-"; // Retrieve current database connection. $schema = $this->database->schema(); $flood = $schema->findTables('flood'); if (!$flood) { $io->error($this->trans('commands.user.login.clear.attempts.errors.no-flood')); return 1; } // Clear login attempts. $this->database->delete('flood')->condition('event', $event)->condition('identifier', $this->database->escapeLike($identifier) . '%', 'LIKE')->execute(); // Command executed successful. $io->success(sprintf($this->trans('commands.user.login.clear.attempts.messages.successful'), $uid)); }
/** * Builds an EntityQuery to get entities. * * @param $match * Text to match the label against. * * @return \Drupal\Core\Entity\Query\QueryInterface * The EntityQuery object with the basic conditions and sorting applied to * it. */ protected function buildEntityQuery($match) { $match = $this->database->escapeLike($match); $entity_type = $this->entityManager->getDefinition($this->target_type); $query = $this->entityManager->getStorage($this->target_type)->getQuery(); $label_key = $entity_type->getKey('label'); if ($label_key) { $query->condition($label_key, '%' . $match . '%', 'LIKE'); $query->sort($label_key, 'ASC'); } // Bundle check. if (!empty($this->configuration['bundles']) && $bundle_key = $entity_type->getKey('bundle')) { $query->condition($bundle_key, $this->configuration['bundles'], 'IN'); } // Add tags to let other modules alter the query. $query->addTag('linkit_entity_autocomplete'); $query->addTag('linkit_entity_' . $this->target_type . '_autocomplete'); // Add access tag for the query. $query->addTag('entity_access'); $query->addTag($this->target_type . '_access'); return $query; }
/** * {@inheritdoc} */ public function listAll($prefix = '') { try { return $this->connection->query('SELECT name FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND name LIKE :name', array(':collection' => $this->collection, ':name' => $this->connection->escapeLike($prefix) . '%'), $this->options)->fetchCol(); } catch (\Exception $e) { return array(); } }
/** * {@inheritdoc} */ public function pathHasMatchingAlias($initial_substring) { $query = $this->connection->select(static::TABLE, 'u'); $query->addExpression(1); try { return (bool) $query->condition('u.source', $this->connection->escapeLike($initial_substring) . '%', 'LIKE')->range(0, 1)->execute()->fetchField(); } catch (\Exception $e) { $this->catchException($e); return FALSE; } }
/** * {@inheritdoc} */ public function pathHasMatchingAlias($initial_substring) { $query = $this->connection->select('url_alias', 'u'); $query->addExpression(1); return (bool) $query->condition('u.source', $this->connection->escapeLike($initial_substring) . '%', 'LIKE')->range(0, 1)->execute()->fetchField(); }
/** * Implements SearchApiAutocompleteInterface::getAutocompleteSuggestions(). */ public function getAutocompleteSuggestions(QueryInterface $query, SearchApiAutocompleteSearch $search, $incomplete_key, $user_input) { $settings = isset($this->configuration['autocomplete']) ? $this->configuration['autocomplete'] : array(); $settings += array( 'suggest_suffix' => TRUE, 'suggest_words' => TRUE, ); // If none of these options is checked, the user apparently chose a very // roundabout way of telling us he doesn't want autocompletion. if (!array_filter($settings)) { return array(); } $index = $query->getIndex(); $db_info = $this->getIndexDbInfo($index); if (empty($db_info['field_tables'])) { throw new SearchApiException(new FormattableMarkup('Unknown index @id.', array('@id' => $index->id()))); } $fields = $this->getFieldInfo($index); $suggestions = array(); $passes = array(); $incomplete_like = NULL; // Make the input lowercase as the indexed data is (usually) also all // lowercase. $incomplete_key = Unicode::strtolower($incomplete_key); $user_input = Unicode::strtolower($user_input); // Decide which methods we want to use. if ($incomplete_key && $settings['suggest_suffix']) { $passes[] = 1; $incomplete_like = $this->database->escapeLike($incomplete_key) . '%'; } if ($settings['suggest_words'] && (!$incomplete_key || strlen($incomplete_key) >= $this->configuration['min_chars'])) { $passes[] = 2; } if (!$passes) { return array(); } // We want about half of the suggestions from each enabled method. $limit = $query->getOption('limit', 10); $limit /= count($passes); $limit = ceil($limit); // Also collect all keywords already contained in the query so we don't // suggest them. $keys = preg_split('/[^\p{L}\p{N}]+/u', $user_input, -1, PREG_SPLIT_NO_EMPTY); $keys = array_combine($keys, $keys); if ($incomplete_key) { $keys[$incomplete_key] = $incomplete_key; } foreach ($passes as $pass) { if ($pass == 2 && $incomplete_key) { $query->keys($user_input); } // To avoid suggesting incomplete words, we have to temporarily disable // the "partial_matches" option. (There should be no way we'll save the // server during the createDbQuery() call, so this should be safe.) $options = $this->options; $this->options['partial_matches'] = FALSE; $db_query = $this->createDbQuery($query, $fields); $this->options = $options; // We need a list of all current results to match the suggestions against. // However, since MySQL doesn't allow using a temporary table multiple // times in one query, we regrettably have to do it this way. $fulltext_fields = $this->getQueryFulltextFields($query); if (count($fulltext_fields) > 1) { $all_results = $db_query->execute()->fetchCol(); // Compute the total number of results so we can later sort out matches // that occur too often. $total = count($all_results); } else { $table = $this->getTemporaryResultsTable($db_query); if (!$table) { return array(); } $all_results = $this->database->select($table, 't') ->fields('t', array('item_id')); $total = $this->database->query("SELECT COUNT(item_id) FROM {{$table}}")->fetchField(); } $max_occurrences = $this->getConfigFactory()->get('search_api_db.settings')->get('autocomplete_max_occurrences'); $max_occurrences = max(1, floor($total * $max_occurrences)); if (!$total) { if ($pass == 1) { return NULL; } continue; } /** @var \Drupal\Core\Database\Query\SelectInterface|null $word_query */ $word_query = NULL; foreach ($fulltext_fields as $field) { if (!isset($fields[$field]) || !Utility::isTextType($fields[$field]['type'])) { continue; } $field_query = $this->database->select($fields[$field]['table'], 't'); $field_query->fields('t', array('word', 'item_id')) ->condition('item_id', $all_results, 'IN'); if ($pass == 1) { $field_query->condition('word', $incomplete_like, 'LIKE') ->condition('word', $keys, 'NOT IN'); } if (!isset($word_query)) { $word_query = $field_query; } else { $word_query->union($field_query); } } if (!$word_query) { return array(); } $db_query = $this->database->select($word_query, 't'); $db_query->addExpression('COUNT(DISTINCT item_id)', 'results'); $db_query->fields('t', array('word')) ->groupBy('word') ->having('results <= :max', array(':max' => $max_occurrences)) ->orderBy('results', 'DESC') ->range(0, $limit); $incomp_len = strlen($incomplete_key); foreach ($db_query->execute() as $row) { $suffix = ($pass == 1) ? substr($row->word, $incomp_len) : ' ' . $row->word; $suggestions[] = array( 'suggestion_suffix' => $suffix, 'results' => $row->results, ); } } return $suggestions; }