Add JSON output for backups

This commit is contained in:
Evan Prodromou 2013-05-24 13:50:24 -04:00
parent b359854150
commit 4f818c5c81
2 changed files with 37 additions and 11 deletions

View File

@ -99,12 +99,10 @@ class UserActivityStream extends AtomUserNoticeFeed
* Interleave the pre-sorted subs/groups/faves with the user's * Interleave the pre-sorted subs/groups/faves with the user's
* notices, all in reverse chron order. * notices, all in reverse chron order.
*/ */
function renderEntries() function renderEntries($format=Feed::ATOM, $handle=null)
{ {
$end = time() + 1; $end = time() + 1;
$i = 0;
foreach ($this->objs as $obj) { foreach ($this->objs as $obj) {
$i++;
try { try {
$act = $obj->asActivity(); $act = $obj->asActivity();
} catch (Exception $e) { } catch (Exception $e) {
@ -122,7 +120,11 @@ class UserActivityStream extends AtomUserNoticeFeed
foreach ($notices as $noticeAct) { foreach ($notices as $noticeAct) {
try { try {
$nact = $noticeAct->asActivity(); $nact = $noticeAct->asActivity();
$nact->outputTo($this, false, false); if ($format == Feed::ATOM) {
$nact->outputTo($this, false, false);
} else {
fwrite($handle, json_encode($nact->asArray()));
}
} catch (Exception $e) { } catch (Exception $e) {
common_log(LOG_ERR, $e->getMessage()); common_log(LOG_ERR, $e->getMessage());
continue; continue;
@ -138,8 +140,16 @@ class UserActivityStream extends AtomUserNoticeFeed
$notices = null; $notices = null;
unset($notices); unset($notices);
// Only show the author sub-element if it's different from default user try {
$act->outputTo($this, false, ($act->actor->id != $this->user->uri)); if ($format == Feed::ATOM) {
// Only show the author sub-element if it's different from default user
$act->outputTo($this, false, ($act->actor->id != $this->user->uri));
} else {
fwrite($handle, json_encode($act->asArray()));
}
} catch (Exception $e) {
common_log(LOG_ERR, $e->getMessage());
}
$act = null; $act = null;
unset($act); unset($act);
@ -154,7 +164,11 @@ class UserActivityStream extends AtomUserNoticeFeed
foreach ($notices as $noticeAct) { foreach ($notices as $noticeAct) {
try { try {
$nact = $noticeAct->asActivity(); $nact = $noticeAct->asActivity();
$nact->outputTo($this, false, false); if ($format == Feed::ATOM) {
$nact->outputTo($this, false, false);
} else {
fwrite($handle, json_encode($nact->asArray()));
}
} catch (Exception $e) { } catch (Exception $e) {
common_log(LOG_ERR, $e->getMessage()); common_log(LOG_ERR, $e->getMessage());
continue; continue;
@ -284,4 +298,12 @@ class UserActivityStream extends AtomUserNoticeFeed
return $groups; return $groups;
} }
function writeJSON($handle)
{
require_once INSTALLDIR.'/lib/activitystreamjsondocument.php';
fwrite($handle, '{"items": [');
$this->renderEntries(Feed::JSON, $handle);
fwrite($handle, ']');
}
} }

View File

@ -19,8 +19,8 @@
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
$shortoptions = 'i:n:f:'; $shortoptions = 'i:n:f:j';
$longoptions = array('id=', 'nickname=', 'file='); $longoptions = array('id=', 'nickname=', 'file=', 'json');
$helptext = <<<END_OF_EXPORTACTIVITYSTREAM_HELP $helptext = <<<END_OF_EXPORTACTIVITYSTREAM_HELP
exportactivitystream.php [options] exportactivitystream.php [options]
@ -28,7 +28,7 @@ Export a StatusNet user history to a file
-i --id ID of user to export -i --id ID of user to export
-n --nickname nickname of the user to export -n --nickname nickname of the user to export
-f --file file to export to (default STDOUT) -j --json Output JSON (default Atom)
END_OF_EXPORTACTIVITYSTREAM_HELP; END_OF_EXPORTACTIVITYSTREAM_HELP;
@ -37,7 +37,11 @@ require_once INSTALLDIR.'/scripts/commandline.inc';
try { try {
$user = getUser(); $user = getUser();
$actstr = new UserActivityStream($user, true, UserActivityStream::OUTPUT_RAW); $actstr = new UserActivityStream($user, true, UserActivityStream::OUTPUT_RAW);
print $actstr->getString(); if (have_option('j', 'json')) {
$actstr->writeJSON(STDOUT);
} else {
print $actstr->getString();
}
} catch (Exception $e) { } catch (Exception $e) {
print $e->getMessage()."\n"; print $e->getMessage()."\n";
exit(1); exit(1);