[OfflineBackup] Fix plugin

This commit is contained in:
Diogo Cordeiro 2019-08-10 22:35:45 +01:00
parent 566214ac62
commit 25ee5ed1f9
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
* @link http://status.net/
*/
class OfflineBackupPlugin extends Plugin
{
const PLUGIN_VERSION = '2.0.0';
function onRouterInitialized($m)
public function onRouterInitialized($m)
{
$m->connect('main/backupaccount',
['action' => 'offlinebackup']);
$m->connect(
'main/backupaccount',
['action' => 'offlinebackup'],
[],
true // Overwrite native backup tool
);
return true;
}
@ -63,10 +66,9 @@ class OfflineBackupPlugin extends Plugin
*
* @param QueueManager $qm current queue manager
*
* @return boolean hook value
* @return bool hook value
*/
function onEndInitializeQueueManager($qm)
public function onEndInitializeQueueManager($qm)
{
$qm->connect('backoff', 'OfflineBackupQueueHandler');
return true;
@ -74,13 +76,15 @@ class OfflineBackupPlugin extends Plugin
public function onPluginVersion(array &$versions): bool
{
$versions[] = array('name' => 'OfflineBackup',
'version' => self::PLUGIN_VERSION,
'author' => 'Evan Prodromou',
'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/OfflineBackup',
'rawdescription' =>
// TRANS: Plugin description.
_m('Backup user data in offline queue and email when ready.'));
$versions[] = [
'name' => 'OfflineBackup',
'version' => self::PLUGIN_VERSION,
'author' => 'Evan Prodromou',
'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/OfflineBackup',
'rawdescription' =>
// TRANS: Module description.
_m('Backup user data in offline queue and email when ready.')
];
return true;
}
}

View File

@ -4,7 +4,7 @@
* Copyright (C) 2011, StatusNet, Inc.
*
* Initiate an offline backup
*
*
* PHP version 5
*
* 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
* @link http://status.net/
*/
class OfflinebackupAction extends BackupaccountAction
{
/**
* Handler method
*
* @param array $argarray is ignored since it's now passed in in prepare()
*
* @return void
* @throws ClientException
* @throws ReflectionException
* @throws ServerException
*/
function handle($argarray=null)
public function handle(): void
{
if ($this->isPost()) {
$this->queueBackup();
} else {
$this->showPage();
}
return;
}
function queueBackup()
public function queueBackup(): void
{
$cur = common_current_user();
@ -76,7 +74,7 @@ class OfflinebackupAction extends BackupaccountAction
$this->showPage();
}
function showContent()
public function showContent(): void
{
if ($this->isPost()) {
$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.
*
* Offline backup queue handler
*
*
* PHP version 5
*
* 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
* @link http://status.net/
*/
class OfflineBackupQueueHandler extends QueueHandler
{
function transport()
public function transport()
{
return 'backoff';
}
function handle($object) : bool
public function handle($object): bool
{
$userId = $object;
@ -69,7 +68,7 @@ class OfflineBackupQueueHandler extends QueueHandler
return true;
}
function makeBackupFile($user)
public function makeBackupFile($user)
{
// XXX: this is pretty lose-y; try another way
@ -95,23 +94,27 @@ class OfflineBackupQueueHandler extends QueueHandler
return $fileName;
}
function notifyBackupFile($user, $fileName)
public function notifyBackupFile($user, $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".
"Thanks for your time,\n",
"%s\n"),
$fileUrl,
common_config('site', 'name'));
"%s\n"
),
$fileUrl,
common_config('site', 'name')
);
$headers = _mail_prepare_headers('offlinebackup', $user->nickname, $user->nickname);
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);
@ -122,12 +125,11 @@ class OfflineBackupQueueHandler extends QueueHandler
$page = 1;
do {
$notice = $stream->getNotices(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
while ($notice->fetch()) {
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);
common_log(LOG_INFO, 'dumping notice ' . $notice->id . ' to file ' . $fname);
file_put_contents($fname, $data);
@ -139,14 +141,13 @@ class OfflineBackupQueueHandler extends QueueHandler
}
$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);
$page = 1;
do {
@ -154,7 +155,7 @@ class OfflineBackupQueueHandler extends QueueHandler
while ($fave->fetch()) {
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();
$data = $act->asString(false, false, false);
common_log(LOG_INFO, 'dumping fave of ' . $fave->notice_id . ' to file ' . $fname);
@ -165,16 +166,15 @@ class OfflineBackupQueueHandler extends QueueHandler
continue;
}
}
$page++;
$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);
$page = 1;
do {
@ -185,7 +185,7 @@ class OfflineBackupQueueHandler extends QueueHandler
if ($sub->subscribed == $user->id) {
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();
$data = $act->asString(false, false, false);
common_log(LOG_INFO, 'dumping sub of ' . $sub->subscribed . ' to file ' . $fname);
@ -198,14 +198,13 @@ class OfflineBackupQueueHandler extends QueueHandler
}
$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);
$page = 1;
do {
@ -216,7 +215,7 @@ class OfflineBackupQueueHandler extends QueueHandler
if ($sub->subscriber == $user->id) {
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();
$data = $act->asString(false, true, false);
common_log(LOG_INFO, 'dumping sub by ' . $sub->subscriber . ' to file ' . $fname);
@ -229,23 +228,21 @@ class OfflineBackupQueueHandler extends QueueHandler
}
$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);
$page = 1;
do {
$mem = Group_member::byMember($user->id, ($page-1)*GROUPS_PER_PAGE, GROUPS_PER_PAGE + 1);
while ($mem->fetch()) {
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();
$data = $act->asString(false, false, false);
common_log(LOG_INFO, 'dumping membership in ' . $mem->group_id . ' to file ' . $fname);
@ -258,11 +255,10 @@ class OfflineBackupQueueHandler extends QueueHandler
}
$page++;
} while ($mem->N > GROUPS_PER_PAGE);
}
function makeActivityFeed($user, $tmpdir, $fullPath)
public function makeActivityFeed($user, $tmpdir, $fullPath)
{
$handle = fopen($fullPath, 'c');
@ -285,7 +281,7 @@ class OfflineBackupQueueHandler extends QueueHandler
fclose($handle);
}
function writeFeedHeader($user, $handle)
public function writeFeedHeader($user, $handle)
{
fwrite($handle, '<?xml version="1.0" encoding="UTF-8"?>');
fwrite($handle, "\n");
@ -302,12 +298,12 @@ class OfflineBackupQueueHandler extends QueueHandler
fwrite($handle, "\n");
}
function writeFeedFooter($user, $handle)
public function writeFeedFooter($user, $handle)
{
fwrite($handle, '</feed>');
}
function delTree($dir)
public function delTree($dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);