]> git.mxchange.org Git - friendica.git/blob - src/Console/FixAPDeliveryWorkerTaskParameters.php
spelling: unable
[friendica.git] / src / Console / FixAPDeliveryWorkerTaskParameters.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Console;
23
24 use Friendica\App;
25 use Friendica\Database\Database;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\Contact;
29 use Friendica\Util\Strings;
30 use RuntimeException;
31
32 /**
33  * License: AGPLv3 or later, same as Friendica
34  */
35 class FixAPDeliveryWorkerTaskParameters extends \Asika\SimpleConsole\Console
36 {
37         protected $helpOptions = ['h', 'help', '?'];
38
39         /**
40          * @var App\Mode
41          */
42         private $appMode;
43         /**
44          * @var Database
45          */
46         private $dba;
47         /**
48          * @var int
49          */
50         private $examined;
51         /**
52          * @var int
53          */
54         private $processed;
55         /**
56          * @var int
57          */
58         private $errored;
59
60         protected function getHelp()
61         {
62                 $help = <<<HELP
63 console fixapdeliveryworkertaskparameters - fix APDelivery worker task parameters corrupted during the 2020.12 RC period
64 Usage
65         bin/console fixapdeliveryworkertaskparameters [-h|--help|-?] [-v]
66
67 Description
68         During the 2020.12 RC period some worker task parameters have been corrupted, resulting in the impossibility to execute them.
69         This command restores their expected parameters.
70         If you didn't run Friendica during the 2020.12 RC period, you do not need to use this command. 
71
72 Options
73     -h|--help|-? Show help information
74     -v           Show more debug information.
75 HELP;
76                 return $help;
77         }
78
79         public function __construct(App\Mode $appMode, Database $dba, \Friendica\Core\L10n $l10n, array $argv = null)
80         {
81                 parent::__construct($argv);
82
83                 $this->appMode = $appMode;
84                 $this->dba = $dba;
85                 $this->l10n = $l10n;
86         }
87
88         protected function doExecute(): int
89         {
90                 if ($this->getOption('v')) {
91                         $this->out('Class: ' . __CLASS__);
92                         $this->out('Arguments: ' . var_export($this->args, true));
93                         $this->out('Options: ' . var_export($this->options, true));
94                 }
95
96                 if (count($this->args) > 0) {
97                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
98                 }
99
100                 if ($this->appMode->isInstall()) {
101                         throw new RuntimeException('Friendica isn\'t properly installed yet.');
102                 }
103
104                 $this->examined = 0;
105                 $this->processed = 0;
106                 $this->errored = 0;
107
108                 do {
109                         $result = $this->dba->select('workerqueue', ['id', 'parameter'], ["`command` = ? AND `parameter` LIKE ?", "APDelivery", "[\"%\",\"\",%"], ['limit' => [$this->examined, 100]]);
110                         while ($row = $this->dba->fetch($result)) {
111                                 $this->examined++;
112                                 $this->processRow($row);
113                         }
114                 } while ($this->dba->isResult($result));
115
116                 if ($this->getOption('v')) {
117                         $this->out('Examined: ' . $this->examined);
118                         $this->out('Processed: ' . $this->processed);
119                         $this->out('Errored: ' . $this->errored);
120                 }
121
122                 return 0;
123         }
124
125         private function processRow(array $workerqueueItem)
126         {
127                 $parameters = json_decode($workerqueueItem['parameter'], true);
128
129                 if (!$parameters) {
130                         $this->errored++;
131                         if ($this->getOption('v')) {
132                                 $this->out('Unable to parse parameter JSON of the row with id ' . $workerqueueItem['id']);
133                                 $this->out('JSON: ' . var_export($workerqueueItem['parameter'], true));
134                         }
135                 }
136
137                 if ($parameters[1] !== '' && !is_array($parameters[2])) {
138                         // Nothing to do, we save a write
139                         return;
140                 }
141
142                 if ($parameters[1] === '') {
143                         $parameters[1] = 0;
144                 }
145
146                 if (is_array($parameters[2])) {
147                         $parameters[4] = $parameters[2];
148                         $contact = Contact::getById(current($parameters[2]), ['url']);
149                         $parameters[2] = $contact['url'];
150                 }
151
152                 $fields = ['parameter' => json_encode($parameters)];
153                 if ($this->dba->update('workerqueue', $fields, ['id' => $workerqueueItem['id']])) {
154                         $this->processed++;
155                 } else {
156                         $this->errored++;
157                         if ($this->getOption('v')) {
158                                 $this->out('Unable to update the row with id ' . $workerqueueItem['id']);
159                                 $this->out('Fields: ' . var_export($fields, true));
160                         }
161                 }
162         }
163 }