/usr/share/pyshared/MoinMoin/Page.py is in python-moinmoin 1.9.3-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 | # -*- coding: iso-8859-1 -*-
"""
MoinMoin - Page class
Page is used for read-only access to a wiki page. For r/w access see PageEditor.
A Page object is used to access a wiki page (in general) as well as to access
some specific revision of a wiki page.
The RootPage is some virtual page located at / and is mainly used to do namespace
operations like getting the page list.
Currently, this is all a big mixture between high-level page code, intermediate
data/underlay layering code, caching code and low-level filesystem storage code.
To see the filesystem storage layout we use, best is to look into data/pages/
(underlay dir uses the same format).
TODO:
* Cleanly separate the code into packages for:
* Page (or rather: Item)
* Layering
* Cache
* Storage
* ACLs should be handled on a low layer, raising an Exception when access
is denied, so we won't have security issues just because someone forgot to check
user.may.read(secretpage).
* The distinction between a item and a item revision should be clearer.
* Items can be anything, not just wiki pages, but also files of any mimetype.
The mimetype hierarchy should be modelled by a MimeTypeItem class hierarchy.
@copyright: 2000-2004 by Juergen Hermann <jh@web.de>,
2005-2008 by MoinMoin:ThomasWaldmann,
2006 by MoinMoin:FlorianFesti,
2007 by MoinMoin:ReimarBauer
@license: GNU GPL, see COPYING for details.
"""
import os, re, codecs
from MoinMoin import log
logging = log.getLogger(__name__)
from MoinMoin import config, caching, user, util, wikiutil
from MoinMoin.logfile import eventlog
def is_cache_exception(e):
args = e.args
return not (len(args) != 1 or args[0] != 'CacheNeedsUpdate')
class ItemCache:
""" Cache some page item related data, as meta data or pagelist
We only cache this to RAM in request.cfg (this is the only kind of
server object we have), because it might be too big for pickling it
in and out.
"""
def __init__(self, name):
""" Initialize ItemCache object.
@param name: name of the object, used for display in logging and
influences behaviour of refresh().
"""
self.name = name
self.cache = {}
self.log_pos = None # TODO: initialize this to EOF pos of log
# to avoid reading in the whole log on first request
self.requests = 0
self.hits = 0
self.loglevel = logging.NOTSET
def putItem(self, request, name, key, data):
""" Remembers some data for item name under a key.
@param request: currently unused
@param name: name of the item (page), unicode
@param key: used as secondary access key after name
@param data: the data item that should be remembered
"""
d = self.cache.setdefault(name, {})
d[key] = data
def getItem(self, request, name, key):
""" Returns some item stored for item name under key.
@param request: the request object
@param name: name of the item (page), unicode
@param key: used as secondary access key after name
@return: the data or None, if there is no such name or key.
"""
self.refresh(request)
try:
data = self.cache[name][key]
self.hits += 1
hit_str = 'hit'
except KeyError:
data = None
hit_str = 'miss'
self.requests += 1
logging.log(self.loglevel, "%s cache %s (h/r %2.1f%%) for %r %r" % (
self.name,
hit_str,
float(self.hits * 100) / self.requests,
name,
key,
))
return data
def refresh(self, request):
""" Refresh the cache - if anything has changed in the wiki, we see it
in the edit-log and either delete cached data for the changed items
(for 'meta') or the complete cache ('pagelists').
@param request: the request object
"""
from MoinMoin.logfile import editlog
elog = editlog.EditLog(request)
old_pos = self.log_pos
new_pos, items = elog.news(old_pos)
if items:
if self.name == 'meta':
for item in items:
logging.log(self.loglevel, "cache: removing %r" % item)
try:
del self.cache[item]
except:
pass
elif self.name == 'pagelists':
logging.log(self.loglevel, "cache: clearing pagelist cache")
self.cache = {}
self.log_pos = new_pos # important to do this at the end -
# avoids threading race conditions
class Page(object):
""" Page - Manage an (immutable) page associated with a WikiName.
To change a page's content, use the PageEditor class.
"""
def __init__(self, request, page_name, **kw):
""" Create page object.
Note that this is a 'lean' operation, since the text for the page
is loaded on demand. Thus, things like `Page(name).link_to()` are
efficient.
@param page_name: WikiName of the page
@keyword rev: number of older revision
@keyword formatter: formatter instance or mimetype str,
None or no kw arg will use default formatter
@keyword include_self: if 1, include current user (default: 0)
"""
self.request = request
self.cfg = request.cfg
self.page_name = page_name
self.rev = kw.get('rev', 0) # revision of this page
self.include_self = kw.get('include_self', 0)
formatter = kw.get('formatter', None)
if isinstance(formatter, (str, unicode)): # mimetype given
mimetype = str(formatter)
self.formatter = None
self.output_mimetype = mimetype
self.default_formatter = mimetype == "text/html"
elif formatter is not None: # formatter instance given
self.formatter = formatter
self.default_formatter = 0
self.output_mimetype = "text/todo" # TODO where do we get this value from?
else:
self.formatter = None
self.default_formatter = 1
self.output_mimetype = "text/html"
self.output_charset = config.charset # correct for wiki pages
self._text_filename_force = None
self.hilite_re = None
self.__body = None # unicode page body == metadata + data
self.__body_modified = 0 # was __body modified in RAM so it differs from disk?
self.__meta = None # list of raw tuples of page metadata (currently: the # stuff at top of the page)
self.__pi = None # dict of preprocessed page metadata (processing instructions)
self.__data = None # unicode page data = body - metadata
self.reset()
def reset(self):
""" Reset page state """
page_name = self.page_name
# page_name quoted for file system usage, needs to be reset to
# None when pagename changes
qpagename = wikiutil.quoteWikinameFS(page_name)
self.page_name_fs = qpagename
# the normal and the underlay path used for this page
normalpath = os.path.join(self.cfg.data_dir, "pages", qpagename)
if not self.cfg.data_underlay_dir is None:
underlaypath = os.path.join(self.cfg.data_underlay_dir, "pages", qpagename)
else:
underlaypath = None
# TUNING - remember some essential values
# does the page come from normal page storage (0) or from
# underlay dir (1) (can be used as index into following list)
self._underlay = None
# path to normal / underlay page dir
self._pagepath = [normalpath, underlaypath]
# now we define some properties to lazy load some attributes on first access:
def get_body(self):
if self.__body is None:
# try to open file
try:
f = codecs.open(self._text_filename(), 'rb', config.charset)
except IOError, er:
import errno
if er.errno == errno.ENOENT:
# just doesn't exist, return empty text (note that we
# never store empty pages, so this is detectable and also
# safe when passed to a function expecting a string)
return ""
else:
raise
# read file content and make sure it is closed properly
try:
text = f.read()
text = self.decodeTextMimeType(text)
self.__body = text
finally:
f.close()
return self.__body
def set_body(self, newbody):
self.__body = newbody
self.__meta = None
self.__data = None
body = property(fget=get_body, fset=set_body) # complete page text
def get_meta(self):
if self.__meta is None:
self.__meta, self.__data = wikiutil.get_processing_instructions(self.body)
return self.__meta
meta = property(fget=get_meta) # processing instructions, ACLs (upper part of page text)
def get_data(self):
if self.__data is None:
self.__meta, self.__data = wikiutil.get_processing_instructions(self.body)
return self.__data
data = property(fget=get_data) # content (lower part of page text)
def get_pi(self):
if self.__pi is None:
self.__pi = self.parse_processing_instructions()
return self.__pi
pi = property(fget=get_pi) # processed meta stuff
def getlines(self):
""" Return a list of all lines in body.
@rtype: list
@return: list of strs body_lines
"""
return self.body.split('\n')
def get_raw_body(self):
""" Load the raw markup from the page file.
@rtype: unicode
@return: raw page contents of this page, unicode
"""
return self.body
def get_raw_body_str(self):
""" Returns the raw markup from the page file, as a string.
@rtype: str
@return: raw page contents of this page, utf-8-encoded
"""
return self.body.encode("utf-8")
def set_raw_body(self, body, modified=0):
""" Set the raw body text (prevents loading from disk).
TODO: this should not be a public function, as Page is immutable.
@param body: raw body text
@param modified: 1 means that we internally modified the raw text and
that it is not in sync with the page file on disk. This is
used e.g. by PageEditor when previewing the page.
"""
self.body = body
self.__body_modified = modified
def get_current_from_pagedir(self, pagedir):
""" Get the current revision number from an arbitrary pagedir.
Does not modify page object's state, uncached, direct disk access.
@param pagedir: the pagedir with the 'current' file to read
@return: int currentrev
"""
revfilename = os.path.join(pagedir, 'current')
try:
revfile = file(revfilename)
revstr = revfile.read().strip()
revfile.close()
rev = int(revstr)
except:
rev = 99999999 # XXX do some better error handling
return rev
def get_rev_dir(self, pagedir, rev=0):
""" Get a revision of a page from an arbitrary pagedir.
Does not modify page object's state, uncached, direct disk access.
@param pagedir: the path to the page storage area
@param rev: int revision to get (default is 0 and means the current
revision (in this case, the real revint is returned)
@return: (str path to file of the revision,
int realrevint,
bool exists)
"""
if rev == 0:
rev = self.get_current_from_pagedir(pagedir)
revstr = '%08d' % rev
pagefile = os.path.join(pagedir, 'revisions', revstr)
if rev != 99999999:
exists = os.path.exists(pagefile)
if exists:
self._setRealPageName(pagedir)
else:
exists = False
return pagefile, rev, exists
def _setRealPageName(self, pagedir):
""" Set page_name to the real case of page name
On case insensitive file system, "pagename" exists even if the
real page name is "PageName" or "PAGENAME". This leads to
confusion in urls, links and logs.
See MoinMoinBugs/MacHfsPlusCaseInsensitive
Correct the case of the page name. Elements created from the
page name in reset() are not updated because it's too messy, and
this fix seems to be enough for now.
Problems to fix later:
- ["helponnavigation"] link to HelpOnNavigation but not
considered as backlink.
@param pagedir: the storage path to the page directory
"""
if self._text_filename_force is None:
# we only do this for normal pages, but not for the MissingPage,
# because the code below is wrong in that case
realPath = util.filesys.realPathCase(pagedir)
if realPath is not None:
realPath = wikiutil.unquoteWikiname(realPath)
self.page_name = realPath[-len(self.page_name):]
def get_rev(self, use_underlay=-1, rev=0):
""" Get information about a revision.
filename, number, and (existance test) of this page and revision.
@param use_underlay: -1 == auto, 0 == normal, 1 == underlay
@param rev: int revision to get (default is 0 and means the current
revision (in this case, the real revint is returned)
@return: (str path to current revision of page,
int realrevint,
bool exists)
"""
def layername(underlay):
if underlay == -1:
return 'layer_auto'
elif underlay == 0:
return 'layer_normal'
else: # 1
return 'layer_underlay'
request = self.request
cache_name = self.page_name
cache_key = layername(use_underlay)
if self._text_filename_force is None:
cache_data = request.cfg.cache.meta.getItem(request, cache_name, cache_key)
if cache_data and (rev == 0 or rev == cache_data[1]):
# we got the correct rev data from the cache
#logging.debug("got data from cache: %r %r %r" % cache_data)
return cache_data
# Figure out if we should use underlay or not, if needed.
if use_underlay == -1:
underlay, pagedir = self.getPageStatus(check_create=0)
else:
underlay, pagedir = use_underlay, self._pagepath[use_underlay]
# Find current revision, if automatic selection is requested.
if rev == 0:
realrev = self.get_current_from_pagedir(pagedir)
else:
realrev = rev
data = self.get_rev_dir(pagedir, realrev)
if rev == 0 and self._text_filename_force is None:
# we only save the current rev to the cache
request.cfg.cache.meta.putItem(request, cache_name, cache_key, data)
return data
def current_rev(self):
""" Return number of current revision.
This is the same as get_rev()[1].
@return: int revision
"""
pagefile, rev, exists = self.get_rev()
return rev
def get_real_rev(self):
""" Returns the real revision number of this page.
A rev==0 is translated to the current revision.
@returns: revision number > 0
@rtype: int
"""
if self.rev == 0:
return self.current_rev()
return self.rev
def getPageBasePath(self, use_underlay=-1):
""" Get full path to a page-specific storage area. `args` can
contain additional path components that are added to the base path.
@param use_underlay: force using a specific pagedir, default -1
-1 = automatically choose page dir
1 = use underlay page dir
0 = use standard page dir
@rtype: string
@return: int underlay,
str the full path to the storage area
"""
standardpath, underlaypath = self._pagepath
if underlaypath is None:
use_underlay = 0
if use_underlay == -1: # automatic
if self._underlay is None:
underlay, path = 0, standardpath
pagefile, rev, exists = self.get_rev(use_underlay=0)
if not exists:
pagefile, rev, exists = self.get_rev(use_underlay=1)
if exists:
underlay, path = 1, underlaypath
self._underlay = underlay
else:
underlay = self._underlay
path = self._pagepath[underlay]
else: # normal or underlay
underlay, path = use_underlay, self._pagepath[use_underlay]
return underlay, path
def getPageStatus(self, *args, **kw):
""" Get full path to a page-specific storage area. `args` can
contain additional path components that are added to the base path.
@param args: additional path components
@keyword use_underlay: force using a specific pagedir, default '-1'
-1 = automatically choose page dir
1 = use underlay page dir
0 = use standard page dir
@keyword check_create: if true, ensures that the path requested really exists
(if it doesn't, create all directories automatically).
(default true)
@keyword isfile: is the last component in args a filename? (default is false)
@rtype: string
@return: (int underlay (1 if using underlay, 0 otherwise),
str the full path to the storage area )
"""
check_create = kw.get('check_create', 1)
isfile = kw.get('isfile', 0)
use_underlay = kw.get('use_underlay', -1)
underlay, path = self.getPageBasePath(use_underlay)
fullpath = os.path.join(*((path, ) + args))
if check_create:
if isfile:
dirname, filename = os.path.split(fullpath)
else:
dirname = fullpath
try:
os.makedirs(dirname)
except OSError, err:
if not os.path.exists(dirname):
raise
return underlay, fullpath
def getPagePath(self, *args, **kw):
""" Return path to the page storage area. """
return self.getPageStatus(*args, **kw)[1]
def _text_filename(self, **kw):
""" The name of the page file, possibly of an older page.
@keyword rev: page revision, overriding self.rev
@rtype: string
@return: complete filename (including path) to this page
"""
if self._text_filename_force is not None:
return self._text_filename_force
rev = kw.get('rev', 0)
if not rev and self.rev:
rev = self.rev
fname, rev, exists = self.get_rev(-1, rev)
return fname
def editlog_entry(self):
""" Return the edit-log entry for this Page object (can be an old revision).
"""
request = self.request
use_cache = self.rev == 0 # use the cache for current rev
if use_cache:
cache_name, cache_key = self.page_name, 'lastlog'
entry = request.cfg.cache.meta.getItem(request, cache_name, cache_key)
else:
entry = None
if entry is None:
from MoinMoin.logfile import editlog
wanted_rev = "%08d" % self.get_real_rev()
edit_log = editlog.EditLog(request, rootpagename=self.page_name)
for entry in edit_log.reverse():
if entry.rev == wanted_rev:
break
else:
entry = () # don't use None
if use_cache:
request.cfg.cache.meta.putItem(request, cache_name, cache_key, entry)
return entry
def edit_info(self):
""" Return timestamp/editor info for this Page object (can be an old revision).
Note: if you ask about a deleted revision, it will report timestamp and editor
for the delete action (in the edit-log, this is just a SAVE).
This is used by MoinMoin/xmlrpc/__init__.py.
@rtype: dict
@return: timestamp and editor information
"""
line = self.editlog_entry()
if line:
editordata = line.getInterwikiEditorData(self.request)
if editordata[0] == 'interwiki':
editor = "%s:%s" % editordata[1]
else:
editor = editordata[1] # ip or email or anon
result = {
'timestamp': line.ed_time_usecs,
'editor': editor,
}
del line
else:
result = {}
return result
def last_edit(self, request):
# XXX usage of last_edit is DEPRECATED - use edit_info()
if not self.exists(): # XXX doesn't make much sense, but still kept
return None # XXX here until we remove last_edit()
return self.edit_info()
def lastEditInfo(self, request=None):
""" Return the last edit info.
Note: if you ask about a deleted revision, it will report timestamp and editor
for the delete action (in the edit-log, this is just a SAVE).
@param request: the request object (DEPRECATED, unused)
@rtype: dict
@return: timestamp and editor information
"""
log = self.editlog_entry()
if log:
request = self.request
editor = log.getEditor(request)
time = wikiutil.version2timestamp(log.ed_time_usecs)
time = request.user.getFormattedDateTime(time) # Use user time format
result = {'editor': editor, 'time': time}
del log
else:
result = {}
return result
def isWritable(self):
""" Can this page be changed?
@rtype: bool
@return: true, if this page is writable or does not exist
"""
return os.access(self._text_filename(), os.W_OK) or not self.exists()
def isUnderlayPage(self, includeDeleted=True):
""" Does this page live in the underlay dir?
Return true even if the data dir has a copy of this page. To
check for underlay only page, use ifUnderlayPage() and not
isStandardPage()
@param includeDeleted: include deleted pages
@rtype: bool
@return: true if page lives in the underlay dir
"""
return self.exists(domain='underlay', includeDeleted=includeDeleted)
def isStandardPage(self, includeDeleted=True):
""" Does this page live in the data dir?
Return true even if this is a copy of an underlay page. To check
for data only page, use isStandardPage() and not isUnderlayPage().
@param includeDeleted: include deleted pages
@rtype: bool
@return: true if page lives in the data dir
"""
return self.exists(domain='standard', includeDeleted=includeDeleted)
def exists(self, rev=0, domain=None, includeDeleted=False):
""" Does this page exist?
This is the lower level method for checking page existence. Use
the higher level methods isUnderlayPage and isStandardPage for
cleaner code.
@param rev: revision to look for. Default: check current
@param domain: where to look for the page. Default: look in all,
available values: 'underlay', 'standard'
@param includeDeleted: ignore page state, just check its pagedir
@rtype: bool
@return: true, if page exists
"""
# Edge cases
if domain == 'underlay' and not self.request.cfg.data_underlay_dir:
return False
if includeDeleted:
# Look for page directory, ignore page state
if domain is None:
checklist = [0, 1]
else:
checklist = [domain == 'underlay']
for use_underlay in checklist:
pagedir = self.getPagePath(use_underlay=use_underlay, check_create=0)
if os.path.exists(pagedir):
return True
return False
else:
# Look for non-deleted pages only, using get_rev
if not rev and self.rev:
rev = self.rev
if domain is None:
use_underlay = -1
else:
use_underlay = domain == 'underlay'
d, d, exists = self.get_rev(use_underlay, rev)
return exists
def size(self, rev=0):
""" Get Page size.
@rtype: int
@return: page size, 0 for non-existent pages.
"""
if rev == self.rev: # same revision as self
if self.__body is not None:
return len(self.__body)
try:
return os.path.getsize(self._text_filename(rev=rev))
except EnvironmentError, e:
import errno
if e.errno == errno.ENOENT:
return 0
raise
def mtime_usecs(self):
""" Get modification timestamp of this page (from edit-log, can be for an old revision).
@rtype: int
@return: mtime of page (or 0 if page / edit-log entry does not exist)
"""
entry = self.editlog_entry()
return entry and entry.ed_time_usecs or 0
def mtime_printable(self, request):
""" Get printable (as per user's preferences) modification timestamp of this page.
@rtype: string
@return: formatted string with mtime of page
"""
t = self.mtime_usecs()
if not t:
result = "0" # TODO: i18n, "Ever", "Beginning of time"...?
else:
result = request.user.getFormattedDateTime(
wikiutil.version2timestamp(t))
return result
def split_title(self, force=0):
""" Return a string with the page name split by spaces, if the user wants that.
@param force: if != 0, then force splitting the page_name
@rtype: unicode
@return: pagename of this page, splitted into space separated words
"""
request = self.request
if not force and not request.user.wikiname_add_spaces:
return self.page_name
# look for the end of words and the start of a new word,
# and insert a space there
splitted = config.split_regex.sub(r'\1 \2', self.page_name)
return splitted
def url(self, request, querystr=None, anchor=None, relative=False, **kw):
""" Return complete URL for this page, including scriptname.
The URL is NOT escaped, if you write it to HTML, use wikiutil.escape
(at least if you have a querystr, to escape the & chars).
@param request: the request object
@param querystr: the query string to add after a "?" after the url
(str or dict, see wikiutil.makeQueryString)
@param anchor: if specified, make a link to this anchor
@param relative: create a relative link (default: False), note that this
changed in 1.7, in 1.6, the default was True.
@rtype: str
@return: complete url of this page, including scriptname
"""
assert(isinstance(anchor, (type(None), str, unicode)))
# Create url, excluding scriptname
url = wikiutil.quoteWikinameURL(self.page_name)
if querystr:
if isinstance(querystr, dict):
action = querystr.get('action', None)
else:
action = None # we don't support getting the action out of a str
querystr = wikiutil.makeQueryString(querystr)
# make action URLs denyable by robots.txt:
if action is not None and request.cfg.url_prefix_action is not None:
url = "%s/%s/%s" % (request.cfg.url_prefix_action, action, url)
url = '%s?%s' % (url, querystr)
if not relative:
url = '%s/%s' % (request.script_root, url)
# Add anchor
if anchor:
fmt = getattr(self, 'formatter', request.html_formatter)
if fmt:
anchor = fmt.sanitize_to_id(anchor)
url = "%s#%s" % (url, anchor)
return url
def link_to_raw(self, request, text, querystr=None, anchor=None, **kw):
""" core functionality of link_to, without the magic """
url = self.url(request, querystr, anchor=anchor, relative=True) # scriptName is added by link_tag
# escaping is done by link_tag -> formatter.url -> ._open()
link = wikiutil.link_tag(request, url, text,
formatter=getattr(self, 'formatter', None), **kw)
return link
def link_to(self, request, text=None, querystr=None, anchor=None, **kw):
""" Return HTML markup that links to this page.
See wikiutil.link_tag() for possible keyword parameters.
@param request: the request object
@param text: inner text of the link - it gets automatically escaped
@param querystr: the query string to add after a "?" after the url
@param anchor: if specified, make a link to this anchor
@keyword on: opening/closing tag only
@keyword attachment_indicator: if 1, add attachment indicator after link tag
@keyword css_class: css class to use
@rtype: string
@return: formatted link
"""
if not text:
text = self.split_title()
text = wikiutil.escape(text)
# Add css class for non existing page
if not self.exists():
kw['css_class'] = 'nonexistent'
attachment_indicator = kw.get('attachment_indicator')
if attachment_indicator is None:
attachment_indicator = 0 # default is off
else:
del kw['attachment_indicator'] # avoid having this as <a> tag attribute
link = self.link_to_raw(request, text, querystr, anchor, **kw)
# Create a link to attachments if any exist
if attachment_indicator:
from MoinMoin.action import AttachFile
link += AttachFile.getIndicator(request, self.page_name)
return link
def getSubscribers(self, request, **kw):
""" Get all subscribers of this page.
@param request: the request object
@keyword include_self: if 1, include current user (default: 0)
@keyword return_users: if 1, return user instances (default: 0)
@rtype: dict
@return: lists of subscribed email addresses in a dict by language key
"""
include_self = kw.get('include_self', self.include_self)
return_users = kw.get('return_users', 0)
# extract categories of this page
pageList = self.getCategories(request)
# add current page name for list matching
pageList.append(self.page_name)
if self.cfg.SecurityPolicy:
UserPerms = self.cfg.SecurityPolicy
else:
from MoinMoin.security import Default as UserPerms
# get email addresses of the all wiki user which have a profile stored;
# add the address only if the user has subscribed to the page and
# the user is not the current editor
userlist = user.getUserList(request)
subscriber_list = {}
for uid in userlist:
if uid == request.user.id and not include_self:
continue # no self notification
subscriber = user.User(request, uid)
# The following tests should be ordered in order of
# decreasing computation complexity, in particular
# the permissions check may be expensive; see the bug
# MoinMoinBugs/GetSubscribersPerformanceProblem
# This is a bit wrong if return_users=1 (which implies that the caller will process
# user attributes and may, for example choose to send an SMS)
# So it _should_ be "not (subscriber.email and return_users)" but that breaks at the moment.
if not subscriber.email:
continue # skip empty email addresses
# skip people not subscribed
if not subscriber.isSubscribedTo(pageList):
continue
# skip people who can't read the page
if not UserPerms(subscriber).read(self.page_name):
continue
# add the user to the list
lang = subscriber.language or request.cfg.language_default
if not lang in subscriber_list:
subscriber_list[lang] = []
if return_users:
subscriber_list[lang].append(subscriber)
else:
subscriber_list[lang].append(subscriber.email)
return subscriber_list
def parse_processing_instructions(self):
""" Parse page text and extract processing instructions,
return a dict of PIs and the non-PI rest of the body.
"""
from MoinMoin import i18n
from MoinMoin import security
request = self.request
pi = {} # we collect the processing instructions here
# default language from cfg
pi['language'] = self.cfg.language_default or "en"
body = self.body
# TODO: remove this hack once we have separate metadata and can use mimetype there
if body.startswith('<?xml'): # check for XML content
pi['lines'] = 0
pi['format'] = "xslt"
pi['formatargs'] = ''
pi['acl'] = security.AccessControlList(request.cfg, []) # avoid KeyError on acl check
return pi
meta = self.meta
# default is wiki markup
pi['format'] = self.cfg.default_markup or "wiki"
pi['formatargs'] = ''
pi['lines'] = len(meta)
acl = []
for verb, args in meta:
if verb == "format": # markup format
format, formatargs = (args + ' ').split(' ', 1)
pi['format'] = format.lower()
pi['formatargs'] = formatargs.strip()
elif verb == "acl":
acl.append(args)
elif verb == "language":
# Page language. Check if args is a known moin language
if args in i18n.wikiLanguages():
pi['language'] = args
elif verb == "refresh":
if self.cfg.refresh:
try:
mindelay, targetallowed = self.cfg.refresh
args = args.split()
if len(args) >= 1:
delay = max(int(args[0]), mindelay)
if len(args) >= 2:
target = args[1]
else:
target = self.page_name
if '://' in target:
if targetallowed == 'internal':
raise ValueError
elif targetallowed == 'external':
url = target
else:
url = Page(request, target).url(request)
pi['refresh'] = (delay, url)
except (ValueError, ):
pass
elif verb == "redirect":
pi['redirect'] = args
elif verb == "deprecated":
pi['deprecated'] = True
elif verb == "openiduser":
if request.cfg.openid_server_enable_user:
pi['openid.user'] = args
elif verb == "pragma":
try:
key, val = args.split(' ', 1)
except (ValueError, TypeError):
pass
else:
request.setPragma(key, val)
pi['acl'] = security.AccessControlList(request.cfg, acl)
return pi
def send_raw(self, content_disposition=None, mimetype=None):
""" Output the raw page data (action=raw).
With no content_disposition, the browser usually just displays the
data on the screen, with content_disposition='attachment', it will
offer a dialogue to save it to disk (used by Save action).
Supplied mimetype overrides default text/plain.
"""
request = self.request
request.mimetype = mimetype or 'text/plain'
if self.exists():
# use the correct last-modified value from the on-disk file
# to ensure cacheability where supported. Because we are sending
# RAW (file) content, the file mtime is correct as Last-Modified header.
request.status_code = 200
request.last_modified = os.path.getmtime(self._text_filename())
text = self.encodeTextMimeType(self.body)
#request.headers['Content-Length'] = len(text) # XXX WRONG! text is unicode obj, but we send utf-8!
if content_disposition:
# TODO: fix the encoding here, plain 8 bit is not allowed according to the RFCs
# There is no solution that is compatible to IE except stripping non-ascii chars
filename_enc = "%s.txt" % self.page_name.encode(config.charset)
dispo_string = '%s; filename="%s"' % (content_disposition, filename_enc)
request.headers['Content-Disposition'] = dispo_string
else:
request.status_code = 404
text = u"Page %s not found." % self.page_name
request.write(text)
def send_page(self, **keywords):
""" Output the formatted page.
TODO: "kill send_page(), quick" (since 2002 :)
@keyword content_only: if 1, omit http headers, page header and footer
@keyword content_id: set the id of the enclosing div
@keyword count_hit: if 1, add an event to the log
@keyword send_special: if True, this is a special page send
@keyword omit_footnotes: if True, do not send footnotes (used by include macro)
"""
request = self.request
_ = request.getText
request.clock.start('send_page')
emit_headers = keywords.get('emit_headers', 1)
content_only = keywords.get('content_only', 0)
omit_footnotes = keywords.get('omit_footnotes', 0)
content_id = keywords.get('content_id', 'content')
do_cache = keywords.get('do_cache', 1)
send_special = keywords.get('send_special', False)
print_mode = keywords.get('print_mode', 0)
if print_mode:
media = request.values.get('media', 'print')
else:
media = 'screen'
self.hilite_re = (keywords.get('hilite_re') or
request.values.get('highlight'))
# count hit?
if keywords.get('count_hit', 0):
eventlog.EventLog(request).add(request, 'VIEWPAGE', {'pagename': self.page_name})
# load the text
body = self.data
pi = self.pi
if 'redirect' in pi and not (
'action' in request.values or 'redirect' in request.values or content_only):
# redirect to another page
# note that by including "action=show", we prevent endless looping
# (see code in "request") or any cascaded redirection
pagename, anchor = wikiutil.split_anchor(pi['redirect'])
redirect_url = Page(request, pagename).url(request,
querystr={'action': 'show', 'redirect': self.page_name, },
anchor=anchor)
request.http_redirect(redirect_url, code=301)
return
# if necessary, load the formatter
if self.default_formatter:
from MoinMoin.formatter.text_html import Formatter
self.formatter = Formatter(request, store_pagelinks=1)
elif not self.formatter:
Formatter = wikiutil.searchAndImportPlugin(request.cfg, "formatter", self.output_mimetype)
self.formatter = Formatter(request)
# save formatter
no_formatter = object()
old_formatter = getattr(request, "formatter", no_formatter)
request.formatter = self.formatter
self.formatter.setPage(self)
if self.hilite_re:
try:
self.formatter.set_highlight_re(self.hilite_re)
except re.error, err:
request.theme.add_msg(_('Invalid highlighting regular expression "%(regex)s": %(error)s') % {
'regex': wikiutil.escape(self.hilite_re),
'error': wikiutil.escape(str(err)),
}, "warning")
self.hilite_re = None
if 'deprecated' in pi:
# deprecated page, append last backup version to current contents
# (which should be a short reason why the page is deprecated)
request.theme.add_msg(_('The backed up content of this page is deprecated and will rank lower in search results!'), "warning")
revisions = self.getRevList()
if len(revisions) >= 2: # XXX shouldn't that be ever the case!? Looks like not.
oldpage = Page(request, self.page_name, rev=revisions[1])
body += oldpage.get_data()
del oldpage
lang = self.pi.get('language', request.cfg.language_default)
request.setContentLanguage(lang)
# start document output
page_exists = self.exists()
if not content_only:
if emit_headers:
request.content_type = "%s; charset=%s" % (self.output_mimetype, self.output_charset)
if page_exists:
if not request.user.may.read(self.page_name):
request.status_code = 403
else:
request.status_code = 200
if not request.cacheable:
# use "nocache" headers if we're using a method that is not simply "display"
request.disableHttpCaching(level=2)
elif request.user.valid:
# use nocache headers if a user is logged in (which triggers personalisation features)
request.disableHttpCaching(level=1)
else:
# TODO: we need to know if a page generates dynamic content -
# if it does, we must not use the page file mtime as last modified value
# The following code is commented because it is incorrect for dynamic pages:
#lastmod = os.path.getmtime(self._text_filename())
#request.headers['Last-Modified'] = util.timefuncs.formathttpdate(lastmod)
pass
else:
request.status_code = 404
if not page_exists and self.request.isSpiderAgent:
# don't send any 404 content to bots
return
request.write(self.formatter.startDocument(self.page_name))
# send the page header
if self.default_formatter:
if self.rev:
request.theme.add_msg("<strong>%s</strong><br>" % (
_('Revision %(rev)d as of %(date)s') % {
'rev': self.rev,
'date': wikiutil.escape(self.mtime_printable(request))
}), "info")
# This redirect message is very annoying.
# Less annoying now without the warning sign.
if 'redirect' in request.values:
redir = request.values['redirect']
request.theme.add_msg('<strong>%s</strong><br>' % (
_('Redirected from page "%(page)s"') % {'page':
wikiutil.link_tag(request, wikiutil.quoteWikinameURL(redir) + "?action=show", self.formatter.text(redir))}), "info")
if 'redirect' in pi:
request.theme.add_msg('<strong>%s</strong><br>' % (
_('This page redirects to page "%(page)s"') % {'page': wikiutil.escape(pi['redirect'])}), "info")
# Page trail
trail = None
if not print_mode:
request.user.addTrail(self)
trail = request.user.getTrail()
title = self.split_title()
html_head = ''
if request.cfg.openid_server_enabled:
openid_username = self.page_name
userid = user.getUserId(request, openid_username)
if userid is None and 'openid.user' in self.pi:
openid_username = self.pi['openid.user']
userid = user.getUserId(request, openid_username)
openid_group_name = request.cfg.openid_server_restricted_users_group
if userid is not None and (
not openid_group_name or (
openid_group_name in request.groups and
openid_username in request.groups[openid_group_name])):
html_head = '<link rel="openid2.provider" href="%s">' % \
wikiutil.escape(request.getQualifiedURL(self.url(request,
querystr={'action': 'serveopenid'})), True)
html_head += '<link rel="openid.server" href="%s">' % \
wikiutil.escape(request.getQualifiedURL(self.url(request,
querystr={'action': 'serveopenid'})), True)
html_head += '<meta http-equiv="x-xrds-location" content="%s">' % \
wikiutil.escape(request.getQualifiedURL(self.url(request,
querystr={'action': 'serveopenid', 'yadis': 'ep'})), True)
elif self.page_name == request.cfg.page_front_page:
html_head = '<meta http-equiv="x-xrds-location" content="%s">' % \
wikiutil.escape(request.getQualifiedURL(self.url(request,
querystr={'action': 'serveopenid', 'yadis': 'idp'})), True)
request.theme.send_title(title, page=self,
print_mode=print_mode,
media=media, pi_refresh=pi.get('refresh'),
allow_doubleclick=1, trail=trail,
html_head=html_head,
)
# special pages handling, including denying access
special = None
if not send_special:
if not page_exists and not body:
special = 'missing'
elif not request.user.may.read(self.page_name):
special = 'denied'
# if we have a special page, output it, unless
# - we should only output content (this is for say the pagelinks formatter)
# - we have a non-default formatter
if special and not content_only and self.default_formatter:
self._specialPageText(request, special) # this recursively calls send_page
# if we didn't short-cut to a special page, output this page
if not special:
# start wiki content div
request.write(self.formatter.startContent(content_id))
# parse the text and send the page content
self.send_page_content(request, body,
format=pi['format'],
format_args=pi['formatargs'],
do_cache=do_cache,
start_line=pi['lines'])
# check for pending footnotes
if getattr(request, 'footnotes', None) and not omit_footnotes:
from MoinMoin.macro.FootNote import emit_footnotes
request.write(emit_footnotes(request, self.formatter))
# end wiki content div
request.write(self.formatter.endContent())
# end document output
if not content_only:
# send the page footer
if self.default_formatter:
request.theme.send_footer(self.page_name, print_mode=print_mode)
request.write(self.formatter.endDocument())
request.clock.stop('send_page')
if not content_only and self.default_formatter:
request.theme.send_closing_html()
# cache the pagelinks
if do_cache and self.default_formatter and page_exists:
cache = caching.CacheEntry(request, self, 'pagelinks', scope='item', use_pickle=True)
if cache.needsUpdate(self._text_filename()):
links = self.formatter.pagelinks
cache.update(links)
# restore old formatter (hopefully we dont throw any exception that is catched again)
if old_formatter is no_formatter:
del request.formatter
else:
request.formatter = old_formatter
def getFormatterName(self):
""" Return a formatter name as used in the caching system
@rtype: string
@return: formatter name as used in caching
"""
if not hasattr(self, 'formatter') or self.formatter is None:
return ''
module = self.formatter.__module__
return module[module.rfind('.') + 1:]
def canUseCache(self, parser=None):
""" Is caching available for this request?
This make sure we can try to use the caching system for this
request, but it does not make sure that this will
succeed. Themes can use this to decide if a Refresh action
should be displayed.
@param parser: the parser used to render the page
@rtype: bool
@return: if this page can use caching
"""
if (not self.rev and
not self.hilite_re and
not self.__body_modified and
self.getFormatterName() in self.cfg.caching_formats):
# Everything is fine, now check the parser:
if parser is None:
try:
parser = wikiutil.searchAndImportPlugin(self.request.cfg, "parser", self.pi['format'])
except wikiutil.PluginMissingError:
return False
return getattr(parser, 'caching', False)
return False
def send_page_content(self, request, body, format='wiki', format_args='', do_cache=1, **kw):
""" Output the formatted wiki page, using caching if possible
@param request: the request object
@param body: text of the wiki page
@param format: format of content, default 'wiki'
@param format_args: #format arguments, used by some parsers
@param do_cache: if True, use cached content
"""
request.clock.start('send_page_content')
# Load the parser
try:
Parser = wikiutil.searchAndImportPlugin(request.cfg, "parser", format)
except wikiutil.PluginMissingError:
Parser = wikiutil.searchAndImportPlugin(request.cfg, "parser", "plain")
parser = Parser(body, request, format_args=format_args, **kw)
if not (do_cache and self.canUseCache(Parser)):
self.format(parser)
else:
try:
code = self.loadCache(request)
self.execute(request, parser, code)
except Exception, e:
if not is_cache_exception(e):
raise
try:
code = self.makeCache(request, parser)
self.execute(request, parser, code)
except Exception, e:
if not is_cache_exception(e):
raise
logging.error('page cache failed after creation')
self.format(parser)
request.clock.stop('send_page_content')
def format(self, parser):
""" Format and write page content without caching """
parser.format(self.formatter)
def execute(self, request, parser, code):
""" Write page content by executing cache code """
formatter = self.formatter
request.clock.start("Page.execute")
try:
from MoinMoin.macro import Macro
macro_obj = Macro(parser)
# Fix __file__ when running from a zip package
import MoinMoin
if hasattr(MoinMoin, '__loader__'):
__file__ = os.path.join(MoinMoin.__loader__.archive, 'dummy')
try:
exec code
except "CacheNeedsUpdate": # convert the exception
raise Exception("CacheNeedsUpdate")
finally:
request.clock.stop("Page.execute")
def loadCache(self, request):
""" Return page content cache or raises 'CacheNeedsUpdate' """
cache = caching.CacheEntry(request, self, self.getFormatterName(), scope='item')
attachmentsPath = self.getPagePath('attachments', check_create=0)
if cache.needsUpdate(self._text_filename(), attachmentsPath):
raise Exception('CacheNeedsUpdate')
import marshal
try:
return marshal.loads(cache.content())
except (EOFError, ValueError, TypeError):
# Bad marshal data, must update the cache.
# See http://docs.python.org/lib/module-marshal.html
raise Exception('CacheNeedsUpdate')
except Exception, err:
logging.info('failed to load "%s" cache: %s' %
(self.page_name, str(err)))
raise Exception('CacheNeedsUpdate')
def makeCache(self, request, parser):
""" Format content into code, update cache and return code """
import marshal
from MoinMoin.formatter.text_python import Formatter
formatter = Formatter(request, ["page"], self.formatter)
# Save request state while formatting page
saved_current_lang = request.current_lang
try:
text = request.redirectedOutput(parser.format, formatter)
finally:
request.current_lang = saved_current_lang
src = formatter.assemble_code(text)
code = compile(src.encode(config.charset),
self.page_name.encode(config.charset), 'exec')
cache = caching.CacheEntry(request, self, self.getFormatterName(), scope='item')
cache.update(marshal.dumps(code))
return code
def _specialPageText(self, request, special_type):
""" Output the default page content for new pages.
@param request: the request object
"""
_ = request.getText
if special_type == 'missing':
if request.user.valid and request.user.name == self.page_name and \
request.cfg.user_homewiki in ('Self', request.cfg.interwikiname):
page = wikiutil.getLocalizedPage(request, 'MissingHomePage')
else:
page = wikiutil.getLocalizedPage(request, 'MissingPage')
alternative_text = u"'''<<Action(action=edit, text=\"%s\")>>'''" % _('Create New Page')
elif special_type == 'denied':
page = wikiutil.getLocalizedPage(request, 'PermissionDeniedPage')
alternative_text = u"'''%s'''" % _('You are not allowed to view this page.')
else:
assert False
special_exists = page.exists()
if special_exists:
page._text_filename_force = page._text_filename()
else:
page.body = alternative_text
logging.warn('The page "%s" could not be found. Check your'
' underlay directory setting.' % page.page_name)
page.page_name = self.page_name
page.send_page(content_only=True, do_cache=not special_exists, send_special=True)
def getRevList(self):
""" Get a page revision list of this page, including the current version,
sorted by revision number in descending order (current page first).
@rtype: list of ints
@return: page revisions
"""
revisions = []
if self.page_name:
rev_dir = self.getPagePath('revisions', check_create=0)
if os.path.isdir(rev_dir):
for rev in os.listdir(rev_dir):
try:
revint = int(rev)
revisions.append(revint)
except ValueError:
pass
revisions.sort()
revisions.reverse()
return revisions
def olderrevision(self, rev=0):
""" Get revision of the next older page revision than rev.
rev == 0 means this page objects revision (that may be an old
revision already!)
"""
if rev == 0:
rev = self.rev
revisions = self.getRevList()
for r in revisions:
if r < rev:
older = r
break
return older
def getPageText(self, start=0, length=None):
""" Convenience function to get the page text, skipping the header
@rtype: unicode
@return: page text, excluding the header
"""
if length is None:
return self.data[start:]
else:
return self.data[start:start+length]
def getPageHeader(self, start=0, length=None):
""" Convenience function to get the page header
@rtype: unicode
@return: page header
"""
header = ['#%s %s' % t for t in self.meta]
header = '\n'.join(header)
if header:
if length is None:
return header[start:]
else:
return header[start:start+length]
return ''
def getPageLinks(self, request):
""" Get a list of the links on this page.
@param request: the request object
@rtype: list
@return: page names this page links to
"""
if self.exists():
cache = caching.CacheEntry(request, self, 'pagelinks', scope='item', do_locking=False, use_pickle=True)
if cache.needsUpdate(self._text_filename()):
links = self.parsePageLinks(request)
cache.update(links)
else:
try:
links = cache.content()
except caching.CacheError:
links = self.parsePageLinks(request)
cache.update(links)
else:
links = []
return links
def parsePageLinks(self, request):
""" Parse page links by formatting with a pagelinks formatter
This is a old hack to get the pagelinks by rendering the page
with send_page. We can remove this hack after factoring
send_page and send_page_content into small reuseable methods.
More efficient now by using special pagelinks formatter and
redirecting possible output into null file.
"""
pagename = self.page_name
if request.parsePageLinks_running.get(pagename, False):
#logging.debug("avoid recursion for page %r" % pagename)
return [] # avoid recursion
#logging.debug("running parsePageLinks for page %r" % pagename)
# remember we are already running this function for this page:
request.parsePageLinks_running[pagename] = True
request.clock.start('parsePageLinks')
class Null:
def write(self, data):
pass
request.redirect(Null())
request.mode_getpagelinks += 1
#logging.debug("mode_getpagelinks == %r" % request.mode_getpagelinks)
try:
try:
from MoinMoin.formatter.pagelinks import Formatter
formatter = Formatter(request, store_pagelinks=1)
page = Page(request, pagename, formatter=formatter)
page.send_page(content_only=1)
except:
logging.exception("pagelinks formatter failed, traceback follows")
finally:
request.mode_getpagelinks -= 1
#logging.debug("mode_getpagelinks == %r" % request.mode_getpagelinks)
request.redirect()
if hasattr(request, '_fmt_hd_counters'):
del request._fmt_hd_counters
request.clock.stop('parsePageLinks')
return formatter.pagelinks
def getCategories(self, request):
""" Get categories this page belongs to.
@param request: the request object
@rtype: list
@return: categories this page belongs to
"""
return wikiutil.filterCategoryPages(request, self.getPageLinks(request))
def getParentPage(self):
""" Return parent page or None
@rtype: Page
@return: parent page or None
"""
if self.page_name:
pos = self.page_name.rfind('/')
if pos > 0:
parent = Page(self.request, self.page_name[:pos])
if parent.exists():
return parent
return None
def getACL(self, request):
""" Get cached ACLs of this page.
Return cached ACL or invoke parseACL and update the cache.
@param request: the request object
@rtype: MoinMoin.security.AccessControlList
@return: ACL of this page
"""
try:
return self.__acl # for request.page, this is n-1 times used
except AttributeError:
# the caching here is still useful for pages != request.page,
# when we have multiple page objects for the same page name.
request.clock.start('getACL')
# Try the cache or parse acl and update the cache
currentRevision = self.current_rev()
cache_name = self.page_name
cache_key = 'acl'
cache_data = request.cfg.cache.meta.getItem(request, cache_name, cache_key)
if cache_data is None:
aclRevision, acl = None, None
else:
aclRevision, acl = cache_data
#logging.debug("currrev: %r, cachedaclrev: %r" % (currentRevision, aclRevision))
if aclRevision != currentRevision:
acl = self.parseACL()
if currentRevision != 99999999:
# don't use cache for non existing pages
# otherwise in the process of creating copies by filesys.copytree (PageEditor.copyPage)
# the first may test will create a cache entry with the default_acls for a non existing page
# At the time the page is created acls on that page would be ignored until the process
# is completed by adding a log entry into edit-log
cache_data = (currentRevision, acl)
request.cfg.cache.meta.putItem(request, cache_name, cache_key, cache_data)
self.__acl = acl
request.clock.stop('getACL')
return acl
def parseACL(self):
""" Return ACLs parsed from the last available revision
The effective ACL is always from the last revision, even if
you access an older revision.
"""
from MoinMoin import security
if self.exists() and self.rev == 0:
return self.pi['acl']
try:
lastRevision = self.getRevList()[0]
except IndexError:
return security.AccessControlList(self.request.cfg)
if self.rev == lastRevision:
return self.pi['acl']
return Page(self.request, self.page_name, rev=lastRevision).parseACL()
# Text format -------------------------------------------------------
def encodeTextMimeType(self, text):
""" Encode text from moin internal representation to text/* mime type
Make sure text uses CRLF line ends, keep trailing newline.
@param text: text to encode (unicode)
@rtype: unicode
@return: encoded text
"""
if text:
lines = text.splitlines()
# Keep trailing newline
if text.endswith(u'\n') and not lines[-1] == u'':
lines.append(u'')
text = u'\r\n'.join(lines)
return text
def decodeTextMimeType(self, text):
""" Decode text from text/* mime type to moin internal representation
@param text: text to decode (unicode). Text must use CRLF!
@rtype: unicode
@return: text using internal representation
"""
text = text.replace(u'\r', u'')
return text
def isConflict(self):
""" Returns true if there is a known editing conflict for that page.
@return: true if there is a known conflict.
"""
cache = caching.CacheEntry(self.request, self, 'conflict', scope='item')
return cache.exists()
def setConflict(self, state):
""" Sets the editing conflict flag.
@param state: bool, true if there is a conflict.
"""
cache = caching.CacheEntry(self.request, self, 'conflict', scope='item')
if state:
cache.update("") # touch it!
else:
cache.remove()
class RootPage(Page):
""" These functions were removed from the Page class to remove hierarchical
page storage support until after we have a storage api (and really need it).
Currently, there is only 1 instance of this class: request.rootpage
"""
def __init__(self, request):
page_name = u''
Page.__init__(self, request, page_name)
def getPageBasePath(self, use_underlay=0):
""" Get full path to a page-specific storage area. `args` can
contain additional path components that are added to the base path.
@param use_underlay: force using a specific pagedir, default 0:
1 = use underlay page dir
0 = use standard page dir
Note: we do NOT have special support for -1
here, that will just behave as 0!
@rtype: string
@return: int underlay,
str the full path to the storage area
"""
if self.cfg.data_underlay_dir is None:
use_underlay = 0
# 'auto' doesn't make sense here. maybe not even 'underlay':
if use_underlay == 1:
underlay, path = 1, self.cfg.data_underlay_dir
# no need to check 'standard' case, we just use path in that case!
else:
# this is the location of the virtual root page
underlay, path = 0, self.cfg.data_dir
return underlay, path
def getPageList(self, user=None, exists=1, filter=None, include_underlay=True, return_objects=False):
""" List user readable pages under current page
Currently only request.rootpage is used to list pages, but if we
have true sub pages, any page can list its sub pages.
The default behavior is listing all the pages readable by the
current user. If you want to get a page list for another user,
specify the user name.
If you want to get the full page list, without user filtering,
call with user="". Use this only if really needed, and do not
display pages the user can not read.
filter is usually compiled re match or search method, but can be
any method that get a unicode argument and return bool. If you
want to filter the page list, do it with this filter function,
and NOT on the output of this function. page.exists() and
user.may.read are very expensive, and should be done on the
smallest data set.
@param user: the user requesting the pages (MoinMoin.user.User)
@param filter: filter function
@param exists: filter existing pages
@param include_underlay: determines if underlay pages are returned as well
@param return_objects: lets it return a list of Page objects instead of
names
@rtype: list of unicode strings
@return: user readable wiki page names
"""
request = self.request
request.clock.start('getPageList')
# Check input
if user is None:
user = request.user
# Get pages cache or create it
cachedlist = request.cfg.cache.pagelists.getItem(request, 'all', None)
if cachedlist is None:
cachedlist = {}
for name in self._listPages():
# Unquote file system names
pagename = wikiutil.unquoteWikiname(name)
# Filter those annoying editor backups - current moin does not create
# those pages any more, but users have them already in data/pages
# until we remove them by a mig script...
if pagename.endswith(u'/MoinEditorBackup'):
continue
cachedlist[pagename] = None
request.cfg.cache.pagelists.putItem(request, 'all', None, cachedlist)
if user or exists or filter or not include_underlay or return_objects:
# Filter names
pages = []
for name in cachedlist:
# First, custom filter - exists and acl check are very
# expensive!
if filter and not filter(name):
continue
page = Page(request, name)
# Filter underlay pages
if not include_underlay and page.getPageStatus()[0]: # is an underlay page
continue
# Filter deleted pages
if exists and not page.exists():
continue
# Filter out page user may not read.
if user and not user.may.read(name):
continue
if return_objects:
pages.append(page)
else:
pages.append(name)
else:
pages = cachedlist.keys()
request.clock.stop('getPageList')
return pages
def getPageDict(self, user=None, exists=1, filter=None, include_underlay=True):
""" Return a dictionary of filtered page objects readable by user
Invoke getPageList then create a dict from the page list. See
getPageList docstring for more details.
@param user: the user requesting the pages
@param filter: filter function
@param exists: only existing pages
@rtype: dict {unicode: Page}
@return: user readable pages
"""
pages = {}
for name in self.getPageList(user=user, exists=exists, filter=filter, include_underlay=include_underlay):
pages[name] = Page(self.request, name)
return pages
def _listPages(self):
""" Return a list of file system page names
This is the lowest level disk access, don't use it unless you
really need it.
NOTE: names are returned in file system encoding, not in unicode!
@rtype: dict
@return: dict of page names using file system encoding
"""
# Get pages in standard dir
path = self.getPagePath('pages')
pages = self._listPageInPath(path)
if self.cfg.data_underlay_dir is not None:
# Merge with pages from underlay
path = self.getPagePath('pages', use_underlay=1)
underlay = self._listPageInPath(path)
pages.update(underlay)
return pages
def _listPageInPath(self, path):
""" List page names in domain, using path
This is the lowest level disk access, don't use it unless you
really need it.
NOTE: names are returned in file system encoding, not in unicode!
@param path: directory to list (string)
@rtype: dict
@return: dict of page names using file system encoding
"""
pages = {}
for name in os.listdir(path):
# Filter non-pages in quoted wiki names
# List all pages in pages directory - assume flat namespace.
# We exclude everything starting with '.' to get rid of . and ..
# directory entries. If we ever create pagedirs starting with '.'
# it will be with the intention to have them not show up in page
# list (like .name won't show up for ls command under UNIX).
# Note that a . within a wiki page name will be quoted to (2e).
if not name.startswith('.'):
pages[name] = None
if 'CVS' in pages:
del pages['CVS'] # XXX DEPRECATED: remove this directory name just in
# case someone has the pages dir under CVS control.
return pages
def getPageCount(self, exists=0):
""" Return page count
The default value does the fastest listing, and return count of
all pages, including deleted pages, ignoring acl rights.
If you want to get a more accurate number, call with
exists=1. This will be about 100 times slower though.
@param exists: filter existing pages
@rtype: int
@return: number of pages
"""
self.request.clock.start('getPageCount')
if exists:
# WARNING: SLOW
pages = self.getPageList(user='')
else:
pages = self._listPages()
count = len(pages)
self.request.clock.stop('getPageCount')
return count
|