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