<?xml version="1.0"?>
<!--
 -
 -  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
 -
-->
<?vsp WV.WIKI.VSPHEADER (path,params,lines, 'Advanced Search');

?>
<!-- $Id$ -->
 <v:page name="main" 
	style="comp.xsl" 
	on-error-redirect="../main/" 
	xmlns:v="http://www.openlinksw.com/vspx/" 
	xmlns:vm="http://www.openlinksw.com/vspx/macro">
			<v:variable name="ds_data" type="any" default="null"/>
			<v:variable name="ds_len" type="integer" default="0"/>
			<v:variable name="result_str" type="varchar" default="''"/>
			<v:variable name="search_t" type="varchar" default="'c'"/>
			<v:variable name="ds_meta" type="any" default="null"/>
			<v:variable name="rowcnt" type="int" default="10" param-name="cnt"/>
			<v:variable name="pageno" type="int" default="1" param-name="pageno"/>
			<v:variable name="d_before" type="datetime" default="null" param-name="from"/>
			<v:variable name="d_after" type="datetime" default="null" param-name="to"/>
			<v:variable name="s_cat" type="varchar" default="null" param-name="cat"/>
			<v:variable name="s_tag" type="varchar" default="null" param-name="tags"/>
			<v:variable name="upar" type="varchar" default="''" persist="temp"/>
			<v:variable name="qry" type="varchar" default="null" param-name="q"/>
    <v:variable name="stype" type="integer" default="0" param-name="stype"/>
			<v:variable name="sqry" type="varchar" default="null" param-name="output"/>
			<v:variable name="clusters" type="any" default="null"/>
			<v:variable name="output" type="varchar" default="'text'"/>
			<v:variable name="base" type="varchar" default="''"/>
			<v:variable name="s_cluster" type="varchar" default="'**all public**'"/>
			<v:variable name="scope" type="varchar" default="'**all public**'" param-name="scope"/>
			<v:variable name="sort_by" type="varchar" default="'More Relevant First'"/>
			<v:variable name="sort_by_1" type="varchar" default="'More Relevant First'" param-name="sort_by_1"/>
			<v:variable name="source_page" type="varchar" default="'Main.WelcomeVisitors'" param-name="page"/>
			<v:variable name="cluster_key" type="varchar" param-name="cluster"/>
    <v:variable name="_cluster" type="varchar" default="'Main'" persist="1"/>
			<v:variable name="vspx_user" type="varchar" default="'WikiGuest'" persist="1"/>
    <v:on-init>
      <![CDATA[
        declare cookie_vec any;
        cookie_vec := DB.DBA.vsp_ua_get_cookie_vec(lines);
        self.sid := coalesce(coalesce (get_keyword('sid', cookie_vec), get_keyword ('sid', params)), '');
        self.realm := 'wa';
        self.ds_data := vector ();
        self.ds_meta := vector ();
        self.clusters := (select XMLELEMENT ('clusters', XMLAGG (XMLELEMENT ('cluster', WAI_NAME )))
                                      from WA_INSTANCE where WAI_TYPE_NAME = 'oWiki' and WAI_IS_PUBLIC);
        declare ent any;
        ent := xpath_eval ('/clusters', self.clusters);
        XMLAppendChildren (ent, XMLELEMENT ('cluster', '**all public**'));
        if (get_keyword ('search', params) is null) {
	    self.scope := null;
	  }
        return;
      ]]>
    </v:on-init>
				
    <v:method name="run_search" arglist="inout e vspx_event">
      <![CDATA[
        declare xmldoc, ses, arr, res, qes any;
        declare i, l, max1 int;
        declare url any;
        declare inx, s_off, e_off int;
        declare d1, d2, cats, qry, stat, msg, dta, mdta, h, pars, dexp, catw, tagw, atb, twhere any;
	if (self.maxhit.ufl_value is null)
	  self.maxhit.ufl_value := '10';

        self.rowcnt := atoi (self.maxhit.ufl_value);
        s_off := (self.pageno - 1) * self.rowcnt;
        e_off := s_off + self.rowcnt;
        declare exit handler for sqlstate '*'
        {
          self.vc_is_valid := 0;
          self.vc_error_message := __SQL_MESSAGE;
          return;
        };
	
	declare res any;
	vectorbld_init(res);

        if (self.scope is not null) {
	    self.cluster_1.ufl_value := WV.WIKI.TRIM_EX (self.scope);
	  }

	if (self.sqry is not null)
	  {
	    d1 := self.d_before;
	    d2 := self.d_after;
	    catw := self.s_cat;
	    tagw := self.s_tag;
	    if (d1 is not null)
	      self.upar := self.upar||sprintf ('&from=%d-%d-%d', year (d1), month (d1), dayofmonth (d1));
	    if (d2 is not null)
	      self.upar := self.upar||sprintf ('&to=%d-%d-%d', year (d2), month (d2), dayofmonth (d2));
	    self.sortby.ufl_value := self.sort_by_1;
	  }
	else
	  {
	    d1 := null;
	    d2 := null;
	    catw := trim (self.catw.ufl_value);
	    tagw := trim (self.tagw.ufl_value);
	  }
        if (length (self.t_sinceDate.ufl_value))
	  {
          self.t_sinceDate.ufl_value := trim (self.t_sinceDate.ufl_value);
          if (isnull (regexp_match('^((?:19|20)[0-9][0-9])[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])\$', self.t_sinceDate.ufl_value)))
	    {
	      self.vc_is_valid := 0;
            self.vc_error_message := 'Please specify correct date for "Updated from".';
	      return;
	  }
          d1 := stringdate (self.t_sinceDate.ufl_value);
          self.upar := self.upar||sprintf ('&from=%s',self.t_sinceDate.ufl_value);
        }
        if (length (self.t_beforeDate.ufl_value))
	  {
          self.t_beforeDate.ufl_value := trim (self.t_beforeDate.ufl_value);
          if (isnull (regexp_match('^((?:19|20)[0-9][0-9])[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])\$', self.t_beforeDate.ufl_value)))
	    {
	      self.vc_is_valid := 0;
            self.vc_error_message := 'Please specify correct date for "Updated to".';
            return;
          }
          d2 := stringdate (self.t_beforeDate.ufl_value);
          if ((d1 is not null) and (d1 > d2))
          {
            self.vc_is_valid := 0;
            self.vc_error_message := 'Date for "Updated to" should be greater then date for "Updated from".';
	      return;
          }
          self.upar := self.upar||sprintf ('&to=%s',self.t_beforeDate.ufl_value);
	  }
	if (length (catw))
	  self.upar := self.upar||sprintf ('&cat=%U',catw);
	if (length (tagw))
	  self.upar := self.upar||sprintf ('&tags=%U',tagw);
	  
  declare _col_id int;
  if (self.cluster_1.ufl_value <> '**all public**')
    _col_id := (select ColId from WV.WIKI.CLUSTERS where ClusterName = self.cluster_1.ufl_value);

  declare search_word varchar;
  search_word := WV.WIKI.TRIM (self.expr.ufl_value);
  
        declare _search_name varchar;
        _search_name := search_word;

        if (self.stype = 2)
          goto topic_search;

        if (self.stype = 1)
        {
          if ((search_word is not null) and WV.WIKI.ISWIKIWORD (search_word))
          {
            declare exit handler for sqlstate '*' {
              self.stype := 0;
              goto _freetext_search;
            };
            declare exit handler for not found {
              --dbg_obj_princ (__SQL_STATE, __SQL_MESSAGE);
              self.stype := 0;
              goto _freetext_search;
            };

            declare _current_cluster_name, _current_topic_name, _cluster_name, _topic_name varchar;
            declare _current_cluster_id, _cluster_id integer;

            _current_cluster_name := subseq (self.source_page, 0, strstr (self.source_page, '/'));
            _current_cluster_id := (select ClusterId from WV.WIKI.CLUSTERS where ClusterName = _current_cluster_name);

            _topic_name := (select coalesce( LocalName, LocalName2) from WV.WIKI.TOPIC where (LocalName = _search_name or LocalName2 = _search_name) and ClusterId = _current_cluster_id);

            declare topics_count integer;
            topics_count := 0;
            topics_count := (select count (*) from WV.WIKI.TOPIC t, WV.WIKI.CLUSTERS c
                                             where (t.LocalName = _search_name or t.LocalName2 = _search_name)
                                               and t.ClusterId = c.ClusterId
                                               and WV.WIKI.member_of_cluster (self.vspx_user, c.ClusterName));
            if (topics_count = 0)
            {
              return WV.WIKI.redirect (WV.WIKI.wiki_cluster_uri (_current_cluster_name) || coalesce (_topic_name, _search_name), self.sid, self.realm);
            }
            if (topics_count = 1)
            {
              select c.ClusterName, coalesce( LocalName, LocalName2) into _cluster_name, _topic_name
                from WV.WIKI.TOPIC t, WV.WIKI.CLUSTERS c
               where (t.LocalName = _search_name or t.LocalName2 = _search_name) and t.ClusterId = c.ClusterId and WV.WIKI.member_of_cluster (self.vspx_user, c.ClusterName);
              return WV.WIKI.redirect (WV.WIKI.wiki_cluster_uri (_cluster_name) || _topic_name, self.sid, self.realm);
            }

            goto topic_search;
          } else {
            self.stype := 0;
          }
        }

_freetext_search:
  self.upar := self.upar || sprintf ('&q=%U', search_word);

  declare exp1,exp varchar;
  declare hit_words, vt, war any;
  declare n,m varchar;
  catw := WV.WIKI.TRIM_EX (coalesce (catw, ''));
  tagw := WV.WIKI.TRIM_EX (coalesce (tagw, ''));
  search_word := WV.WIKI.TRIM_EX (search_word);
  if (catw <> '')
          search_word := WV.WIKI.TRIM_EX(search_word || ' ' || WV.WIKI.STRJOIN (' AND ', WV.WIKI.ADDCATEGORY_PREFIX (split_and_decode (catw, 0, '\0\0 '))));

 if (tagw <> '')
   {        
     if (search_word = '')
       exp1 := null;
     else
       exp1 := search_word;
          search_word := WV.WIKI.ADD_SYSINFO_VECT (exp1, WV.WIKI.FLATTEN (WV.WIKI.ZIP ('tag', WV.WIKI.VECTOR_DROP_NOISE_WORDS(
                WV.WIKI.VECTOR_CONCAT_UNIQ (vector(), WV.WIKI.TRIM_EX(split_and_decode (WV.WIKI.TRIM_EX(tagw), 0, '\0\0,')))))), 1)   ;
   }

  search_word := WV.WIKI.PARSE_SEARCH_STR (search_word);
  exp1 := search_word;
  exp := search_word;
  hit_words := vector();
 vt := vt_batch ();
 vt_batch_feed (vt, exp, 0, 0, 'x-ViDoc');
 war := vt_batch_strings_array (vt);

 m := length (war);
 n := 0;
 while (n < m)
   {
     declare word1 varchar;
     if (war[n] <> 'AND' and war[n] <> 'NOT' and war[n] <> 'NEAR' and war[n] <> 'OR' and length (war[n]) > 1 and not vt_is_noise (war[n], 'utf-8', 'x-ViDoc'))
       {
			 word1 := war[n];
			 hit_words := vector_concat (hit_words, vector (word1));
       }
     n := n + 2;
   }

topic_search:
	declare cr_text, cr_len_text varchar;
	declare cr_params any;
	
	vectorbld_init(cr_params);
        if (self.stype > 0)
        {
          cr_text := 'select ' || case when s_off <> ''
                             then 'top (' || cast (s_off as varchar) || ',' || cast (self.rowcnt as varchar) || ')'
                             else '' end
               || ' RES_ID, RES_OWNER, RES_NAME, length (RES_CONTENT) as RES_LEN, RES_CR_TIME as RES_CR_TIME_STR,RES_PERMS, RES_FULL_PATH, RES_MOD_TIME as RES_MOD_TIME_STR, \'\' as SCORE'
               || '   from WS.WS.SYS_DAV_RES'
               || '  where RES_ID in (select t.ResId from WV.WIKI.TOPIC t, WV.WIKI.CLUSTERS c'
               || '                    where (t.LocalName = \'' || _search_name || '\' or t.LocalName2 = \'' ||_search_name || '\')'
               || '                      and t.ClusterId = c.ClusterId '
               || '                      and WV.WIKI.member_of_cluster (\'' || self.vspx_user || '\', c.ClusterName))';
        } else
	cr_text := 'select ' || case when s_off <> '' 
	                          then 'top (' || cast (s_off as varchar) || ',' || cast (self.rowcnt as varchar) || ')'
	                          else '' end
               || ' RES_ID, RES_OWNER, RES_NAME, length (RES_CONTENT) as RES_LEN, RES_CR_TIME as RES_CR_TIME_STR,RES_PERMS, RES_FULL_PATH, RES_MOD_TIME as RES_MOD_TIME_STR, SCORE'
               || '   from WS.WS.SYS_DAV_RES'
               || '  where contains (RES_CONTENT, concat (''[__lang "x-ViDoc"] '',?)) ';

        if (self.stype > 0)
        {
          cr_len_text := 'select count (*) from WV.WIKI.TOPIC t, WV.WIKI.CLUSTERS c'
               || '                    where (t.LocalName = \'' || _search_name || '\' or t.LocalName2 = \'' ||_search_name || '\')'
               || '                      and t.ClusterId = c.ClusterId '
               || '                      and WV.WIKI.member_of_cluster (\'' || self.vspx_user || '\', c.ClusterName)';
        } else
          cr_len_text := 'select count (*) from WS.WS.SYS_DAV_RES where contains (RES_CONTENT, concat (''[__lang "x-ViDoc"] '',?))';
	
	declare exit handler for sqlstate '37000' {
	  self.ds_data := vector ();
	  self.ds_len := 0;
	  self.result_str := 'Nothing has been found';
	  self.ct1.vc_data_bind (e);
	  -- self.report_name.vc_data_bind (e);
	  self.button_prev.vc_data_bind (e);
	  self.button_next.vc_data_bind (e);
	  self.ds.vc_data_bind(e);
	  return;
	};
	declare _idx any;
	_idx := 0;
	declare _res_id, _u_name, _res_name, _res_len, _cr_time, _mod_time, _perms, _full_path any;

        if (self.stype = 0)
        {
	declare _cluster_search int;
	_cluster_search := case when self.cluster_1.ufl_value = '**all public**' then 0 else 1 end;
	self.upar := self.upar || sprintf ('&scope=%U&sort_by_1=%U', self.cluster_1.ufl_value, self.sortby.ufl_value);
	
	if (_cluster_search)
	  exp1 := WV.WIKI.ADD_SYSINFO (exp1, 'cluster', self.cluster_1.ufl_value, 1);
	else -- needed to distinguish wiki topic from text files
	  exp1 := WV.WIKI.ADD_SYSINFO (exp1, 'cluster', '', 1);
        }

        self.upar := self.upar || '&sid=' || self.sid;
        self.upar := self.upar || '&realm=' || self.realm;
	  
	vectorbld_acc (cr_params, exp1);
	declare add_cr_text varchar;
	add_cr_text := '';
	if (d1 is not null)
	  {
	    add_cr_text := add_cr_text || ' and RES_MOD_TIME > ? ';
	    vectorbld_acc (cr_params, d1);
	  }
	if (d2 is not null)
	  {
	    add_cr_text := add_cr_text || ' and RES_MOD_TIME < ? ';
	    vectorbld_acc (cr_params, d2);	   
	  }

-- sorting
	if (self.sortby.ufl_value = 'More Recent First')
	  {
	    add_cr_text := add_cr_text || ' order by RES_MOD_TIME desc';
	  }
	else
	{
          add_cr_text := add_cr_text || ' order by SCORE desc';
	}

	cr_text := cr_text || add_cr_text;
	cr_len_text := cr_len_text || add_cr_text;	
	self.ds_len := 0;
	whenever not found goto endf;
	
	vectorbld_final(cr_params);
	stat := '00000';
	exec (cr_len_text, stat, msg, cr_params, 1, null, dta);
	if (stat <> '00000')
	    signal (stat, msg);
	self.ds_len := dta[0][0];

	stat := '00000';
	exec (cr_text, stat, msg, cr_params, 0, null, null, h);
	if (stat <> '00000')
	    signal (stat, msg);

	self.base := aref (split_and_decode (http_request_header (e.ve_lines, 'Host'), 0, '\0\0:'), 0);
	self.base := 'http://' || self.base || ':' || server_http_port () || '/';
	    
	while (0 = exec_next (h, null, null, dta))
    {
	  declare uf_name varchar;
       _res_id := dta[0];
	  for select U_NAME, U_FULL_NAME from DB.DBA.SYS_USERS where U_ID = dta[1] do
	    {
	      _u_name := U_NAME;
	      uf_name := coalesce (U_FULL_NAME, _u_name);
	    }
       _res_name := dta[2];
       _res_len := dta[3];
       _cr_time := dta[4];
       _perms := dta[5];
       _full_path := dta[6];
       _mod_time := dta[7];
	
      declare href varchar;
      declare link varchar;
      href := WV.WIKI.MAKEHREFFROMRES (_res_id, _res_name, self.sid, self.realm, self.base) ;
      link := WV.WIKI.MAKELINKFROMRES (_res_id, self.base);
      if (href is not null)
        {
            declare _text varchar;
            _text := '';
            if (self.stype = 0)
            {
              declare _text_ses, cl_name, lexer, lexer_name, txt, xt, xd varchar;
          _text_ses := string_output ();
	      cl_name := (select ClusterName from WV.WIKI.TOPIC natural join WV.WIKI.CLUSTERS where ResId = _res_id);
	      WV..LEXER (cl_name, lexer, lexer_name);
	      _text_ses := blob_to_string ((select RES_CONTENT from WS.WS.SYS_DAV_RES where RES_ID = _res_id));
	      lexer_name := cast (call (lexer_name) () as varchar);
	      txt := call (lexer) (_text_ses || '\r\n', cl_name, '', cl_name, vector ('SYNTAX', lexer_name));
	      xt := xtree_doc ('<text>' || txt || '</text>', 2);
	      xd := xpath_eval ('string (//*)', xt);
	      txt := serialize_to_UTF8_xml (xd);

              _text := coalesce (search_excerpt (hit_words, txt, 200000, 90, 200, 'b', 1), '');
              --_text := coalesce (search_excerpt (hit_words,  WV.WIKI.DELETE_SYSINFO_FOR (string_output_string (_text_ses), null), 200000, 90, 200, 'b', 1), '');
            }
          vectorbld_acc (res, vector (
	     	  		link,
	      		  	WV.WIKI.PRINTLENGTH(_res_len),
		       		_u_name,
			       	_mod_time,
			       	_text,
			       	WV.WIKI.TOPICNAME (_res_id),
		       		href,
		   _cr_time,
		   dta[8], -- SCORE
		   uf_name
		));
	     }
	 }

endf:
	vectorbld_final (res);
	self.ds_data := res;
	if (self.sqry in ('atom', 'rss20', 'rdf'))
	  {
	    self.output := self.sqry;
	    return;
	  }  

	self.ds.vc_enabled := 1;
	self.ds.vc_data_bind (e);
	self.ds.vc_enabled := 1;
				if (self.ds_len = 0) {
	  			self.result_str := sprintf('Nothing has been found containing: <strong>"%V"</strong>.', self.expr.ufl_value);
				} else {
	  			self.result_str := sprintf ('There are (%d) items found containing: <strong>"%V"</strong>.', self.ds_len, self.expr.ufl_value);
				}
	self.ct1.vc_data_bind (e);
        -- self.report_name.vc_data_bind (e);
	self.button_prev.vc_data_bind (e);
	self.button_next.vc_data_bind (e);
	
	self.result_in_formats.vc_data_bind (e);
      ]]>
    </v:method>
    <v:before-data-bind>
      <![CDATA[
        self.vspx_user := coalesce((select vs_uid from VSPX_SESSION where vs_sid = self.sid and vs_realm = self.realm), 'WikiGuest');
      ]]>
    </v:before-data-bind>
    <v:after-data-bind>
      <![CDATA[
    	   if (self.cluster_key is not null)
          self._cluster := self.cluster_key;
        self.login.vc_data_bind(e);
        if (self.qry is null)
          return;
        self.expr.ufl_value := WV.WIKI.TRIM (self.qry);
        if (not e.ve_is_post)
          self.run_search (e);
      ]]>
    </v:after-data-bind>
			<vm:body>
					<v:form type="simple" method="POST" action="advanced_search.vspx">
						<div class="advanced-search">
							<v:template name="sboxt" type="simple">
							
								<h2>Search Query Details</h2>

								<div class="error">
									<v:error-summary/>
								</div>

		<div id="search_type">
			<span class="search_head">Search method: </span>
		
						<span id="search_type_ft">
                      <v:radio-button name="stype_0" xhtml_id="stype_0" group-name="stype" value="0" auto-submit="0">
                        <v:before-render>
                          <![CDATA[
                            control.ufl_selected := equ (self.stype, 0);
                          ]]>
                        </v:before-render>
			                </v:radio-button>
											<label for="stype_0">Free-text Search</label>
						</span>
						
						<span id="search_type_topic">
                      <v:radio-button name="stype_1" xhtml_id="stype_1" group-name="stype" value="2" auto-submit="0">
                        <v:before-render>
                          <![CDATA[
                            control.ufl_selected := gt (self.stype, 0);
                          ]]>
                        </v:before-render>
                      </v:radio-button>
                      <label for="stype_1">Topic Search</label>
            </span>
		</div>

		<div class="search_text">
			<h3>Search Text</h3>
											<v:textarea name="expr" value="" xhtml_rows="2" xhtml_cols="50" error-glyph="*">
													<v:before-render>
            control.ufl_value := charset_recode (control.ufl_value, 'UTF-8', '_WIDE_');
          </v:before-render>
											</v:textarea>
		</div>

		<div id="search_options">
		
            	<?vsp if (1=1 or self.stype = 0)
            	  {
            	?>

			<h3>Free-text Search Options</h3>
		
			<div id="search_clusters" title="Choose the Wiki clusters (primary topic domains) to search">
				<span class="search_head">Clusters:</span>
											<v:data-list name="cluster_1" xhtml_id="cluster" value="--self.s_cluster" list-document="--self.clusters" list-match="/clusters/cluster" list-key-path="." list-value-path="."/>
			</div>
		
			<div id="search_category" title="Documents must be part of a category with the following words">
				<span class="search_head">Category key-words: </span>
											<v:text name="catw" value="" xhtml_size="20" xhtml_class="textbox" xhtml_disabled="@@hidden@@"/>
			</div>
		
			<div id="search_tags" title="Documents must have the following tags">
				<span class="search_head">Tag key-words: </span>
											<v:text name="tagw" value="" xhtml_size="20" xhtml_class="textbox" xhtml_disabled="@@hidden@@"/>
			</div>

			<div id="search_timespan" title="Documents must have been updated between the dates supplied">
				<span class="search_head">Updated between: </span>
					<span id="search_from">
	                  <v:label for="t_sinceDate" value="From: " />
                  <v:text name="t_sinceDate" xhtml_id="t_sinceDate" xhtml_onclick="cPopup.select(\$(\'t_sinceDate\'), \'t_sinceDate_select\', \'yyyy-MM-dd\')" xhtml_size="12" />
                  <a href="#" name="t_sinceDate_select" id="t_sinceDate_select" onclick="cPopup.select($('t_sinceDate'), 't_sinceDate_select', 'yyyy-MM-dd'); return false;" class="noapp">
                    <img border="0" title="pick" alt="pick" src="images/pick_calendar.gif" />
                  </a>
	        </span>
	        <span id="search_until">
	                  <v:label for="t_beforeDate" value="To: " />
                  <v:text name="t_beforeDate" xhtml_id="t_beforeDate" xhtml_onclick="cPopup.select(\$(\'t_beforeDate\'), \'t_beforeDate_select\', \'yyyy-MM-dd\')" xhtml_size="12" />
                  <a href="#" name="t_beforeDate_select" id="t_beforeDate_select" onclick="cPopup.select($('t_beforeDate'), 't_beforeDate_select', 'yyyy-MM-dd'); return false;" class="noapp">
                    <img border="0" title="pick" alt="pick" src="images/pick_calendar.gif" />
                  </a>
	        </span>
			</div>

            	<?vsp
            	  }
            	?>
		</div>
            	

		<div id="search_sort" title="Sort my results by">
			<span class="search_head">Sort by: </span>
											<v:data-list name="sortby" xhtml_id="sortby" value="--self.sort_by" list-document="--XMLELEMENT('sortby', XMLELEMENT ('type', 'More Relevant First'), XMLELEMENT ('type', 'More Recent First'))" list-match="/sortby/type" list-key-path="." list-value-path="."/>
		</div>

									<v:template type="simple">
		<div id="search_max" title="Maximum number of results to be returned per page">
			<span class="search_head">Max results: </span>
           <v:text name="maxhit" default_value="10" xhtml_size="5" error-glyph="*">
														<v:validator name="vvnum1" test="regexp" regexp="^[0-9]+$" message="Only digits are allowed for setting &quot;Max results&quot;" runat="client"/>
														<v:validator name="vvnum2" test="value" min="1" max="100" message="&quot;Max results&quot; value must be less than 100 and greater than 0" runat="client"/>
													</v:text>
												</div>
									</v:template>

		<div id="search_buttons">
											<v:button action="simple" name="post" value="Search" xhtml_title="Search" xhtml_alt="Search">
												<v:on-post><![CDATA[   self.pageno := 1; self.run_search (e);     ]]></v:on-post>
											</v:button>
	
											<vm:back-button/>

