Fix the détection of bad publication date. fix #245

The previous one was based on the fact that the PHP version used was 64bits and so the unix timestamp could go way back.

With a 32bits PHP that's not the case.  Check the notes in http://php.net/manual/en/function.strtotime.php for more information.
This commit is contained in:
Sébastien Lucas 2016-02-13 06:25:20 +01:00
parent df592eb668
commit 0a0007a096
3 changed files with 39 additions and 6 deletions

View file

@ -173,6 +173,16 @@ class OPDSRenderer
self::getXmlStream ()->endElement (); self::getXmlStream ()->endElement ();
} }
private function getPublicationDate($book) {
$dateYmd = substr($book->pubdate, 0, 10);
$pubdate = \DateTime::createFromFormat('Y-m-d', $dateYmd);
if ($pubdate === false ||
$pubdate->format ("Y") == "0101" ||
$pubdate->format ("Y") == "0100") {
return "";
}
return $pubdate->format("Y-m-d");
}
private function renderEntry ($entry) { private function renderEntry ($entry) {
self::getXmlStream ()->startElement ("title"); self::getXmlStream ()->startElement ("title");
@ -218,10 +228,10 @@ class OPDSRenderer
} }
if ($entry->book->getPubDate () != "") { if ($entry->book->getPubDate () != "") {
self::getXmlStream ()->startElement ("dcterms:issued"); self::getXmlStream ()->startElement ("dcterms:issued");
self::getXmlStream ()->text (date ("Y-m-d", $entry->book->pubdate)); self::getXmlStream ()->text (self::getPublicationDate($entry->book));
self::getXmlStream ()->endElement (); self::getXmlStream ()->endElement ();
self::getXmlStream ()->startElement ("published"); self::getXmlStream ()->startElement ("published");
self::getXmlStream ()->text (date ("Y-m-d", $entry->book->pubdate) . "T08:08:08Z"); self::getXmlStream ()->text (self::getPublicationDate($entry->book) . "T08:08:08Z");
self::getXmlStream ()->endElement (); self::getXmlStream ()->endElement ();
} }

View file

@ -94,7 +94,7 @@ class Book extends Base {
$this->id = $line->id; $this->id = $line->id;
$this->title = $line->title; $this->title = $line->title;
$this->timestamp = strtotime ($line->timestamp); $this->timestamp = strtotime ($line->timestamp);
$this->pubdate = strtotime ($line->pubdate); $this->pubdate = $line->pubdate;
$this->path = Base::getDbDirectory () . $line->path; $this->path = Base::getDbDirectory () . $line->path;
$this->relativePath = $line->path; $this->relativePath = $line->path;
$this->seriesIndex = $line->series_index; $this->seriesIndex = $line->series_index;
@ -265,12 +265,14 @@ class Book extends Base {
} }
public function getPubDate () { public function getPubDate () {
if (is_null ($this->pubdate) || ($this->pubdate <= -58979923200)) { if (empty ($this->pubdate)) {
return ""; return "";
} }
else { $dateY = (int) substr($this->pubdate, 0, 4);
return date ("Y", $this->pubdate); if ($dateY > 102) {
return str_pad($dateY, 4, "0", STR_PAD_LEFT);
} }
return "";
} }
public function getComment ($withSerie = true) { public function getComment ($withSerie = true) {

View file

@ -173,6 +173,27 @@ class BookTest extends PHPUnit_Framework_TestCase
$this->assertCount (15, $entryArray); $this->assertCount (15, $entryArray);
} }
/**
* @dataProvider providerPublicationDate
*/
public function testGetPubDate ($pubdate, $expectedYear)
{
$book = Book::getBookById(2);
$book->pubdate = $pubdate;
$this->assertEquals($expectedYear, $book->getPubDate());
}
public function providerPublicationDate() {
return array(
array('2010-10-05 22:00:00+00:00', '2010'),
array('1982-11-15 13:05:29.908657+00:00', '1982'),
array('1562-10-05 00:00:00+00:00', '1562'),
array('0100-12-31 23:00:00+00:00', ''),
array('', ''),
array(NULL, '')
);
}
public function testGetBookById () public function testGetBookById ()
{ {
// also check most of book's class methods // also check most of book's class methods