paul@41 | 1 | # -*- coding: iso-8859-1 -*- |
paul@41 | 2 | """ |
paul@41 | 3 | MoinMoin - ItemSupport library |
paul@41 | 4 | |
paul@41 | 5 | @copyright: 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@41 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@41 | 7 | """ |
paul@41 | 8 | |
paul@96 | 9 | from MoinMoin.Page import Page |
paul@95 | 10 | from MoinMoin.PageEditor import PageEditor |
paul@98 | 11 | from MoinMoin.security import Permissions |
paul@41 | 12 | from MoinMoin.util import lock |
paul@96 | 13 | from MoinSupport import getMetadata, getPagesForSearch |
paul@41 | 14 | import os |
paul@41 | 15 | |
paul@41 | 16 | # Content storage support. |
paul@41 | 17 | |
paul@98 | 18 | class AdminPermissionsForPage(Permissions): |
paul@98 | 19 | |
paul@98 | 20 | "Permit saving of ACL-enabled pages." |
paul@98 | 21 | |
paul@98 | 22 | def __init__(self, user, pagename): |
paul@98 | 23 | Permissions.__init__(self, user) |
paul@98 | 24 | self.pagename = pagename |
paul@98 | 25 | |
paul@98 | 26 | def admin(self, pagename): |
paul@98 | 27 | return pagename == self.pagename |
paul@98 | 28 | |
paul@95 | 29 | # Underlying storage mechanisms. |
paul@95 | 30 | |
paul@92 | 31 | class GeneralItemStore: |
paul@91 | 32 | |
paul@91 | 33 | "Common item store functionality." |
paul@91 | 34 | |
paul@91 | 35 | def __init__(self, lock_dir): |
paul@91 | 36 | |
paul@91 | 37 | "Initialise an item store with the given 'lock_dir' guarding access." |
paul@91 | 38 | |
paul@91 | 39 | self.lock_dir = lock_dir |
paul@91 | 40 | self.writelock = lock.WriteLock(lock_dir) |
paul@91 | 41 | self.readlock = lock.ReadLock(lock_dir) |
paul@91 | 42 | |
paul@91 | 43 | def deduce_next(self): |
paul@91 | 44 | |
paul@91 | 45 | "Deduce the next item number from the existing item files." |
paul@91 | 46 | |
paul@91 | 47 | return max(self.get_keys() or [-1]) + 1 |
paul@91 | 48 | |
paul@91 | 49 | # High-level methods. |
paul@91 | 50 | |
paul@91 | 51 | def __len__(self): |
paul@91 | 52 | |
paul@91 | 53 | """ |
paul@91 | 54 | Return the number of items. |
paul@91 | 55 | """ |
paul@91 | 56 | |
paul@91 | 57 | return len(self.keys()) |
paul@91 | 58 | |
paul@91 | 59 | def __iter__(self): |
paul@91 | 60 | |
paul@91 | 61 | "Return an iterator over the items in the store." |
paul@91 | 62 | |
paul@91 | 63 | return ItemIterator(self) |
paul@91 | 64 | |
paul@91 | 65 | def keys(self): |
paul@91 | 66 | |
paul@91 | 67 | "Return a list of keys for items in the store." |
paul@91 | 68 | |
paul@91 | 69 | self.readlock.acquire() |
paul@91 | 70 | try: |
paul@91 | 71 | return self.get_keys() |
paul@91 | 72 | finally: |
paul@91 | 73 | self.readlock.release() |
paul@91 | 74 | |
paul@91 | 75 | def __getitem__(self, number): |
paul@91 | 76 | |
paul@91 | 77 | "Return the item with the given 'number'." |
paul@91 | 78 | |
paul@91 | 79 | self.readlock.acquire() |
paul@91 | 80 | try: |
paul@91 | 81 | try: |
paul@91 | 82 | return self.read_item(number) |
paul@91 | 83 | except (IOError, OSError): |
paul@91 | 84 | raise IndexError, number |
paul@91 | 85 | finally: |
paul@91 | 86 | self.readlock.release() |
paul@91 | 87 | |
paul@91 | 88 | def __delitem__(self, number): |
paul@91 | 89 | |
paul@91 | 90 | "Remove the item with the given 'number' from the store." |
paul@91 | 91 | |
paul@91 | 92 | self.writelock.acquire() |
paul@91 | 93 | try: |
paul@91 | 94 | try: |
paul@91 | 95 | self.remove_item(number) |
paul@91 | 96 | except (IOError, OSError): |
paul@91 | 97 | raise IndexError, number |
paul@91 | 98 | finally: |
paul@91 | 99 | self.writelock.release() |
paul@91 | 100 | |
paul@91 | 101 | def next(self): |
paul@91 | 102 | |
paul@91 | 103 | """ |
paul@91 | 104 | Return the number of the next item (which should also be the number of |
paul@91 | 105 | items if none have been deleted). |
paul@91 | 106 | """ |
paul@91 | 107 | |
paul@91 | 108 | self.writelock.acquire() |
paul@91 | 109 | try: |
paul@91 | 110 | return self.get_next() |
paul@91 | 111 | finally: |
paul@91 | 112 | self.writelock.release() |
paul@91 | 113 | |
paul@92 | 114 | class DirectoryItemStore(GeneralItemStore): |
paul@41 | 115 | |
paul@41 | 116 | "A directory-based item store." |
paul@41 | 117 | |
paul@41 | 118 | def __init__(self, path, lock_dir): |
paul@41 | 119 | |
paul@41 | 120 | "Initialise an item store for the given 'path' and 'lock_dir'." |
paul@41 | 121 | |
paul@41 | 122 | self.path = path |
paul@41 | 123 | self.next_path = os.path.join(self.path, "next") |
paul@41 | 124 | self.lock_dir = lock_dir |
paul@41 | 125 | self.writelock = lock.WriteLock(lock_dir) |
paul@41 | 126 | self.readlock = lock.ReadLock(lock_dir) |
paul@41 | 127 | |
paul@47 | 128 | def mtime(self): |
paul@47 | 129 | |
paul@47 | 130 | "Return the last modified time of the item store directory." |
paul@47 | 131 | |
paul@47 | 132 | return os.path.getmtime(self.path) |
paul@47 | 133 | |
paul@41 | 134 | def get_next(self): |
paul@41 | 135 | |
paul@41 | 136 | "Return the next item number." |
paul@41 | 137 | |
paul@41 | 138 | next = self.read_next() |
paul@41 | 139 | if next is None: |
paul@41 | 140 | next = self.deduce_next() |
paul@41 | 141 | self.write_next(next) |
paul@41 | 142 | return next |
paul@41 | 143 | |
paul@63 | 144 | def get_keys(self): |
paul@63 | 145 | |
paul@63 | 146 | "Return the item keys." |
paul@63 | 147 | |
paul@63 | 148 | return [int(filename) for filename in os.listdir(self.path) if filename.isdigit()] |
paul@63 | 149 | |
paul@41 | 150 | def read_next(self): |
paul@41 | 151 | |
paul@41 | 152 | "Read the next item number from a special file." |
paul@41 | 153 | |
paul@41 | 154 | if not os.path.exists(self.next_path): |
paul@41 | 155 | return None |
paul@41 | 156 | |
paul@41 | 157 | f = open(self.next_path) |
paul@41 | 158 | try: |
paul@41 | 159 | try: |
paul@41 | 160 | return int(f.read()) |
paul@41 | 161 | except ValueError: |
paul@41 | 162 | return None |
paul@41 | 163 | finally: |
paul@41 | 164 | f.close() |
paul@41 | 165 | |
paul@41 | 166 | def write_next(self, next): |
paul@41 | 167 | |
paul@41 | 168 | "Write the 'next' item number to a special file." |
paul@41 | 169 | |
paul@41 | 170 | f = open(self.next_path, "w") |
paul@41 | 171 | try: |
paul@41 | 172 | f.write(str(next)) |
paul@41 | 173 | finally: |
paul@41 | 174 | f.close() |
paul@41 | 175 | |
paul@41 | 176 | def write_item(self, item, next): |
paul@41 | 177 | |
paul@41 | 178 | "Write the given 'item' to a file with the given 'next' item number." |
paul@41 | 179 | |
paul@41 | 180 | f = open(self.get_item_path(next), "w") |
paul@41 | 181 | try: |
paul@41 | 182 | f.write(item) |
paul@41 | 183 | finally: |
paul@41 | 184 | f.close() |
paul@41 | 185 | |
paul@41 | 186 | def read_item(self, number): |
paul@41 | 187 | |
paul@41 | 188 | "Read the item with the given item 'number'." |
paul@41 | 189 | |
paul@41 | 190 | f = open(self.get_item_path(number)) |
paul@41 | 191 | try: |
paul@41 | 192 | return f.read() |
paul@41 | 193 | finally: |
paul@41 | 194 | f.close() |
paul@41 | 195 | |
paul@63 | 196 | def remove_item(self, number): |
paul@63 | 197 | |
paul@63 | 198 | "Remove the item with the given item 'number'." |
paul@63 | 199 | |
paul@63 | 200 | os.remove(self.get_item_path(number)) |
paul@63 | 201 | |
paul@41 | 202 | def get_item_path(self, number): |
paul@41 | 203 | |
paul@41 | 204 | "Get the path for the given item 'number'." |
paul@41 | 205 | |
paul@41 | 206 | path = os.path.abspath(os.path.join(self.path, str(number))) |
paul@41 | 207 | basepath = os.path.join(self.path, "") |
paul@41 | 208 | |
paul@41 | 209 | if os.path.commonprefix([path, basepath]) != basepath: |
paul@41 | 210 | raise OSError, path |
paul@41 | 211 | |
paul@41 | 212 | return path |
paul@41 | 213 | |
paul@41 | 214 | # High-level methods. |
paul@41 | 215 | |
paul@41 | 216 | def append(self, item): |
paul@41 | 217 | |
paul@41 | 218 | "Append the given 'item' to the store." |
paul@41 | 219 | |
paul@41 | 220 | self.writelock.acquire() |
paul@41 | 221 | try: |
paul@41 | 222 | next = self.get_next() |
paul@41 | 223 | self.write_item(item, next) |
paul@41 | 224 | self.write_next(next + 1) |
paul@41 | 225 | finally: |
paul@41 | 226 | self.writelock.release() |
paul@41 | 227 | |
paul@95 | 228 | class SubpageItemStore(GeneralItemStore): |
paul@95 | 229 | |
paul@95 | 230 | "A subpage-based item store." |
paul@95 | 231 | |
paul@95 | 232 | def __init__(self, page, lock_dir): |
paul@95 | 233 | |
paul@95 | 234 | "Initialise an item store for subpages under the given 'page'." |
paul@95 | 235 | |
paul@95 | 236 | GeneralItemStore.__init__(self, lock_dir) |
paul@95 | 237 | self.page = page |
paul@95 | 238 | |
paul@95 | 239 | def mtime(self): |
paul@95 | 240 | |
paul@95 | 241 | "Return the last modified time of the item store." |
paul@95 | 242 | |
paul@95 | 243 | keys = self.get_keys() |
paul@95 | 244 | if not keys: |
paul@95 | 245 | page = self.page |
paul@95 | 246 | else: |
paul@95 | 247 | page = Page(self.page.request, self.get_item_path(max(keys))) |
paul@95 | 248 | |
paul@95 | 249 | return wikiutil.version2timestamp( |
paul@95 | 250 | getMetadata(page)["last-modified"] |
paul@95 | 251 | ) |
paul@95 | 252 | |
paul@95 | 253 | def get_next(self): |
paul@95 | 254 | |
paul@95 | 255 | "Return the next item number." |
paul@95 | 256 | |
paul@95 | 257 | return self.deduce_next() |
paul@95 | 258 | |
paul@95 | 259 | def get_keys(self): |
paul@95 | 260 | |
paul@95 | 261 | "Return the item keys." |
paul@95 | 262 | |
paul@95 | 263 | # Collect the strict subpages of the parent page. |
paul@95 | 264 | |
paul@95 | 265 | leafnames = [] |
paul@95 | 266 | parentname = self.page.page_name |
paul@95 | 267 | |
paul@96 | 268 | for page in getPagesForSearch("title:regex:^%s/" % parentname, self.page.request): |
paul@96 | 269 | basename, leafname = page.page_name.rsplit("/", 1) |
paul@95 | 270 | |
paul@95 | 271 | # Only collect numbered pages immediately below the parent. |
paul@95 | 272 | |
paul@96 | 273 | if basename == parentname and leafname.isdigit(): |
paul@96 | 274 | leafnames.append(int(leafname)) |
paul@95 | 275 | |
paul@95 | 276 | return leafnames |
paul@95 | 277 | |
paul@95 | 278 | def write_item(self, item, next): |
paul@95 | 279 | |
paul@95 | 280 | "Write the given 'item' to a file with the given 'next' item number." |
paul@95 | 281 | |
paul@98 | 282 | request = self.page.request |
paul@98 | 283 | acl = self.page.getACL(request) |
paul@98 | 284 | pagename = self.get_item_path(next) |
paul@98 | 285 | |
paul@98 | 286 | # To add a page with an ACL, a special policy is required. |
paul@98 | 287 | |
paul@98 | 288 | may = request.user.may |
paul@98 | 289 | request.user.may = AdminPermissionsForPage(request.user, pagename) |
paul@98 | 290 | |
paul@98 | 291 | # Attempt to save the page, copying any ACL. |
paul@98 | 292 | |
paul@98 | 293 | try: |
paul@98 | 294 | page = PageEditor(request, pagename) |
paul@98 | 295 | page.saveText(acl.getString() + item, 0) |
paul@98 | 296 | |
paul@98 | 297 | # Restore the original policy. |
paul@98 | 298 | |
paul@98 | 299 | finally: |
paul@98 | 300 | request.user.may = may |
paul@95 | 301 | |
paul@95 | 302 | def read_item(self, number): |
paul@95 | 303 | |
paul@95 | 304 | "Read the item with the given item 'number'." |
paul@95 | 305 | |
paul@95 | 306 | page = Page(self.page.request, self.get_item_path(number)) |
paul@98 | 307 | |
paul@98 | 308 | # Remove any page directives. |
paul@98 | 309 | |
paul@98 | 310 | return page.data |
paul@95 | 311 | |
paul@95 | 312 | def remove_item(self, number): |
paul@95 | 313 | |
paul@95 | 314 | "Remove the item with the given item 'number'." |
paul@95 | 315 | |
paul@95 | 316 | page = PageEditor(self.page.request, self.get_item_path(number)) |
paul@95 | 317 | page.deletePage() |
paul@95 | 318 | |
paul@95 | 319 | def get_item_path(self, number): |
paul@95 | 320 | |
paul@95 | 321 | "Get the path for the given item 'number'." |
paul@95 | 322 | |
paul@95 | 323 | return "%s/%s" % (self.page.page_name, number) |
paul@95 | 324 | |
paul@95 | 325 | # High-level methods. |
paul@95 | 326 | |
paul@95 | 327 | def append(self, item): |
paul@95 | 328 | |
paul@95 | 329 | "Append the given 'item' to the store." |
paul@95 | 330 | |
paul@95 | 331 | self.writelock.acquire() |
paul@95 | 332 | try: |
paul@95 | 333 | next = self.get_next() |
paul@95 | 334 | self.write_item(item, next) |
paul@95 | 335 | finally: |
paul@95 | 336 | self.writelock.release() |
paul@95 | 337 | |
paul@41 | 338 | class ItemIterator: |
paul@41 | 339 | |
paul@41 | 340 | "An iterator over items in a store." |
paul@41 | 341 | |
paul@46 | 342 | def __init__(self, store, direction=1): |
paul@41 | 343 | self.store = store |
paul@46 | 344 | self.direction = direction |
paul@46 | 345 | self.reset() |
paul@46 | 346 | |
paul@46 | 347 | def reset(self): |
paul@46 | 348 | if self.direction == 1: |
paul@46 | 349 | self._next = 0 |
paul@63 | 350 | self.final = self.store.next() |
paul@46 | 351 | else: |
paul@63 | 352 | self._next = self.store.next() - 1 |
paul@46 | 353 | self.final = 0 |
paul@46 | 354 | |
paul@46 | 355 | def more(self): |
paul@46 | 356 | if self.direction == 1: |
paul@46 | 357 | return self._next < self.final |
paul@46 | 358 | else: |
paul@46 | 359 | return self._next >= self.final |
paul@46 | 360 | |
paul@46 | 361 | def get_next(self): |
paul@46 | 362 | next = self._next |
paul@46 | 363 | self._next += self.direction |
paul@46 | 364 | return next |
paul@41 | 365 | |
paul@41 | 366 | def next(self): |
paul@46 | 367 | while self.more(): |
paul@41 | 368 | try: |
paul@46 | 369 | return self.store[self.get_next()] |
paul@41 | 370 | except IndexError: |
paul@41 | 371 | pass |
paul@41 | 372 | |
paul@41 | 373 | raise StopIteration |
paul@41 | 374 | |
paul@46 | 375 | def reverse(self): |
paul@46 | 376 | self.direction = -self.direction |
paul@46 | 377 | self.reset() |
paul@46 | 378 | |
paul@46 | 379 | def reversed(self): |
paul@46 | 380 | self.reverse() |
paul@46 | 381 | return self |
paul@46 | 382 | |
paul@46 | 383 | def __iter__(self): |
paul@46 | 384 | return self |
paul@46 | 385 | |
paul@95 | 386 | def getDirectoryItemStoreForPage(page, item_dir, lock_dir): |
paul@95 | 387 | |
paul@95 | 388 | """ |
paul@95 | 389 | A convenience function returning a directory-based store for the given |
paul@95 | 390 | 'page', using the given 'item_dir' and 'lock_dir'. |
paul@95 | 391 | """ |
paul@95 | 392 | |
paul@95 | 393 | item_dir_path = tuple(item_dir.split("/")) |
paul@95 | 394 | lock_dir_path = tuple(lock_dir.split("/")) |
paul@95 | 395 | return DirectoryItemStore(page.getPagePath(*item_dir_path), page.getPagePath(*lock_dir_path)) |
paul@95 | 396 | |
paul@95 | 397 | def getSubpageItemStoreForPage(page, lock_dir): |
paul@95 | 398 | |
paul@95 | 399 | """ |
paul@95 | 400 | A convenience function returning a subpage-based store for the given |
paul@95 | 401 | 'page', using the given 'lock_dir'. |
paul@95 | 402 | """ |
paul@95 | 403 | |
paul@95 | 404 | lock_dir_path = tuple(lock_dir.split("/")) |
paul@95 | 405 | return SubpageItemStore(page, page.getPagePath(*lock_dir_path)) |
paul@95 | 406 | |
paul@95 | 407 | # Page-oriented item store classes. |
paul@95 | 408 | |
paul@95 | 409 | class ItemStoreBase: |
paul@95 | 410 | |
paul@95 | 411 | "Access item stores via pages, observing page access restrictions." |
paul@95 | 412 | |
paul@95 | 413 | def __init__(self, page, store): |
paul@95 | 414 | self.page = page |
paul@95 | 415 | self.store = store |
paul@95 | 416 | |
paul@95 | 417 | def can_write(self): |
paul@95 | 418 | |
paul@95 | 419 | """ |
paul@95 | 420 | Return whether the user associated with the request can write to the |
paul@95 | 421 | page owning this store. |
paul@95 | 422 | """ |
paul@95 | 423 | |
paul@95 | 424 | user = self.page.request.user |
paul@95 | 425 | return user and user.may.write(self.page.page_name) |
paul@95 | 426 | |
paul@95 | 427 | def can_read(self): |
paul@95 | 428 | |
paul@95 | 429 | """ |
paul@95 | 430 | Return whether the user associated with the request can read from the |
paul@95 | 431 | page owning this store. |
paul@95 | 432 | """ |
paul@95 | 433 | |
paul@95 | 434 | user = self.page.request.user |
paul@95 | 435 | return user and user.may.read(self.page.page_name) |
paul@95 | 436 | |
paul@95 | 437 | def can_delete(self): |
paul@95 | 438 | |
paul@95 | 439 | """ |
paul@95 | 440 | Return whether the user associated with the request can delete the |
paul@95 | 441 | page owning this store. |
paul@95 | 442 | """ |
paul@95 | 443 | |
paul@95 | 444 | user = self.page.request.user |
paul@95 | 445 | return user and user.may.delete(self.page.page_name) |
paul@95 | 446 | |
paul@95 | 447 | # Store-specific methods. |
paul@95 | 448 | |
paul@95 | 449 | def mtime(self): |
paul@95 | 450 | return self.store.mtime() |
paul@95 | 451 | |
paul@95 | 452 | # High-level methods. |
paul@95 | 453 | |
paul@95 | 454 | def keys(self): |
paul@95 | 455 | |
paul@95 | 456 | "Return a list of keys for items in the store." |
paul@95 | 457 | |
paul@95 | 458 | if not self.can_read(): |
paul@95 | 459 | return 0 |
paul@95 | 460 | |
paul@95 | 461 | return self.store.keys() |
paul@95 | 462 | |
paul@95 | 463 | def append(self, item): |
paul@95 | 464 | |
paul@95 | 465 | "Append the given 'item' to the store." |
paul@95 | 466 | |
paul@95 | 467 | if not self.can_write(): |
paul@95 | 468 | return |
paul@95 | 469 | |
paul@95 | 470 | self.store.append(item) |
paul@95 | 471 | |
paul@95 | 472 | def __len__(self): |
paul@95 | 473 | |
paul@95 | 474 | "Return the number of items in the store." |
paul@95 | 475 | |
paul@95 | 476 | if not self.can_read(): |
paul@95 | 477 | return 0 |
paul@95 | 478 | |
paul@95 | 479 | return len(self.store) |
paul@95 | 480 | |
paul@95 | 481 | def __getitem__(self, number): |
paul@95 | 482 | |
paul@95 | 483 | "Return the item with the given 'number'." |
paul@95 | 484 | |
paul@95 | 485 | if not self.can_read(): |
paul@95 | 486 | raise IndexError, number |
paul@95 | 487 | |
paul@95 | 488 | return self.store.__getitem__(number) |
paul@95 | 489 | |
paul@95 | 490 | def __delitem__(self, number): |
paul@95 | 491 | |
paul@95 | 492 | "Remove the item with the given 'number'." |
paul@95 | 493 | |
paul@95 | 494 | if not self.can_delete(): |
paul@95 | 495 | return |
paul@95 | 496 | |
paul@95 | 497 | return self.store.__delitem__(number) |
paul@95 | 498 | |
paul@95 | 499 | def __iter__(self): |
paul@95 | 500 | return self.store.__iter__() |
paul@95 | 501 | |
paul@95 | 502 | def next(self): |
paul@95 | 503 | return self.store.next() |
paul@95 | 504 | |
paul@95 | 505 | # Convenience store classes. |
paul@95 | 506 | |
paul@95 | 507 | class ItemStore(ItemStoreBase): |
paul@95 | 508 | |
paul@95 | 509 | "Store items in a directory via a page." |
paul@95 | 510 | |
paul@95 | 511 | def __init__(self, page, item_dir="items", lock_dir="item_locks"): |
paul@95 | 512 | ItemStoreBase.__init__(self, page, getDirectoryItemStoreForPage(page, item_dir, lock_dir)) |
paul@95 | 513 | |
paul@95 | 514 | class ItemSubpageStore(ItemStoreBase): |
paul@95 | 515 | |
paul@95 | 516 | "Store items in subpages of a page." |
paul@95 | 517 | |
paul@95 | 518 | def __init__(self, page, lock_dir="item_locks"): |
paul@95 | 519 | ItemStoreBase.__init__(self, page, getSubpageItemStoreForPage(page, lock_dir)) |
paul@95 | 520 | |
paul@41 | 521 | # vim: tabstop=4 expandtab shiftwidth=4 |