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 Defines the Exporter namespace.
 28  */
 29 
 30 /**
 31  * The Exporter namespace provides methods for exporting a site.
 32  * @namespace
 33  */
 34 var Exporter = {}
 35 
 36 /**
 37  * Exports a site with the specified user’s content
 38  * The created XML file will be added to the site’s file collection.
 39  * @param {Site} site The site to export.
 40  * @param {User} user The user whose content will be exported.
 41  */
 42 Exporter.run = function(site, user) {
 43    try {
 44       var file;
 45       if (site.export_id && (file = File.getById(site.export_id))) {
 46          File.remove.call(file);
 47       }
 48    
 49       var rssUrl = site.href("rss.xml");
 50       var baseDir = site.getStaticFile();
 51       var member = site.members.get(user.name);
 52    
 53       var xml = new helma.File(baseDir, "export.xml");
 54       xml.remove();
 55       xml.open();
 56 
 57       var add = function(s) {
 58          return xml.write(s);
 59       }
 60 
 61       add('<?xml version="1.0" encoding="UTF-8"?>');
 62       add('<?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?>');
 63       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">');
 64       add('<id>tag:blogger.com,1999:blog-' + site._id + '.archive</id>');
 65       add('<updated>' + site.modified.format(Date.ISOFORMAT) + '</updated>');
 66       add('<title type="text">' + encodeXml(site.title) + '</title>');
 67       add('<link rel="http://schemas.google.com/g/2005#feed" type="application/rss+xml" href="' + rssUrl + '"/>');
 68       add('<link rel="self" type="application/rss+xml" href="' + rssUrl + '"/>');
 69       add('<link rel="http://schemas.google.com/g/2005#post" type="application/rss+xml" href="' + rssUrl + '"/>');
 70       add('<link rel="alternate" type="text/html" href="' + site.href() + '"/>');
 71       add('<author>');
 72       add('<name>' + site.creator.name + '</name>');
 73       add('<email>' + site.creator.email + '</email>');
 74       add('</author>');
 75       // Currently, blogger.com does not accept other generators
 76       //add('<generator version="' + Root.VERSION + '" uri="' + root.href() + '">Antville</generator>');
 77       add('<generator version="7.00" uri="http://www.blogger.com">Blogger</generator>');
 78       member.stories.forEach(function() {
 79          add('<entry>');
 80          add('<id>tag:blogger.com,1999:blog-' + site._id + '.post-' + this._id + '</id>');
 81          add('<published>' + this.created.format(Date.ISOFORMAT) + '</published>');
 82          add('<updated>' + this.modified.format(Date.ISOFORMAT) + '</updated>');
 83          add('<title type="text">' + (this.title ? encodeXml(this.title.stripTags()) : '') + '</title>');
 84          add('<content type="html">' + encodeXml(this.format_filter(this.text, {})) + '</content>');
 85          add('<link rel="alternate" type="text/html" href="' + this.href() + '"></link>');
 86          add('<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/blogger/2008/kind#post"/>');
 87          add('<author>');
 88          add('<name>' + this.creator.name + '</name>');
 89          this.creator.url && add('<uri>' + this.creator.url + '</uri>');
 90          add('<email>' + this.creator.email + '</email>');
 91          add('</author>');
 92          add('</entry>');
 93       });
 94       add('</feed>');
 95 
 96       xml.close();
 97       // Provide the exported data as downloadable file
 98       // FIXME: Adding a file to a site could be a little bit simpler :/
 99       file = new File;
100       file.site = site;
101       file.update({file: {contentLength: 0}, file_origin: "file://" + 
102             xml.getPath(), name: site.name + "-export"});
103       site.files.add(file);
104       file.creator = user;
105       site.export_id = file._id;
106    } catch (ex) {
107       app.log(ex);
108    } finally {
109       xml.close();
110       xml.remove();
111    }
112 
113    // Reset the site’s export status
114    site.job = null;
115    return;
116 }
117