<?xml-stylesheet href="http://www.w3.org/StyleSheets/base.css" type="text/css"?>
<?xml-stylesheet href="http://www.w3.org/2002/02/style-xsl.css" type="text/css"?>
<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:spam="http://www.w3.org/2003/08/mlspam#" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:anno="http://www.w3.org/2000/10/annotation-ns#"  exclude-result-prefixes="rdf spam anno html" xmlns="http://www.w3.org/1999/xhtml">
  <xsl:output method="xml" indent='yes' doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"  encoding="utf-8" media-type="application/xhtml+xml"/>
  <xsl:param name="xmlfile"/>
  <xsl:param name="firstPeriod"/>
  <xsl:param name="lastPeriod"/>
  <xsl:param name="auth"/>

  <!-- noLoad param allows not to pre-load existing annoations; 
  this allows to speed up pages loading when there was a lot of messages marked as spam in the given period -->
  <xsl:param name="noLoad" select="1"/>

  <xsl:variable name="doc_uri">
    <xsl:choose>
      <xsl:when test="starts-with($xmlfile,'http://cgi.w3.org/cgi-bin/tidy?docAddr=')">
        <xsl:value-of select="substring-before(concat(substring-after($xmlfile,'http://cgi.w3.org/cgi-bin/tidy?docAddr='),'&amp;'),'&amp;')"/>        
      </xsl:when>
      <xsl:when test="starts-with($xmlfile,'http://cgi.w3.org/cgi-bin/tidy-if?docAddr=')"> 
      <xsl:value-of select="substring-before(concat(substring-after($xmlfile,'http://cgi.w3.org/cgi-bin/tidy-if?docAddr='),'&amp;'),'&amp;')"/>        
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$xmlfile"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <!-- the URI of the period in which the message is listed -->
  <xsl:variable name="baseURI" select="substring-before(concat($doc_uri,'_'),substring-after(substring-after(substring-after(substring-after(concat($doc_uri,'_'),'http://lists.w3.org/Archives/'),'/'),'/'),'/'))"/>

  <!--
    Algae query, old syntax :/
    (ask '((http://www.w3.org/1999/02/22-rdf-syntax-ns#type ?annotation http://www.w3.org/2003/08/mlspam#ArchivedSpam) (http://www.w3.org/2003/08/mlspam#listedBy ?annotation $baseURI)(http://www.w3.org/2000/10/annotation-ns#annotates ?annotation ?message)) :collect '(?annotation ?message))
  -->

  <xsl:variable name="annotationQueryString">
    <xsl:if test="not($lastPeriod) and not($noLoad)">
      <xsl:value-of select="concat('&amp;w3c_algaeQuery=%28ask+%27%28%28http%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23type+%3Fannotation+http%3A%2F%2Fwww.w3.org%2F2003%2F08%2Fmlspam%23ArchivedSpam%29+%28http%3A%2F%2Fwww.w3.org%2F2003%2F08%2Fmlspam%23listedBy+%3Fannotation+',$baseURI,'%29%28http%3A%2F%2Fwww.w3.org%2F2000%2F10%2Fannotation-ns%23annotates+%3Fannotation+%3Fmessage%29%29+%3Acollect+%27%28%3Fannotation+%3Fmessage%29%29&amp;w3c_submit=query+RDF+DB')"/>
    </xsl:if>
  </xsl:variable>

 <xsl:variable name="isAnnotated" select="document(concat('http://annotest.w3.org/annospam/annotations?w3c_forceText=*',$annotationQueryString))/rdf:RDF"/>

  <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
      <link rel="stylesheet" type="text/css" href="http://www.w3.org/StyleSheets/base.css"/>
      <link rel="stylesheet" type="text/css" href="http://www.w3.org/2003/03/site-css/css/1.css"/>
      <title>Spam identifier XSLT</title>
    </head>
    <body>
      <div class="head">
        <a href="http://www.w3.org/" title="Go to W3C Home Page"><img height="48" width="72" alt="W3C" src="http://www.w3.org/Icons/w3c_home"/></a> 
        <h1>Spam identifier XSLT</h1>
      </div>
      <p>This XSLT allows to parse W3C Hypermail archives to propose a list of messages to be marked as spam.</p>
      <form action="http://www.w3.org/2002/08/xslt4html">
        <p><input type="hidden" name="auth" value="proxy"/><input type="hidden" name="xslfile" value="http://www.w3.org/2003/08/kill-spam.xsl"/><label><input type="text" name="xmlfile"/> URI of the messages list or archived message to be parsed</label></p>
        <p><input type="submit"/><input type="reset"/></p>
      </form>
      <address><a href="http://www.w3.org/Team/Dom/">Dom</a> - $Id: kill-spam.xsl,v 1.118 2009/03/18 08:30:12 dom Exp $</address>
    </body>
  </html>

  <xsl:template match="html:html">
    <xsl:copy>
      <xsl:apply-templates select="*|@*[not(name()='xml:lang')]|text()|comment()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="html:head">
    <xsl:copy>
      <base href="{$doc_uri}" />
      <xsl:apply-templates select="*|@*|text()|comment()"/>
      <script type="text/javascript">
      function allCheckBoxes(action,id) {
        fields = document.getElementsByTagName("input");
          for (var i = 0; i &lt; fields.length; i++) {
          var field = fields[i];
          if (field.type=="checkbox" &amp;&amp; (id=='' || field.parentNode.parentNode.parentNode.parentNode.id==id)) {
          if (action == -1 || (action == 0 &amp;&amp; field.checked == true)) {
          field.checked = false;
          } else {
          field.checked = true;
          }
          }
          }
          }
        </script>

    </xsl:copy>
  </xsl:template>

  <xsl:template match="html:body">
    <xsl:copy>
      <xsl:apply-templates select="*|@*|text()|comment()"/>
      <address>Spam annotations done through <a href="http://www.w3.org/2003/08/kill-spam">an XSLT style sheet</a> ($Revision: 1.118 $) run on the <a href="http://www.w3.org/2001/05/xslt">XSLT servlet</a></address>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="html:table">
    <xsl:choose>
      <xsl:when test="starts-with($lastPeriod,'http://lists.w3.org/Archives/')">
        <form action="http://cgi.w3.org/team-bin/annotate-spam" method="post" id="poster">
          <p>The checkboxes below allow to mark messages in our archives as spam. Please review carefully each message listed below and check the matching checkbox only if you know it is spam. If a checkbox is in read-only mode, it means that the message has already been marked as spam.</p>
          <dl>
            <xsl:apply-templates select="html:tbody[1]/html:tr[1]/html:td[1]" mode="includePeriod"/>
          </dl>
          <p>By submitting this form, you assert that all the messages marked above are spam.</p>
          <div class="toggleButtons">        
            <!-- buttons to use javascript to toggle the URI selections -->
            <input type="button" value="all off" onclick="allCheckBoxes(-1,'')" />
            <input type="button" value="all on" onclick="allCheckBoxes(1,'')" />
            
            <input type="button" value="toggle selection" onclick="allCheckBoxes(0,'')" />
          </div>

          <p><input type="hidden" name="count"><xsl:attribute name="value"><xsl:apply-templates select="html:tbody[1]/html:tr[1]/html:td[1]" mode="includePeriod"><xsl:with-param name="operation" select="'count'"/></xsl:apply-templates></xsl:attribute></input><input type="submit" value="Mark the checked messages as spam"/>&#xA0;<input type="reset" value="Reset"/></p>

        </form>
      </xsl:when>
      <xsl:otherwise>
        <form action="http://www.w3.org/2000/06/webdata/xslt" method="get">
          
          <p>Show all mails sent to this list between <label title="Include messages from period starting"><select name="firstPeriod"><xsl:call-template name="listPeriods"/></select> (most recent period)</label> and  <label title="Include messages from period going back to"><select name="lastPeriod"><xsl:call-template name="listPeriods"/></select> (oldest period)</label> <input type="hidden" name="xmlfile" value="{$xmlfile}" /><input type="hidden" name="xslfile" value="http://www.w3.org/2003/08/kill-spam" /><input type="hidden" name="auth" value="{$auth }" /></p>
          <p><input type="submit" name="process" value="Display the selected range of periods for spam annotations" /></p>
        </form>
        <xsl:copy>
          <xsl:apply-templates select="*|@*|text()|comment()"/>
        </xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="listPeriods">
    <xsl:for-each select="html:tbody/html:tr/html:td[1]">
      <option value="{concat($doc_uri,html:a/@href)}"><xsl:value-of select="."/></option>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="html:td" mode="includePeriod">
    <xsl:param name="operation"/>
    <xsl:param name="count" select="0"/>
    <xsl:param name="started"/>
    <xsl:variable name="start" select="$started or not(starts-with($firstPeriod,'http://lists.w3.org/')) or concat($doc_uri,html:a/@href)=$firstPeriod"/>
    <xsl:choose>
      <xsl:when test="not(html:a[concat($doc_uri,@href)=$lastPeriod])">
        <xsl:variable name="number" select="count(document(concat('http://cgi.w3.org/cgi-bin/tidy?docAddr=',$doc_uri,html:a/@href,'&amp;forceXML=1'))/html:html/html:body//html:div[@class='messages-list']//html:li[html:a/@href])"/>
        <xsl:if test="not($operation='count') and $start">
          <dt><xsl:value-of select="document(concat('http://cgi.w3.org/cgi-bin/tidy?docAddr=',$doc_uri,html:a/@href,'&amp;forceXML=1'))/html:html/html:head/html:title"/></dt>
          <dd><ul>
            <xsl:apply-templates select="document(concat('http://cgi.w3.org/cgi-bin/tidy?docAddr=',$doc_uri,html:a/@href,'&amp;forceXML=1'))/html:html/html:body//html:div[@class='messages-list']//html:li[html:a/@href]">
              <xsl:with-param name="baseURI" select="concat($doc_uri,html:a/@href)"/>
              <xsl:with-param name="baseCount" select="$count"/>
            </xsl:apply-templates>
          </ul></dd>
        </xsl:if>
        <xsl:apply-templates select="following::html:tr[1]/html:td[1]" mode="includePeriod">
          <xsl:with-param name="operation" select="$operation"/>
          <xsl:with-param name="count" select="$count + $number * $start" />
          <xsl:with-param name="started" select="$start" />
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:if test="$operation='count'">
          <xsl:value-of select="$count"/>
        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="html:div[@class='messages-list']|html:div[@class='mail']">
    <xsl:param name="baseURI" select="$baseURI"/>
    <form action="http://cgi.w3.org/team-bin/annotate-spam" method="post" id="poster">
      <xsl:choose>
        <xsl:when test="/html:html/html:head/html:link[contains(@href,'message') and not(substring-after(@href,'message'))]">
          <!-- We are in message view -->
          <xsl:variable name="markedAsSpam">
            <xsl:call-template name="isSpam">
              <xsl:with-param name="uri" select="$doc_uri"/>
            </xsl:call-template>
          </xsl:variable>

          <p>
          <label><input type="checkbox" name="uri1" value="{$doc_uri}"><xsl:if test="normalize-space($markedAsSpam)"><xsl:attribute name="disabled"><xsl:value-of select="'disabled'"/></xsl:attribute><xsl:attribute name="checked"><xsl:value-of select="'checked'"/></xsl:attribute></xsl:if></input> Mark this message as spam</label>
        </p>

        <p>By submitting this form, you assert that the message below is spam.</p>
        <p><input type="submit" value="Mark the checked messages as spam"/>&#xA0;<input type="reset" value="Reset"/></p>
        <p>If you realize later that a message was not a spam, send a request to <a href="mailto:sysreq@w3.org">sysreq@w3.org</a>.</p>

          <xsl:copy>
            <xsl:apply-templates select="*|@*|text()|comment()"/>
          </xsl:copy>

          <p>
          <label><input type="checkbox" name="uri1" value="{$doc_uri}"><xsl:if test="normalize-space($markedAsSpam)"><xsl:attribute name="disabled">xsl:value-of select="'disabled'"/></xsl:attribute><xsl:attribute name="checked"><xsl:value-of select="'checked'"/></xsl:attribute></xsl:if></input> Mark this message as spam</label>
        </p>
      </xsl:when>
      <xsl:when test="/html:html/html:head/html:link[contains(@href,'messagelist')]">
        <p>The checkboxes below allow to mark messages in our archives as spam. Please review carefully each message listed below and check the matching checkbox only if you know it is spam. If a checkbox is in read-only mode, it means that the message has already been marked as spam.</p>
        <xsl:copy>
          <xsl:apply-templates select="*|@*|text()|comment()"/>
        </xsl:copy>
        <div class="toggleButtons">        
        <!-- buttons to use javascript to toggle the URI selections -->
        <input type="button" value="all off" onclick="allCheckBoxes(-1,'')" />
        <input type="button" value="all on" onclick="allCheckBoxes(1,'')" />

        <input type="button" value="toggle selection" onclick="allCheckBoxes(0,'')" />
      </div>

      </xsl:when>
    </xsl:choose>

    <p>By submitting this form, you assert that all the messages marked above are spam.</p>
    <p><input type="hidden" name="count" value="{count(descendant::html:li[html:a/@href]) + count(self::html:div[@class='mail'])}"/><input type="submit" value="Mark the checked messages as spam"/>&#xA0;<input type="reset" value="Reset"/></p>
   </form>
 </xsl:template>

 <xsl:template match="html:div[@class='messages-list']//html:li[html:a/@href]">
    <xsl:param name="baseURI" select="$baseURI"/>
    <xsl:param name="baseCount" select="0"/>
    <xsl:variable name="markedAsSpam">
      <xsl:call-template name="isSpam">
        <xsl:with-param name="uri" select="concat($baseURI,html:a/@href)"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:variable name="position" select="count(preceding::html:li[html:a/@href][ancestor::html:div[@class='messages-list']])+1+$baseCount"/>
      
      <label title="mark as spam"><input type="checkbox" id="uri{$position}" name="uri{$position}" value="{concat($baseURI,html:a/@href)}"><xsl:if test="normalize-space($markedAsSpam)"><xsl:attribute name="disabled">xsl:value-of select="'disabled'"/></xsl:attribute><xsl:attribute name="checked"><xsl:value-of select="'checked'"/></xsl:attribute></xsl:if></input>
      <a href="{concat($baseURI,html:a/@href)}"><xsl:apply-templates select="html:a/text()"/></a>
      <xsl:apply-templates select="*[not(local-name()='a' and @href)]|text()|comment()"/>
      </label>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="html:div[@class='messages-list']//html:li[html:dfn]">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:attribute name="id"><xsl:value-of select="concat('dfn',position())"/></xsl:attribute>
      <xsl:apply-templates select="html:dfn"/>
      <xsl:if test="count(descendant::html:li) &gt; 2">
        <xsl:comment>Only shows all on/off button on lists with more than 5 entries</xsl:comment>
        <input type="button" onclick="allCheckBoxes(-1,&apos;{concat('dfn',position())}&apos;)" value="All off in list below"/>      <input type="button" onclick="allCheckBoxes(1,&apos;{concat('dfn',position())}&apos;)" value="All on in list below"/>
      </xsl:if>
      <xsl:apply-templates select="*[not(local-name()='dfn')]|comment()|text()"/>
    </xsl:copy>
  </xsl:template>

  <!-- default: Identity Transformation -->
  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates select="*|@*|comment()|text()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@*|comment()|text()">
    <xsl:copy>
      <xsl:apply-templates select="*|@*|comment()|text()"/>
    </xsl:copy>
  </xsl:template>


  <xsl:template name="isSpam">
    <xsl:param name="uri"/>
    <xsl:if test="$isAnnotated">
      <xsl:value-of select="$isAnnotated/*[anno:annotates/@rdf:resource=$uri][(local-name()='ArchivedSpam' and namespace-uri()='http://www.w3.org/2003/08/mlspam#') or rdf:type/@rdf:resource='http://www.w3.org/2003/08/mlspam#ArchivedSpam']/@rdf:about"/>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>