diff --git a/.cspell-project-words.txt b/.cspell-project-words.txt new file mode 100644 index 0000000000000000000000000000000000000000..c181c5568fac82aaec80869172c64326e2cb21cf --- /dev/null +++ b/.cspell-project-words.txt @@ -0,0 +1,16 @@ +ahref +ahve +authnet +bodyurls +cainfo +catanoi +configuraton +hrefurl +liliancatanoi +optain +shorte +verifypeer +nicolae +optasy +procopan +thebumik \ No newline at end of file diff --git a/README.md b/README.md index e644cebad986856ac8f724eab29b32e420059002..856b13b6535fd8b3f3e2b24707e33664dce77af5 100644 --- a/README.md +++ b/README.md @@ -44,5 +44,5 @@ MAINTAINERS This project is sponsored by: * [OPTASY](https://www.optasy.com) is a Canadian Drupal development & web development company based in Toronto. In the past we provided Drupal - development solutions for a variety of Canadian and foregin companies with + development solutions for a variety of Canadian and foreign companies with outstanding results. diff --git a/composer.json b/composer.json index 409084121cec56132d45f2c9b2c177019d466dbf..b63df6ef8567bfa5b13920d59cbde844e1900c7c 100644 --- a/composer.json +++ b/composer.json @@ -8,11 +8,16 @@ { "name": "Nicolae Procopan (thebumik)", "homepage": "https://www.drupal.org/u/thebumik", - "role": "Maintainer" + "role": "Former Maintainer" }, { "name": "Lilian Catanoi (liliancatanoi90)", "homepage": "https://www.drupal.org/u/liliancatanoi90", + "role": "Former Maintainer" + }, + { + "name": "Daniel Rodriguez (danrod)", + "homepage": "https://www.drupal.org/u/danrod", "role": "Maintainer" } ], @@ -20,5 +25,8 @@ "issues": "https://www.drupal.org/project/issues/brokenlinks", "source": "http://cgit.drupalcode.org/brokenlinks" }, - "minimum-stability": "dev" + "minimum-stability": "dev", + "require": { + "php": "^8.1" + } } diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..9bef9ca06939774ebb01621f6c8cb619bab9ab1a --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,18 @@ +<?xml version="1.0"?> +<ruleset name="commerce"> + <description>Default PHP CodeSniffer configuration for Brokenlinks.</description> + <file>.</file> + <arg name="extensions" value="php,module,inc,install,test,profile,theme"/> + <config name="drupal_core_version" value="8"/> + + <exclude-pattern>*/.git/*</exclude-pattern> + <exclude-pattern>*/config/*</exclude-pattern> + <exclude-pattern>*/css/*</exclude-pattern> + <exclude-pattern>*/js/*</exclude-pattern> + <exclude-pattern>*/icons/*</exclude-pattern> + <exclude-pattern>README.md</exclude-pattern> + + <rule ref="Drupal"> + </rule> + +</ruleset> diff --git a/src/Form/SeoBrokenLinksBulkForm.php b/src/Form/SeoBrokenLinksBulkForm.php index 339084da77a36239c832f0f68bf21a3c82bed3d2..a3d6f2baef937a91d94a2f7ec77dc267186fa2ef 100644 --- a/src/Form/SeoBrokenLinksBulkForm.php +++ b/src/Form/SeoBrokenLinksBulkForm.php @@ -93,7 +93,8 @@ class SeoBrokenLinksBulkForm extends FormBase { QueueWorkerManagerInterface $queue_manager, MessengerInterface $messenger, ConfigFactoryInterface $config_factory, - EntityTypeBundleInfo $bundle_info) { + EntityTypeBundleInfo $bundle_info, + ) { $this->entityTypeManager = $entity_manager; $this->queueFactory = $queue; @@ -267,7 +268,7 @@ class SeoBrokenLinksBulkForm extends FormBase { } } - // Process the queue items immediatly using batch API. + // Process the queue items immediately using batch API. if ($handler['#name'] == 'fix_now') { $batch = [ 'operations' => [], diff --git a/src/Form/SeoBrokenLinksNodeEditForm.php b/src/Form/SeoBrokenLinksNodeEditForm.php index 83b6f0eae3bfa737c446afdce2c22dd8a277c384..37f3c3fffa7723f16bfdc73618376abfe4f68a40 100644 --- a/src/Form/SeoBrokenLinksNodeEditForm.php +++ b/src/Form/SeoBrokenLinksNodeEditForm.php @@ -1,58 +1,57 @@ <?php -namespace Drupal\brokenlinks\Form; - -use Drupal\Core\Form\FormStateInterface; -use Drupal\node\NodeForm; -use Drupal\brokenlinks\Plugin\QueueWorker\SeoFixLinksBase; -use Drupal\field\Entity\FieldConfig; - /** + * @file * Form handler Class for the NodeForm. */ -class SeoBrokenLinksNodeEditForm extends NodeForm { - - /** - * {@inheritdoc} - */ - public function save(array $form, FormStateInterface $form_state) { - $shorten = \Drupal::service('brokenlinks.shorten_url'); - $entity = $form_state->getFormObject()->getEntity(); - - // Get each Formatted text fields from the entities. - foreach ($entity->getFieldDefinitions() as $key => $field) { - if ($field instanceof FieldConfig && - in_array($field->getType(), SeoFixLinksBase::FORMATTED_TEXTS)) { - - // Retrieve form body value. - $bodyField = $form_state->getValue($key)[0]['value']; - - // Parse each broken url and create a shorten url for it. - if ($urls = $shorten->getBrokenUrls($bodyField)) { - foreach ($urls as $longUrl) { - $shortUrls[$longUrl] = $shorten->googlShorten($longUrl); - } - - // Replace body broken links with updated ones. - if (!empty($shortUrls)) { - $shortUrls = array_filter($shortUrls); - $bodyField = str_replace( - array_keys($shortUrls), - array_values($shortUrls), - $bodyField - ); - - $format = $this->entity->get($key)->format; - - $this->entity->{$key}->setValue( - ['value' => $bodyField, 'format' => $format] - ); - } - } - } - } - - parent::save($form, $form_state); - } +namespace Drupal\brokenlinks\Form; + +/* class SeoBrokenLinksNodeEditForm extends NodeForm { + +public function save(array $form, FormStateInterface $form_state) { +$shorten = \Drupal::service('brokenlinks.shorten_url'); +$entity = $form_state->getFormObject()->getEntity(); + +// Get each Formatted text fields from the entities. +foreach ($entity->getFieldDefinitions() as $key => $field) { +if ($field instanceof FieldConfig && +in_array($field->getType(), SeoFixLinksBase::FORMATTED_TEXTS)) { + +// Retrieve form body value. +$bodyField = $form_state->getValue($key)[0]['value']; + +// Parse each broken url and create a shorten url for it. +if ($urls = $shorten->getBrokenUrls($bodyField)) { +foreach ($urls as $longUrl) { +$shortUrls[$longUrl] = $shorten->googlShorten($longUrl); } + +// Replace body broken links with updated ones. +if (!empty($shortUrls)) { +$shortUrls = array_filter($shortUrls); +$bodyField = str_replace( +array_keys($shortUrls), +array_values($shortUrls), +$bodyField +); + +$format = $this->entity->get($key)->format; + +$this->entity->{$key}->setValue( +['value' => $bodyField, 'format' => $format] +); +} +} +} +} + +if (parent::save($form, $form_state)) { +return TRUE; +} +else { +return FALSE; +} +} + +} */ diff --git a/src/GoogleShortener.php b/src/GoogleShortener.php index f14eaa0226206d00b53e4dcbd8b83098b8f34949..b00dce6969186ef7cd0f41e827d445fb2ddf9b8f 100644 --- a/src/GoogleShortener.php +++ b/src/GoogleShortener.php @@ -3,13 +3,36 @@ namespace Drupal\brokenlinks; /** - * Class GoogleShortener. + * The Google Shortener Service. */ class GoogleShortener { + /** + * The Google API Endpoint. + * + * @var string + */ private $endpoint; + + /** + * The Google API Key. + * + * @var string + */ private $apiKey; + + /** + * The cURL handle. + * + * @var object + */ private $ch; + + /** + * Connection params. + * + * @var array + */ private $params; /** @@ -17,7 +40,8 @@ class GoogleShortener { */ public function __construct( $api_key, - $endpoint = 'https://www.googleapis.com/urlshortener/v1/url') { + $endpoint = 'https://www.googleapis.com/urlshortener/v1/url', + ) { $this->apiKey = $api_key; $this->endpoint = $endpoint; diff --git a/src/Plugin/QueueWorker/SeoFixLinksBase.php b/src/Plugin/QueueWorker/SeoFixLinksBase.php index 2c1bfc693292b01333bbbb6631d44dc43fa2c9e7..59889899d3df74481908b487ef676d5c856fe288 100644 --- a/src/Plugin/QueueWorker/SeoFixLinksBase.php +++ b/src/Plugin/QueueWorker/SeoFixLinksBase.php @@ -13,7 +13,7 @@ use Drupal\field\Entity\FieldConfig; * Provides base functionality for the BrokenLinksFixer Queue Workers. */ abstract class SeoFixLinksBase extends QueueWorkerBase implements - ContainerFactoryPluginInterface { + ContainerFactoryPluginInterface { const FORMATTED_TEXTS = [ 'text_with_summary', @@ -45,7 +45,8 @@ abstract class SeoFixLinksBase extends QueueWorkerBase implements */ public function __construct( SeoBrokenLinksShorten $short_service, - EntityTypeManagerInterface $entity_manager) { + EntityTypeManagerInterface $entity_manager, + ) { $this->shortenService = $short_service; $this->entityTypeManager = $entity_manager; @@ -58,7 +59,8 @@ abstract class SeoFixLinksBase extends QueueWorkerBase implements ContainerInterface $container, array $configuration, $plugin_id, - $plugin_definition) { + $plugin_definition, + ) { return new static( $container->get('brokenlinks.shorten_url'), diff --git a/src/SeoBrokenLinksShorten.php b/src/SeoBrokenLinksShorten.php index 67d5f09998bfc625662df82925244dd5205110b8..ba05d34affc9aeac255ba8efa3dc4a39c8584a6a 100644 --- a/src/SeoBrokenLinksShorten.php +++ b/src/SeoBrokenLinksShorten.php @@ -5,9 +5,11 @@ namespace Drupal\brokenlinks; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\DependencyInjection\ContainerInterface; +use GuzzleHttp\Exception\RequestException; /** - * Class SeoBrokenLinksShorten. + * Wrapper class to do the calls to the Google Shortener Service. * * @package Drupal\brokenlinks */ @@ -42,12 +44,13 @@ class SeoBrokenLinksShorten { * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory * The core logger factory. * @param \Symfony\Component\HttpFoundation\RequestStack $request - * The sympfony request stack. + * The symfony request stack. */ public function __construct( ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, - RequestStack $request) { + RequestStack $request, + ) { $this->configFactory = $config_factory; $this->logger = $logger_factory->get('brokenlinks'); @@ -84,7 +87,7 @@ class SeoBrokenLinksShorten { $data = json_decode($result); if (!empty($data->id)) { - $this->logger->info($this->t( + $this->logger->info(t( 'Successfully converted @long to @short', ['@long' => $longUrl, '@short' => $data->id] )); @@ -92,7 +95,7 @@ class SeoBrokenLinksShorten { return $data->id; } - $this->logger->info($this->t( + $this->logger->info(t( 'Could not convert "@long" : @error', ['@long' => $longUrl, '@error' => $data->error->message] )); diff --git a/src/SeoFixBrokenLinkBatch.php b/src/SeoFixBrokenLinkBatch.php index 8f81e4875682cc53ec67290025023cf5656a548f..498eb13e3cbba735fdc28b7286e6c2c8262744cb 100644 --- a/src/SeoFixBrokenLinkBatch.php +++ b/src/SeoFixBrokenLinkBatch.php @@ -2,6 +2,8 @@ namespace Drupal\brokenlinks; +use Drupal\Core\Utility\Error; +use Drupal\Component\Utility\DeprecationHelper; use Drupal\Core\Queue\SuspendQueueException; /** @@ -23,9 +25,9 @@ class SeoFixBrokenLinkBatch { return; } - /* @var $queueManager \Drupal\Core\Queue\QueueWorkerManagerInterface */ + /** @var \Drupal\Core\Queue\QueueWorkerManagerInterface $queueManager */ $queueManager = \Drupal::service('plugin.manager.queue_worker'); - /* @var \Drupal\Core\Queue\QueueFactory $queueFactory */ + /** @var \Drupal\Core\Queue\QueueFactory $queueFactory */ $queueFactory = \Drupal::service('queue'); $info = $queueManager->getDefinition($queue_name); @@ -35,6 +37,7 @@ class SeoFixBrokenLinkBatch { $queue_worker = $queueManager->createInstance($queue_name); $queue = $queueFactory->get($queue_name); + $logger = \Drupal::logger('brokenlinks'); if ($item = $queue->claimItem()) { try { @@ -48,7 +51,7 @@ class SeoFixBrokenLinkBatch { // release the item and skip to the next queue. $queue->releaseItem($item); - \Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => \Drupal\Core\Utility\Error::logException(\Drupal::logger('cron'), $e), fn() => watchdog_exception('cron', $e)); + DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => Error::logException(\Drupal::logger('cron'), $e), fn() => Error::logException($logger, $e)); // Skip to the next queue. $context['interrupted'] = TRUE; @@ -56,7 +59,7 @@ class SeoFixBrokenLinkBatch { catch (\Exception $e) { // In case of any other kind of exception, log it and leave the item // in the queue to be processed again later. - \Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => \Drupal\Core\Utility\Error::logException(\Drupal::logger('cron'), $e), fn() => watchdog_exception('cron', $e)); + DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => Error::logException(\Drupal::logger('cron'), $e), fn() => Error::logException($logger, $e)); } } }