From d93ed5c9902ef32b24e92dc76fa626101b44a8ae Mon Sep 17 00:00:00 2001
From: Brett Bieber <brett.bieber@gmail.com>
Date: Thu, 29 Apr 2010 15:08:38 +0000
Subject: [PATCH] Add some validator improvements. Add a json validation
 results handler. Add some js for validating links. Modify page logger to
 always show the validation status, if known. Add some simple CSS.

---
 src/UNL/WDN/Assessment.php                    |  8 +--
 src/UNL/WDN/Assessment/PageLogger.php         |  4 +-
 .../WDN/Assessment/ValidityStatusLogger.php   |  3 +-
 www/batchval.css                              |  7 +++
 www/batchval.js                               | 51 +++++++++++++++++++
 www/index.php                                 | 20 +++-----
 www/validator.php                             | 10 ++++
 7 files changed, 85 insertions(+), 18 deletions(-)
 create mode 100644 www/batchval.css
 create mode 100644 www/batchval.js
 create mode 100644 www/validator.php

diff --git a/src/UNL/WDN/Assessment.php b/src/UNL/WDN/Assessment.php
index 2e13c74..5d13295 100644
--- a/src/UNL/WDN/Assessment.php
+++ b/src/UNL/WDN/Assessment.php
@@ -45,18 +45,18 @@ class UNL_WDN_Assessment
         $this->removeEntries();
         
         $vlogger = new UNL_WDN_Assessment_ValidationLogger($this);
-        $slogger = new UNL_WDN_Assessment_ValidityStatusLogger($this);
+        //$slogger = new UNL_WDN_Assessment_ValidityStatusLogger($this);
         $spider  = $this->getSpider();
         $spider->addLogger($vlogger);
-        $spider->addLogger($slogger);
+        //$spider->addLogger($slogger);
         $spider->spider($this->baseUri);
     }
     
     function logPages()
     {
-        $slogger = new UNL_WDN_Assessment_ValidityStatusLogger($this);
+        //$slogger = new UNL_WDN_Assessment_ValidityStatusLogger($this);
         $spider = $this->getSpider();
-        $spider->addLogger($slogger);
+        //$spider->addLogger($slogger);
         $spider->spider($this->baseUri);
     }
     
diff --git a/src/UNL/WDN/Assessment/PageLogger.php b/src/UNL/WDN/Assessment/PageLogger.php
index f0c1c02..e8ac99b 100644
--- a/src/UNL/WDN/Assessment/PageLogger.php
+++ b/src/UNL/WDN/Assessment/PageLogger.php
@@ -15,7 +15,9 @@ class UNL_WDN_Assessment_PageLogger extends Spider_LoggerAbstract
     
     function log($uri, $depth, DOMXPath $xpath)
     {
-        echo PHP_EOL.'|'.str_repeat('-', $depth).$uri;
         $this->assessment->addUri($uri);
+        echo PHP_EOL.'<div id="uri_'.md5($uri).'" class="depth_'.$depth.' '.$this->assessment->getValidityStatus($uri).'">
+        <span class="uri">'.$uri.'</span>
+        </div>'.PHP_EOL;
     }
 }
diff --git a/src/UNL/WDN/Assessment/ValidityStatusLogger.php b/src/UNL/WDN/Assessment/ValidityStatusLogger.php
index d063a7d..94f920f 100644
--- a/src/UNL/WDN/Assessment/ValidityStatusLogger.php
+++ b/src/UNL/WDN/Assessment/ValidityStatusLogger.php
@@ -15,6 +15,7 @@ class UNL_WDN_Assessment_ValidityStatusLogger extends Spider_LoggerAbstract
     
     function log($uri, $depth, DOMXPath $xpath)
     {
-        echo ' '.$this->assessment->getValidityStatus($uri);
+        $status = $this->assessment->getValidityStatus($uri);
+        echo '<span id="validity_'.md5($uri).'" class=" validity '.$status.'">'.$status.'</span>'.PHP_EOL;
     }
 }
