-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moving from content-vic and adding drush services file
- Loading branch information
1 parent
86d5c24
commit cb396c8
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |