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

?>
    <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="vspx_user" type="varchar" default="'WikiGuest'" persist="1"/>
      <v:variable name="id" type="integer" default="NULL" param-name="id"/>
      <v:variable name="cluster_key" type="varchar" param-name="cluster"/>
      <v:variable name="_cluster" type="varchar" default="'Main'" persist="1"/>

      <v:variable name="topic" type="WV.WIKI.TOPICINFO" default="NULL"/>
      <v:variable name="allow_change" type="int" default="0"/>
      <v:variable name="revision" type="varchar" default="'1'" param-name="rev"/>
      <v:on-init><![CDATA[
        self.sid := get_keyword('sid', params);
        self.realm := get_keyword('realm', params, 'wiki');
	self.vspx_user := coalesce ((select VS_UID from DB.DBA.VSPX_SESSION where VS_SID = self.sid and VS_REALM = self.realm), 'WikiGuest');
	declare _topic WV.WIKI.TOPICINFO;
	_topic := WV.WIKI.TOPICINFO();
	_topic.ti_id := self.id;
	_topic.ti_find_metadata_by_id();	
	self.topic := _topic;
	
        declare pwd varchar;
	pwd := (select pwd_magic_calc (U_NAME, U_PASSWORD, 1) from DB.DBA.SYS_USERS where U_NAME = self.vspx_user);
	if (DAV_HIDE_ERROR (DAV_AUTHENTICATE (self.topic.ti_res_id, 'R', '_1_', self.vspx_user, pwd)) is not null)
	  self.allow_change := 1;
      ]]></v:on-init>
      <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.vspx_user = 'WikiGuest')
	  {
	    self.allow_change := 0;
	  }
	self.t1.vc_data_bind(e);
      ]]></v:after-data-bind>
      <vm:dialog-body>
	 <v:template name="t1" type="simple">
	  <div class="wiki-error"><v:error-summary/></div>
	    Rollback to revision <b><v:label name="rev" value="--'1.' || self.revision"/></b> of
	    <v:label name="topic_name" value="--self.topic.ti_cluster_name || '.' || self.topic.ti_local_name"/>
	      <table class="wikitable" width="70%">
		<v:form type="simple" method="POST">
		 <tr>
		  <td>Are you sure?
		    <v:button name="OK" value="Yes" action="simple">
		      <v:on-post><![CDATA[
		        declare _rev_file, _hist_col, _path, last_ver varchar;
			last_ver := DAV_HIDE_ERROR (DAV_PROP_GET_INT (self.topic.ti_res_id, 'R',
			   'DAV:checked-in', 0));
			if (last_ver is null)
			  signal ('WV200', 'Revision either does not exist or resource has been checked out');
			declare parts any;
			parts := split_and_decode (last_ver, 0, '\0\0/');
			parts[length(parts) - 1] := self.revision;
			
			declare _text, _type varchar;
			declare _id any;
			declare _owner int;
			_id := DAV_SEARCH_ID (WV.WIKI.STRJOIN ('/', parts), 'R');
	 		declare _result int;
			_result := DAV_RES_CONTENT_INT (_id, _text, _type, 0, 0);
			if (DAV_HIDE_ERROR (_result) is null)
			  signal ('WV201', 'Can not get content of revision ' || self.revision || ' of ' || WV.WIKI.STRJOIN ('/', parts) || ' [' || cast (_result as varchar) || ']');
         		DAV_CHECKOUT_INT (self.topic.ti_full_path(), null, null, 0);
			WV.WIKI.UPLOADPAGE (
			  self.topic.ti_col_id,
			  self.topic.ti_res_name(),
			  _text,
			  (select U_ID from DB.DBA.SYS_USERS where U_NAME = self.vspx_user),
			  self.topic.ti_cluster_id,
			  self.vspx_user);
--			DAV_CHECKIN_INT (self.topic.ti_full_path(), null, null, 0);
		        self.vc_redirect (sprintf ('history.vspx?id=%ld', self.topic.ti_id));
		      ]]></v:on-post>
		    </v:button>
		    <v:button name="Cancel" value="No" action="simple">
		      <v:on-post><![CDATA[
		        self.vc_redirect (sprintf ('history.vspx?id=%ld', self.topic.ti_id));
			]]>
		      </v:on-post>
		    </v:button>
		  </td>
		 </tr>
	    	</v:form>
	      </table>
	  </v:template>
      </vm:dialog-body>
    </v:page>
<?vsp WV.WIKI.VSPFOOTER (path,params,lines);

?>
