]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/ArchiveContact.php
Merge pull request #5096 from tobiasd/3.6.1
[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                 require_once '.htconfig.php';
58                 $result = \dba::connect($db_host, $db_user, $db_pass, $db_data);
59                 unset($db_host, $db_user, $db_pass, $db_data);
60
61                 if (!$result) {
62                         throw new \RuntimeException('Unable to connect to database');
63                 }
64
65                 $nurl = normalise_link($this->getArgument(0));
66                 if (!dba::exists('contact', ['nurl' => $nurl, 'archive' => false])) {
67                         throw new \RuntimeException(L10n::t('Could not find any unarchived contact entry for this URL (%s)', $nurl));
68                 }
69                 if (dba::update('contact', ['archive' => true], ['nurl' => $nurl])) {
70                         $condition = ["`cid` IN (SELECT `id` FROM `contact` WHERE `archive`)"];
71                         dba::delete('queue', $condition);
72                         $this->out(L10n::t('The contact entries have been archived'));
73                 } else {
74                         throw new \RuntimeException('The contact archival failed.');
75                 }
76
77                 return 0;
78         }
79 }