1 // The Antville Project
  2 // http://code.google.com/p/antville
  3 //
  4 // Copyright 2007-2011 by Tobi Schäfer.
  5 //
  6 // Copyright 2001–2007 Robert Gaggl, Hannes Wallnöfer, Tobi Schäfer,
  7 // Matthias & Michael Platzer, Christoph Lincke.
  8 //
  9 // Licensed under the Apache License, Version 2.0 (the ``License'');
 10 // you may not use this file except in compliance with the License.
 11 // You may obtain a copy of the License at
 12 //
 13 //    http://www.apache.org/licenses/LICENSE-2.0
 14 //
 15 // Unless required by applicable law or agreed to in writing, software
 16 // distributed under the License is distributed on an ``AS IS'' BASIS,
 17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 18 // See the License for the specific language governing permissions and
 19 // limitations under the License.
 20 //
 21 // $Revision$
 22 // $LastChangedBy$
 23 // $LastChangedDate$
 24 // $URL$
 25 
 26 /**
 27  * @fileOverview
 28  */
 29 
 30 var Exporter = {}
 31 
 32 Exporter.run = function(site, user) {
 33    try {
 34       var file;
 35       if (site.export_id && (file = File.getById(site.export_id))) {
 36          File.remove.call(file);
 37       }
 38    
 39       var rssUrl = site.href("rss.xml");
 40       var baseDir = site.getStaticFile();
 41       var member = site.members.get(user.name);
 42    
 43       var xml = new helma.File(baseDir, "export.xml");
 44       xml.remove();
 45       xml.open();
 46 
 47       var add = function(s) {
 48          return xml.write(s);
 49       }
 50 
 51       add('<?xml version="1.0" encoding="UTF-8"?>');
 52       add('<?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?>');
 53       add('<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:thr="http://purl.org/syndication/thread/1.0">');
 54       add('<id>tag:blogger.com,1999:blog-' + site._id + '.archive</id>');
 55       add('<updated>' + site.modified.format(Date.ISOFORMAT) + '</updated>');
 56       add('<title type="text">' + encodeXml(site.title) + '</title>');
 57       add('<link rel="http://schemas.google.com/g/2005#feed" type="application/rss+xml" href="' + rssUrl + '"/>');
 58       add('<link rel="self" type="application/rss+xml" href="' + rssUrl + '"/>');
 59       add('<link rel="http://schemas.google.com/g/2005#post" type="application/rss+xml" href="' + rssUrl + '"/>');
 60       add('<link rel="alternate" type="text/html" href="' + site.href() + '"/>');
 61       add('<author>');
 62       add('<name>' + site.creator.name + '</name>');
 63       add('<email>' + site.creator.email + '</email>');
 64       add('</author>');
 65       // Currently, blogger.com does not accept other generators
 66       //add('<generator version="' + Root.VERSION + '" uri="' + root.href() + '">Antville</generator>');
 67       add('<generator version="7.00" uri="http://www.blogger.com">Blogger</generator>');
 68       member.stories.forEach(function() {
 69          add('<entry>');
 70          add('<id>tag:blogger.com,1999:blog-' + site._id + '.post-' + this._id + '</id>');
 71          add('<published>' + this.created.format(Date.ISOFORMAT) + '</published>');
 72          add('<updated>' + this.modified.format(Date.ISOFORMAT) + '</updated>');
 73          add('<title type="text">' + (this.title ? encodeXml(this.title.stripTags()) : '') + '</title>');
 74          add('<content type="html">' + encodeXml(this.format_filter(this.text, {})) + '</content>');
 75          add('<link rel="alternate" type="text/html" href="' + this.href() + '"></link>');
 76          add('<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/blogger/2008/kind#post"/>');
 77          add('<author>');
 78          add('<name>' + this.creator.name + '</name>');
 79          this.creator.url && add('<uri>' + this.creator.url + '</uri>');
 80          add('<email>' + this.creator.email + '</email>');
 81          add('</author>');
 82          add('</entry>');
 83       });
 84       add('</feed>');
 85 
 86       xml.close();
 87       // Provide the exported data as downloadable file
 88       // FIXME: Adding a file to a site could be a little bit simpler :/
 89       file = new File;
 90       file.site = site;
 91       file.update({file: {contentLength: 0}, file_origin: "file://" + 
 92             xml.getPath(), name: site.name + "-export"});
 93       site.files.add(file);
 94       file.creator = user;
 95       site.export_id = file._id;
 96    } catch (ex) {
 97       app.log(ex);
 98    } finally {
 99       xml.close();
100       xml.remove();
101    }
102 
103    // Reset the site’s export status
104    site.job = null;
105    return;
106 }
107