Skip to content

Commit

Permalink
Strict standards improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
JanPetterMG committed Feb 7, 2016
1 parent 6b36354 commit 490c0eb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
28 changes: 15 additions & 13 deletions src/XRobotsTagParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ class XRobotsTagParser
const DIRECTIVE_NO_TRANSLATE = 'notranslate';
const DIRECTIVE_UNAVAILABLE_AFTER = 'unavailable_after';

// TODO: Shuld be RFC-850, but disabled due to an rule parsing bug
const DATE_FORMAT_DEFAULT = 'd M Y H:i:s T';

private $supportedDateFormats = [
self::DATE_FORMAT_DEFAULT,
DATE_RFC1123,
DATE_RFC850,
'd M Y H:i:s T'
DATE_RFC850, // from Google specification
'd M Y H:i:s T' // from Google examples
];

private $strict = false;
Expand Down Expand Up @@ -137,7 +135,7 @@ private function detectDirectives()
}
if (in_array($pair[0], $this->directiveArray())) {
$this->currentDirective = $pair[0];
$this->currentValue = isset($pair[1]) ? $pair[1] : null;
$this->currentValue = isset($pair[1]) ? $pair[1] : '';
$this->addRule();
}
}
Expand Down Expand Up @@ -173,6 +171,16 @@ protected function directiveArray()
private function addRule()
{
switch ($this->currentDirective) {
case self::DIRECTIVE_ALL:
if (!$this->strict) break;
$this->rules[$this->currentUserAgent][self::DIRECTIVE_ALL] = true;
break;
case self::DIRECTIVE_NONE:
$this->rules[$this->currentUserAgent][self::DIRECTIVE_NONE] = true;
if ($this->strict) break;
$this->rules[$this->currentUserAgent][self::DIRECTIVE_NO_INDEX] = true;
$this->rules[$this->currentUserAgent][self::DIRECTIVE_NO_FOLLOW] = true;
break;
case self::DIRECTIVE_NO_ARCHIVE:
case self::DIRECTIVE_NO_FOLLOW:
case self::DIRECTIVE_NO_IMAGE_INDEX:
Expand All @@ -182,18 +190,12 @@ private function addRule()
case self::DIRECTIVE_NO_TRANSLATE:
$this->rules[$this->currentUserAgent][$this->currentDirective] = true;
break;
case self::DIRECTIVE_NONE:
$this->rules[$this->currentUserAgent][self::DIRECTIVE_NONE] = true;
if ($this->strict) break;
$this->rules[$this->currentUserAgent][self::DIRECTIVE_NO_INDEX] = true;
$this->rules[$this->currentUserAgent][self::DIRECTIVE_NO_FOLLOW] = true;
break;
case self::DIRECTIVE_UNAVAILABLE_AFTER:
if ($this->strict) $this->supportedDateFormats = [self::DATE_FORMAT_DEFAULT];
foreach (array_unique($this->supportedDateFormats) as $format) {
$dateTime = date_create_from_format($format, $this->currentValue);
if ($dateTime === false) continue;
$this->rules[$this->currentUserAgent][self::DIRECTIVE_UNAVAILABLE_AFTER] = $dateTime->format(self::DATE_FORMAT_DEFAULT);
$this->rules[$this->currentUserAgent][self::DIRECTIVE_UNAVAILABLE_AFTER] = $dateTime->format(DATE_RFC850);
if ($this->strict) break;
if (time() >= $dateTime->getTimestamp()) {
$this->rules[$this->currentUserAgent][self::DIRECTIVE_NO_INDEX] = true;
Expand All @@ -205,7 +207,7 @@ private function addRule()
}

/**
* CleanUp before next rule read
* Cleanup before next rule is read
*
* @return void
*/
Expand Down
6 changes: 3 additions & 3 deletions test/cases/UnavailableAfterStrictTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public function testUnavailableAfterStrict($url, $bot, $strict, $headers)
$this->assertInstanceOf('vipnytt\XRobotsTagParser', $parser);

// TODO: Disabled due to an RFC-850 parsing bug
//$this->assertEquals(['unavailable_after' => '01 Jul 2000 07:00:00 PST'], $parser->getRules());
//$this->assertEquals(['unavailable_after' => '31 Dec 2050 23:00:00 PST'], $parser->export()['']);
//$this->assertEquals(['unavailable_after' => '01 Jul 2000 07:00:00 PST'], $parser->export()['googlebot']);
//$this->assertEquals(['unavailable_after' => 'Saturday, 01-Jul-00 07:00:00 PST'], $parser->getRules());
//$this->assertEquals(['unavailable_after' => 'Saturday, 31-Dec-50 23:00:00 PST'], $parser->export()['']);
//$this->assertEquals(['unavailable_after' => 'Saturday, 01-Jul-00 07:00:00 PST'], $parser->export()['googlebot']);
//$this->assertArrayNotHasKey('unavailable_after', $parser->export()['bingbot']);
}

Expand Down
6 changes: 3 additions & 3 deletions test/cases/UnavailableAfterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function testUnavailableAfter($url, $bot, $strict, $headers)
$parser = new XRobotsTagParser($url, $bot, $strict, $headers);
$this->assertInstanceOf('vipnytt\XRobotsTagParser', $parser);

$this->assertEquals(['unavailable_after' => '01 Jul 2000 07:00:00 PST', 'noindex' => true], $parser->getRules());
$this->assertEquals(['unavailable_after' => '31 Dec 2050 23:00:00 PST'], $parser->export()['']);
$this->assertEquals(['unavailable_after' => '01 Jul 2000 07:00:00 PST', 'noindex' => true], $parser->export()['googlebot']);
$this->assertEquals(['unavailable_after' => 'Saturday, 01-Jul-00 07:00:00 PST', 'noindex' => true], $parser->getRules());
$this->assertEquals(['unavailable_after' => 'Saturday, 31-Dec-50 23:00:00 PST'], $parser->export()['']);
$this->assertEquals(['unavailable_after' => 'Saturday, 01-Jul-00 07:00:00 PST', 'noindex' => true], $parser->export()['googlebot']);
}

/**
Expand Down

0 comments on commit 490c0eb

Please sign in to comment.