Monday, November 08, 2010 at 12:05 AM.

system.verbs.builtins.radio.html.backupAndRestorePage

on backupAndRestorePage () {
	<<Changes
		<<2/8/03; 6:27:32 PM by JES
			<<If there's no pageHeader in the page table, patch the JavaScript or meta-refresh tag into the template instead.
		<<1/29/03; 5:56:47 PM by JES
			<<If the browser is IE for Windows, use JavaScript to scroll to the bottom of the page instead of using an anchor in the URL.
		<<1/23/03; 5:18:35 PM by JES
			<<Created. Run the backup/verify/restore page.
	local (pta = html.getPageTableAddress ());
	pta^.title = radio.string.getLocalizedString ("backupAndRestore.title");
	
	local (htext);
	on add (s) {
		htext = htext + (s + "\r");};
	on confirm (s, function) {
		s = "<p>" + s + "<br /><br /></p>";;
		s = s + "<p><form method=\"POST\">";
		s = s + "<input type=\"hidden\" name=\"" + function + "\" value=\"" + function + "\" />";
		s = s + "<input type=\"submit\" name=\"confirm\" value=\"" + radio.string.getLocalizedString ("backupAndRestore." + function + "Button") + "\" />   <input type=\"submit\" name=\"cancel\" value=\"" + radio.string.getLocalizedString ("backupAndRestore.cancelButton") + "\" />";;
		s = s + "<br /><br /><br /></p>";
		return (s)};
	on error (s) {
		s = "<p>" + s + "</p>";;
		return (s)};
	
	radio.backup.init ();
	if pta^.method == "POST" {
		local (adrargs = @pta^.radioResponder.postArgs);
		if defined (adrargs^.cancel) {
			radio.webserver.redirect ("http://" + pta^.host + pta^.uri);
			return ("")};
		if defined (adrargs^.backupNow) {
			if defined (adrargs^.confirm) {
				thread.callScript (@radio.backup.backupAndVerify, {});
				thread.sleepTicks (6)} //wait for 0.1sec for the backup to start
			else { //get user confirmation
				return (confirm (radio.string.getLocalizedString ("backupAndRestore.backupConfirmation"), "backupNow"))}};
		if defined (adrargs^.restoreNow) {
			if defined (adrargs^.confirm) {
				if file.exists (user.radio.backup.prefs.folder + "files.xml") {
					thread.callScript (@radio.backup.restore, {user.radio.backup.prefs.folder})}
				else { //try to restore from the backup folder in the cloud
					local (homeUrl = user.radio.prefs.defaultCloudData.url); //this comes from the community server
					local (backupFolderUrl = homeUrl + "backups/");
					local (filesUrl = backupFolderUrl + "files.xml");
					local (urlparts = string.urlSplit (filesUrl));
					local (server = urlparts[2], port = 80);
					if server contains ":" {
						port = string.nthField (server, ":", 2);
						server = string.nthField (server, ":", 1)};
					local (headers = tcp.httpClient (server:server, port:port, path:urlparts[3], ctFollowRedirects:3, flJustHeaders:true));
					local (code = string.nthField (headers, " ", 2));
					if code == "200" { //files.xml exists in the cloud -- restore can proceed
						thread.callScript (@radio.backup.restore, {backupFolderUrl})}
					else { //error
						return (error (radio.string.getLocalizedString ("backupAndRestore.backupNotFoundError")))}};
				thread.sleepTicks (6)} //wait for 0.1sec for the restore to start
			else { //get user confirmation
				return (confirm (radio.string.getLocalizedString ("backupAndRestore.restoreConfirmation"), "restoreNow"))}}};
	if system.temp.radio.backup.flInProgress {
		local (userAgent = pta^.requestHeaders.["User-Agent"]);
		local (flIeWin = (userAgent contains "MSIE") and (userAgent contains "Win") );
		pta^.title = pta^.title + " - " + radio.string.getLocalizedString ("backupAndRestore.inProgressSubTitle");
		bundle { //patch in a meta tag for refreshing every 15 seconds -- we can see the status this way
			local (flPageHeader = false, theString);
			if defined (pta^.pageHeader) {
				flPageHeader = true};
			if flPageHeader { //get the header
				theString = string (pta^.pageHeader)}
			else { //get the template -- there's no header
				theString = string (pta^.template)};
			local (ix = string.patternMatch ("<head>", string.lower (theString)) + 6);
			if ix > 0 { //there'd better be a <head> tag, or we can't add any code to the <head> of the page
				local (urlToRefresh = "http://" + pta^.host + "" + pta^.uri + "?run");
				if not flIeWin { //work around an IE bug -- URLs with #anchor references in them won't refresh automatically
					urlToRefresh = urlToRefresh + "#status"};
				theString = string.insert ("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"15; URL=" + urlToRefresh + "\">", theString, ix);
				if flIeWin { //use JavaScript to do the refresh instead of a meta-refresh tag
					local (jscript = "function window.onload () {document.all[\"status\"].scrollIntoView();}");
					theString = string.insert ("<script language=\"JavaScript\">" + jscript + "</script>", theString, ix)};
				if flPageHeader { //assign the header
					table.assign (@pta^.pageHeader, theString)}
				else { //assign the template -- there's no header
					table.assign (@pta^.template, theString)}}};
		add (radio.string.getLocalizedString ("backupAndRestore.inProgress"));
		add ("<ul>" + system.temp.radio.backup.statusHtml + "</ul>");
		add ("<a name=\"status\"> </a>")}
	else { //not in progress -- either we're done, or we're waiting for the user to tell us to do something
		if defined (pta^.radioResponder.getArgs.run) { //the last operation is complete -- tell the user
			case system.temp.radio.backup.lastOperation {
				"backup" {
					if system.temp.radio.backup.lastError != "" { //there was an error
						add (radio.string.getLocalizedString ("backupAndRestore.backupError"));
						add (system.temp.radio.backup.lastError)}
					else { //no error
						add (radio.string.getLocalizedString ("backupAndRestore.backupComplete"))}};
				"restore" {
					if system.temp.radio.backup.lastError != "" { //there was an error
						add (radio.string.getLocalizedString ("backupAndRestore.restoreError"));
						add (system.temp.radio.backup.lastError)}
					else { //no error
						add (radio.string.getLocalizedString ("backupAndRestore.restoreComplete"))}}}}
		else { //backup/restore text & buttons
			bundle { //backup now and intro text
				add ("<p>" + radio.string.getLocalizedString ("backupAndRestore.backupNowIntro") + "</p>");
				add ("<p><form method=\"POST\"><input type=\"submit\" name=\"backupNow\" value=\"" + radio.string.getLocalizedString ("backupAndRestore.backupNowButton") + "\" /></form><br /></p>")};
			bundle { //restore now and intro text
				add ("<p>" + radio.string.getLocalizedString ("backupAndRestore.restoreNowIntro") + "</p>");
				add ("<p><form method=\"POST\"><input type=\"submit\" name=\"restoreNow\" value=\"" + radio.string.getLocalizedString ("backupAndRestore.restoreNowButton") + "\" /></form><br /></p>")}}};
	
	return (htext)}



This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.