]> git.mxchange.org Git - friendica.git/blob - src/Console/ArchiveContact.php
Fix usage
[friendica.git] / src / Console / ArchiveContact.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Util\Strings;
28 use RuntimeException;
29
30 /**
31  * tool to archive a contact on the server
32  *
33  * With this tool you can archive a contact when you know that it isn't existing anymore.
34  * Normally this does happen automatically after a few days.
35  *
36  * License: AGPLv3 or later, same as Friendica
37  *
38  */
39 class ArchiveContact extends \Asika\SimpleConsole\Console
40 {
41         protected $helpOptions = ['h', 'help', '?'];
42
43         /**
44          * @var App\Mode
45          */
46         private $appMode;
47         /**
48          * @var Database
49          */
50         private $dba;
51         /**
52          * @var \Friendica\Core\L10n
53          */
54         private $l10n;
55
56         protected function getHelp()
57         {
58                 $help = <<<HELP
59 console archivecontact - archive a contact
60 Usage
61         bin/console archivecontact <profile_url> [-h|--help|-?] [-v]
62
63 Description
64         Archive a contact when you know that it isn't existing anymore. Normally this does happen automatically after a few days.
65
66 Options
67     -h|--help|-? Show help information
68     -v           Show more debug information.
69 HELP;
70                 return $help;
71         }
72
73         public function __construct(App\Mode $appMode, Database $dba, \Friendica\Core\L10n $l10n, array $argv = null)
74         {
75                 parent::__construct($argv);
76
77                 $this->appMode = $appMode;
78                 $this->dba = $dba;
79                 $this->l10n = $l10n;
80         }
81
82         protected function doExecute()
83         {
84                 if ($this->getOption('v')) {
85                         $this->out('Class: ' . __CLASS__);
86                         $this->out('Arguments: ' . var_export($this->args, true));
87                         $this->out('Options: ' . var_export($this->options, true));
88                 }
89
90                 if (count($this->args) == 0) {
91                         $this->out($this->getHelp());
92                         return 0;
93                 }
94
95                 if (count($this->args) > 1) {
96                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
97                 }
98
99                 if ($this->appMode->isInstall()) {
100                         throw new RuntimeException('Friendica isn\'t properly installed yet.');
101                 }
102
103                 $nurl = Strings::normaliseLink($this->getArgument(0));
104                 if (!$this->dba->exists('contact', ['nurl' => $nurl, 'archive' => false])) {
105                         throw new RuntimeException(DI::l10n()->t('Could not find any unarchived contact entry for this URL (%s)', $nurl));
106                 }
107                 if ($this->dba->update('contact', ['archive' => true], ['nurl' => $nurl])) {
108                         $this->out($this->l10n->t('The contact entries have been archived'));
109                 } else {
110                         throw new RuntimeException('The contact archival failed.');
111                 }
112
113                 return 0;
114         }
115 }