diff --git a/src/Console/FixAPDeliveryWorkerTaskParameters.php b/src/Console/FixAPDeliveryWorkerTaskParameters.php new file mode 100644 index 0000000000..9023d84ac2 --- /dev/null +++ b/src/Console/FixAPDeliveryWorkerTaskParameters.php @@ -0,0 +1,163 @@ +. + * + */ + +namespace Friendica\Console; + +use Friendica\App; +use Friendica\Database\Database; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Model\Contact; +use Friendica\Util\Strings; +use RuntimeException; + +/** + * License: AGPLv3 or later, same as Friendica + */ +class FixAPDeliveryWorkerTaskParameters extends \Asika\SimpleConsole\Console +{ + protected $helpOptions = ['h', 'help', '?']; + + /** + * @var App\Mode + */ + private $appMode; + /** + * @var Database + */ + private $dba; + /** + * @var int + */ + private $examined; + /** + * @var int + */ + private $processed; + /** + * @var int + */ + private $errored; + + protected function getHelp() + { + $help = <<appMode = $appMode; + $this->dba = $dba; + $this->l10n = $l10n; + } + + protected function doExecute() + { + if ($this->getOption('v')) { + $this->out('Class: ' . __CLASS__); + $this->out('Arguments: ' . var_export($this->args, true)); + $this->out('Options: ' . var_export($this->options, true)); + } + + if (count($this->args) > 0) { + throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments'); + } + + if ($this->appMode->isInstall()) { + throw new RuntimeException('Friendica isn\'t properly installed yet.'); + } + + $this->examined = 0; + $this->processed = 0; + $this->errored = 0; + + do { + $result = $this->dba->p('SELECT `id`, `parameter` FROM `workerqueue` WHERE `command` = "APDelivery" AND `parameter` LIKE "[\"%\",\"\",%" LIMIT ' . $this->examined . ', 100'); + while ($row = $this->dba->fetch($result)) { + $this->examined++; + $this->processRow($row); + } + } while ($this->dba->isResult($result)); + + if ($this->getOption('v')) { + $this->out('Examined: ' . $this->examined); + $this->out('Processed: ' . $this->processed); + $this->out('Errored: ' . $this->errored); + } + + return 0; + } + + private function processRow(array $workerqueueItem) + { + $parameters = json_decode($workerqueueItem['parameter'], true); + + if (!$parameters) { + $this->errored++; + if ($this->getOption('v')) { + $this->out('Unabled to parse parameter JSON of the row with id ' . $workerqueueItem['id']); + $this->out('JSON: ' . var_export($workerqueueItem['parameter'], true)); + } + } + + if ($parameters[1] !== '' && !is_array($parameters[2])) { + // Nothing to do, we save a write + return; + } + + if ($parameters[1] === '') { + $parameters[1] = 0; + } + + if (is_array($parameters[2])) { + $parameters[4] = $parameters[2]; + $contact = Contact::getById(current($parameters[2]), ['url']); + $parameters[2] = $contact['url']; + } + + $fields = ['parameter' => json_encode($parameters)]; + if ($this->dba->update('workerqueue', $fields, ['id' => $workerqueueItem['id']])) { + $this->processed++; + } else { + $this->errored++; + if ($this->getOption('v')) { + $this->out('Unabled to update the row with id ' . $workerqueueItem['id']); + $this->out('Fields: ' . var_export($fields, true)); + } + } + } +} diff --git a/src/Core/Console.php b/src/Core/Console.php index 4a4dc13ef7..f43b89e9e6 100644 --- a/src/Core/Console.php +++ b/src/Core/Console.php @@ -94,6 +94,7 @@ HELP; 'serverblock' => Friendica\Console\ServerBlock::class, 'storage' => Friendica\Console\Storage::class, 'relay' => Friendica\Console\Relay::class, + 'fixapdeliveryworkertaskparameters' => Friendica\Console\FixAPDeliveryWorkerTaskParameters::class, ]; /**