]> git.mxchange.org Git - friendica.git/blob - src/Console/ArchiveContact.php
Add Mastodon API entity classes
[friendica.git] / src / Console / ArchiveContact.php
1 <?php
2
3 namespace Friendica\Console;
4
5 use Friendica\App;
6 use Friendica\Core\L10n;
7 use Friendica\Database\Database;
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         /**
25          * @var App\Mode
26          */
27         private $appMode;
28         /**
29          * @var Database
30          */
31         private $dba;
32         /**
33          * @var L10n\L10n
34          */
35         private $l10n;
36
37         protected function getHelp()
38         {
39                 $help = <<<HELP
40 console archivecontact - archive a contact
41 Usage
42         bin/console archivecontact <profile_url> [-h|--help|-?] [-v]
43
44 Description
45         Archive a contact when you know that it isn't existing anymore. Normally this does happen automatically after a few days.
46
47 Options
48     -h|--help|-? Show help information
49     -v           Show more debug information.
50 HELP;
51                 return $help;
52         }
53
54         public function __construct(App\Mode $appMode, Database $dba, L10n\L10n $l10n, array $argv = null)
55         {
56                 parent::__construct($argv);
57
58                 $this->appMode = $appMode;
59                 $this->dba = $dba;
60                 $this->l10n = $l10n;
61         }
62
63         protected function doExecute()
64         {
65                 if ($this->getOption('v')) {
66                         $this->out('Class: ' . __CLASS__);
67                         $this->out('Arguments: ' . var_export($this->args, true));
68                         $this->out('Options: ' . var_export($this->options, true));
69                 }
70
71                 if (count($this->args) == 0) {
72                         $this->out($this->getHelp());
73                         return 0;
74                 }
75
76                 if (count($this->args) > 1) {
77                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
78                 }
79
80                 if ($this->appMode->isInstall()) {
81                         throw new RuntimeException('Friendica isn\'t properly installed yet.');
82                 }
83
84                 $nurl = Strings::normaliseLink($this->getArgument(0));
85                 if (!$this->dba->exists('contact', ['nurl' => $nurl, 'archive' => false])) {
86                         throw new RuntimeException(L10n::t('Could not find any unarchived contact entry for this URL (%s)', $nurl));
87                 }
88                 if ($this->dba->update('contact', ['archive' => true], ['nurl' => $nurl])) {
89                         $this->out($this->l10n->t('The contact entries have been archived'));
90                 } else {
91                         throw new RuntimeException('The contact archival failed.');
92                 }
93
94                 return 0;
95         }
96 }