<?xml version="1.0"?>
<!--
 -
 -  $Id$
 -
 -  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 -  project.
 -
 -  Copyright (C) 1998-2018 OpenLink Software
 -
 -  This project is free software; you can redistribute it and/or modify it
 -  under the terms of the GNU General Public License as published by the
 -  Free Software Foundation; only version 2 of the License, dated June 1991.
 -
 -  This program is distributed in the hope that it will be useful, but
 -  WITHOUT ANY WARRANTY; without even the implied warranty of
 -  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 -  General Public License for more details.
 -
 -  You should have received a copy of the GNU General Public License along
 -  with this program; if not, write to the Free Software Foundation, Inc.,
 -  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 -
-->
<v:page name="export" fast-render="1" button-anchors="1" xmlns:v="http://www.openlinksw.com/vspx/" doctype="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <v:variable name="v_mode" type="varchar" default="''" param-name="mode"/>

  <v:variable name="domain_id" type="integer" default="-1"/>
  <v:variable name="account_id" type="integer" default="-1"/>

  <v:variable name="v_output" type="varchar" param-name="output" default="''"/>
  <v:variable name="v_keywords" type="varchar" default="''" param-name="keywords"/>
  <v:variable name="v_expression" type="varchar" default="''" param-name="expression"/>
  <v:variable name="v_tags" type="varchar" default="''" param-name="tags"/>
  <v:variable name="v_read" type="varchar" default="''" param-name="read"/>
  <v:variable name="v_flag" type="varchar" default="''" param-name="flag"/>
  <v:variable name="v_after" type="varchar" default="''" param-name="after"/>
  <v:variable name="v_before" type="varchar" default="''" param-name="before"/>

  <v:variable name="v_type" type="varchar" default="'text'" param-name="type"/>
  <v:variable name="v_max" type="varchar" default="'100'" param-name="max"/>
  <v:variable name="v_order" type="varchar" default="'EFI_PUBLISH_DATE'" param-name="order"/>
  <v:variable name="v_direction" type="varchar" default="'desc'" param-name="direction"/>

  <![CDATA[
    <?vsp
      set http_charset = 'UTF-8';
      http_rewrite ();
      http_request_status ('HTTP/1.1 200 OK');

      self.domain_id := ENEWS.WA.session_domain (params);
      self.account_id := ENEWS.WA.domain_owner_id (self.domain_id);

      if (self.v_mode = 'opensearchdescription')
      {
        http_header ('Content-Type: text/xml\r\n');
        http (         '<?xml version="1.0" encoding="UTF-8"?>\n');
        http (         '<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">');
        http (sprintf ('<Url type="text/html" template="%Vsearch.vspx?mode=opensearch&amp;q={searchTerms}&amp;cnt={count}&amp;page={startPage}" />', ENEWS.WA.enews_url (self.domain_id)));
        http (sprintf ('<ShortName>%V</ShortName>', ENEWS.WA.domain_name (self.domain_id)));
        http (sprintf ('<LongName>%V</LongName>', ENEWS.WA.domain_description (self.domain_id)));
        http (sprintf ('<Description>About: %V</Description>', ENEWS.WA.domain_description (self.domain_id)));
        http (         '<Tags>contacts</Tags>');
        http (sprintf ('<Image height="16" width="16" type="image/png">%V/ods/images/icons/ods_feeds_16.png</Image>', ENEWS.WA.host_url ()));
        http (sprintf ('<Developer>%V</Developer>', ENEWS.WA.account_name (self.account_id)));
        http (sprintf ('<Contact>%V</Contact>', ENEWS.WA.account_mail (self.account_id)));
        http (sprintf ('<Attribution>%V</Attribution>', (select top 1 WS_COPYRIGHT from WA_SETTINGS)));
        http (         '<SyndicationRight>open</SyndicationRight>');
        http (         '<AdultContent>false</AdultContent>');
        http (         '</OpenSearchDescription>');
      }
      else if (self.v_output = 'OPML')
      {
        http_header ('Content-Type: text/xml\r\n');
        http(ENEWS.WA.xml2string(ENEWS.WA.export_opml_xml(self.domain_id, self.account_id)));
      }
      else if (self.v_output in ('rss', 'rdf', 'xbel', 'atom03', 'atom10'))
      {
        declare data, sql, state, msg, meta, result any;

        ENEWS.WA.xml_set('keywords', data, self.v_keywords);
        ENEWS.WA.xml_set('expression', data, self.v_expression);
        ENEWS.WA.xml_set('tags', data, self.v_tags);
        ENEWS.WA.xml_set('read', data, self.v_read);
        ENEWS.WA.xml_set('flag', data, self.v_flag);
        ENEWS.WA.xml_set('afterDate', data, self.v_after);
        ENEWS.WA.xml_set('beforeDate', data, self.v_before);

        if (length(xpath_eval('//entry', data, 0)) = 0)
          goto _error;

    	  sql := ENEWS.WA.sfolder_sql(self.domain_id, self.account_id, 'R', data, self.v_type, self.v_max);
        sql := concat(sql, 'order by ', self.v_order, ' ', self.v_direction);

        state := '00000';
        exec(sql, state, msg, vector(), 0, meta, result);
        if (state <> '00000')
          goto _error;

        declare _header, _links varchar;

        _header := case when (self.v_output like '%atom%') then 'Content-Type: application/atom+xml\r\n' else 'Content-Type: text/xml\r\n' end;
        _links := ODS..PSH_HEADER_LINKS (self.domain_id);
        if (length (_links))
          _header := _header || sprintf ('Link: %s\r\n', _links);
        http_header (_header);

          http ('<?xml version="1.0" encoding="UTF-8"?>\n');
          http ('<rss version="2.0">\n');
          http ('<channel>\n');
          for (select U_NAME, U_FULL_NAME, U_E_MAIL from DB.DBA.SYS_USERS where U_ID = self.account_id) do
          {
            http ('<title>');
              http_value (ENEWS.WA.domain_name(self.domain_id));
            http ('</title>\n');
            http ('<description>');
              http_value (ENEWS.WA.domain_description(self.domain_id));
            http ('</description>\n');
            http ('<managingEditor>');
              http_value (U_E_MAIL);
            http ('</managingEditor>\n');
            http ('<pubDate>');
              http_value (ENEWS.WA.dt_rfc1123(now()));
            http ('</pubDate>\n');
            http ('<generator>');
              http_value ('Virtuoso Universal Server ' || sys_stat('st_dbms_ver'));
            http ('</generator>\n');
            http ('<webMaster>');
              http_value (U_E_MAIL);
            http ('</webMaster>\n');
            http ('<link>');
              http_value (ENEWS.WA.enews_url(self.domain_id));
            http ('</link>\n');
          if (self.v_output like '%atom%')
          {
            _links := ODS..PSH_ATOM_LINKS (self.domain_id);
            if (length (_links))
              http (_links);
          }
          }
          foreach (any row in result) do
          {
            http ('<item>\n');
              http ('<title>');
                http_value (row[1]);
              http ('</title>\n');
              http ('<description>');
              http_value (ENEWS.WA.utf2wide(ENEWS.WA.xml2string(ENEWS.WA.feed_description(row[0]))));
              http ('</description>\n');
              http ('<link>');
                http_value (row[4]);
              http ('</link>\n');
              http ('<guid>');
                http_value (row[4]);
              http ('</guid>\n');
              http ('<pubDate>');
                http_value (ENEWS.WA.dt_rfc1123 (row[3]));
              http ('</pubDate>\n');
              if (self.v_output <> 'rss')
              {
                http ('<ods:modified xmlns:ods="http://www.openlinksw.com/ods/">');
                  http_value (ENEWS.WA.dt_iso8601 (row[3]));
                http ('</ods:modified>\n');
              }
            http ('</item>\n');
          }
          http ('</channel>\n');
          http ('</rss>');

          if (self.v_output = 'atom03')
    	      http_xslt (ENEWS.WA.xslt_full ('rss2atom03.xsl'));

          else if (self.v_output = 'atom10')
    	      http_xslt (ENEWS.WA.xslt_full ('rss2atom.xsl'));

          else if (self.v_output = 'rdf')
     	      http_xslt (ENEWS.WA.xslt_full ('rss2rdf.xsl'));

          else if (self.v_output = 'xbel')
    	      http_xslt (ENEWS.WA.xslt_full ('rss2xbel.xsl'));
      }
    _end:
      return;

    _error:
      http('<?xml version="1.0" ?><empty />');
    ?>
  ]]>
</v:page>
