diff Page.inc @ 88:7a9c45b53866

Add possibility to validate using validator.w3.org
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Wed, 17 Oct 2012 20:43:07 +0200
parents b9654b9c4a66
children 1d4c980f4255
line wrap: on
line diff
--- a/Page.inc	Mon Oct 15 18:32:56 2012 +0200
+++ b/Page.inc	Wed Oct 17 20:43:07 2012 +0200
@@ -4,7 +4,8 @@
 /// @cond
 $baseDir = dirname(__FILE__);
 $cache = ScriptIncludeCache::instance(__FILE__);
-$cache->includeOnce('Options.inc', dirname(__FILE__));
+$cache->includeOnce('Options.inc', $baseDir);
+$cache->includeOnce('Validator.inc', $baseDir);
 /// @endcond
 
 class PageContent
@@ -76,6 +77,8 @@
    */
   function mayCompress()
   {
+    if (!array_key_exists('HTTP_ACCEPT_ENCODING', $_SERVER))
+      return false;
     return (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'));
   }
 
@@ -89,7 +92,14 @@
    */
   function mayValidate()
   {
-    return !$_GET['novalidate'];
+    if (!VALIDATE)
+      return false;
+    if (array_key_exists('novalidate', $_GET))
+      return !$_GET['novalidate'];
+    if (!array_key_exists('HTTP_USER_AGENT', $_SERVER))
+      return false;
+    //UserAgent should be W3C_Validator/1.3
+    return !startswith($_SERVER['HTTP_USER_AGENT'], 'W3C');
   }
 
 
@@ -128,12 +138,18 @@
       $this->cache->CheckHttpModified();
     }
     $res = $this->generateContent();
+    if ($this->mayValidate()) {
+      $request = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
+      $validator = new Validator($request);
+      if (!$validator->check())
+	throw new LogicException('The page could be generated, but contained errors');
+    }
     if ($this->mayCompress()) {
       $this->startCompression();
     }
     $t = gettype($res);
     if ($t === "string") {
-      $res = new Content($res);
+      $res = new PageContent($res);
     }
     elseif (get_class($res) !== "PageContent") {
       throw new InvalidArgumentException("generateContent returned an unexpected type");