1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +00:00
Arsse/lib/ImportExport/OPML.php

72 lines
3.1 KiB
PHP
Raw Normal View History

2019-03-29 01:53:04 +00:00
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\ImportExport;
use JKingWeb\Arsse\Arsse;
class OPML {
public function export(string $user, bool $flat = false): string {
$tags = [];
2019-03-29 01:53:04 +00:00
$folders = [];
$parents = [0 => null];
// create a base document
2019-03-29 01:53:04 +00:00
$document = new \DOMDocument("1.0", "utf-8");
$document->formatOutput = true;
$document->appendChild($document->createElement("opml"));
$document->documentElement->setAttribute("version", "2.0");
$document->documentElement->appendChild($document->createElement("head"));
// create the "root folder" node (the body node, in OPML terms)
$folders[0] = $document->createElement("body");
// begin a transaction for read isolation
2019-03-29 01:53:04 +00:00
$transaction = Arsse::$db->begin();
// gather up the list of tags for each subscription
2019-03-29 01:53:04 +00:00
foreach (Arsse::$db->tagSummarize($user) as $r) {
$sub = $r['subscription'];
$tag = $r['name'];
// strip out any commas in the tag name; sadly this is lossy as OPML has no escape mechanism
2019-03-29 01:53:04 +00:00
$tag = str_replace(",", "", $tag);
if (!isset($tags[$sub])) {
$tags[$sub] = [];
}
$tags[$sub][] = $tag;
}
if (!$flat) {
// unless the output is requested flat, gather up the list of folders, using their database IDs as array indices
2019-03-29 01:53:04 +00:00
foreach (Arsse::$db->folderList($user) as $r) {
// note the index of its parent folder for later tree construction
2019-03-29 01:53:04 +00:00
$parents[$r['id']] = $r['parent'] ?? 0;
// create a DOM node for each folder; we don't insert it yet
2019-03-29 01:53:04 +00:00
$el = $document->createElement("outline");
$el->setAttribute("text", $r['name']);
$folders[$r['id']] = $el;
}
// insert each folder into its parent node; for the root folder the parent is the document root node
foreach ($folders as $id => $el) {
$parent = $parents[$id] ?? $document->documentElement;
$parent->appendChild($el);
}
2019-03-29 01:53:04 +00:00
}
// create a DOM node for each subscription and insert them directly into their folder DOM node
2019-03-29 01:53:04 +00:00
foreach (Arsse::$db->subscriptionList($user) as $r) {
$el = $document->createElement(("outline"));
$el->setAttribute("text", $r['title']);
$el->setAttribute("type", "rss");
$el->setAttribute("xmlUrl", $r['url']);
// include the category attribute only if there are tags
2019-03-29 01:53:04 +00:00
if (sizeof($tags[$r['id']])) {
$el->setAttribute("category", implode(",", $tags[$r['id']]));
}
// if flat output was requested subscriptions are inserted into the root folder
2019-03-29 01:53:04 +00:00
($folders[$r['folder'] ?? 0] ?? $folders[0])->appendChild($el);
}
// release the transaction
2019-03-29 01:53:04 +00:00
$transaction->rollback();
// return the serialization
return $document->saveXML();
}
}