]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/ArchiveContact.php
Avoid memory issue in exception
[friendica.git] / src / Core / Console / ArchiveContact.php
1 <?php
2
3 namespace Friendica\Core\Console;
4
5 use Friendica\Core\L10n;
6 use Friendica\Database\DBA;
7 use Friendica\Util\Strings;
8 use RuntimeException;
9
10 /**
11  * @brief 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         protected function getHelp()
24         {
25                 $help = <<<HELP
26 console archivecontact - archive a contact
27 Usage
28         bin/console archivecontact <profile_url> [-h|--help|-?] [-v]
29
30 Description
31         Archive a contact when you know that it isn't existing anymore. Normally this does happen automatically after a few days.
32
33 Options
34     -h|--help|-? Show help information
35     -v           Show more debug information.
36 HELP;
37                 return $help;
38         }
39
40         protected function doExecute()
41         {
42                 $a = \Friendica\BaseObject::getApp();
43
44                 if ($this->getOption('v')) {
45                         $this->out('Class: ' . __CLASS__);
46                         $this->out('Arguments: ' . var_export($this->args, true));
47                         $this->out('Options: ' . var_export($this->options, true));
48                 }
49
50                 if (count($this->args) == 0) {
51                         $this->out($this->getHelp());
52                         return 0;
53                 }
54
55                 if (count($this->args) > 1) {
56                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
57                 }
58
59                 if ($a->getMode()->isInstall()) {
60                         throw new RuntimeException('Friendica isn\'t properly installed yet.');
61                 }
62
63                 $nurl = Strings::normaliseLink($this->getArgument(0));
64                 if (!DBA::exists('contact', ['nurl' => $nurl, 'archive' => false])) {
65                         throw new RuntimeException(L10n::t('Could not find any unarchived contact entry for this URL (%s)', $nurl));
66                 }
67                 if (DBA::update('contact', ['archive' => true], ['nurl' => $nurl])) {
68                         $condition = ["`cid` IN (SELECT `id` FROM `contact` WHERE `archive`)"];
69                         DBA::delete('queue', $condition);
70                         $this->out(L10n::t('The contact entries have been archived'));
71                 } else {
72                         throw new RuntimeException('The contact archival failed.');
73                 }
74
75                 return 0;
76         }
77 }