View source
<?php
namespace Drupal\linkchecker\Plugin\LinkStatusHandler;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\linkchecker\LinkCheckerLinkInterface;
use Drupal\linkchecker\Plugin\LinkStatusHandlerBase;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class Repair301 extends LinkStatusHandlerBase {
protected $request;
public function __construct(array $configuration, $plugin_id, $plugin_definition, QueueFactory $queueFactory, EntityTypeManagerInterface $entityTypeManager, AccountSwitcherInterface $accountSwitcher, ImmutableConfig $linkcheckerSetting, RequestStack $requestStack) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $queueFactory, $entityTypeManager, $accountSwitcher, $linkcheckerSetting);
$this->request = $requestStack
->getCurrentRequest();
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('queue'), $container
->get('entity_type.manager'), $container
->get('account_switcher'), $container
->get('config.factory')
->get('linkchecker.settings'), $container
->get('request_stack'));
}
protected function getItems(LinkCheckerLinkInterface $link, ResponseInterface $response) {
$autoRepair301 = $this->linkcheckerSetting
->get('error.action_status_code_301');
$redirectUrl = $response
->getHeaderLine('Location');
if ($autoRepair301 && $autoRepair301 <= $link
->getFailCount() && UrlHelper::isValid($redirectUrl, TRUE)) {
return parent::getItems($link, $response);
}
else {
return [];
}
}
protected function doHandle(LinkCheckerLinkInterface $link, ResponseInterface $response, FieldableEntityInterface $entity) {
$autoRepair301 = $this->linkcheckerSetting
->get('error.action_status_code_301');
$redirectUrl = $response
->getHeaderLine('Location');
if ($autoRepair301 && $autoRepair301 <= $link
->getFailCount() && UrlHelper::isValid($redirectUrl, TRUE) && $link
->isExists()) {
$values = $entity
->get($link
->getParentEntityFieldName())
->getValue();
foreach ($values as $key => $value) {
if (isset($value['value'])) {
$values[$key]['value'] = $this
->linkReplace($value['value'], $link
->getUrl(), $redirectUrl);
}
}
$entity
->set($link
->getParentEntityFieldName(), $values);
$entity
->save();
}
}
protected function linkReplace($value, $oldLinkAbsolute, $newLinkAbsolute) {
if (empty($value) || empty($oldLinkAbsolute) || empty($newLinkAbsolute)) {
return $value;
}
if (isset($this->request)) {
$baseUrl = $this->request
->getSchemeAndHttpHost();
}
else {
$httpProtocol = $this->linkcheckerSetting
->get('default_url_scheme');
$baseUrl = $httpProtocol . $this->linkcheckerSetting
->get('base_path');
}
$baseRoot = mb_strtolower($baseUrl);
$oldLink = str_replace($baseRoot, '', $oldLinkAbsolute);
$newLink = str_replace($baseRoot, '', $newLinkAbsolute);
$oldHtmlLinkAbsolute = UrlHelper::filterBadProtocol($oldLinkAbsolute);
$newHtmlLinkAbsolute = UrlHelper::filterBadProtocol($newLinkAbsolute);
$oldHtmlLink = UrlHelper::filterBadProtocol($oldLink);
$newHtmlLink = UrlHelper::filterBadProtocol($newLink);
if (in_array($value, [
$oldHtmlLinkAbsolute,
$oldHtmlLink,
$oldLinkAbsolute,
$oldLink,
])) {
$value = str_replace($oldHtmlLinkAbsolute, $newHtmlLinkAbsolute, $value);
$value = str_replace($oldHtmlLink, $newHtmlLink, $value);
$value = str_replace($oldLinkAbsolute, $newLinkAbsolute, $value);
$value = str_replace($oldLink, $newLink, $value);
}
else {
$oldLinks = [
$oldHtmlLinkAbsolute,
$oldHtmlLink,
$oldLink,
];
$oldLinks = array_unique($oldLinks);
$htmlDom = Html::load($value);
if ($this->linkcheckerSetting
->get('extract.from_a') == TRUE) {
$links = $htmlDom
->getElementsByTagName('a');
foreach ($links as $link) {
if (in_array($link
->getAttribute('href'), $oldLinks)) {
$link
->setAttribute('href', $newHtmlLink);
}
if (in_array($link->nodeValue, $oldLinks)) {
$link->nodeValue = $newHtmlLink;
}
}
$links = $htmlDom
->getElementsByTagName('area');
foreach ($links as $link) {
if (in_array($link
->getAttribute('href'), $oldLinks)) {
$link
->setAttribute('href', $newHtmlLink);
}
}
}
if ($this->linkcheckerSetting
->get('extract.from_a') == TRUE) {
$audios = $htmlDom
->getElementsByTagName('audio');
foreach ($audios as $audio) {
if (in_array($audio
->getAttribute('src'), $oldLinks)) {
$audio
->setAttribute('src', $newHtmlLink);
}
$sources = $audio
->getElementsByTagName('source');
foreach ($sources as $source) {
if (in_array($source
->getAttribute('src'), $oldLinks)) {
$source
->setAttribute('src', $newHtmlLink);
}
}
$tracks = $audio
->getElementsByTagName('track');
foreach ($tracks as $track) {
if (in_array($track
->getAttribute('src'), $oldLinks)) {
$track
->setAttribute('src', $newHtmlLink);
}
}
}
}
if ($this->linkcheckerSetting
->get('extract.from_embed') == TRUE) {
$embeds = $htmlDom
->getElementsByTagName('embed');
foreach ($embeds as $embed) {
if (in_array($embed
->getAttribute('src'), $oldLinks)) {
$embed
->setAttribute('src', $newHtmlLink);
}
if (in_array($embed
->getAttribute('pluginurl'), $oldLinks)) {
$embed
->setAttribute('pluginurl', $newHtmlLink);
}
if (in_array($embed
->getAttribute('pluginspage'), $oldLinks)) {
$embed
->setAttribute('pluginspage', $newHtmlLink);
}
}
}
if ($this->linkcheckerSetting
->get('extract.from_iframe') == TRUE) {
$iframes = $htmlDom
->getElementsByTagName('iframe');
foreach ($iframes as $iframe) {
if (in_array($iframe
->getAttribute('src'), $oldLinks)) {
$iframe
->setAttribute('src', $newHtmlLink);
}
}
}
if ($this->linkcheckerSetting
->get('extract.from_img') == TRUE) {
$imgs = $htmlDom
->getElementsByTagName('img');
foreach ($imgs as $img) {
if (in_array($img
->getAttribute('src'), $oldLinks)) {
$img
->setAttribute('src', $newHtmlLink);
}
if (in_array($img
->getAttribute('longdesc'), $oldLinks)) {
$img
->setAttribute('longdesc', $newHtmlLink);
}
}
}
if ($this->linkcheckerSetting
->get('extract.from_object') == TRUE) {
$objects = $htmlDom
->getElementsByTagName('object');
foreach ($objects as $object) {
if (in_array($object
->getAttribute('data'), $oldLinks)) {
$object
->setAttribute('data', $newHtmlLink);
}
if (in_array($object
->getAttribute('codebase'), $oldLinks)) {
$object
->setAttribute('codebase', $newHtmlLink);
}
$params = $object
->getElementsByTagName('param');
foreach ($params as $param) {
$names = [
'archive',
'filename',
'href',
'movie',
'src',
'url',
];
if ($param
->hasAttribute('name') && in_array($param
->getAttribute('name'), $names)) {
if (in_array($param
->getAttribute('value'), $oldLinks)) {
$param
->setAttribute('value', $newHtmlLink);
}
}
$srcs = [
'movie',
];
if ($param
->hasAttribute('src') && in_array($param
->getAttribute('src'), $srcs)) {
if (in_array($param
->getAttribute('value'), $oldLinks)) {
$param
->setAttribute('value', $newHtmlLink);
}
}
}
}
}
if ($this->linkcheckerSetting
->get('extract.from_video') == TRUE) {
$videos = $htmlDom
->getElementsByTagName('video');
foreach ($videos as $video) {
if (in_array($video
->getAttribute('poster'), $oldLinks)) {
$video
->setAttribute('poster', $newHtmlLink);
}
if (in_array($video
->getAttribute('src'), $oldLinks)) {
$video
->setAttribute('src', $newHtmlLink);
}
$sources = $video
->getElementsByTagName('source');
foreach ($sources as $source) {
if (in_array($source
->getAttribute('src'), $oldLinks)) {
$source
->setAttribute('src', $newHtmlLink);
}
}
$tracks = $video
->getElementsByTagName('track');
foreach ($tracks as $track) {
if (in_array($track
->getAttribute('src'), $oldLinks)) {
$track
->setAttribute('src', $newHtmlLink);
}
}
}
}
$value = Html::serialize($htmlDom);
}
return $value;
}
}