2b25d84433cf0fbe2f51ecc8772a13bc2f571a8b
[friendica.git/.git] / src / Core / Console / GlobalCommunitySilence.php
1 <?php
2
3 namespace Friendica\Core\Console;
4
5 use Friendica\Core\Protocol;
6 use Friendica\Database\DBA;
7 use Friendica\Network\Probe;
8 use RuntimeException;
9
10 require_once 'include/text.php';
11
12 /**
13  * @brief tool to silence accounts on the global community page
14  *
15  * With this tool, you can silence an account on the global community page.
16  * Postings from silenced accounts will not be displayed on the community
17  * page. This silencing does only affect the display on the community page,
18  * accounts following the silenced accounts will still get their postings.
19  *
20  * License: AGPLv3 or later, same as Friendica
21  *
22  * @author Tobias Diekershoff
23  * @author Hypolite Petovan <mrpetovan@gmail.com>
24  */
25 class GlobalCommunitySilence extends \Asika\SimpleConsole\Console
26 {
27         protected $helpOptions = ['h', 'help', '?'];
28
29         protected function getHelp()
30         {
31                 $help = <<<HELP
32 console globalcommunitysilence - Silence remote profile from global community page
33 Usage
34         bin/console globalcommunitysilence <profile_url> [-h|--help|-?] [-v]
35
36 Description
37         With this tool, you can silence an account on the global community page.
38         Postings from silenced accounts will not be displayed on the community page.
39         This silencing does only affect the display on the community page, accounts
40         following the silenced accounts will still get their postings.
41
42 Options
43     -h|--help|-? Show help information
44     -v           Show more debug information.
45 HELP;
46                 return $help;
47         }
48
49         protected function doExecute()
50         {
51                 $a = get_app();
52
53                 if ($this->getOption('v')) {
54                         $this->out('Class: ' . __CLASS__);
55                         $this->out('Arguments: ' . var_export($this->args, true));
56                         $this->out('Options: ' . var_export($this->options, true));
57                 }
58
59                 if (count($this->args) == 0) {
60                         $this->out($this->getHelp());
61                         return 0;
62                 }
63
64                 if (count($this->args) > 1) {
65                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
66                 }
67
68                 if ($a->isInstallMode()) {
69                         throw new RuntimeException('Database isn\'t ready or populated yet');
70                 }
71
72                 /**
73                  * 1. make nurl from last parameter
74                  * 2. check DB (contact) if there is a contact with uid=0 and that nurl, get the ID
75                  * 3. set the flag hidden=1 for the contact entry with the found ID
76                  * */
77                 $net = Probe::uri($this->getArgument(0));
78                 if (in_array($net['network'], [Protocol::PHANTOM, Protocol::MAIL])) {
79                         throw new RuntimeException('This account seems not to exist.');
80                 }
81
82                 $nurl = normalise_link($net['url']);
83                 $contact = DBA::selectFirst("contact", ["id"], ["nurl" => $nurl, "uid" => 0]);
84                 if (DBA::isResult($contact)) {
85                         DBA::update("contact", ["hidden" => true], ["id" => $contact["id"]]);
86                         $this->out('NOTICE: The account should be silenced from the global community page');
87                 } else {
88                         throw new RuntimeException('NOTICE: Could not find any entry for this URL (' . $nurl . ')');
89                 }
90
91                 return 0;
92         }
93 }