From 2580c636d021a43d7edb166b8c70accb7965922d Mon Sep 17 00:00:00 2001 From: fabrixxm Date: Tue, 27 Apr 2021 09:54:42 +0200 Subject: [PATCH 1/4] OPML Export Export user's active RSS/Atom contacts as OPML. --- opmlexport/README.md | 5 ++ opmlexport/opmlexport.php | 89 +++++++++++++++++++++++++++++++ opmlexport/templates/settings.tpl | 12 +++++ 3 files changed, 106 insertions(+) create mode 100644 opmlexport/README.md create mode 100644 opmlexport/opmlexport.php create mode 100644 opmlexport/templates/settings.tpl diff --git a/opmlexport/README.md b/opmlexport/README.md new file mode 100644 index 000000000..6d944de9e --- /dev/null +++ b/opmlexport/README.md @@ -0,0 +1,5 @@ +# OPML Export + +Export user's active RSS/Atom contacts as OPML. + +Launch from `Settings` -> `Addons` -> `OPML Export` \ No newline at end of file diff --git a/opmlexport/opmlexport.php b/opmlexport/opmlexport.php new file mode 100644 index 000000000..0c53fec6c --- /dev/null +++ b/opmlexport/opmlexport.php @@ -0,0 +1,89 @@ + + * License: 3-clause BSD license + */ + +use Friendica\DI; +use Friendica\App; +use Friendica\Core\Hook; +use Friendica\Core\Logger; +use Friendica\Network\HTTPException; +use Friendica\Database\DBA; +use Friendica\Core\Renderer; + +function opmlexport_install() { + Hook::register("addon_settings", __FILE__, "opmlexport_addon_settings"); + Hook::register("addon_settings_post", __FILE__, "opmlexport_addon_settings_post"); + Logger::log("installed opmlexport Addon"); +} + + +function opmlexport(App $a) { + $stmt = DBA::p("SELECT * + FROM `contact` + WHERE `uid` = ? + AND `self` = 0 + AND NOT `deleted` + AND NOT `archive` AND NOT `blocked` AND NOT `pending` + AND network = ? + ORDER BY `name` ASC", + [local_user(), "feed"] + ); + + + $xml = new \DOMDocument( "1.0", "utf-8" ); + $opml = $xml->createElement("opml"); + $head = $xml->createElement("head"); + $body = $xml->createElement("body"); + $outline = $xml->createElement("outline"); + $outline->setAttribute("title", $a->user['username'] . "'s RSS/Atom contacts"); + $outline->setAttribute("text", $a->user['username'] . "'s RSS/Atom contacts"); + + while ($c = DBA::fetch($stmt)) { + $entry = $xml->createElement("outline"); + $entry->setAttribute("title", $c["name"]); + $entry->setAttribute("text", $c["name"]); + $entry->setAttribute("xmlUrl", $c["url"]); + // $entry->setAttribute("htmlUrl", $c[""]); + $outline->appendChild($entry); + } + DBA::close($stmt); + + $body->appendChild($outline); + $opml->appendChild($head); + $opml->appendChild($body); + $xml->appendChild($opml); + header("Content-Type: text/x-opml"); + header("Content-Disposition: attachment; filename=feeds.opml"); + $xml->formatOutput = true; + echo $xml->saveXML(); + die(); +} + + +function opmlexport_addon_settings(App $a, &$s) { + if (!local_user()) { + return; + } + + $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/opmlexport/'); + $s .= Renderer::replaceMacros($t, [ + '$title' => DI::l10n()->t('OPML Export'), + '$submit' => DI::l10n()->t('Export RSS/Atom contacts'), + ]); +} + + +function opmlexport_addon_settings_post(App $a, &$b) +{ + if (!local_user() || empty($_POST['opmlexport-submit'])) { + return; + } + opmlexport($a); +} + + diff --git a/opmlexport/templates/settings.tpl b/opmlexport/templates/settings.tpl new file mode 100644 index 000000000..9f26c512e --- /dev/null +++ b/opmlexport/templates/settings.tpl @@ -0,0 +1,12 @@ + +

{{$title}}

+
+ From e30188f143af209d68afdee600c94c1277d9b473 Mon Sep 17 00:00:00 2001 From: fabrixxm Date: Tue, 27 Apr 2021 20:52:20 +0200 Subject: [PATCH 2/4] Code standards --- opmlexport/opmlexport.php | 70 ++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/opmlexport/opmlexport.php b/opmlexport/opmlexport.php index 0c53fec6c..5714d83dc 100644 --- a/opmlexport/opmlexport.php +++ b/opmlexport/opmlexport.php @@ -14,15 +14,17 @@ use Friendica\Core\Logger; use Friendica\Network\HTTPException; use Friendica\Database\DBA; use Friendica\Core\Renderer; +use Friendica\Core\Protocol; +use Friendica\Model\Contact; -function opmlexport_install() { - Hook::register("addon_settings", __FILE__, "opmlexport_addon_settings"); - Hook::register("addon_settings_post", __FILE__, "opmlexport_addon_settings_post"); - Logger::log("installed opmlexport Addon"); +function opmlexport_install() +{ + Hook::register('addon_settings', __FILE__, 'opmlexport_addon_settings'); + Hook::register('addon_settings_post', __FILE__, 'opmlexport_addon_settings_post'); + Logger::log('installed opmlexport Addon'); } -function opmlexport(App $a) { $stmt = DBA::p("SELECT * FROM `contact` WHERE `uid` = ? @@ -34,21 +36,22 @@ function opmlexport(App $a) { [local_user(), "feed"] ); +function opmlexport(App $a) +{ - $xml = new \DOMDocument( "1.0", "utf-8" ); - $opml = $xml->createElement("opml"); - $head = $xml->createElement("head"); - $body = $xml->createElement("body"); - $outline = $xml->createElement("outline"); - $outline->setAttribute("title", $a->user['username'] . "'s RSS/Atom contacts"); - $outline->setAttribute("text", $a->user['username'] . "'s RSS/Atom contacts"); + $xml = new \DOMDocument( '1.0', 'utf-8' ); + $opml = $xml->createElement('opml'); + $head = $xml->createElement('head'); + $body = $xml->createElement('body'); + $outline = $xml->createElement('outline'); + $outline->setAttribute('title', $a->user['username'] . '\'s RSS/Atom contacts'); + $outline->setAttribute('text', $a->user['username'] . '\'s RSS/Atom contacts'); while ($c = DBA::fetch($stmt)) { - $entry = $xml->createElement("outline"); - $entry->setAttribute("title", $c["name"]); - $entry->setAttribute("text", $c["name"]); - $entry->setAttribute("xmlUrl", $c["url"]); - // $entry->setAttribute("htmlUrl", $c[""]); + $entry = $xml->createElement('outline'); + $entry->setAttribute('title', $c['name']); + $entry->setAttribute('text', $c['name']); + $entry->setAttribute('xmlUrl', $c['url']); $outline->appendChild($entry); } DBA::close($stmt); @@ -57,33 +60,32 @@ function opmlexport(App $a) { $opml->appendChild($head); $opml->appendChild($body); $xml->appendChild($opml); - header("Content-Type: text/x-opml"); - header("Content-Disposition: attachment; filename=feeds.opml"); + header('Content-Type: text/x-opml'); + header('Content-Disposition: attachment; filename=feeds.opml'); $xml->formatOutput = true; echo $xml->saveXML(); die(); } -function opmlexport_addon_settings(App $a, &$s) { - if (!local_user()) { - return; - } +function opmlexport_addon_settings(App $a, &$s) +{ + if (!local_user()) { + return; + } - $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/opmlexport/'); - $s .= Renderer::replaceMacros($t, [ - '$title' => DI::l10n()->t('OPML Export'), - '$submit' => DI::l10n()->t('Export RSS/Atom contacts'), - ]); + $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/opmlexport/'); + $s .= Renderer::replaceMacros($t, [ + '$title' => DI::l10n()->t('OPML Export'), + '$submit' => DI::l10n()->t('Export RSS/Atom contacts'), + ]); } function opmlexport_addon_settings_post(App $a, &$b) { - if (!local_user() || empty($_POST['opmlexport-submit'])) { - return; - } - opmlexport($a); + if (!local_user() || empty($_POST['opmlexport-submit'])) { + return; + } + opmlexport($a); } - - From 9795ad4d5ea3979a2d2060cd1593079ddb587333 Mon Sep 17 00:00:00 2001 From: fabrixxm Date: Tue, 27 Apr 2021 21:08:42 +0200 Subject: [PATCH 3/4] Replace `DBA:p` with `Contact:selectToArray` --- opmlexport/opmlexport.php | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/opmlexport/opmlexport.php b/opmlexport/opmlexport.php index 5714d83dc..4e49562d3 100644 --- a/opmlexport/opmlexport.php +++ b/opmlexport/opmlexport.php @@ -25,19 +25,18 @@ function opmlexport_install() } - $stmt = DBA::p("SELECT * - FROM `contact` - WHERE `uid` = ? - AND `self` = 0 - AND NOT `deleted` - AND NOT `archive` AND NOT `blocked` AND NOT `pending` - AND network = ? - ORDER BY `name` ASC", - [local_user(), "feed"] - ); - function opmlexport(App $a) { + $condition = [ + 'uid' => local_user(), + 'self' => false, + 'deleted' => false, + 'archive' => false, + 'blocked' => false, + 'pending' => false, + 'network' => Protocol::FEED + ]; + $data = Contact::selectToArray([], $condition, ['order' => ['name']]); $xml = new \DOMDocument( '1.0', 'utf-8' ); $opml = $xml->createElement('opml'); @@ -47,14 +46,13 @@ function opmlexport(App $a) $outline->setAttribute('title', $a->user['username'] . '\'s RSS/Atom contacts'); $outline->setAttribute('text', $a->user['username'] . '\'s RSS/Atom contacts'); - while ($c = DBA::fetch($stmt)) { + foreach($data as $c) { $entry = $xml->createElement('outline'); $entry->setAttribute('title', $c['name']); $entry->setAttribute('text', $c['name']); $entry->setAttribute('xmlUrl', $c['url']); $outline->appendChild($entry); } - DBA::close($stmt); $body->appendChild($outline); $opml->appendChild($head); From 323904db0a2d44cbe3f4fee059e0620238cdcceb Mon Sep 17 00:00:00 2001 From: fabrixxm Date: Tue, 27 Apr 2021 21:21:58 +0200 Subject: [PATCH 4/4] Fix indentation. Again. --- opmlexport/opmlexport.php | 96 +++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/opmlexport/opmlexport.php b/opmlexport/opmlexport.php index 4e49562d3..ca65cbaab 100644 --- a/opmlexport/opmlexport.php +++ b/opmlexport/opmlexport.php @@ -19,71 +19,71 @@ use Friendica\Model\Contact; function opmlexport_install() { - Hook::register('addon_settings', __FILE__, 'opmlexport_addon_settings'); - Hook::register('addon_settings_post', __FILE__, 'opmlexport_addon_settings_post'); - Logger::log('installed opmlexport Addon'); + Hook::register('addon_settings', __FILE__, 'opmlexport_addon_settings'); + Hook::register('addon_settings_post', __FILE__, 'opmlexport_addon_settings_post'); + Logger::log('installed opmlexport Addon'); } function opmlexport(App $a) { - $condition = [ - 'uid' => local_user(), - 'self' => false, - 'deleted' => false, - 'archive' => false, - 'blocked' => false, - 'pending' => false, - 'network' => Protocol::FEED - ]; - $data = Contact::selectToArray([], $condition, ['order' => ['name']]); + $condition = [ + 'uid' => local_user(), + 'self' => false, + 'deleted' => false, + 'archive' => false, + 'blocked' => false, + 'pending' => false, + 'network' => Protocol::FEED + ]; + $data = Contact::selectToArray([], $condition, ['order' => ['name']]); - $xml = new \DOMDocument( '1.0', 'utf-8' ); - $opml = $xml->createElement('opml'); - $head = $xml->createElement('head'); - $body = $xml->createElement('body'); - $outline = $xml->createElement('outline'); - $outline->setAttribute('title', $a->user['username'] . '\'s RSS/Atom contacts'); - $outline->setAttribute('text', $a->user['username'] . '\'s RSS/Atom contacts'); + $xml = new \DOMDocument( '1.0', 'utf-8' ); + $opml = $xml->createElement('opml'); + $head = $xml->createElement('head'); + $body = $xml->createElement('body'); + $outline = $xml->createElement('outline'); + $outline->setAttribute('title', $a->user['username'] . '\'s RSS/Atom contacts'); + $outline->setAttribute('text', $a->user['username'] . '\'s RSS/Atom contacts'); - foreach($data as $c) { - $entry = $xml->createElement('outline'); - $entry->setAttribute('title', $c['name']); - $entry->setAttribute('text', $c['name']); - $entry->setAttribute('xmlUrl', $c['url']); - $outline->appendChild($entry); - } + foreach($data as $c) { + $entry = $xml->createElement('outline'); + $entry->setAttribute('title', $c['name']); + $entry->setAttribute('text', $c['name']); + $entry->setAttribute('xmlUrl', $c['url']); + $outline->appendChild($entry); + } - $body->appendChild($outline); - $opml->appendChild($head); - $opml->appendChild($body); - $xml->appendChild($opml); - header('Content-Type: text/x-opml'); - header('Content-Disposition: attachment; filename=feeds.opml'); - $xml->formatOutput = true; - echo $xml->saveXML(); - die(); + $body->appendChild($outline); + $opml->appendChild($head); + $opml->appendChild($body); + $xml->appendChild($opml); + header('Content-Type: text/x-opml'); + header('Content-Disposition: attachment; filename=feeds.opml'); + $xml->formatOutput = true; + echo $xml->saveXML(); + die(); } function opmlexport_addon_settings(App $a, &$s) { - if (!local_user()) { - return; - } + if (!local_user()) { + return; + } - $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/opmlexport/'); - $s .= Renderer::replaceMacros($t, [ - '$title' => DI::l10n()->t('OPML Export'), - '$submit' => DI::l10n()->t('Export RSS/Atom contacts'), - ]); + $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/opmlexport/'); + $s .= Renderer::replaceMacros($t, [ + '$title' => DI::l10n()->t('OPML Export'), + '$submit' => DI::l10n()->t('Export RSS/Atom contacts'), + ]); } function opmlexport_addon_settings_post(App $a, &$b) { - if (!local_user() || empty($_POST['opmlexport-submit'])) { - return; - } - opmlexport($a); + if (!local_user() || empty($_POST['opmlexport-submit'])) { + return; + } + opmlexport($a); }