]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/ArchiveContact.php
ffcd5a1658a245a57ea9dae1d7307798c2e6b0a5
[friendica.git] / src / Core / Console / ArchiveContact.php
1 <?php
2
3 namespace Friendica\Core\Console;
4
5 use Friendica\App;
6 use Friendica\Core\L10n;
7 use Friendica\Database\DBA;
8 use Friendica\Util\Strings;
9 use RuntimeException;
10
11 /**
12  * @brief tool to archive a contact on the server
13  *
14  * With this tool you can archive a contact when you know that it isn't existing anymore.
15  * Normally this does happen automatically after a few days.
16  *
17  * License: AGPLv3 or later, same as Friendica
18  *
19  */
20 class ArchiveContact extends \Asika\SimpleConsole\Console
21 {
22         protected $helpOptions = ['h', 'help', '?'];
23
24         protected function getHelp()
25         {
26                 $help = <<<HELP
27 console archivecontact - archive a contact
28 Usage
29         bin/console archivecontact <profile_url> [-h|--help|-?] [-v]
30
31 Description
32         Archive a contact when you know that it isn't existing anymore. Normally this does happen automatically after a few days.
33
34 Options
35     -h|--help|-? Show help information
36     -v           Show more debug information.
37 HELP;
38                 return $help;
39         }
40
41         protected function doExecute()
42         {
43                 $a = \Friendica\BaseObject::getApp();
44
45                 if ($this->getOption('v')) {
46                         $this->out('Class: ' . __CLASS__);
47                         $this->out('Arguments: ' . var_export($this->args, true));
48                         $this->out('Options: ' . var_export($this->options, true));
49                 }
50
51                 if (count($this->args) == 0) {
52                         $this->out($this->getHelp());
53                         return 0;
54                 }
55
56                 if (count($this->args) > 1) {
57                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
58                 }
59
60                 if ($a->getMode()->isInstall()) {
61                         throw new RuntimeException('Friendica isn\'t properly installed yet.');
62                 }
63
64                 $nurl = Strings::normaliseLink($this->getArgument(0));
65                 if (!DBA::exists('contact', ['nurl' => $nurl, 'archive' => false])) {
66                         throw new RuntimeException(L10n::t('Could not find any unarchived contact entry for this URL (%s)', $nurl));
67                 }
68                 if (DBA::update('contact', ['archive' => true], ['nurl' => $nurl])) {
69                         $condition = ["`cid` IN (SELECT `id` FROM `contact` WHERE `archive`)"];
70                         DBA::delete('queue', $condition);
71                         $this->out(L10n::t('The contact entries have been archived'));
72                 } else {
73                         throw new RuntimeException('The contact archival failed.');
74                 }
75
76                 return 0;
77         }
78 }