Skip to content
Snippets Groups Projects
Commit 5a19f6e1 authored by Jay Friendly's avatar Jay Friendly
Browse files

Adding Drush command to prepare private message module for installation by...

Adding Drush command to prepare private message module for installation by deleting all private message content
parent 8ff25334
No related branches found
Tags 1.0.2
No related merge requests found
......@@ -18,5 +18,12 @@
"minimum-stability": "dev",
"require": {
"drupal/message_notify": "^1.0"
},
"extra": {
"drush": {
"services": {
"drush.services.yml": "^9"
}
}
}
}
services:
private_message.commands:
class: \Drupal\private_message\Commands\PrivateMessageCommands
arguments:
- '@private_message.service'
- '@entity_type.manager'
tags:
- { name: drush.command }
<?php
namespace Drupal\private_message\Commands;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\private_message\Service\PrivateMessageService;
use Drush\Commands\DrushCommands;
/**
* Creates Drush commands for the Private Message module.
*
* In addition to this file, you need a drush.services.yml
* in root of your module, and a composer.json file that provides the name
* of the services file to use.
*
* See these files for an example of injecting Drupal services:
* - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
* - http://cgit.drupalcode.org/devel/tree/drush.services.yml
*/
class PrivateMessageCommands extends DrushCommands {
use StringTranslationTrait;
/**
* The private message service.
*
* @var \Drupal\private_message\Service\PrivateMessageService
*/
protected $privateMessageService;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a object.
*
* @param \Drupal\private_message\Service\PrivateMessageService $privateMessageService
* The private message service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager service.
*/
public function __construct(
PrivateMessageService $privateMessageService,
EntityTypeManagerInterface $entityTypeManager
) {
parent::__construct();
$this->privateMessageService = $privateMessageService;
$this->entityTypeManager = $entityTypeManager;
}
/**
* Prepares the Private Message module for uninstallation.
*
* @command private_message:prepare_uninstall
*
* @aliases pu
*/
public function prepareUninstall() {
if ($this->io()->confirm($this->t('Proceeding will delete all private messages and private message threads in your system. They cannot be recovered. Do you wish to proceed?')->render())) {
// Get the IDs of all threads in the system.
$thread_ids = $this->privateMessageService->getThreadIds();
// Load the private message thread storage.
$thread_storage = $this->entityTypeManager->getStorage('private_message_thread');
$message_storage = $this->entityTypeManager->getStorage('private_message');
$access_time_storage = $this->entityTypeManager->getStorage('pm_thread_access_time');
$delete_time_storage = $this->entityTypeManager->getStorage('pm_thread_delete_time');
$output = $this->output();
// Note that each thread ID is looped through, as there may be a massive
// number of threads in the system, meaning that loading them all could be
// a crazy memory hog and crash.
foreach ($thread_ids as $thread_id) {
// Load the thread.
$thread = $thread_storage->load($thread_id);
$message_storage->delete($thread->getMessages());
$access_time_storage->delete($thread->getLastAccessTimes());
$delete_time_storage->delete($thread->getLastDeleteTimes());
// Delete the thread.
$thread_storage->delete([$thread]);
// Notify the user.
$output->writeln($this->t('Deleted thread: @id', ['@id' => $thread_id])->render());
}
// Inform the user the process is finished.
$output->writeln($this->t('All private message content deleted.')->render());
}
}
}
......@@ -277,4 +277,11 @@ class PrivateMessageMapper implements PrivateMessageMapperInterface {
)->fetchField();
}
/**
* {@inheritdoc}
*/
public function getThreadIds() {
return $this->database->query('SELECT id FROM {private_message_threads}')->fetchCol();
}
}
......@@ -152,4 +152,12 @@ interface PrivateMessageMapperInterface {
*/
public function getThreadIdFromMessage(PrivateMessageInterface $privateMessage);
/**
* Retrieve the IDs of all threads in the system.
*
* @return array
* An array of thread IDs for threads in the system.
*/
public function getThreadIds();
}
......@@ -361,6 +361,13 @@ class PrivateMessageService implements PrivateMessageServiceInterface {
}
}
/**
* {@inheritdoc}
*/
public function getThreadIds() {
return $this->mapper->getThreadIds();
}
/**
* Create a new private message thread for the given users.
*
......
......@@ -202,4 +202,12 @@ interface PrivateMessageServiceInterface {
*/
public function createRenderablePrivateMessageThreadLink(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode);
/**
* Retrieve the IDs of all threads in the system.
*
* @return array
* An array of thread IDs for threads in the system.
*/
public function getThreadIds();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment