mirror of
https://github.com/standardebooks/web.git
synced 2025-07-15 19:06:49 -04:00
Flesh out OPDS generation backend to be more robust and to support generic Atom feeds
This commit is contained in:
parent
35188195f1
commit
f6df03cfca
23 changed files with 1549 additions and 267 deletions
54
lib/AtomFeed.php
Normal file
54
lib/AtomFeed.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?
|
||||
use function Safe\file_get_contents;
|
||||
use function Safe\file_put_contents;
|
||||
use function Safe\preg_replace;
|
||||
use function Safe\rename;
|
||||
use function Safe\tempnam;
|
||||
use function Safe\unlink;
|
||||
|
||||
class AtomFeed extends Feed{
|
||||
public $Id;
|
||||
|
||||
public function __construct(string $url, string $title, string $path, array $entries){
|
||||
parent::__construct($url, $title, $path, $entries);
|
||||
$this->Id = 'https://standardebooks.org' . $url;
|
||||
}
|
||||
|
||||
private function Sha1Entries(string $xmlString): string{
|
||||
try{
|
||||
$xml = new SimpleXMLElement(str_replace('xmlns=', 'ns=', $xmlString));
|
||||
$xml->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/');
|
||||
$xml->registerXPathNamespace('schema', 'http://schema.org/');
|
||||
|
||||
// Remove any <updated> elements, we don't want to compare against those.
|
||||
foreach($xml->xpath('/feed/updated') ?: [] as $element){
|
||||
unset($element[0]);
|
||||
}
|
||||
|
||||
$output = '';
|
||||
foreach($xml->xpath('/feed/entry') ?: [] as $entry){
|
||||
$output .= $entry->asXml();
|
||||
}
|
||||
|
||||
return sha1(preg_replace('/\s/ius', '', $output));
|
||||
}
|
||||
catch(Exception $ex){
|
||||
// Invalid XML
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
protected function GetXmlString(): string{
|
||||
if($this->XmlString === null){
|
||||
$feed = Template::AtomFeed(['id' => $this->Id, 'url' => $this->Url, 'title' => $this->Title, 'entries' => $this->Entries]);
|
||||
|
||||
$this->XmlString = $this->CleanXmlString($feed);
|
||||
}
|
||||
|
||||
return $this->XmlString;
|
||||
}
|
||||
|
||||
protected function HasChanged(string $path): bool{
|
||||
return !is_file($path) || ($this->Sha1Entries($this->GetXmlString()) != $this->Sha1Entries(file_get_contents($path)));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue