Skip to content

Commit

Permalink
Merge branch '8.x-1.x' into feature/media-unpublish
Browse files Browse the repository at this point in the history
Manually fixed conflicts in: src/Seed.php
  • Loading branch information
kepol committed Jan 28, 2025
2 parents 78779d9 + 8a68482 commit 2c1c0a5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 22 deletions.
9 changes: 7 additions & 2 deletions modules/quant_api/src/EventSubscriber/QuantApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,13 @@ public function onOutput(QuantEvent $event) {
/** @var \DOMElement $node */
foreach ($xpath->query('//iframe[contains(@src, "/media/oembed")]') as $node) {
$oembed_url = $new_href = $node->getAttribute('src');
$oembed_item = new RouteItem(['route' => $oembed_url]);
$oembed_item->send();
// Only add if it's a relative URL to handle the case where the URL
// contains `/media/oembed` but is from another website.
if (str_starts_with($oembed_url, '/')) {
$oembed_item = new RouteItem(['route' => $oembed_url]);
$oembed_item->send();
$this->logger->notice("[route_item] $oembed_url");
}
}

// @todo Report on forms that need proxying (attachments.forms).
Expand Down
15 changes: 12 additions & 3 deletions src/Commands/QuantDrushCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ class QuantDrushCommands extends DrushCommands {
*/
private $runningProcs = [];

/**
* Returns lock file location (project specific).
*/
private function getLockFileLocation() {
$config = \Drupal::configFactory()->getEditable('quant_api.settings');
return sys_get_temp_dir() . '/' . $config->get('api_project') . '_quant_seed_worker.lock';
}

/**
* Returns path to drush binary for process forking.
*
Expand Down Expand Up @@ -76,13 +84,14 @@ private function getDrushPath() {
public function message($options = ['threads' => 5]) {
$this->output()->writeln("<info>Forking seed worker.</info>");
$drushPath = $this->getDrushPath();
$lockFilePath = sys_get_temp_dir() . '/quant_seed_worker.lock';
$lockFilePath = $this->getLockFileLocation();
$cmd = $drushPath . ' queue:run quant_seed_worker';
$this->output()->writeln("<comment>Using drush binary at $drushPath. Override with \$DRUSH_PATH if required.</comment>");

// Bail if another run is in progress.
if (file_exists($lockFilePath)) {
$this->output()->writeln("<info>Seeding bailed. Another seed run is in progress.</info>");
$this->output()->writeln("<info>Seeding bailed. Another seed run is in progress (lockfile is present: {$lockFilePath})</info>");
$this->output()->writeln("<info>Run drush quant:unlock-queue to manually unlock the queue.</info>");
return;
}
else {
Expand Down Expand Up @@ -118,7 +127,7 @@ public function message($options = ['threads' => 5]) {
* @usage quant:unlock-queue
*/
public function unlock($options = []) {
$lockFilePath = sys_get_temp_dir() . '/quant_seed_worker.lock';
$lockFilePath = $this->getLockFileLocation();
unlink($lockFilePath);

$this->output()->writeln("Unlocked Quant queue.");
Expand Down
49 changes: 35 additions & 14 deletions src/Seed.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,9 @@ public static function getRedirectLocationsFromRedirect($redirect) {

// Get language and prefix configuration.
$langcode = $redirect->language()->getId();
$siteDefaultLangcode = \Drupal::service('language.default')->get()->getId();
$defaultLangcode = \Drupal::service('language.default')->get()->getId();
$pathPrefixes = \Drupal::config('language.negotiation')->get('url.prefixes');
$defaultPrefix = $pathPrefixes[$defaultLangcode] ?? '';

// Multilingual redirects can be configured for a specific language or
// for all languages. If the redirect is configured for all languages,
Expand All @@ -151,8 +152,8 @@ public static function getRedirectLocationsFromRedirect($redirect) {
// Check if a node or term for this path exists.
$node = NULL;
$term = NULL;
$aliasWithoutLangcode = preg_replace('/^\/(' . $siteDefaultLangcode . ')\//', '/', $destination);
$path = \Drupal::service('path_alias.manager')->getPathByAlias($aliasWithoutLangcode);
$aliasWithoutPrefix = preg_replace('/^\/(' . $defaultPrefix . ')\//', '/', $destination);
$path = \Drupal::service('path_alias.manager')->getPathByAlias($aliasWithoutPrefix);
if (preg_match('/node\/(\d+)/', $path, $matches)) {
$node = Node::load($matches[1]);
}
Expand Down Expand Up @@ -184,7 +185,7 @@ public static function getRedirectLocationsFromRedirect($redirect) {
}
// @todo Test use case where page is not a node or term.
else {
$updatedDestination = preg_replace('/^\/(' . $siteDefaultLangcode . ')\//', $pathPrefix . '/', $destination);
$updatedDestination = preg_replace('/^\/(' . $defaultPrefix . ')\//', $pathPrefix . '/', $destination);
}
$redirects[] = [
'source' => $updatedSource,
Expand Down Expand Up @@ -560,7 +561,7 @@ public static function handleInternalPathRedirects($entity, $langcode, $url) {
$id = $entity->id();
$published = $entity->isPublished();
$internalPath = ($type == 'node') ? "/node/{$id}" : "/taxonomy/term/{$id}";
$usesPrefixes = Utility::usesLanguagePathPrefixes();
$prefix = Utility::getPathPrefix($langcode);

// If there is default language content, then the internal path redirect can
// use the default URL. Otherwise, it should use the current language.
Expand All @@ -578,19 +579,19 @@ public static function handleInternalPathRedirects($entity, $langcode, $url) {
// Only create redirects if the content has an alias.
if ($internalPath != $url) {
\Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent($internalPath, $defaultUrl, 301), QuantRedirectEvent::UPDATE);
if ($usesPrefixes) {
if ($prefix) {
// Handle redirects with path prefix too.
\Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent("/{$langcode}{$internalPath}", $languageUrl, 301), QuantRedirectEvent::UPDATE);
\Drupal::service('event_dispatcher')->dispatch(new QuantRedirectEvent("/{$prefix}{$internalPath}", $languageUrl, 301), QuantRedirectEvent::UPDATE);
}
}

// Unpublish redirects.
if (!$defaultPublished) {
Utility::unpublishUrl($internalPath, 'Unpublished internal path');
}
if (!$published && $usesPrefixes) {
if (!$published && $prefix) {
// Handle redirects with path prefix too.
Utility::unpublishUrl("/{$langcode}{$internalPath}", 'Unpublished internal path');
Utility::unpublishUrl("/{$prefix}{$internalPath}", 'Unpublished internal path');
}
}

Expand Down Expand Up @@ -756,17 +757,37 @@ public static function rewriteRelative($markup) {
// Do not strip host domain unless configured.
$strip = $config->get('host_domain_strip') ?: FALSE;
if (!$strip) {
// Handle iframes even if `host_domain_strip` is disabled, so iframe
// renders correctly.
$markup = self::rewriteRelativeIframe($markup);

return $markup;
}

// Strip the host domain from everywhere in the content including header
// metadata such as canonical links.
$hostname = $config->get('host_domain') ?: $_SERVER['SERVER_NAME'];
$port = $_SERVER['SERVER_PORT'];
$markup = preg_replace("/(https?:\/\/)?{$hostname}(\:{$port})?/i", '', $markup);
$markup = Utility::stripLocalHost($markup);

// Edge case: Replace http://default when run via drush without base_url.
$markup = preg_replace("/http:\/\/default/i", '', $markup);
return $markup;
}

/**
* Replaces absolute iframe URLs with relative in markup.
*
* @param string $markup
* The markup to search and rewire relative iframe paths for.
*
* @return string
* Sanitized markup string.
*/
public static function rewriteRelativeIframe($markup) {
$pattern = '/<iframe src="([^"]+)"/i';
if (preg_match_all($pattern, $markup, $matches)) {
foreach ($matches[1] as $url) {
$updated_url = Utility::stripLocalHost($url);
$markup = str_replace($url, $updated_url, $markup);
}
}

return $markup;
}
Expand Down
28 changes: 25 additions & 3 deletions src/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public static function getPathPrefix(string $langcode = NULL) : string {
if (!$langcode) {
$langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
}
// @todo Handle when prefix is different than the langcode.
$prefix = '/' . $langcode;
$prefixes = \Drupal::config('language.negotiation')->get('url.prefixes');
$prefix .= $prefixes[$langcode] ?? '';
}

return $prefix;
Expand All @@ -109,7 +109,7 @@ public static function getPathPrefix(string $langcode = NULL) : string {
* @return string
* The relative canonical URL.
*/
public static function getCanonicalUrl($type, $id, $langcode) {
public static function getCanonicalUrl(string $type, int $id, string $langcode) : string {
$options = ['absolute' => FALSE];
if (!empty($langcode)) {
$language = \Drupal::languageManager()->getLanguage($langcode);
Expand All @@ -120,6 +120,28 @@ public static function getCanonicalUrl($type, $id, $langcode) {
return Url::fromRoute('entity.' . $type . '.canonical', [$type => $id], $options)->toString();
}

/**
* Strip local host from text.
*
* @param string $text
* The text.
*
* @return string
* The text without local host.
*/
public static function stripLocalHost(string $text) : string {

$config = \Drupal::config('quant.settings');
$hostname = $config->get('host_domain') ?: $_SERVER['SERVER_NAME'];
$port = $_SERVER['SERVER_PORT'];
$text = preg_replace("/(https?:\/\/)?{$hostname}(\:{$port})?/i", '', $text);

// Edge case: Replace http://default when run via drush without base_url.
$text = preg_replace("/http:\/\/default/i", '', $text);

return $text;
}

/**
* Checks if it's an external URL.
*
Expand Down

0 comments on commit 2c1c0a5

Please sign in to comment.