Source

moin-2.0 / MoinMoin / items / __init__.py

   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
# Copyright: 2009 MoinMoin:ThomasWaldmann
# Copyright: 2009 MoinMoin:ReimarBauer
# Copyright: 2009 MoinMoin:ChristopherDenter
# Copyright: 2008,2009 MoinMoin:BastianBlank
# Copyright: 2010 MoinMoin:ValentinJaniaut
# Copyright: 2010 MoinMoin:DiogenesAugusto
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
    MoinMoin - misc. mimetype items

    While MoinMoin.storage cares for backend storage of items,
    this module cares for more high-level, frontend items,
    e.g. showing, editing, etc. of wiki items.
"""
# TODO: split this huge module into multiple ones after code has stabilized

import os, re, time, datetime, base64
import tarfile
import zipfile
import tempfile
from StringIO import StringIO
from array import array

from flatland import Form, String, Integer, Boolean, Enum
from flatland.validation import Validator, Present, IsEmail, ValueBetween, URLValidator, Converted

from MoinMoin.security.textcha import TextCha, TextChaizedForm, TextChaValid
from MoinMoin.signalling import item_modified
from MoinMoin.util.forms import make_generator
from MoinMoin.util.mimetype import MimeType
from MoinMoin.util.mime import Type, type_moin_document
from MoinMoin.util.tree import moin_page, html, xlink, docbook
from MoinMoin.util.iri import Iri
from MoinMoin.util.crypto import cache_key

try:
    import PIL
    from PIL import Image as PILImage
    from PIL.ImageChops import difference as PILdiff
except ImportError:
    PIL = None

from MoinMoin import log
logging = log.getLogger(__name__)

try:
    import json
except ImportError:
    import simplejson as json

from flask import current_app as app
from flask import g as flaskg

from flask import request, url_for, flash, Response, redirect, abort, escape

from werkzeug import is_resource_modified
from jinja2 import Markup

from MoinMoin.i18n import _, L_, N_
from MoinMoin.themes import render_template
from MoinMoin import wikiutil, config, user
from MoinMoin.util.send_file import send_file
from MoinMoin.storage.error import NoSuchItemError, NoSuchRevisionError, AccessDeniedError, \
                                   StorageError
from MoinMoin.config import UUID, NAME, NAME_OLD, MTIME, REVERTED_TO, ACL, \
                            IS_SYSITEM, SYSITEM_VERSION,  USERGROUP, SOMEDICT, \
                            CONTENTTYPE, SIZE, LANGUAGE, ITEMLINKS, ITEMTRANSCLUSIONS, \
                            TAGS, ACTION, ADDRESS, HOSTNAME, USERID, EXTRA, COMMENT, \
                            HASH_ALGORITHM

COLS = 80
ROWS_DATA = 20
ROWS_META = 10


from ..util.registry import RegistryBase


class RegistryItem(RegistryBase):
    class Entry(object):
        def __init__(self, factory, content_type, priority):
            self.factory = factory
            self.content_type = content_type
            self.priority = priority

        def __call__(self, name, content_type, kw):
            if self.content_type.issupertype(content_type):
                return self.factory(name, content_type, **kw)

        def __eq__(self, other):
            if isinstance(other, self.__class__):
                return (self.factory == other.factory and
                        self.content_type == other.content_type and
                        self.priority == other.priority)
            return NotImplemented

        def __lt__(self, other):
            if isinstance(other, self.__class__):
                if self.priority < other.priority:
                    return True
                if self.content_type != other.content_type:
                    return other.content_type.issupertype(self.content_type)
                return False
            return NotImplemented

        def __repr__(self):
            return '<%s: %s, prio %d [%r]>' % (self.__class__.__name__,
                    self.content_type,
                    self.priority,
                    self.factory)

    def get(self, name, content_type, **kw):
        for entry in self._entries:
            item = entry(name, content_type, kw)
            if item is not None:
                return item

    def register(self, factory, content_type, priority=RegistryBase.PRIORITY_MIDDLE):
        """
        Register a factory

        :param factory: Factory to register. Callable, must return an object.
        """
        return self._register(self.Entry(factory, content_type, priority))


item_registry = RegistryItem()


def conv_serialize(doc, namespaces):
    out = array('u')
    flaskg.clock.start('conv_serialize')
    doc.write(out.fromunicode, namespaces=namespaces, method='xml')
    out = out.tounicode()
    flaskg.clock.stop('conv_serialize')
    return out


class DummyRev(dict):
    """ if we have no stored Revision, we use this dummy """
    def __init__(self, item, contenttype):
        self[CONTENTTYPE] = contenttype
        self.item = item
        self.timestamp = 0
        self.revno = None
    def read(self, size=-1):
        return ''
    def seek(self, offset, whence=0):
        pass
    def tell(self):
        return 0


class DummyItem(object):
    """ if we have no stored Item, we use this dummy """
    def __init__(self, name):
        self.name = name
    def list_revisions(self):
        return [] # same as an empty Item


class Item(object):
    """ Highlevel (not storage) Item """
    @classmethod
    def _factory(cls, name=u'', contenttype=None, **kw):
        return cls(name, contenttype=unicode(contenttype), **kw)

    @classmethod
    def create(cls, name=u'', contenttype=None, rev_no=None, item=None):
        if rev_no is None:
            rev_no = -1
        if contenttype is None:
            contenttype = 'application/x-nonexistent'

        try:
            if item is None:
                item = flaskg.storage.get_item(name)
            else:
                name = item.name
        except NoSuchItemError:
            logging.debug("No such item: %r" % name)
            item = DummyItem(name)
            rev = DummyRev(item, contenttype)
            logging.debug("Item %r, created dummy revision with contenttype %r" % (name, contenttype))
        else:
            logging.debug("Got item: %r" % name)
            try:
                rev = item.get_revision(rev_no)
                contenttype = 'application/octet-stream' # it exists
            except NoSuchRevisionError:
                try:
                    rev = item.get_revision(-1) # fall back to current revision
                    # XXX add some message about invalid revision
                except NoSuchRevisionError:
                    logging.debug("Item %r has no revisions." % name)
                    rev = DummyRev(item, contenttype)
                    logging.debug("Item %r, created dummy revision with contenttype %r" % (name, contenttype))
            logging.debug("Got item %r, revision: %r" % (name, rev_no))
        contenttype = rev.get(CONTENTTYPE) or contenttype # use contenttype in case our metadata does not provide CONTENTTYPE
        logging.debug("Item %r, got contenttype %r from revision meta" % (name, contenttype))
        logging.debug("Item %r, rev meta dict: %r" % (name, dict(rev)))

        item = item_registry.get(name, Type(contenttype), rev=rev)
        logging.debug("ItemClass %r handles %r" % (item.__class__, contenttype))
        return item

    def __init__(self, name, rev=None, contenttype=None):
        self.name = name
        self.rev = rev
        self.contenttype = contenttype

    def get_meta(self):
        return self.rev or {}
    meta = property(fget=get_meta)

    def _render_meta(self):
        # override this in child classes
        return ''

    def feed_input_conv(self):
        return self.name

    def internal_representation(self, converters=['smiley']):
        """
        Return the internal representation of a document using a DOM Tree
        """
        flaskg.clock.start('conv_in_dom')
        hash_name = HASH_ALGORITHM
        hash_hexdigest = self.rev.get(hash_name)
        if hash_hexdigest:
            cid = cache_key(usage="internal_representation",
                            hash_name=hash_name,
                            hash_hexdigest=hash_hexdigest)
            doc = app.cache.get(cid)
        else:
            # likely a non-existing item
            doc = cid = None
        if doc is None:
            # We will see if we can perform the conversion:
            # FROM_mimetype --> DOM
            # if so we perform the transformation, otherwise we don't
            from MoinMoin.converter import default_registry as reg
            input_conv = reg.get(Type(self.contenttype), type_moin_document)
            if not input_conv:
                raise TypeError("We cannot handle the conversion from %s to the DOM tree" % self.contenttype)
            smiley_conv = reg.get(type_moin_document, type_moin_document,
                    icon='smiley')

            # We can process the conversion
            links = Iri(scheme='wiki', authority='', path='/' + self.name)
            input = self.feed_input_conv()
            doc = input_conv(input)
            # XXX is the following assuming that the top element of the doc tree
            # is a moin_page.page element? if yes, this is the wrong place to do that
            # as not every doc will have that element (e.g. for images, we just get
            # moin_page.object, for a tar item, we get a moin_page.table):
            doc.set(moin_page.page_href, unicode(links))
            for conv in converters:
                if conv == 'smiley':
                    doc = smiley_conv(doc)
            if cid:
                app.cache.set(cid, doc)
        flaskg.clock.stop('conv_in_dom')
        return doc

    def _expand_document(self, doc):
        from MoinMoin.converter import default_registry as reg
        include_conv = reg.get(type_moin_document, type_moin_document, includes='expandall')
        macro_conv = reg.get(type_moin_document, type_moin_document, macros='expandall')
        link_conv = reg.get(type_moin_document, type_moin_document, links='extern',
                url_root=Iri(request.url_root))
        flaskg.clock.start('conv_include')
        doc = include_conv(doc)
        flaskg.clock.stop('conv_include')
        flaskg.clock.start('conv_macro')
        doc = macro_conv(doc)
        flaskg.clock.stop('conv_macro')
        flaskg.clock.start('conv_link')
        doc = link_conv(doc)
        flaskg.clock.stop('conv_link')
        return doc

    def _render_data(self):
        from MoinMoin.converter import default_registry as reg
        include_conv = reg.get(type_moin_document, type_moin_document, includes='expandall')
        macro_conv = reg.get(type_moin_document, type_moin_document, macros='expandall')
        # TODO: Real output format
        html_conv = reg.get(type_moin_document, Type('application/x-xhtml-moin-page'))
        doc = self.internal_representation()
        doc = self._expand_document(doc)
        flaskg.clock.start('conv_dom_html')
        doc = html_conv(doc)
        flaskg.clock.stop('conv_dom_html')
        return conv_serialize(doc, {html.namespace: ''})

    def _render_data_xml(self):
        doc = self.internal_representation()
        return conv_serialize(doc,
                              {moin_page.namespace: '',
                               xlink.namespace: 'xlink',
                               html.namespace: 'html',
                              })

    def _render_data_highlight(self):
        # override this in child classes
        return ''

    def _do_modify_show_templates(self):
        # call this if the item is still empty
        rev_nos = []
        item_templates = self.get_templates(self.contenttype)
        return render_template('modify_show_template_selection.html',
                               item_name=self.name,
                               rev=self.rev,
                               contenttype=self.contenttype,
                               templates=item_templates,
                               first_rev_no=rev_nos and rev_nos[0],
                               last_rev_no=rev_nos and rev_nos[-1],
                               meta_rendered='',
                               data_rendered='',
                              )

    def meta_filter(self, meta):
        """ kill metadata entries that we set automatically when saving """
        kill_keys = [# shall not get copied from old rev to new rev
                     SYSITEM_VERSION,
                     NAME_OLD,
                     # are automatically implanted when saving
                     NAME,
                     UUID,
                     HASH_ALGORITHM,
                     SIZE,
                     COMMENT,
                     MTIME,
                     ACTION,
                     ADDRESS, HOSTNAME, USERID,
                    ]
        for key in kill_keys:
            meta.pop(key, None)
        return meta

    def meta_text_to_dict(self, text):
        """ convert meta data from a text fragment to a dict """
        meta = json.loads(text)
        return self.meta_filter(meta)

    def meta_dict_to_text(self, meta, use_filter=True):
        """ convert meta data from a dict to a text fragment """
        meta = dict(meta)
        if use_filter:
            meta = self.meta_filter(meta)
        return json.dumps(meta, sort_keys=True, indent=2, ensure_ascii=False)

    def get_data(self):
        return '' # TODO create a better method for binary stuff
    data = property(fget=get_data)

    def _write_stream(self, content, new_rev, bufsize=8192):
        written = 0
        if hasattr(content, "read"):
            while True:
                buf = content.read(bufsize)
                if not buf:
                    break
                new_rev.write(buf)
                written += len(buf)
        elif isinstance(content, str):
            new_rev.write(content)
            written += len(content)
        else:
            raise StorageError("unsupported content object: %r" % content)
        return written

    def copy(self, name, comment=u''):
        """
        copy this item to item <name>
        """
        old_item = self.rev.item
        flaskg.storage.copy_item(old_item, name=name)
        current_rev = old_item.get_revision(-1)
        # we just create a new revision with almost same meta/data to show up on RC
        self._save(current_rev, current_rev, name=name, action=u'COPY', comment=comment)

    def _rename(self, name, comment, action):
        self.rev.item.rename(name)
        self._save(self.meta, self.data, name=name, action=action, comment=comment)

    def rename(self, name, comment=u''):
        """
        rename this item to item <name>
        """
        return self._rename(name, comment, action=u'RENAME')

    def delete(self, comment=u''):
        """
        delete this item by moving it to the trashbin
        """
        trash_prefix = u'Trash/' # XXX move to config
        now = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())
        # make trash name unique by including timestamp:
        trashname = u'%s%s (%s UTC)' % (trash_prefix, self.name, now)
        return self._rename(trashname, comment, action=u'TRASH')

    def revert(self):
        # called from revert UI/POST
        comment = request.form.get('comment')
        self._save(self.meta, self.data, action=u'REVERT', comment=comment)

    def destroy(self, comment=u'', destroy_item=False):
        # called from destroy UI/POST
        if destroy_item:
            # destroy complete item with all revisions, metadata, etc.
            self.rev.item.destroy()
        else:
            # just destroy this revision
            self.rev.destroy()

    def modify(self):
        # called from modify UI/POST
        meta = data = contenttype_guessed = None
        contenttype_qs = request.values.get('contenttype')
        data_file = request.files.get('data_file')
        if data_file and data_file.filename: # XXX is this the right way to check if there was a file uploaded?
            data = data_file.stream
            # this is likely a guess by the browser, based on the filename
            contenttype_guessed = data_file.content_type # comes from form multipart data
        if data is None:
            # no file upload, try taking stuff from textarea
            data = request.form.get('data_text')
            if data is not None:
                # there was a data_text field with (possibly empty) content
                assert isinstance(data, unicode) # we get unicode from the form
                data = self.data_form_to_internal(data)
                data = self.data_internal_to_storage(data)
                # we know it is text and utf-8 - XXX is there a way to get the charset of the form?
                contenttype_guessed = 'text/plain;charset=utf-8'
        # data might be None here, if we have a form with just the data_file field, no file was uploaded
        # and no data_text field. this can happen if just metadata of a non-text item is edited.

        meta_text = request.form.get('meta_text')
        if meta_text is not None:
            # there was a meta_text field with (possibly empty) content
            try:
                meta = self.meta_text_to_dict(meta_text)
            except ValueError:
                # XXX maybe rather validate and reject invalid json
                pass
        if meta is None:
            # no form metadata - reuse some stuff from previous metadata?
            meta = {}

        if contenttype_qs:
            # we use querystring param to FORCE content type
            meta[CONTENTTYPE] = contenttype_qs

        comment = request.form.get('comment')
        return self._save(meta, data, contenttype_guessed=contenttype_guessed, comment=comment)

    def _save(self, meta, data=None, name=None, action=u'SAVE', contenttype_guessed=None, comment=u''):
        if name is None:
            name = self.name
        backend = flaskg.storage
        try:
            storage_item = backend.get_item(name)
        except NoSuchItemError:
            storage_item = backend.create_item(name)
        try:
            currentrev = storage_item.get_revision(-1)
            rev_no = currentrev.revno
            contenttype_current = currentrev.get(CONTENTTYPE)
        except NoSuchRevisionError:
            currentrev = None
            rev_no = -1
            contenttype_current = None
        new_rev_no = rev_no + 1
        newrev = storage_item.create_revision(new_rev_no)
        for k, v in meta.iteritems():
            # TODO Put metadata into newrev here for now. There should be a safer way
            #      of input for this.
            newrev[k] = v

        # we store the previous (if different) and current item name into revision metadata
        # this is useful for rename history and backends that use item uids internally
        oldname = meta.get(NAME)
        if oldname and oldname != name:
            newrev[NAME_OLD] = oldname
        newrev[NAME] = name

        if data is None:
            if currentrev is not None:
                # we don't have (new) data, just copy the old one.
                # a valid usecase of this is to just edit metadata.
                data = currentrev
            else:
                data = ''
        size = self._write_stream(data, newrev)

        # XXX if meta is from old revision, and user did not give a non-empty
        # XXX comment, re-using the old rev's comment is wrong behaviour:
        comment = unicode(comment or meta.get(COMMENT, ''))
        if comment:
            newrev[COMMENT] = comment

        if CONTENTTYPE not in newrev:
            # make sure we have CONTENTTYPE
            newrev[CONTENTTYPE] = unicode(contenttype_current or contenttype_guessed or 'application/octet-stream')

        newrev[ACTION] = unicode(action)
        self.before_revision_commit(newrev, data)
        storage_item.commit()
        item_modified.send(app._get_current_object(), item_name=name)
        return new_rev_no, size

    def before_revision_commit(self, newrev, data):
        """
        hook that can be used to add more meta data to a revision before
        it is committed.

        :param newrev: new (still uncommitted) revision - modify as wanted
        :param data: either str or open file (we can avoid having to read/seek
                     rev's data with this)
        """
        remote_addr = request.remote_addr
        if remote_addr:
            if app.cfg.log_remote_addr:
                newrev[ADDRESS] = unicode(remote_addr)
                hostname = wikiutil.get_hostname(remote_addr)
                if hostname:
                    newrev[HOSTNAME] = hostname
        if flaskg.user.valid:
            newrev[USERID] = unicode(flaskg.user.id)

    def search_items(self, term=None):
        """ search items matching the term or,
            if term is None, return all items
        """
        if term:
            backend_items = flaskg.storage.search_items(term)
        else:
            # special case: we just want all items
            backend_items = flaskg.storage.iteritems()
        for item in backend_items:
            yield Item.create(item=item)

    list_items = search_items  # just for cosmetics

    def count_items(self, term=None):
        """
        Return item count for matching items. See search_items() for details.
        """
        count = 0
        # we intentionally use a loop to avoid creating a list with all item objects:
        for item in self.list_items(term):
            count += 1
        return count

    def get_index(self):
        """ create an index of sub items of this item """
        import re
        from MoinMoin.storage.terms import NameRE

        if self.name:
            prefix = self.name + u'/'
        else:
            # trick: an item of empty name can be considered as "virtual root item",
            # that has all wiki items as sub items
            prefix = u''
        sub_item_re = u"^%s.*" % re.escape(prefix)
        regex = re.compile(sub_item_re, re.UNICODE)

        item_iterator = self.search_items(NameRE(regex))

        # We only want the sub-item part of the item names, not the whole item objects.
        prefix_len = len(prefix)
        items = [(item.name, item.name[prefix_len:], item.meta.get(CONTENTTYPE))
                 for item in item_iterator]
        return sorted(items)

    def flat_index(self):
        index = self.get_index()
        index = [(fullname, relname, contenttype)
                 for fullname, relname, contenttype in index
                 if u'/' not in relname]
        return index

    index_template = 'index.html'


class NonExistent(Item):
    contenttype_groups = [
        ('markup text items', [
            ('text/x.moin.wiki;charset=utf-8', 'Wiki (MoinMoin)'),
            ('text/x.moin.creole;charset=utf-8', 'Wiki (Creole)'),
            ('text/x-mediawiki;charset=utf-8', 'Wiki (MediaWiki)'),
            ('text/x-rst;charset=utf-8', 'ReST'),
            ('application/docbook+xml;charset=utf-8', 'DocBook'),
            ('text/html;charset=utf-8', 'HTML'),
        ]),
        ('other text items', [
            ('text/plain;charset=utf-8', 'plain text'),
            ('text/x-diff;charset=utf-8', 'diff/patch'),
            ('text/x-python;charset=utf-8', 'python code'),
            ('text/csv;charset=utf-8', 'csv'),
            ('text/x-irclog;charset=utf-8', 'IRC log'),
        ]),
        ('image items', [
            ('image/jpeg', 'JPEG'),
            ('image/png', 'PNG'),
            ('image/svg+xml', 'SVG'),
        ]),
        ('audio items', [
            ('audio/wave', 'WAV'),
            ('audio/ogg', 'OGG'),
            ('audio/mpeg', 'MP3'),
            ('audio/webm', 'WebM'),
        ]),
        ('video items', [
            ('video/ogg', 'OGG'),
            ('video/webm', 'WebM'),
            ('video/mp4', 'MP4'),
        ]),
        ('drawing items', [
            ('application/x-twikidraw', 'TDRAW'),
            ('application/x-anywikidraw', 'ADRAW'),
            ('application/x-svgdraw', 'SVGDRAW'),
        ]),

        ('other items', [
            ('application/pdf', 'PDF'),
            ('application/zip', 'ZIP'),
            ('application/x-tar', 'TAR'),
            ('application/x-gtar', 'TGZ'),
            ('application/octet-stream', 'binary file'),
        ]),
    ]

    def do_get(self):
        abort(404)

    def _convert(self):
        abort(404)

    def do_modify(self, contenttype, template_name):
        # XXX think about and add item template support
        return render_template('modify_show_type_selection.html',
                               item_name=self.name,
                               contenttype_groups=self.contenttype_groups,
                              )

item_registry.register(NonExistent._factory, Type('application/x-nonexistent'))


class Binary(Item):
    """ An arbitrary binary item, fallback class for every item mimetype. """
    modify_help = """\
There is no help, you're doomed!
"""

    template = "modify_binary.html"

    # XXX reads item rev data into memory!
    def get_data(self):
        if self.rev is not None:
            return self.rev.read()
        else:
            return ''
    data = property(fget=get_data)

    def _render_meta(self):
        return "<pre>%s</pre>" % escape(self.meta_dict_to_text(self.meta, use_filter=False))

    def get_templates(self, contenttype=None):
        """ create a list of templates (for some specific contenttype) """
        from MoinMoin.storage.terms import AND, LastRevisionMetaDataMatch
        term = LastRevisionMetaDataMatch(TAGS, ['template']) # XXX there might be other tags
        if contenttype:
            term = AND(term, LastRevisionMetaDataMatch(CONTENTTYPE, contenttype))
        item_iterator = self.search_items(term)
        items = [item.name for item in item_iterator]
        return sorted(items)

    def do_modify(self, contenttype, template_name):
        # XXX think about and add item template support
        #if template_name is None and isinstance(self.rev, DummyRev):
        #    return self._do_modify_show_templates()
        from MoinMoin.apps.frontend.views import CommentForm
        class ModifyForm(CommentForm):
            rev = Integer.using(optional=False)
            meta_text = String.using(optional=False).with_properties(placeholder=L_("MetaData (JSON)"))

        if request.method == 'GET':
            form = ModifyForm.from_defaults()
            TextCha(form).amend_form()
            form['meta_text'] = self.meta_dict_to_text(self.meta)
            form['rev'] = self.rev.revno if self.rev.revno is not None else -1
        elif request.method == 'POST':
            form = ModifyForm.from_flat(request.form)
            TextCha(form).amend_form()
            valid = form.validate()
            if valid:
                try:
                    self.modify() # XXX
                except AccessDeniedError:
                    abort(403)
                else:
                    return redirect(url_for('frontend.show_item', item_name=self.name))
        return render_template(self.template,
                               item_name=self.name,
                               rows_meta=str(ROWS_META), cols=str(COLS),
                               help=self.modify_help,
                               form=form,
                               gen=make_generator(),
                              )

    copy_template = 'copy.html'
    delete_template = 'delete.html'
    destroy_template = 'destroy.html'
    diff_template = 'diff.html'
    rename_template = 'rename.html'
    revert_template = 'revert.html'

    def _render_data_diff(self, oldrev, newrev):
        hash_name = HASH_ALGORITHM
        if oldrev[hash_name] == newrev[hash_name]:
            return _("The items have the same data hash code (that means they very likely have the same data).")
        else:
            return _("The items have different data.")

    _render_data_diff_text = _render_data_diff
    _render_data_diff_raw = _render_data_diff

    def _convert(self):
        return _("Impossible to convert the data to the contenttype: %(contenttype)s",
                 contenttype=request.values.get('contenttype'))

    def do_get(self):
        hash = self.rev.get(HASH_ALGORITHM)
        if is_resource_modified(request.environ, hash): # use hash as etag
            return self._do_get_modified(hash)
        else:
            return Response(status=304)

    def _do_get_modified(self, hash):
        member = request.values.get('member')
        return self._do_get(hash, member)

    def _do_get(self, hash, member=None):
        filename = None
        if member: # content = file contained within a archive item revision
            path, filename = os.path.split(member)
            mt = MimeType(filename=filename)
            content_disposition = mt.content_disposition(app.cfg)
            content_type = mt.content_type()
            content_length = None
            file_to_send = self.get_member(member)
        else: # content = item revision
            rev = self.rev
            try:
                mimestr = rev[CONTENTTYPE]
            except KeyError:
                mt = MimeType(filename=rev.item.name)
            else:
                mt = MimeType(mimestr=mimestr)
            content_disposition = mt.content_disposition(app.cfg)
            content_type = mt.content_type()
            content_length = rev[SIZE]
            file_to_send = rev

        # TODO: handle content_disposition is not None
        # Important: empty filename keeps flask from trying to autodetect filename,
        # as this would not work for us, because our file's are not necessarily fs files.
        return send_file(file=file_to_send,
                         mimetype=content_type,
                         as_attachment=False, attachment_filename=filename,
                         cache_timeout=10, # wiki data can change rapidly
                         add_etags=True, etag=hash, conditional=True)

item_registry.register(Binary._factory, Type('*/*'))


class RenderableBinary(Binary):
    """ Base class for some binary stuff that renders with a object tag. """


class Application(Binary):
    """ Base class for application/* """


class TarMixin(object):
    """
    TarMixin offers additional functionality for tar-like items to list and
    access member files and to create new revisions by multiple posts.
    """
    def list_members(self):
        """
        list tar file contents (member file names)
        """
        self.rev.seek(0)
        tf = tarfile.open(fileobj=self.rev, mode='r')
        return tf.getnames()

    def get_member(self, name):
        """
        return a file-like object with the member file data

        :param name: name of the data in the container file
        """
        self.rev.seek(0)
        tf = tarfile.open(fileobj=self.rev, mode='r')
        return tf.extractfile(name)

    def put_member(self, name, content, content_length, expected_members):
        """
        puts a new member file into a temporary tar container.
        If all expected members have been put, it saves the tar container
        to a new item revision.

        :param name: name of the data in the container file
        :param content: the data to store into the tar file (str or file-like)
        :param content_length: byte-length of content (for str, None can be given)
        :param expected_members: set of expected member file names
        """
        if not name in expected_members:
            raise StorageError("tried to add unexpected member %r to container item %r" % (name, self.name))
        if isinstance(name, unicode):
            name = name.encode('utf-8')
        temp_fname = os.path.join(tempfile.gettempdir(), 'TarContainer_' +
                                  cache_key(usage='TarContainer', name=self.name))
        tf = tarfile.TarFile(temp_fname, mode='a')
        ti = tarfile.TarInfo(name)
        if isinstance(content, str):
            if content_length is None:
                content_length = len(content)
            content = StringIO(content) # we need a file obj
        elif not hasattr(content, 'read'):
            logging.error("unsupported content object: %r" % content)
            raise StorageError("unsupported content object: %r" % content)
        assert content_length >= 0  # we don't want -1 interpreted as 4G-1
        ti.size = content_length
        tf.addfile(ti, content)
        tf_members = set(tf.getnames())
        tf.close()
        if tf_members - expected_members:
            msg = "found unexpected members in container item %r" % self.name
            logging.error(msg)
            os.remove(temp_fname)
            raise StorageError(msg)
        if tf_members == expected_members:
            # everything we expected has been added to the tar file, save the container as revision
            meta = {CONTENTTYPE: self.contenttype}
            data = open(temp_fname, 'rb')
            self._save(meta, data, name=self.name, action=u'SAVE', comment='')
            data.close()
            os.remove(temp_fname)


class ApplicationXTar(TarMixin, Application):
    def feed_input_conv(self):
        return self.rev

item_registry.register(ApplicationXTar._factory, Type('application/x-tar'))
item_registry.register(ApplicationXTar._factory, Type('application/x-gtar'))


class ZipMixin(object):
    """
    ZipMixin offers additional functionality for zip-like items to list and
    access member files.
    """
    def list_members(self):
        """
        list zip file contents (member file names)
        """
        self.rev.seek(0)
        zf = zipfile.ZipFile(self.rev, mode='r')
        return zf.namelist()

    def get_member(self, name):
        """
        return a file-like object with the member file data

        :param name: name of the data in the zip file
        """
        self.rev.seek(0)
        zf = zipfile.ZipFile(self.rev, mode='r')
        return zf.open(name, mode='r')

    def put_member(self, name, content, content_length, expected_members):
        raise NotImplementedError


class ApplicationZip(ZipMixin, Application):
    def feed_input_conv(self):
        return self.rev

item_registry.register(ApplicationZip._factory, Type('application/zip'))


class PDF(Application):
    """ PDF """

item_registry.register(PDF._factory, Type('application/pdf'))


class Video(Binary):
    """ Base class for video/* """

item_registry.register(Video._factory, Type('video/*'))


class Audio(Binary):
    """ Base class for audio/* """

item_registry.register(Audio._factory, Type('audio/*'))


class Image(Binary):
    """ Base class for image/* """

item_registry.register(Image._factory, Type('image/*'))


class RenderableImage(RenderableBinary):
    """ Base class for renderable Image mimetypes """


class SvgImage(RenderableImage):
    """ SVG images use <object> tag mechanism from RenderableBinary base class """

item_registry.register(SvgImage._factory, Type('image/svg+xml'))


class RenderableBitmapImage(RenderableImage):
    """ PNG/JPEG/GIF images use <img> tag (better browser support than <object>) """
    # if mimetype is also transformable, please register in TransformableImage ONLY!


class TransformableBitmapImage(RenderableBitmapImage):
    """ We can transform (resize, rotate, mirror) some image types """
    def _transform(self, content_type, size=None, transpose_op=None):
        """ resize to new size (optional), transpose according to exif infos,
            result data should be content_type.
        """
        try:
            from PIL import Image as PILImage
        except ImportError:
            # no PIL, we can't do anything, we just output the revision data as is
            return content_type, self.rev.read()

        if content_type == 'image/jpeg':
            output_type = 'JPEG'
        elif content_type == 'image/png':
            output_type = 'PNG'
        elif content_type == 'image/gif':
            output_type = 'GIF'
        else:
            raise ValueError("content_type %r not supported" % content_type)

        # revision obj has read() seek() tell(), thus this works:
        image = PILImage.open(self.rev)
        image.load()

        try:
            # if we have EXIF data, we can transpose (e.g. rotate left),
            # so the rendered image is correctly oriented:
            transpose_op = transpose_op or 1 # or self.exif['Orientation']
        except KeyError:
            transpose_op = 1 # no change

        if size is not None:
            image = image.copy() # create copy first as thumbnail works in-place
            image.thumbnail(size, PILImage.ANTIALIAS)

        transpose_func = {
            1: lambda image: image,
            2: lambda image: image.transpose(PILImage.FLIP_LEFT_RIGHT),
            3: lambda image: image.transpose(PILImage.ROTATE_180),
            4: lambda image: image.transpose(PILImage.FLIP_TOP_BOTTOM),
            5: lambda image: image.transpose(PILImage.ROTATE_90).transpose(PILImage.FLIP_TOP_BOTTOM),
            6: lambda image: image.transpose(PILImage.ROTATE_270),
            7: lambda image: image.transpose(PILImage.ROTATE_90).transpose(PILImage.FLIP_LEFT_RIGHT),
            8: lambda image: image.transpose(PILImage.ROTATE_90),
        }
        image = transpose_func[transpose_op](image)

        outfile = StringIO()
        image.save(outfile, output_type)
        data = outfile.getvalue()
        outfile.close()
        return content_type, data

    def _do_get_modified(self, hash):
        try:
            width = int(request.values.get('w'))
        except (TypeError, ValueError):
            width = None
        try:
            height = int(request.values.get('h'))
        except (TypeError, ValueError):
            height = None
        try:
            transpose = int(request.values.get('t'))
            assert 1 <= transpose <= 8
        except (TypeError, ValueError, AssertionError):
            transpose = 1
        if width or height or transpose != 1:
            # resize requested, XXX check ACL behaviour! XXX
            hash_name = HASH_ALGORITHM
            hash_hexdigest = self.rev[hash_name]
            cid = cache_key(usage="ImageTransform",
                            hash_name=hash_name,
                            hash_hexdigest=hash_hexdigest,
                            width=width, height=height, transpose=transpose)
            c = app.cache.get(cid)
            if c is None:
                content_type = self.rev[CONTENTTYPE]
                size = (width or 99999, height or 99999)
                content_type, data = self._transform(content_type, size=size, transpose_op=transpose)
                headers = wikiutil.file_headers(content_type=content_type, content_length=len(data))
                app.cache.set(cid, (headers, data))
            else:
                # XXX TODO check ACL behaviour
                headers, data = c
            return Response(data, headers=headers)
        else:
            return self._do_get(hash)

    def _render_data_diff(self, oldrev, newrev):
        if PIL is None:
            # no PIL, we can't do anything, we just call the base class method
            return super(TransformableBitmapImage, self)._render_data_diff(oldrev, newrev)
        url = url_for('frontend.diffraw', item_name=self.name, rev1=oldrev.revno, rev2=newrev.revno)
        return Markup('<img src="%s" />' % escape(url))

    def _render_data_diff_raw(self, oldrev, newrev):
        hash_name = HASH_ALGORITHM
        cid = cache_key(usage="ImageDiff",
                        hash_name=hash_name,
                        hash_old=oldrev[hash_name],
                        hash_new=newrev[hash_name])
        c = app.cache.get(cid)
        if c is None:
            if PIL is None:
                abort(404)

            content_type = newrev[CONTENTTYPE]
            if content_type == 'image/jpeg':
                output_type = 'JPEG'
            elif content_type == 'image/png':
                output_type = 'PNG'
            elif content_type == 'image/gif':
                output_type = 'GIF'
            else:
                raise ValueError("content_type %r not supported" % content_type)

            oldimage = PILImage.open(oldrev)
            newimage = PILImage.open(newrev)
            oldimage.load()
            newimage.load()
            diffimage = PILdiff(newimage, oldimage)
            outfile = StringIO()
            diffimage.save(outfile, output_type)
            data = outfile.getvalue()
            outfile.close()
            headers = wikiutil.file_headers(content_type=content_type, content_length=len(data))
            app.cache.set(cid, (headers, data))
        else:
            # XXX TODO check ACL behaviour
            headers, data = c
        return Response(data, headers=headers)

    def _render_data_diff_text(self, oldrev, newrev):
        return super(TransformableBitmapImage, self)._render_data_diff_text(oldrev, newrev)

item_registry.register(TransformableBitmapImage._factory, Type('image/png'))
item_registry.register(TransformableBitmapImage._factory, Type('image/jpeg'))
item_registry.register(TransformableBitmapImage._factory, Type('image/gif'))


class Text(Binary):
    """ Base class for text/* """
    template = "modify_text.html"

    # text/plain mandates crlf - but in memory, we want lf only
    def data_internal_to_form(self, text):
        """ convert data from memory format to form format """
        return text.replace(u'\n', u'\r\n')

    def data_form_to_internal(self, data):
        """ convert data from form format to memory format """
        return data.replace(u'\r\n', u'\n')

    def data_internal_to_storage(self, text):
        """ convert data from memory format to storage format """
        return text.replace(u'\n', u'\r\n').encode(config.charset)

    def data_storage_to_internal(self, data):
        """ convert data from storage format to memory format """
        return data.decode(config.charset).replace(u'\r\n', u'\n')

    def feed_input_conv(self):
        return self.data_storage_to_internal(self.data).split(u'\n')

    def _render_data_diff(self, oldrev, newrev):
        from MoinMoin.util.diff_html import diff
        old_text = self.data_storage_to_internal(oldrev.read())
        new_text = self.data_storage_to_internal(newrev.read())
        storage_item = flaskg.storage.get_item(self.name)
        revs = storage_item.list_revisions()
        diffs = [(d[0], Markup(d[1]), d[2], Markup(d[3])) for d in diff(old_text, new_text)]
        return Markup(render_template('diff_text.html',
                                      item_name=self.name,
                                      oldrev=oldrev,
                                      newrev=newrev,
                                      min_revno=revs[0],
                                      max_revno=revs[-1],
                                      diffs=diffs,
                                     ))

    def _render_data_diff_text(self, oldrev, newrev):
        from MoinMoin.util import diff_text
        oldlines = self.data_storage_to_internal(oldrev.read()).split('\n')
        newlines = self.data_storage_to_internal(newrev.read()).split('\n')
        difflines = diff_text.diff(oldlines, newlines)
        return '\n'.join(difflines)

    def _render_data_highlight(self):
        from MoinMoin.converter import default_registry as reg
        data_text = self.data_storage_to_internal(self.data)
        # TODO: use registry as soon as it is in there
        from MoinMoin.converter.pygments_in import Converter as PygmentsConverter
        pygments_conv = PygmentsConverter(contenttype=self.contenttype)
        doc = pygments_conv(data_text.split(u'\n'))
        # TODO: Real output format
        html_conv = reg.get(type_moin_document, Type('application/x-xhtml-moin-page'))
        doc = html_conv(doc)
        return conv_serialize(doc, {html.namespace: ''})

    def do_modify(self, contenttype, template_name):
        # XXX think about and add item template support
        #if template_name is None and isinstance(self.rev, DummyRev):
        #    return self._do_modify_show_templates()
        from MoinMoin.apps.frontend.views import CommentForm
        class ModifyForm(CommentForm):
            rev = Integer.using(optional=False)
            meta_text = String.using(optional=False).with_properties(placeholder=L_("MetaData (JSON)"))
            data_text = String.using(optional=False).with_properties(placeholder=L_("Type your text here"))

        if request.method == 'GET':
            if template_name is None and isinstance(self.rev, DummyRev):
                return self._do_modify_show_templates()
            form = ModifyForm.from_defaults()
            TextCha(form).amend_form()
            if template_name:
                item = Item.create(template_name)
                form['data_text'] = self.data_storage_to_internal(item.data)
            else:
                form['data_text'] = self.data_storage_to_internal(self.data)
            form['meta_text'] = self.meta_dict_to_text(self.meta)
            form['rev'] = self.rev.revno if self.rev.revno is not None else -1
        elif request.method == 'POST':
            form = ModifyForm.from_flat(request.form)
            TextCha(form).amend_form()
            valid = form.validate()
            if valid:
                try:
                    self.modify() # XXX
                except AccessDeniedError:
                    abort(403)
                else:
                    return redirect(url_for('frontend.show_item', item_name=self.name))
        return render_template(self.template,
                               item_name=self.name,
                               rows_data=str(ROWS_DATA), rows_meta=str(ROWS_META), cols=str(COLS),
                               help=self.modify_help,
                               form=form,
                               gen=make_generator(),
                              )

item_registry.register(Text._factory, Type('text/*'))


class MarkupItem(Text):
    """
    some kind of item with markup
    (internal links and transcluded items)
    """
    def before_revision_commit(self, newrev, data):
        """
        add ITEMLINKS and ITEMTRANSCLUSIONS metadata
        """
        super(MarkupItem, self).before_revision_commit(newrev, data)

        if hasattr(data, "read"):
            data.seek(0)
            data = data.read()
        elif isinstance(data, str):
            pass
        else:
            raise StorageError("unsupported content object: %r" % data)

        from MoinMoin.converter import default_registry as reg

        input_conv = reg.get(Type(self.contenttype), type_moin_document)
        item_conv = reg.get(type_moin_document, type_moin_document,
                items='refs', url_root=Iri(request.url_root))

        i = Iri(scheme='wiki', authority='', path='/' + self.name)

        doc = input_conv(self.data_storage_to_internal(data).split(u'\n'))
        doc.set(moin_page.page_href, unicode(i))
        doc = item_conv(doc)

        newrev[ITEMLINKS] = item_conv.get_links()
        newrev[ITEMTRANSCLUSIONS] = item_conv.get_transclusions()


class MoinWiki(MarkupItem):
    """ MoinMoin wiki markup """

item_registry.register(MoinWiki._factory, Type('text/x.moin.wiki'))


class CreoleWiki(MarkupItem):
    """ Creole wiki markup """

item_registry.register(CreoleWiki._factory, Type('text/x.moin.creole'))


class MediaWiki(MarkupItem):
    """ MediaWiki markup """

item_registry.register(MediaWiki._factory, Type('text/x-mediawiki'))


class ReST(MarkupItem):
    """ ReStructured Text markup """

item_registry.register(ReST._factory, Type('text/x-rst'))


class HTML(Text):
    """
    HTML markup

    Note: As we use html_in converter to convert this to DOM and later some
          output converterter to produce output format (e.g. html_out for html
          output), all(?) unsafe stuff will get lost.

    Note: If raw revision data is accessed, unsafe stuff might be present!
    """
    template = "modify_text_html.html"

item_registry.register(HTML._factory, Type('text/html'))


class DocBook(MarkupItem):
    """ DocBook Document """
    def _convert(self, doc):
        from emeraldtree import ElementTree as ET
        from MoinMoin.converter import default_registry as reg

        doc = self._expand_document(doc)

        # We convert the internal representation of the document
        # into a DocBook document
        conv = reg.get(type_moin_document, Type('application/docbook+xml'))

        doc = conv(doc)

        # We determine the different namespaces of the output form
        output_namespaces = {
             docbook.namespace: '',
             xlink.namespace: 'xlink',
         }

        # We convert the result into a StringIO object
        # With the appropriate namespace
        # TODO: Some other operation should probably be done here too
        # like adding a doctype
        file_to_send = StringIO()
        tree = ET.ElementTree(doc)
        tree.write(file_to_send, namespaces=output_namespaces)

        # We determine the different parameters for the reply
        mt = MimeType(mimestr='application/docbook+xml;charset=utf-8')
        content_disposition = mt.content_disposition(app.cfg)
        content_type = mt.content_type()
        # After creation of the StringIO, we are at the end of the file
        # so position is the size the file.
        # and then we should move it back at the beginning of the file
        content_length = file_to_send.tell()
        file_to_send.seek(0)
        # Important: empty filename keeps flask from trying to autodetect filename,
        # as this would not work for us, because our file's are not necessarily fs files.
        return send_file(file=file_to_send,
                         mimetype=content_type,
                         as_attachment=False, attachment_filename=None,
                         cache_timeout=10, # wiki data can change rapidly
                         add_etags=False, etag=None, conditional=True)

item_registry.register(DocBook._factory, Type('application/docbook+xml'))


class TWikiDraw(TarMixin, Image):
    """
    drawings by TWikiDraw applet. It creates three files which are stored as tar file.
    """
    modify_help = ""
    template = "modify_twikidraw.html"

    def modify(self):
        # called from modify UI/POST
        file_upload = request.files.get('filepath')
        filename = request.form['filename']
        basepath, basename = os.path.split(filename)
        basename, ext = os.path.splitext(basename)

        filecontent = file_upload.stream
        content_length = None
        if ext == '.draw': # TWikiDraw POSTs this first
            filecontent = filecontent.read() # read file completely into memory
            filecontent = filecontent.replace("\r", "")
        elif ext == '.map':
            filecontent = filecontent.read() # read file completely into memory
            filecontent = filecontent.strip()
        elif ext == '.png':
            #content_length = file_upload.content_length
            # XXX gives -1 for wsgiref, gives 0 for werkzeug :(
            # If this is fixed, we could use the file obj, without reading it into memory completely:
            filecontent = filecontent.read()

        self.put_member('drawing' + ext, filecontent, content_length,
                        expected_members=set(['drawing.draw', 'drawing.map', 'drawing.png']))

    def do_modify(self, contenttype, template_name):
        # XXX think about and add item template support
        #if template_name is None and isinstance(self.rev, DummyRev):
        #    return self._do_modify_show_templates()
        from MoinMoin.apps.frontend.views import CommentForm
        class ModifyForm(CommentForm):
            rev = Integer.using(optional=False)
            # XXX as the "saving" POSTs come from TWikiDraw (not the form), editing meta_text doesn't work
            meta_text = String.using(optional=False).with_properties(placeholder=L_("MetaData (JSON)"))

        if request.method == 'GET':
            form = ModifyForm.from_defaults()
            TextCha(form).amend_form()
            # XXX currently this is rather pointless, as the form does not get POSTed:
            form['meta_text'] = self.meta_dict_to_text(self.meta)
            form['rev'] = self.rev.revno if self.rev.revno is not None else -1
        elif request.method == 'POST':
            # this POST comes directly from TWikiDraw (not from Browser), thus no validation
            try:
                self.modify() # XXX
            except AccessDeniedError:
                abort(403)
            else:
                # TWikiDraw POSTs more than once, redirecting would break them
                return "OK"
        return render_template(self.template,
                               item_name=self.name,
                               rows_meta=str(ROWS_META), cols=str(COLS),
                               help=self.modify_help,
                               form=form,
                               gen=make_generator(),
                              )

    def _render_data(self):
        # TODO: this could be a converter -> dom, then transcluding this kind
        # of items and also rendering them with the code in base class could work
        item_name = self.name
        drawing_url = url_for('frontend.get_item', item_name=item_name, member='drawing.draw')
        png_url = url_for('frontend.get_item', item_name=item_name, member='drawing.png')
        title = _('Edit drawing %(filename)s (opens in new window)', filename=item_name)

        mapfile = self.get_member('drawing.map')
        try:
            image_map = mapfile.read()
            mapfile.close()
        except (IOError, OSError):
            image_map = ''
        if image_map:
            # we have a image map. inline it and add a map ref to the img tag
            mapid = 'ImageMapOf' + item_name
            image_map = image_map.replace('%MAPNAME%', mapid)
            # add alt and title tags to areas
            image_map = re.sub(r'href\s*=\s*"((?!%TWIKIDRAW%).+?)"', r'href="\1" alt="\1" title="\1"', image_map)
            image_map = image_map.replace('%TWIKIDRAW%"', '%s" alt="%s" title="%s"' % (drawing_url, title, title))
            title = _('Clickable drawing: %(filename)s', filename=item_name)

            return Markup(image_map + '<img src="%s" alt="%s" usemap="#%s" />' % (png_url, title, mapid))
        else:
            return Markup('<img src="%s" alt="%s" />' % (png_url, title))

item_registry.register(TWikiDraw._factory, Type('application/x-twikidraw'))


class AnyWikiDraw(TarMixin, Image):
    """
    drawings by AnyWikiDraw applet. It creates three files which are stored as tar file.
    """
    modify_help = ""
    template = "modify_anywikidraw.html"

    def modify(self):
        # called from modify UI/POST
        file_upload = request.files.get('filepath')
        filename = request.form['filename']
        basepath, basename = os.path.split(filename)
        basename, ext = os.path.splitext(basename)
        filecontent = file_upload.stream
        content_length = None
        if ext == '.svg':
            filecontent = filecontent.read() # read file completely into memory
            filecontent = filecontent.replace("\r", "")
        elif ext == '.map':
            filecontent = filecontent.read() # read file completely into memory
            filecontent = filecontent.strip()
        elif ext == '.png':
            #content_length = file_upload.content_length
            # XXX gives -1 for wsgiref, gives 0 for werkzeug :(
            # If this is fixed, we could use the file obj, without reading it into memory completely:
            filecontent = filecontent.read()
        self.put_member('drawing' + ext, filecontent, content_length,
                        expected_members=set(['drawing.svg', 'drawing.map', 'drawing.png']))

    def do_modify(self, contenttype, template_name):
        # XXX think about and add item template support
        #if template_name is None and isinstance(self.rev, DummyRev):
        #    return self._do_modify_show_templates()
        from MoinMoin.apps.frontend.views import CommentForm
        class ModifyForm(CommentForm):
            rev = Integer.using(optional=False)
            # XXX as the "saving" POSTs come from AnyWikiDraw (not the form), editing meta_text doesn't work
            meta_text = String.using(optional=False).with_properties(placeholder=L_("MetaData (JSON)"))

        if request.method == 'GET':
            form = ModifyForm.from_defaults()
            TextCha(form).amend_form()
            # XXX currently this is rather pointless, as the form does not get POSTed:
            form['meta_text'] = self.meta_dict_to_text(self.meta)
            form['rev'] = self.rev.revno if self.rev.revno is not None else -1
        elif request.method == 'POST':
            # this POST comes directly from AnyWikiDraw (not from Browser), thus no validation
            try:
                self.modify() # XXX
            except AccessDeniedError:
                abort(403)
            else:
                # AnyWikiDraw POSTs more than once, redirecting would break them
                return "OK"
        try:
            drawing_exists = 'drawing.svg' in self.list_members()
        except:
            drawing_exists = False
        return render_template(self.template,
                               item_name=self.name,
                               rows_meta=str(ROWS_META), cols=str(COLS),
                               help=self.modify_help,
                               drawing_exists=drawing_exists,
                               form=form,
                               gen=make_generator(),
                              )

    def _render_data(self):
        # TODO: this could be a converter -> dom, then transcluding this kind
        # of items and also rendering them with the code in base class could work
        item_name = self.name
        drawing_url = url_for('frontend.get_item', item_name=item_name, member='drawing.svg')
        png_url = url_for('frontend.get_item', item_name=item_name, member='drawing.png')
        title = _('Edit drawing %(filename)s (opens in new window)', filename=self.name)

        mapfile = self.get_member('drawing.map')
        try:
            image_map = mapfile.read()
            mapfile.close()
        except (IOError, OSError):
            image_map = ''
        if image_map:
            # ToDo mapid must become uniq
            # we have a image map. inline it and add a map ref to the img tag
            # we have also to set a unique ID
            mapid = 'ImageMapOf' + self.name
            image_map = image_map.replace(u'id="drawing.svg"', '')
            image_map = image_map.replace(u'name="drawing.svg"', u'name="%s"' % mapid)
            # unxml, because 4.01 concrete will not validate />
            image_map = image_map.replace(u'/>', u'>')
            title = _('Clickable drawing: %(filename)s', filename=self.name)
            return Markup(image_map + '<img src="%s" alt="%s" usemap="#%s" />' % (png_url, title, mapid))
        else:
            return Markup('<img src="%s" alt="%s" />' % (png_url, title))

item_registry.register(AnyWikiDraw._factory, Type('application/x-anywikidraw'))


class SvgDraw(TarMixin, Image):
    """ drawings by svg-edit. It creates two files (svg, png) which are stored as tar file. """
    modify_help = ""
    template = "modify_svg-edit.html"

    def modify(self):
        # called from modify UI/POST
        file_upload = request.values.get('data')
        filename = request.form['filename']
        filecontent = file_upload.decode('base_64')
        basepath, basename = os.path.split(filename)
        basename, ext = os.path.splitext(basename)
        content_length = None

        if ext == '.png':
            filecontent = base64.urlsafe_b64decode(filecontent.split(',')[1])
        self.put_member(filename, filecontent, content_length,
                        expected_members=set(['drawing.svg', 'drawing.png']))

    def do_modify(self, contenttype, template_name):
        # XXX think about and add item template support
        #if template_name is None and isinstance(self.rev, DummyRev):
        #    return self._do_modify_show_templates()
        from MoinMoin.apps.frontend.views import CommentForm
        class ModifyForm(CommentForm):
            rev = Integer.using(optional=False)
            # XXX as the "saving" POSTs come from SvgDraw (not the form), editing meta_text doesn't work
            meta_text = String.using(optional=False).with_properties(placeholder=L_("MetaData (JSON)"))

        if request.method == 'GET':
            form = ModifyForm.from_defaults()
            TextCha(form).amend_form()
            # XXX currently this is rather pointless, as the form does not get POSTed:
            form['meta_text'] = self.meta_dict_to_text(self.meta)
            form['rev'] = self.rev.revno if self.rev.revno is not None else -1
        elif request.method == 'POST':
            # this POST comes directly from SvgDraw (not from Browser), thus no validation
            try:
                self.modify() # XXX
            except AccessDeniedError:
                abort(403)
            else:
                # SvgDraw POSTs more than once, redirecting would break them
                return "OK"
        return render_template(self.template,
                               item_name=self.name,
                               rows_meta=str(ROWS_META), cols=str(COLS),
                               help=self.modify_help,
                               form=form,
                               gen=make_generator(),
                              )

    def _render_data(self):
        # TODO: this could be a converter -> dom, then transcluding this kind
        # of items and also rendering them with the code in base class could work
        item_name = self.name
        drawing_url = url_for('frontend.get_item', item_name=item_name, member='drawing.svg')
        png_url = url_for('frontend.get_item', item_name=item_name, member='drawing.png')
        return Markup('<img src="%s" alt="%s" />' % (png_url, drawing_url))

item_registry.register(SvgDraw._factory, Type('application/x-svgdraw'))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.