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 // $Author$ 23 // $Date$ 24 // $URL$ 25 26 /** 27 * @fileOverview Methods that implement the MetaWeblog XML-RPC API. 28 * See http://www.xmlrpc.com/metaWeblogApi for further details. 29 */ 30 31 /** @namespace */ 32 Api.metaWeblog = {} 33 34 /** 35 * @param {Story} story 36 * @returns {Object} 37 */ 38 Api.metaWeblog._getStruct = function(story) { 39 return { 40 userid: story.creator.name, 41 postid: story._id, 42 dateCreated: story.created, 43 title: story.getTitle(), 44 description: story.text, 45 categories: story.getTags(), 46 flNotOnHomePage: story.mode === Story.HIDDEN ? true : false, 47 link: story.href(), 48 permaLink: story.href(), 49 mt_excerpt: null, // FIXME: What are these "mt_" prefixed properties? 50 mt_text_more: null, 51 mt_allow_comments: story.commentMode === Story.OPEN ? 1 : 0, 52 mt_allow_pings: 0, 53 mt_convert_breaks: null, 54 mt_keywords: null, 55 postSource: story.getMetadata('postSource') 56 } 57 } 58 59 /** 60 * 61 * @param {Number} id 62 * @param {String} name 63 * @param {String} password 64 * @param {Number} limit 65 * @throws {Error} 66 * @returns {Object[]} 67 */ 68 Api.metaWeblog.getRecentPosts = function(id, name, password, limit) { 69 var site = Api.getSite(id); 70 var user = Api.getUser(name, password); 71 72 Api.constrain(site, user); 73 if (!site.stories.getPermission("main")) { 74 throw Error("Permission denied for user " + user.name + 75 " to get recent posts from site " + site.name); 76 } 77 78 var result = []; 79 var stories = res.handlers.membership.stories; 80 var max = Math.min(stories.size(), Number(limit) || Infinity, 20); 81 for each (var story in stories.list(0, max)) { 82 result.push(Api.metaWeblog._getStruct(story)); 83 } 84 return result; 85 } 86 87 /** 88 * 89 * @param {Number} id 90 * @param {String} name 91 * @param {String} password 92 * @throws {Error} 93 * @returns {Object} 94 */ 95 Api.metaWeblog.getPost = function(id, name, password) { 96 var story = Api.getStory(id); 97 var user = Api.getUser(name, password); 98 Api.constrain(story.site, user); 99 if (!story.getPermission("main")) { 100 throw Error("Permission denied for user " + name + 101 " to get post #" + id); 102 } 103 return Api.metaWeblog._getStruct(story); 104 } 105 106 /** 107 * 108 * @param {Number} id 109 * @param {String} name 110 * @param {String} password 111 * @param {String} content 112 * @param {Boolean} publish 113 * @throws {Error} 114 * @returns {Number} 115 */ 116 Api.metaWeblog.newPost = function(id, name, password, content, publish) { 117 var site = Api.getSite(id); 118 var user = Api.getUser(name, password); 119 120 Api.constrain(site, user); 121 if (!site.stories.getPermission("create")) { 122 throw Error("Permission denied for user " + user.name + 123 " to add a post to site " + site.name); 124 } 125 126 var story = Story.add({ 127 title: content.title, 128 text: content.description, 129 status: publish ? Story.PUBLIC : Story.CLOSED, 130 mode: content.flNotOnHomePage ? Story.HIDDEN : Story.FEATURED, 131 commentMode: content.discussions === 0 ? Story.CLOSED : Story.OPEN, 132 tags: content.categories 133 }, site, user); 134 135 story.setMetadata('postSource', content.postSource); 136 return story._id; 137 } 138 139 /** 140 * 141 * @param {Number} id 142 * @param {String} name 143 * @param {String} password 144 * @param {String} content 145 * @param {Boolean} publish 146 * @throws {Error} 147 * @returns {Boolean} 148 */ 149 Api.metaWeblog.editPost = function(id, name, password, content, publish) { 150 var story = Api.getStory(id); 151 var user = Api.getUser(name, password); 152 153 Api.constrain(story.site, user); 154 if (!story.getPermission("edit")) { 155 throw Error("Permission denied for user " + name + 156 " to edit post #" + id); 157 } 158 159 story.update({ 160 title: content.title, 161 text: content.description, 162 status: publish ? Story.PUBLIC : Story.CLOSED, 163 mode: content.flNotOnHomePage ? Story.HIDDEN : Story.FEATURED, 164 commentMode: content.discussions || content.mt_allow_comments ? 165 Story.OPEN : Story.CLOSED, 166 tags: content.categories 167 }); 168 169 story.setMetadata('postSource', content.postSource); 170 return true; 171 } 172 173 /** 174 * 175 * @param {Number} id 176 * @param {String} name 177 * @param {String} password 178 * @throws {Error} 179 * @returns {Object[]} 180 */ 181 Api.metaWeblog.getCategories = function(id, name, password) { 182 var site = Api.getSite(id); 183 var user = Api.getUser(name, password); 184 185 Api.constrain(site, user); 186 if (!site.stories.getPermission("main")) { 187 throw Error("Permission denied for user " + user.name + 188 " to get categories from site " + site.name); 189 } 190 191 var result = []; 192 var tags = site.getTags("tags", Tags.ALL).list(); 193 for each (var tag in tags) { 194 result.push({ 195 description: tag.name, 196 htmlUrl: tag.href(), 197 rssUrl: tag.href("rss") 198 }); 199 } 200 return result; 201 } 202 203 /** 204 * 205 * @param {Number} id 206 * @param {String} name 207 * @param {String} password 208 * @param {String} media 209 * @throws {Error} 210 * @returns {Object} 211 */ 212 Api.metaWeblog.newMediaObject = function(id, name, password, media) { 213 var site = Api.getSite(id); 214 var user = Api.getUser(name, password); 215 216 Api.constrain(site, user); 217 218 var result = {}; 219 var data = {}; 220 if (media.type && media.type.toLowerCase().startsWith("image/")) { 221 if (!site.images.getPermission("create")) { 222 throw Error("Permission denied for user " + user.name + 223 " to add a media object to site " + site.name); 224 } 225 data.file = new Packages.helma.util.MimePart(media.name, 226 media.bits, media.type); 227 data.file_origin = media.name; 228 data.description = media.description; 229 if (media.maxWidth) { 230 data.maxWidth = media.maxWidth; 231 } 232 if (media.maxHeight) { 233 data.maxHeight = media.maxHeight; 234 } 235 result.url = Image.add(data, site, user).getUrl(); 236 } else { 237 if (!site.files.getPermission("create")) { 238 throw Error("Permission denied for user " + user.name + 239 " to add a media object to site " + site.name); 240 } 241 data.file = new Packages.helma.util.MimePart(media.name, 242 media.bits, media.type); 243 data.file_origin = media.name; 244 data.description = media.description; 245 result.url = File.add(data, site, user).getUrl(); 246 } 247 248 return result; 249 } 250