[OfflineBackup] Fix plugin

This commit is contained in:
Diogo Cordeiro 2019-08-10 22:35:45 +01:00 committed by Diogo Peralta Cordeiro
parent 33ac4aed32
commit 76609d8f37
3 changed files with 57 additions and 59 deletions

View File

@ -46,15 +46,18 @@ if (!defined('STATUSNET')) {
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
class OfflineBackupPlugin extends Plugin class OfflineBackupPlugin extends Plugin
{ {
const PLUGIN_VERSION = '2.0.0'; const PLUGIN_VERSION = '2.0.0';
function onRouterInitialized($m) public function onRouterInitialized($m)
{ {
$m->connect('main/backupaccount', $m->connect(
['action' => 'offlinebackup']); 'main/backupaccount',
['action' => 'offlinebackup'],
[],
true // Overwrite native backup tool
);
return true; return true;
} }
@ -63,10 +66,9 @@ class OfflineBackupPlugin extends Plugin
* *
* @param QueueManager $qm current queue manager * @param QueueManager $qm current queue manager
* *
* @return boolean hook value * @return bool hook value
*/ */
public function onEndInitializeQueueManager($qm)
function onEndInitializeQueueManager($qm)
{ {
$qm->connect('backoff', 'OfflineBackupQueueHandler'); $qm->connect('backoff', 'OfflineBackupQueueHandler');
return true; return true;
@ -74,13 +76,15 @@ class OfflineBackupPlugin extends Plugin
public function onPluginVersion(array &$versions): bool public function onPluginVersion(array &$versions): bool
{ {
$versions[] = array('name' => 'OfflineBackup', $versions[] = [
'version' => self::PLUGIN_VERSION, 'name' => 'OfflineBackup',
'author' => 'Evan Prodromou', 'version' => self::PLUGIN_VERSION,
'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/OfflineBackup', 'author' => 'Evan Prodromou',
'rawdescription' => 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/OfflineBackup',
// TRANS: Plugin description. 'rawdescription' =>
_m('Backup user data in offline queue and email when ready.')); // TRANS: Module description.
_m('Backup user data in offline queue and email when ready.')
];
return true; return true;
} }
} }

View File

@ -4,7 +4,7 @@
* Copyright (C) 2011, StatusNet, Inc. * Copyright (C) 2011, StatusNet, Inc.
* *
* Initiate an offline backup * Initiate an offline backup
* *
* PHP version 5 * PHP version 5
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -44,28 +44,26 @@ if (!defined('STATUSNET')) {
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
class OfflinebackupAction extends BackupaccountAction class OfflinebackupAction extends BackupaccountAction
{ {
/** /**
* Handler method * Handler method
* *
* @param array $argarray is ignored since it's now passed in in prepare()
*
* @return void * @return void
* @throws ClientException
* @throws ReflectionException
* @throws ServerException
*/ */
public function handle(): void
function handle($argarray=null)
{ {
if ($this->isPost()) { if ($this->isPost()) {
$this->queueBackup(); $this->queueBackup();
} else { } else {
$this->showPage(); $this->showPage();
} }
return;
} }
function queueBackup() public function queueBackup(): void
{ {
$cur = common_current_user(); $cur = common_current_user();
@ -76,7 +74,7 @@ class OfflinebackupAction extends BackupaccountAction
$this->showPage(); $this->showPage();
} }
function showContent() public function showContent(): void
{ {
if ($this->isPost()) { if ($this->isPost()) {
$this->text(_('Backup queued. You will get a notification by email when your backup is ready to download.')); $this->text(_('Backup queued. You will get a notification by email when your backup is ready to download.'));

View File

@ -4,7 +4,7 @@
* Copyright (C) 2011, StatusNet, Inc. * Copyright (C) 2011, StatusNet, Inc.
* *
* Offline backup queue handler * Offline backup queue handler
* *
* PHP version 5 * PHP version 5
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -44,15 +44,14 @@ if (!defined('STATUSNET')) {
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
class OfflineBackupQueueHandler extends QueueHandler class OfflineBackupQueueHandler extends QueueHandler
{ {
function transport() public function transport()
{ {
return 'backoff'; return 'backoff';
} }
function handle($object) : bool public function handle($object): bool
{ {
$userId = $object; $userId = $object;
@ -69,7 +68,7 @@ class OfflineBackupQueueHandler extends QueueHandler
return true; return true;
} }
function makeBackupFile($user) public function makeBackupFile($user)
{ {
// XXX: this is pretty lose-y; try another way // XXX: this is pretty lose-y; try another way
@ -95,23 +94,27 @@ class OfflineBackupQueueHandler extends QueueHandler
return $fileName; return $fileName;
} }
function notifyBackupFile($user, $fileName) public function notifyBackupFile($user, $fileName)
{ {
$fileUrl = File::url($fileName); $fileUrl = File::url($fileName);
$body = sprintf(_m("The backup file you requested is ready for download.\n\n". $body = sprintf(
_m(
"The backup file you requested is ready for download.\n\n".
"%s\n". "%s\n".
"Thanks for your time,\n", "Thanks for your time,\n",
"%s\n"), "%s\n"
$fileUrl, ),
common_config('site', 'name')); $fileUrl,
common_config('site', 'name')
);
$headers = _mail_prepare_headers('offlinebackup', $user->nickname, $user->nickname); $headers = _mail_prepare_headers('offlinebackup', $user->nickname, $user->nickname);
mail_to_user($user, _('Backup file ready for download'), $body, $headers); mail_to_user($user, _('Backup file ready for download'), $body, $headers);
} }
function dumpNotices($user, $dir) public function dumpNotices($user, $dir)
{ {
common_log(LOG_INFO, 'dumping notices by ' . $user->nickname . ' to directory ' . $dir); common_log(LOG_INFO, 'dumping notices by ' . $user->nickname . ' to directory ' . $dir);
@ -122,12 +125,11 @@ class OfflineBackupQueueHandler extends QueueHandler
$page = 1; $page = 1;
do { do {
$notice = $stream->getNotices(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1); $notice = $stream->getNotices(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
while ($notice->fetch()) { while ($notice->fetch()) {
try { try {
$fname = $dir . '/'. common_date_iso8601($notice->created) . '-notice-' . $notice->id . '.atom'; $fname = $dir . '/'. common_date_iso8601($notice->created) . '-notice-' . $notice->id . '.atom';
$data = $notice->asAtomEntry(false, false, false, null); $data = $notice->asAtomEntry(false, false, false, null);
common_log(LOG_INFO, 'dumping notice ' . $notice->id . ' to file ' . $fname); common_log(LOG_INFO, 'dumping notice ' . $notice->id . ' to file ' . $fname);
file_put_contents($fname, $data); file_put_contents($fname, $data);
@ -139,14 +141,13 @@ class OfflineBackupQueueHandler extends QueueHandler
} }
$page++; $page++;
} while ($notice->N > NOTICES_PER_PAGE); } while ($notice->N > NOTICES_PER_PAGE);
} }
function dumpFaves($user, $dir) public function dumpFaves($user, $dir)
{ {
common_log(LOG_INFO, 'dumping faves by ' . $user->nickname . ' to directory ' . $dir); common_log(LOG_INFO, 'dumping faves by ' . $user->nickname . ' to directory ' . $dir);
$page = 1; $page = 1;
do { do {
@ -154,7 +155,7 @@ class OfflineBackupQueueHandler extends QueueHandler
while ($fave->fetch()) { while ($fave->fetch()) {
try { try {
$fname = $dir . '/'. common_date_iso8601($fave->modified) . '-fave-' . $fave->notice_id . '.atom'; $fname = $dir . '/'. common_date_iso8601($fave->modified) . '-fave-' . $fave->notice_id . '.atom';
$act = $fave->asActivity(); $act = $fave->asActivity();
$data = $act->asString(false, false, false); $data = $act->asString(false, false, false);
common_log(LOG_INFO, 'dumping fave of ' . $fave->notice_id . ' to file ' . $fname); common_log(LOG_INFO, 'dumping fave of ' . $fave->notice_id . ' to file ' . $fname);
@ -165,16 +166,15 @@ class OfflineBackupQueueHandler extends QueueHandler
continue; continue;
} }
} }
$page++;
$page++;
} while ($fave->N > NOTICES_PER_PAGE); } while ($fave->N > NOTICES_PER_PAGE);
} }
function dumpSubscriptions($user, $dir) public function dumpSubscriptions($user, $dir)
{ {
common_log(LOG_INFO, 'dumping subscriptions by ' . $user->nickname . ' to directory ' . $dir); common_log(LOG_INFO, 'dumping subscriptions by ' . $user->nickname . ' to directory ' . $dir);
$page = 1; $page = 1;
do { do {
@ -185,7 +185,7 @@ class OfflineBackupQueueHandler extends QueueHandler
if ($sub->subscribed == $user->id) { if ($sub->subscribed == $user->id) {
continue; continue;
} }
$fname = $dir . '/'. common_date_iso8601($sub->created) . '-subscription-' . $sub->subscribed . '.atom'; $fname = $dir . '/'. common_date_iso8601($sub->created) . '-subscription-' . $sub->subscribed . '.atom';
$act = $sub->asActivity(); $act = $sub->asActivity();
$data = $act->asString(false, false, false); $data = $act->asString(false, false, false);
common_log(LOG_INFO, 'dumping sub of ' . $sub->subscribed . ' to file ' . $fname); common_log(LOG_INFO, 'dumping sub of ' . $sub->subscribed . ' to file ' . $fname);
@ -198,14 +198,13 @@ class OfflineBackupQueueHandler extends QueueHandler
} }
$page++; $page++;
} while ($sub->N > PROFILES_PER_PAGE); } while ($sub->N > PROFILES_PER_PAGE);
} }
function dumpSubscribers($user, $dir) public function dumpSubscribers($user, $dir)
{ {
common_log(LOG_INFO, 'dumping subscribers to ' . $user->nickname . ' to directory ' . $dir); common_log(LOG_INFO, 'dumping subscribers to ' . $user->nickname . ' to directory ' . $dir);
$page = 1; $page = 1;
do { do {
@ -216,7 +215,7 @@ class OfflineBackupQueueHandler extends QueueHandler
if ($sub->subscriber == $user->id) { if ($sub->subscriber == $user->id) {
continue; continue;
} }
$fname = $dir . '/'. common_date_iso8601($sub->created) . '-subscriber-' . $sub->subscriber . '.atom'; $fname = $dir . '/'. common_date_iso8601($sub->created) . '-subscriber-' . $sub->subscriber . '.atom';
$act = $sub->asActivity(); $act = $sub->asActivity();
$data = $act->asString(false, true, false); $data = $act->asString(false, true, false);
common_log(LOG_INFO, 'dumping sub by ' . $sub->subscriber . ' to file ' . $fname); common_log(LOG_INFO, 'dumping sub by ' . $sub->subscriber . ' to file ' . $fname);
@ -229,23 +228,21 @@ class OfflineBackupQueueHandler extends QueueHandler
} }
$page++; $page++;
} while ($sub->N > PROFILES_PER_PAGE); } while ($sub->N > PROFILES_PER_PAGE);
} }
function dumpGroups($user, $dir) public function dumpGroups($user, $dir)
{ {
common_log(LOG_INFO, 'dumping memberships of ' . $user->nickname . ' to directory ' . $dir); common_log(LOG_INFO, 'dumping memberships of ' . $user->nickname . ' to directory ' . $dir);
$page = 1; $page = 1;
do { do {
$mem = Group_member::byMember($user->id, ($page-1)*GROUPS_PER_PAGE, GROUPS_PER_PAGE + 1); $mem = Group_member::byMember($user->id, ($page-1)*GROUPS_PER_PAGE, GROUPS_PER_PAGE + 1);
while ($mem->fetch()) { while ($mem->fetch()) {
try { try {
$fname = $dir . '/'. common_date_iso8601($mem->created) . '-membership-' . $mem->group_id . '.atom'; $fname = $dir . '/'. common_date_iso8601($mem->created) . '-membership-' . $mem->group_id . '.atom';
$act = $mem->asActivity(); $act = $mem->asActivity();
$data = $act->asString(false, false, false); $data = $act->asString(false, false, false);
common_log(LOG_INFO, 'dumping membership in ' . $mem->group_id . ' to file ' . $fname); common_log(LOG_INFO, 'dumping membership in ' . $mem->group_id . ' to file ' . $fname);
@ -258,11 +255,10 @@ class OfflineBackupQueueHandler extends QueueHandler
} }
$page++; $page++;
} while ($mem->N > GROUPS_PER_PAGE); } while ($mem->N > GROUPS_PER_PAGE);
} }
function makeActivityFeed($user, $tmpdir, $fullPath) public function makeActivityFeed($user, $tmpdir, $fullPath)
{ {
$handle = fopen($fullPath, 'c'); $handle = fopen($fullPath, 'c');
@ -285,7 +281,7 @@ class OfflineBackupQueueHandler extends QueueHandler
fclose($handle); fclose($handle);
} }
function writeFeedHeader($user, $handle) public function writeFeedHeader($user, $handle)
{ {
fwrite($handle, '<?xml version="1.0" encoding="UTF-8"?>'); fwrite($handle, '<?xml version="1.0" encoding="UTF-8"?>');
fwrite($handle, "\n"); fwrite($handle, "\n");
@ -302,12 +298,12 @@ class OfflineBackupQueueHandler extends QueueHandler
fwrite($handle, "\n"); fwrite($handle, "\n");
} }
function writeFeedFooter($user, $handle) public function writeFeedFooter($user, $handle)
{ {
fwrite($handle, '</feed>'); fwrite($handle, '</feed>');
} }
function delTree($dir) public function delTree($dir)
{ {
if (is_dir($dir)) { if (is_dir($dir)) {
$objects = scandir($dir); $objects = scandir($dir);