]> git.mxchange.org Git - friendica.git/blob - src/Core/Console.php
Merge pull request #10202 from mexon/mat/contact-console-command
[friendica.git] / src / Core / Console.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\Core;
23
24 use Dice\Dice;
25 use Friendica;
26
27 /**
28  * Description of Console
29  */
30 class Console extends \Asika\SimpleConsole\Console
31 {
32         // Disables the default help handling
33         protected $helpOptions = [];
34         protected $customHelpOptions = ['h', 'help', '?'];
35
36         /**
37          * @var Dice The DI library
38          */
39         protected $dice;
40
41         protected function getHelp()
42         {
43                 $help = <<<HELP
44 Usage: bin/console [--version] [-h|--help|-?] <command> [<args>] [-v]
45
46 Commands:
47         addon                  Addon management
48         cache                  Manage node cache
49         config                 Edit site config
50         contact                Contact management
51         createdoxygen          Generate Doxygen headers
52         dbstructure            Do database updates
53         docbloxerrorchecker    Check the file tree for DocBlox errors
54         extract                Generate translation string file for the Friendica project (deprecated)
55         globalcommunityblock   Block remote profile from interacting with this node
56         globalcommunitysilence Silence a profile from the global community page
57         archivecontact         Archive a contact when you know that it isn't existing anymore
58         help                   Show help about a command, e.g (bin/console help config)
59         autoinstall            Starts automatic installation of friendica based on values from htconfig.php
60         lock                   Edit site locks
61         maintenance            Set maintenance mode for this node
62         user                   User management
63         php2po                 Generate a messages.po file from a strings.php file
64         po2php                 Generate a strings.php file from a messages.po file
65         typo                   Checks for parse errors in Friendica files
66         postupdate             Execute pending post update scripts (can last days)
67         serverblock            Manage blocked servers
68         storage                Manage storage backend
69         relay                  Manage ActivityPub relay servers
70
71 Options:
72         -h|--help|-? Show help information
73         -v           Show more debug information.
74 HELP;
75                 return $help;
76         }
77
78         protected $subConsoles = [
79                 'addon'                  => Friendica\Console\Addon::class,
80                 'cache'                  => Friendica\Console\Cache::class,
81                 'config'                 => Friendica\Console\Config::class,
82                 'contact'                => Friendica\Console\Contact::class,
83                 'createdoxygen'          => Friendica\Console\CreateDoxygen::class,
84                 'docbloxerrorchecker'    => Friendica\Console\DocBloxErrorChecker::class,
85                 'dbstructure'            => Friendica\Console\DatabaseStructure::class,
86                 'extract'                => Friendica\Console\Extract::class,
87                 'globalcommunityblock'   => Friendica\Console\GlobalCommunityBlock::class,
88                 'globalcommunitysilence' => Friendica\Console\GlobalCommunitySilence::class,
89                 'archivecontact'         => Friendica\Console\ArchiveContact::class,
90                 'autoinstall'            => Friendica\Console\AutomaticInstallation::class,
91                 'lock'                   => Friendica\Console\Lock::class,
92                 'maintenance'            => Friendica\Console\Maintenance::class,
93                 'user'                   => Friendica\Console\User::class,
94                 'php2po'                 => Friendica\Console\PhpToPo::class,
95                 'po2php'                 => Friendica\Console\PoToPhp::class,
96                 'typo'                   => Friendica\Console\Typo::class,
97                 'postupdate'             => Friendica\Console\PostUpdate::class,
98                 'serverblock'            => Friendica\Console\ServerBlock::class,
99                 'storage'                => Friendica\Console\Storage::class,
100                 'relay'                  => Friendica\Console\Relay::class,
101                 'fixapdeliveryworkertaskparameters' => Friendica\Console\FixAPDeliveryWorkerTaskParameters::class,
102         ];
103
104         /**
105          * CliInput Friendica constructor.
106          *
107          * @param Dice $dice The DI library
108          * @param array $argv
109          */
110         public function __construct(Dice $dice, array $argv = null)
111         {
112                 parent::__construct($argv);
113
114                 $this->dice = $dice;
115         }
116
117         protected function doExecute()
118         {
119                 if ($this->getOption('v')) {
120                         $this->out('Executable: ' . $this->executable);
121                         $this->out('Arguments: ' . var_export($this->args, true));
122                         $this->out('Options: ' . var_export($this->options, true));
123                 }
124
125                 $subHelp = false;
126                 $command = null;
127
128                 if ($this->getOption('version')) {
129                         $this->out('Friendica Console version ' . FRIENDICA_VERSION);
130
131                         return 0;
132                 } elseif ((count($this->options) === 0 || $this->getOption($this->customHelpOptions) === true || $this->getOption($this->customHelpOptions) === 1) && count($this->args) === 0
133                 ) {
134                 } elseif (count($this->args) >= 2 && $this->getArgument(0) == 'help') {
135                         $command = $this->getArgument(1);
136                         $subHelp = true;
137                         array_shift($this->args);
138                         array_shift($this->args);
139                 } elseif (count($this->args) >= 1) {
140                         $command = $this->getArgument(0);
141                         array_shift($this->args);
142                 }
143
144                 if (is_null($command)) {
145                         $this->out($this->getHelp());
146                         return 0;
147                 }
148
149                 $console = $this->getSubConsole($command);
150
151                 if ($subHelp) {
152                         $console->setOption($this->customHelpOptions, true);
153                 }
154
155                 return $console->execute();
156         }
157
158         private function getSubConsole($command)
159         {
160                 if ($this->getOption('v')) {
161                         $this->out('Command: ' . $command);
162                 }
163
164                 if (!isset($this->subConsoles[$command])) {
165                         throw new \Asika\SimpleConsole\CommandArgsException('Command ' . $command . ' doesn\'t exist');
166                 }
167
168                 $subargs = $this->args;
169                 array_unshift($subargs, $this->executable);
170
171                 $className = $this->subConsoles[$command];
172
173                 Friendica\DI::init($this->dice);
174
175                 /** @var Console $subconsole */
176                 $subconsole = $this->dice->create($className, [$subargs]);
177
178                 foreach ($this->options as $name => $value) {
179                         $subconsole->setOption($name, $value);
180                 }
181
182                 return $subconsole;
183         }
184
185 }