\ No newline at end of file
diff --git a/www/batchval.css b/www/batchval.css
new file mode 100644
index 0000000..6785732
--- /dev/null
+++ b/www/batchval.css
@@ -0,0 +1,7 @@
+@CHARSET "UTF-8";
+.true {
+	background-color:green;
+}
+.false {
+	background-color:red;
+}
\ No newline at end of file
diff --git a/www/batchval.js b/www/batchval.js
new file mode 100644
index 0000000..4d07ae4
--- /dev/null
+++ b/www/batchval.js
@@ -0,0 +1,51 @@
+function validateAll()
+{
+	WDN.jQuery('.uri').each(function(){
+		// Grab the URI
+		var uri = WDN.jQuery(this).html();
+		
+		var uriDiv = WDN.jQuery(this).parent();
+		
+		checkValidity(uri, uriDiv);
+	});
+}
+
+function validateInvalid()
+{
+	WDN.jQuery('.uri:not(.true)').each(function(){
+		// Grab the URI
+		var uri = WDN.jQuery(this).html();
+		
+		var uriDiv = WDN.jQuery(this).parent();
+		
+		checkValidity(uri, uriDiv);
+	});
+}
+
+function checkValidity(uri, uriDiv)
+{
+	
+	uriDiv.removeClass('true false')
+	
+	// Tell the user we're loading the result
+	uriDiv.append('<img class="loading" src="/wdn/templates_3.0/css/header/images/colorbox/loading.gif" />');
+	
+	// Fetch the validator results in JSON format.
+	WDN.get('validator.php?u='+escape(uri), null, function(result) {
+		handleJSONResult(result, uriDiv);
+	}, 'json');
+}
+
+function handleJSONResult(result, uriDiv)
+{
+	uriDiv.children('.loading').remove();
+	
+	if (result.validity) {
+		// It is valid, say no more!
+		uriDiv.addClass('true');
+		return;
+	}
+
+	uriDiv.addClass('false');
+
+}
diff --git a/www/index.php b/www/index.php
index 99f783a..a9a2452 100644
--- a/www/index.php
+++ b/www/index.php
@@ -47,6 +47,8 @@ if (isset($_GET['uri'])
 <title>UNL | WDN | Batch Validator</title>
 <!-- InstanceEndEditable --><!-- InstanceBeginEditable name="head" -->
 <link rel="stylesheet" type="text/css" href="/wdn/templates_3.0/css/content/forms.css" />
+<link rel="stylesheet" type="text/css" href="batchval.css" />
+<script type="text/javascript" src="batchval.js"></script>
 <!-- InstanceEndEditable -->
 </head>
 <body class="fixed">
@@ -80,6 +82,7 @@ if (isset($_GET['uri'])
         <div id="maincontent">
             <!--THIS IS THE MAIN CONTENT AREA; WDN: see glossary item 'main content area' -->
             <!-- InstanceBeginEditable name="maincontentarea" -->
+            <div class="three_col left">
             <form method="get" action="" class="cool">
                     <fieldset>
                         <legend>Batch Validator</legend>
@@ -106,8 +109,12 @@ if (isset($_GET['uri'])
                     </fieldset>
                     <p class="submit"><input type="submit" id="submit" name="submit" value="Submit" /></p>
             </form>
+            </div>
+            <div class="col right">
+                <p class="submit"><a href="#" onclick="validateInvalid(); return false;">Validate Invalid</a></p>
+                <p class="submit"><a href="#" onclick="validateAll(); return false;">Validate All</a></p>
+            </div>
             <div class="clear">
-                <pre>
                 <?php
                 
                 if (!empty($uri)) {
@@ -143,18 +150,7 @@ if (isset($_GET['uri'])
                             $assessment->logPages();
                     }
                 }
-                
-                //if (isset($assessment)) {
-                //    if ($subPages = $assessment->getSubPages()) {
-                //        echo '<ul>';
-                //        foreach ($subPages as $page) {
-                //            echo '<li class="'.$page['valid'].'">'.$page['url']. ' valid = '.$page['valid'].'</li>';
-                //        }
-                //        echo '</ul>';
-                //    }
-                //}
                 ?>
-                </pre>
             </div>
             <!-- InstanceEndEditable -->
             <div class="clear"></div>
diff --git a/www/validator.php b/www/validator.php
new file mode 100644
index 0000000..4debd8f
--- /dev/null
+++ b/www/validator.php
@@ -0,0 +1,10 @@
+<?php
+require_once 'config.inc.php';
+if (!isset($_GET['u'])) {
+    throw new Exception('You must pass a uri to validate.');
+}
+
+$v = new Services_W3C_HTMLValidator();
+$result = $v->validate($_GET['u']);
+
+echo json_encode($result);
-- 
GitLab