X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fredirect%2Fsrc%2FRedirectRepository.php;fp=web%2Fmodules%2Fcontrib%2Fredirect%2Fsrc%2FRedirectRepository.php;h=6c791990d66cc372dfb01354dd4bf0d9bf373159;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/redirect/src/RedirectRepository.php b/web/modules/contrib/redirect/src/RedirectRepository.php new file mode 100644 index 000000000..6c791990d --- /dev/null +++ b/web/modules/contrib/redirect/src/RedirectRepository.php @@ -0,0 +1,160 @@ +manager = $manager; + $this->connection = $connection; + $this->config = $config_factory->get('redirect.settings'); + } + + /** + * Gets a redirect for given path, query and language. + * + * @param string $source_path + * The redirect source path. + * @param array $query + * The redirect source path query. + * @param $language + * The language for which is the redirect. + * + * @return \Drupal\redirect\Entity\Redirect + * The matched redirect entity. + * + * @throws \Drupal\redirect\Exception\RedirectLoopException + */ + public function findMatchingRedirect($source_path, array $query = [], $language = Language::LANGCODE_NOT_SPECIFIED) { + $hashes = [Redirect::generateHash($source_path, $query, $language)]; + if ($language != Language::LANGCODE_NOT_SPECIFIED) { + $hashes[] = Redirect::generateHash($source_path, $query, Language::LANGCODE_NOT_SPECIFIED); + } + + // Add a hash without the query string if using passthrough querystrings. + if (!empty($query) && $this->config->get('passthrough_querystring')) { + $hashes[] = Redirect::generateHash($source_path, [], $language); + if ($language != Language::LANGCODE_NOT_SPECIFIED) { + $hashes[] = Redirect::generateHash($source_path, [], Language::LANGCODE_NOT_SPECIFIED); + } + } + + // Load redirects by hash. A direct query is used to improve performance. + $rid = $this->connection->query('SELECT rid FROM {redirect} WHERE hash IN (:hashes[]) ORDER BY LENGTH(redirect_source__query) DESC', [':hashes[]' => $hashes])->fetchField(); + + if (!empty($rid)) { + // Check if this is a loop. + if (in_array($rid, $this->foundRedirects)) { + throw new RedirectLoopException('/' . $source_path, $rid); + } + $this->foundRedirects[] = $rid; + + $redirect = $this->load($rid); + + // Find chained redirects. + if ($recursive = $this->findByRedirect($redirect, $language)) { + // Reset found redirects. + $this->foundRedirects = []; + return $recursive; + } + + return $redirect; + } + + return NULL; + } + + /** + * Helper function to find recursive redirects. + * + * @param \Drupal\redirect\Entity\Redirect + * The redirect object. + * @param string $language + * The language to use. + */ + protected function findByRedirect(Redirect $redirect, $language) { + $uri = $redirect->getRedirectUrl(); + $baseUrl = \Drupal::request()->getBaseUrl(); + $path = ltrim(substr($uri->toString(), strlen($baseUrl)), '/'); + $query = $uri->getOption('query') ?: []; + return $this->findMatchingRedirect($path, $query, $language); + } + + /** + * Finds redirects based on the source path. + * + * @param string $source_path + * The redirect source path (without the query). + * + * @return \Drupal\redirect\Entity\Redirect[] + * Array of redirect entities. + */ + public function findBySourcePath($source_path) { + $ids = $this->manager->getStorage('redirect')->getQuery() + ->condition('redirect_source.path', $source_path, 'LIKE') + ->execute(); + return $this->manager->getStorage('redirect')->loadMultiple($ids); + } + + /** + * Load redirect entity by id. + * + * @param int $redirect_id + * The redirect id. + * + * @return \Drupal\redirect\Entity\Redirect + */ + public function load($redirect_id) { + return $this->manager->getStorage('redirect')->load($redirect_id); + } + + /** + * Loads multiple redirect entities. + * + * @param array $redirect_ids + * Redirect ids to load. + * + * @return \Drupal\redirect\Entity\Redirect[] + * List of redirect entities. + */ + public function loadMultiple(array $redirect_ids = NULL) { + return $this->manager->getStorage('redirect')->loadMultiple($redirect_ids); + } +}