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