<script type="text/javascript" language="javascript1.2">

var opt;
opt = 0;

function to1() {
	var e = document.getElementById("search_options");
	if (e == null) {
		return;
	}
	if (opt == 0) {
		e.style.display = 'block';
		e.style.visibility = 'visible';
		opt = 1;
	} else {
		e.style.display = 'none';
		e.style.visibility = 'hidden';
		opt = 0;
	}
}

</script>											

											<input type="button" onClick="to1();" name="toggleOptions" value="Options" />
		</div>


              <vm:if test="(self.cAction <> 'view')">
                <div id="cDiv" style="position: absolute; visibility: hidden; background-color: white; z-index: 10;">
                </div>
              </vm:if>

							</v:template>
							
							<h2>
								<v:label name="ct1" value="--self.result_str" format="%s"/>
							</h2>

							<v:template name="result_in_formats" type="simple" enabled="--length(self.ds_data)">
								<div class="search-result-in-format">
							  search result formats:
							    <a href="<?V 'advanced_search.vspx?' || self.upar || '&output=atom' ?>">
                           <img border="0" alt="ATOM" title="ATOM" src="images/atom-icon-16.gif"/>
							      </a>
							    <a href="<?V 'advanced_search.vspx?' || self.upar || '&output=rss20' ?>">
                           <img border="0" alt="RSS" title="RSS" src="images/rss-icon-16.gif"/>
							      </a>
							    <a href="<?V 'advanced_search.vspx?' || self.upar || '&output=rdf' ?>">
                           <img border="0" alt="RDF" title="RDF" src="images/rdf-icon-16.gif"/>
							      </a>
									</div>
							</v:template>

							  <div id="search_results">

							<v:data-set name="ds" data="--self.ds_data" meta="--self.ds_meta" nrows="100" scrollable="1" instantiate="-- case when self.ds_len > 0 then 1 else 0 end">

									<v:template name="tmpl1" type="repeat">
										<v:template name="tmpl7" type="if-not-exists">
											<div class="message">No matches found</div>
										</v:template>

										<v:template name="tmp18" type="browse">
											<div class="search_result">
											  <span class="search_link">
                                      <?vsp
													http (sprintf ('<a href="%s%s">%s</a>', 
														WV.WIKI.READONLYWIKIIRI (subseq (control.te_rowset[5], 0, strstr(control.te_rowset[5], '.')), 
															subseq (control.te_rowset[5], strstr(control.te_rowset[5], '.') + 1)),
													 case when (self.sid is not null and self.sid <> '')
													   then '?sid=' || self.sid || '&realm=' || self.realm
													   else ''
                          	end, 
                          	control.te_rowset[5] ));
			  								?> 
			  								</span>

												<span class="search_result_exp">
													<?vsp http (control.te_rowset[4]); ?>
		    </span>
			  								
			  								<span class="search_author">Author: 
			  								<?vsp http (sprintf ('<a href="%s">%V</a>',
			  									sioc..person_iri (sioc..user_obj_iri (control.te_rowset[2])), control.te_rowset[9])); ?>
			  								</span>

												<span class="search_lastchange">
													Last modified: <?V DB.DBA.wa_abs_date(control.te_rowset[3]) ?>
		    								</span>
		    								<div class="search_bottom"><span></span></div>
											</div>
										</v:template>

									</v:template>
							</v:data-set>

						</div>

						<div id="search_navbuttons">
						<v:template name="button_prev" type="simple" enabled="--case when self.pageno > 1 then 1 else 0 end">
							<v:button xhtml_class="real_button" action="simple" name="prev" value="Previous" xhtml_title="Previous" html_alt="Previous">
								<v:on-post><![CDATA[
							  self.pageno := self.pageno - 1;
							  self.run_search (e);
							  ]]></v:on-post>
							</v:button>
						</v:template>
						<v:template name="button_next" type="simple" enabled="--case when (self.rowcnt*(self.pageno-1) + length (self.ds_data)) &lt; self.ds_len then 1 else 0 end">
							<v:button xhtml_class="real_button" action="simple" name="next" value="Next" xhtml_title="Next" html_alt="Next">
								<v:on-post><![CDATA[
							  self.pageno := self.pageno + 1;
							  self.run_search (e);
							  ]]></v:on-post>
							</v:button>
						</v:template>
						</div>
						</div>
					</v:form>
			</vm:body>
  &lt;?vsp<![CDATA[
    set http_charset = 'UTF-8';
    if (self.output in ('atom', 'rss20', 'rdf'))
      {
	declare author varchar;
 	declare author_id int;
          author_id := (select AdminId from WV.WIKI.CLUSTERS where ClusterName = self._cluster);
	author := (select U_NAME from DB.DBA.SYS_USERS where U_ID = author_id);
        http_rewrite();
	declare dta any;
        dta := self.ds_data;
        http_rewrite ();
        http_header ('Content-Type: text/xml; charset=UTF-8\r\n');
	http ('<rss version="2.0" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:vi="http://www.openlinksw.com/weblog/">\r\n<channel>\r\n');
        http ('<title>');
        http ('Search result for [' || self.expr.ufl_value || ']');
        http ('</title>');
        http ('<link>');
        http ('http://'); http(WV.WIKI.GET_HOST ()); http_value ('/advanced_search.vspx?' || self.upar || '&output=atom');
        http ('</link>');
        http ('<pubDate>');
	http_value (WV.WIKI.DATEFORMAT (now (), 'rfc1123'));
	http ('</pubDate>');
        http ('<managingEditor>');
	http_value (charset_recode (author,'UTF-8', '_WIDE_'));
	http_value (sprintf ('<%s>', coalesce ((select U_E_MAIL from DB.DBA.SYS_USERS where U_ID = author_id), '')));
	http ('</managingEditor>');
	http ('<description>About ');
        http ('Search result for [' || self.expr.ufl_value || ']');
        http ('</description>');
	http (sprintf ('<openSearch:totalResults>%d</openSearch:totalResults>\r\n', self.ds_len));
	http (sprintf ('<openSearch:startIndex>%d</openSearch:startIndex>\r\n', ((self.pageno - 1)*self.rowcnt + 1)));
	http (sprintf ('<openSearch:itemsPerPage>%d</openSearch:itemsPerPage>\r\n', self.rowcnt));
        foreach (any elm in dta) do
        {
          http ('<item>');
          http ('<title>');
          http_value (charset_recode (elm[5], 'UTF-8', '_WIDE_'));
          http ('</title>');
          http (sprintf ('<link>http://%s/wiki/resource/', WV.WIKI.GET_HOST ()));
          http_value (elm[0]);
          http ('</link>');
          http ('<pubDate>');
          http_value (WV.WIKI.DATEFORMAT (elm[3], 'rfc1123'));
          http ('</pubDate>');
          http ('<vi:modified>');
          http_value (WV.WIKI.DATEFORMAT (elm[7], 'iso8601'));
          http ('</vi:modified>');
          http ('</item>');
        }
        http ('</channel></rss>');
        if (self.output = 'rdf')
  	  http_xslt (WV.WIKI.WA_PPATH_URL ('rss2rdf.xsl'));
        else if (self.output = 'xbel')
  	  http_xslt (WV.WIKI.WA_PPATH_URL ('rss2xbel.xsl'));
	else if (self.output = 'atom')
	  http_xslt (WV.WIKI.WA_PPATH_URL ('rss2atom.xsl'));
        return;
      }
      ]]>
    ?&gt;
  </v:page>
<?vsp
  WV.WIKI.VSPFOOTER (path,params,lines);
?>
