Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SD-279]moving from content-vic and adding drush services file #530

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions drush.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
delete_orphaned_paragraphs_test.commands:
class: Drupal\tide_core\Commands\TideDeleteOrphanedParagraphsTest
arguments: ['@keyvalue']
tags:
- { name: drush.command }

74 changes: 74 additions & 0 deletions src/Commands/TideDeleteOrphanedParagraphs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php


namespace Drupal\tide_core\Commands;

use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drush\Commands\DrushCommands;

/**
* A Drush command file.
*/
class TideDeleteOrphanedParagraphsTest extends DrushCommands {

/**
* The key value store.
*
* @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface
*/
protected $keyValue;

/**
* Constructs a new object.
*
* @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $keyValue
* The key value factory.
*/
public function __construct(
KeyValueFactoryInterface $keyValue
) {
$this->keyValue = $keyValue;
}

/**
* Delete orphaned paragraphs.
*
* @param int $limit
* The maximum number of paragraphs to process.
*
* @param string $number_days_ago
* The number of days to purge the data.
* @command tide_core:delete-orphaned-paragraphs-test
* @aliases dop
* @usage tide_core:delete-orphaned-paragraphs-test 5
*/
public function deleteOrphanedParagraphsTest($limit = 5, $number_days_ago = '-90 days')
{
$kv = $this->keyValue->get('delete-orphaned-paragraphs-test');
$last_checked_p_id = $kv->get('last_checked_p_id', 0);
$thirty_days_ago = strtotime($number_days_ago);
$database = \Drupal::database()
->select('paragraphs_item_field_data', 'p')
->fields('p', ['id'])
->where("p.id > :last_checked_p_id", [':last_checked_p_id' => $last_checked_p_id])
->where("p.created < :thirty_days_ago", [':thirty_days_ago' => $thirty_days_ago])
->orderBy("p.id")
->range(0, $limit);
$results = $database->execute();
$paragraph_ids = $results->fetchCol();
if (empty($paragraph_ids)) {
$this->output()->writeln('No paragraphs found.');
return;
}

foreach ($paragraph_ids as $paragraph_id) {
$paragraph_entity = Paragraph::load($paragraph_id);
if ($paragraph_entity instanceof Paragraph && $paragraph_entity->getParentEntity() === NULL) {
$paragraph_entity->delete();
$this->output()->writeln('Paragraphs deleted.');
}
}
$kv->set('last_checked_p_id', max($paragraph_ids));
}
}
Loading