| 1 |
/* |
| 2 |
* Copyright (C)2005-2012 Haxe Foundation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
* DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
package legacyhaxelib; |
| 23 |
#if haxelib_site |
| 24 |
import legacyhaxelib.SiteDb; |
| 25 |
#end |
| 26 |
import haxe.rtti.CType; |
| 27 |
import haxelib.server.FileStorage; |
| 28 |
import haxe.io.Path; |
| 29 |
|
| 30 |
class Site { |
| 31 |
|
| 32 |
static var db : sys.db.Connection; |
| 33 |
|
| 34 |
static var CWD = neko.Web.getCwd(); |
| 35 |
static var DB_FILE = CWD+"haxelib.db"; |
| 36 |
public static var TMP_DIR = CWD+"../tmp"; |
| 37 |
public static var REP_DIR = CWD+"../"+Data.REPOSITORY; |
| 38 |
|
| 39 |
static function setup() { |
| 40 |
SiteDb.create(db); |
| 41 |
} |
| 42 |
|
| 43 |
static function initDatabase() { |
| 44 |
var path = "legacy/haxelib.db"; |
| 45 |
FileStorage.instance.readFile(path, function(file) { |
| 46 |
if (Path.normalize(DB_FILE) != Path.normalize(file)) |
| 47 |
throw '$file should be the same as $DB_FILE'; |
| 48 |
db = neko.db.Sqlite.open(DB_FILE); |
| 49 |
neko.db.Manager.cnx = db; |
| 50 |
neko.db.Manager.initialize(); |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
static function run() { |
| 55 |
if( !sys.FileSystem.exists(TMP_DIR) ) |
| 56 |
sys.FileSystem.createDirectory(TMP_DIR); |
| 57 |
if( !sys.FileSystem.exists(REP_DIR) ) |
| 58 |
sys.FileSystem.createDirectory(REP_DIR); |
| 59 |
|
| 60 |
var ctx = new haxe.remoting.Context(); |
| 61 |
ctx.addObject("api",new SiteApi(db)); |
| 62 |
if( haxe.remoting.HttpConnection.handleRequest(ctx) ) |
| 63 |
return; |
| 64 |
if( Sys.args()[0] == "setup" ) { |
| 65 |
setup(); |
| 66 |
neko.Lib.print("Setup done\n"); |
| 67 |
return; |
| 68 |
} |
| 69 |
var file = null; |
| 70 |
var sid = null; |
| 71 |
var bytes = 0; |
| 72 |
neko.Web.parseMultipart(function(p,filename) { |
| 73 |
if( p == "file" ) { |
| 74 |
sid = Std.parseInt(filename); |
| 75 |
file = sys.io.File.write(TMP_DIR+"/"+sid+".tmp",true); |
| 76 |
} else |
| 77 |
throw p+" not accepted"; |
| 78 |
},function(data,pos,len) { |
| 79 |
bytes += len; |
| 80 |
file.writeFullBytes(data,pos,len); |
| 81 |
}); |
| 82 |
if( file != null ) { |
| 83 |
file.close(); |
| 84 |
neko.Lib.print("File #"+sid+" accepted : "+bytes+" bytes written"); |
| 85 |
return; |
| 86 |
} |
| 87 |
display(); |
| 88 |
} |
| 89 |
|
| 90 |
static function display() { |
| 91 |
var data = sys.io.File.getContent(CWD + "website.mtt"); |
| 92 |
var page = new haxe.Template(data); |
| 93 |
var ctx : Dynamic = {}; |
| 94 |
var macros = { |
| 95 |
download : function( res, p, v ) { |
| 96 |
return "/"+Data.REPOSITORY+"/"+Data.fileName(res(p).name,res(v).name); |
| 97 |
} |
| 98 |
}; |
| 99 |
if( fillContent(ctx) ) |
| 100 |
neko.Lib.print( page.execute(ctx,macros) ); |
| 101 |
} |
| 102 |
|
| 103 |
static function fillContent( ctx : Dynamic ) { |
| 104 |
var uri = neko.Web.getURI().split("/"); |
| 105 |
var error = function(msg) { ctx.error = StringTools.htmlEscape(msg); return true; } |
| 106 |
if( uri[0] == "" ) |
| 107 |
uri.shift(); |
| 108 |
var domain = "http://" + neko.Web.getHostName(); |
| 109 |
var act = uri.shift(); |
| 110 |
if( act == "legacy" ) { |
| 111 |
act = uri.shift(); |
| 112 |
ctx.basehref = domain + "/legacy/"; |
| 113 |
} |
| 114 |
else |
| 115 |
ctx.basehref = domain + "/"; |
| 116 |
if( act == null || act == "" || act == "index.n" ) |
| 117 |
act = "index"; |
| 118 |
ctx.menuTags = Tag.manager.topTags(10); |
| 119 |
switch( act ) { |
| 120 |
case "p": |
| 121 |
var name = uri.shift(); |
| 122 |
var p = Project.manager.search({ name : name }).first(); |
| 123 |
if( p == null ) |
| 124 |
return error("Unknown project '"+name+"'"); |
| 125 |
ctx.p = p; |
| 126 |
ctx.owner = p.owner; |
| 127 |
ctx.version = p.version; |
| 128 |
ctx.versions = Version.manager.byProject(p); |
| 129 |
var tags = Tag.manager.search({ project : p.id }); |
| 130 |
if( !tags.isEmpty() ) ctx.tags = tags; |
| 131 |
case "u": |
| 132 |
var name = uri.shift(); |
| 133 |
var u = User.manager.search({ name : name }).first(); |
| 134 |
if( u == null ) |
| 135 |
return error("Unknown user '"+name+"'"); |
| 136 |
ctx.u = u; |
| 137 |
ctx.uprojects = Developer.manager.search({ user : u.id }).map(function(d:Developer) { return d.project; }); |
| 138 |
case "t": |
| 139 |
var tag = uri.shift(); |
| 140 |
ctx.tag = StringTools.htmlEscape(tag); |
| 141 |
ctx.tprojects = Tag.manager.search({ tag : tag }).map(function(t) return t.project); |
| 142 |
case "d": |
| 143 |
var name = uri.shift(); |
| 144 |
var p = Project.manager.search({ name : name }).first(); |
| 145 |
if( p == null ) |
| 146 |
return error("Unknown project '"+name+"'"); |
| 147 |
var version = uri.shift(); |
| 148 |
var v; |
| 149 |
if( version == null ) { |
| 150 |
v = p.version; |
| 151 |
version = v.name; |
| 152 |
} else { |
| 153 |
v = Version.manager.search({ project : p.id, name : version }).first(); |
| 154 |
if( v == null ) return error("Unknown version '"+version+"'"); |
| 155 |
} |
| 156 |
if( v.documentation == null ) |
| 157 |
return error("Project "+p.name+" version "+version+" has no documentation"); |
| 158 |
var root : TypeRoot = haxe.Unserializer.run(v.documentation); |
| 159 |
var buf = new StringBuf(); |
| 160 |
// var html = new tools.haxedoc.HtmlPrinter("/d/"+p.name+"/"+version+"/","",""); |
| 161 |
// html.output = function(str) buf.add(str); |
| 162 |
// var path = uri.join(".").toLowerCase().split("."); |
| 163 |
// if( path.length == 1 && path[0] == "" ) |
| 164 |
// path = []; |
| 165 |
// if( path.length == 0 ) { |
| 166 |
// ctx.index = true; |
| 167 |
// html.process(TPackage("root","root",root)); |
| 168 |
// } else { |
| 169 |
// var cl = html.find(root,path,0); |
| 170 |
// if( cl == null ) { |
| 171 |
// // we most likely clicked on a class which is part of the haxe core documentation |
| 172 |
// neko.Web.redirect("http://haxe.org/api/"+path.join("/")); |
| 173 |
// return false; |
| 174 |
// } |
| 175 |
// html.process(cl); |
| 176 |
// } |
| 177 |
// ctx.p = p; |
| 178 |
// ctx.v = v; |
| 179 |
// ctx.content = buf.toString(); |
| 180 |
case "index": |
| 181 |
var vl = Version.manager.latest(10); |
| 182 |
for( v in vl ) { |
| 183 |
var p = v.project; // fetch |
| 184 |
} |
| 185 |
ctx.versions = vl; |
| 186 |
case "all": |
| 187 |
ctx.projects = Project.manager.allByName(); |
| 188 |
case "search": |
| 189 |
var v = neko.Web.getParams().get("v"); |
| 190 |
var p = Project.manager.search({ name : v }).first(); |
| 191 |
if( p != null ) { |
| 192 |
neko.Web.redirect("/legacy/p/"+p.name); |
| 193 |
return false; |
| 194 |
} |
| 195 |
if( Tag.manager.count({ tag : v }) > 0 ) { |
| 196 |
neko.Web.redirect("/legacy/t/"+v); |
| 197 |
return false; |
| 198 |
} |
| 199 |
ctx.projects = Project.manager.containing(v).map(function(p) return Project.manager.get(p.id)); |
| 200 |
ctx.act_all = true; |
| 201 |
ctx.search = StringTools.htmlEscape(v); |
| 202 |
case "rss": |
| 203 |
neko.Web.setHeader("Content-Type", "text/xml; charset=UTF-8"); |
| 204 |
neko.Lib.println('<?xml version="1.0" encoding="UTF-8"?>'); |
| 205 |
neko.Lib.print(buildRss().toString()); |
| 206 |
return false; |
| 207 |
default: |
| 208 |
ctx.error = "Unknown action : "+act; |
| 209 |
return true; |
| 210 |
} |
| 211 |
Reflect.setField(ctx,"act_"+act,true); |
| 212 |
return true; |
| 213 |
} |
| 214 |
|
| 215 |
static function buildRss() : Xml { |
| 216 |
var createChild = function(root:Xml, name:String){ |
| 217 |
var c = Xml.createElement(name); |
| 218 |
root.addChild(c); |
| 219 |
return c; |
| 220 |
} |
| 221 |
var createChildWithContent = function(root:Xml, name:String, content:String){ |
| 222 |
var e = Xml.createElement(name); |
| 223 |
var c = Xml.createPCData(if (content != null) content else ""); |
| 224 |
e.addChild(c); |
| 225 |
root.addChild(e); |
| 226 |
return e; |
| 227 |
} |
| 228 |
var createChildWithCdata = function(root:Xml, name:String, content:String){ |
| 229 |
var e = Xml.createElement(name); |
| 230 |
var c = Xml.createCData(if (content != null) content else ""); |
| 231 |
e.addChild(c); |
| 232 |
root.addChild(e); |
| 233 |
return e; |
| 234 |
} |
| 235 |
Sys.setTimeLocale("en_US.UTF8"); |
| 236 |
var url = "http://"+neko.Web.getClientHeader("Host"); |
| 237 |
var rss = Xml.createElement("rss"); |
| 238 |
rss.set("version","2.0"); |
| 239 |
var channel = createChild(rss, "channel"); |
| 240 |
createChildWithContent(channel, "title", "haxe-libs"); |
| 241 |
createChildWithContent(channel, "link", url); |
| 242 |
createChildWithContent(channel, "description", "lib.haxe.org RSS"); |
| 243 |
createChildWithContent(channel, "generator", "haxe"); |
| 244 |
createChildWithContent(channel, "language", "en"); |
| 245 |
for (v in Version.manager.latest(10)){ |
| 246 |
var project = v.project; |
| 247 |
var item = createChild(channel, "item"); |
| 248 |
createChildWithContent(item, "title", StringTools.htmlEscape(project.name+" "+v.name)); |
| 249 |
createChildWithContent(item, "link", url+"/p/"+project.name); |
| 250 |
createChildWithContent(item, "guid", url+"/p/"+project.name+"?v="+v.id); |
| 251 |
var date = DateTools.format(Date.fromString(v.date), "%a, %e %b %Y %H:%M:%S %z"); |
| 252 |
createChildWithContent(item, "pubDate", date); |
| 253 |
createChildWithContent(item, "author", project.owner.name); |
| 254 |
createChildWithContent(item, "description", StringTools.htmlEscape(v.comments)); |
| 255 |
} |
| 256 |
return rss; |
| 257 |
} |
| 258 |
|
| 259 |
static function main() { |
| 260 |
var error = null; |
| 261 |
initDatabase(); |
| 262 |
try { |
| 263 |
run(); |
| 264 |
} catch( e : Dynamic ) { |
| 265 |
error = { e : e }; |
| 266 |
} |
| 267 |
db.close(); |
| 268 |
neko.db.Manager.cleanup(); |
| 269 |
if( error != null ) |
| 270 |
neko.Lib.rethrow(error.e); |
| 271 |
} |
| 272 |
|
| 273 |
} |