]> git.mxchange.org Git - friendica.git/blob - src/Console/ArchiveContact.php
spelling: unable
[friendica.git] / src / Console / ArchiveContact.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Console;
23
24 use Friendica\App;
25 use Friendica\Database\Database;
26 use Friendica\DI;
27 use Friendica\Model\Contact;
28 use Friendica\Util\Strings;
29 use RuntimeException;
30
31 /**
32  * tool to archive a contact on the server
33  *
34  * With this tool you can archive a contact when you know that it isn't existing anymore.
35  * Normally this does happen automatically after a few days.
36  *
37  * License: AGPLv3 or later, same as Friendica
38  *
39  */
40 class ArchiveContact extends \Asika\SimpleConsole\Console
41 {
42         protected $helpOptions = ['h', 'help', '?'];
43
44         /**
45          * @var App\Mode
46          */
47         private $appMode;
48         /**
49          * @var Database
50          */
51         private $dba;
52         /**
53          * @var \Friendica\Core\L10n
54          */
55         private $l10n;
56
57         protected function getHelp()
58         {
59                 $help = <<<HELP
60 console archivecontact - archive a contact
61 Usage
62         bin/console archivecontact <profile_url> [-h|--help|-?] [-v]
63
64 Description
65         Archive a contact when you know that it isn't existing anymore. Normally this does happen automatically after a few days.
66
67 Options
68     -h|--help|-? Show help information
69     -v           Show more debug information.
70 HELP;
71                 return $help;
72         }
73
74         public function __construct(App\Mode $appMode, Database $dba, \Friendica\Core\L10n $l10n, array $argv = null)
75         {
76                 parent::__construct($argv);
77
78                 $this->appMode = $appMode;
79                 $this->dba = $dba;
80                 $this->l10n = $l10n;
81         }
82
83         protected function doExecute(): int
84         {
85                 if ($this->getOption('v')) {
86                         $this->out('Class: ' . __CLASS__);
87                         $this->out('Arguments: ' . var_export($this->args, true));
88                         $this->out('Options: ' . var_export($this->options, true));
89                 }
90
91                 if (count($this->args) == 0) {
92                         $this->out($this->getHelp());
93                         return 0;
94                 }
95
96                 if (count($this->args) > 1) {
97                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
98                 }
99
100                 if ($this->appMode->isInstall()) {
101                         throw new RuntimeException('Friendica isn\'t properly installed yet.');
102                 }
103
104                 $nurl = Strings::normaliseLink($this->getArgument(0));
105                 if (!$this->dba->exists('contact', ['nurl' => $nurl, 'archive' => false])) {
106                         throw new RuntimeException(DI::l10n()->t('Could not find any unarchived contact entry for this URL (%s)', $nurl));
107                 }
108                 if (Contact::update(['archive' => true], ['nurl' => $nurl])) {
109                         $this->out($this->l10n->t('The contact entries have been archived'));
110                 } else {
111                         throw new RuntimeException('The contact archival failed.');
112                 }
113
114                 return 0;
115         }
116 }