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