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