Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: 0.1a2
Fetching contributors…

Cannot retrieve contributors at this time

file 483 lines (404 sloc) 18.995 kb
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
###############################################################################
##### #####
##### IMPORTANT, READ THIS !!! #####
##### ------------------------ #####
##### #####
##### Bellow is the external method which you enable by adding it #####
##### into your Plone site. #####
##### #####
###############################################################################



import os
import shutil
import simplejson
from datetime import datetime
from Acquisition import aq_base
from AccessControl.Permission import Permission
from Products.CMFCore.utils import getToolByName

COUNTER = 1
HOMEDIR = '/Users/rok/Projects/yaco/unex_exported_data'
CLASSNAME_TO_SKIP_LAUD = ['DTMLMethod', 'ZopePageTemplate', 'ControllerPythonScript',
    'ControllerPageTemplate', 'ControllerValidator', 'PythonScript', 'SQL', 'Connection',
    'ZetadbScript', 'ExternalMethod', 'ZetadbSqlInsert', 'ZetadbMysqlda', 'SiteRoot',
    'ZetadbApplication', 'ContentPanels', 'ContentPanelsTool', 'ZetadbZptInsert',
    'I18NLayer']
CLASSNAME_TO_SKIP = ['CatalogTool', 'MemberDataTool', 'SkinsTool', 'TypesTool',
    'UndoTool', 'URLTool', 'WorkflowTool', 'DiscussionTool', 'MembershipTool',
    'RegistrationTool', 'PropertiesTool', 'MetadataTool', 'SyndicationTool',
    'PloneTool', 'NavigationTool', 'FactoryTool', 'FormTool', 'MigrationTool',
    'CalendarTool', 'QuickInstallerTool', 'GroupsTool', 'GroupDataTool', 'MailHost',
    'CookieCrumbler', 'ContentTypeRegistry', 'GroupUserFolder', 'CachingPolicyManager',
    'InterfaceTool', 'PloneControlPanel', 'FormController', 'SiteErrorLog', 'SinTool',
    'ArchetypeTool', 'RAMCacheManager', 'PloneArticleTool', 'SyndicationInformation',
    'ActionIconsTool', 'AcceleratedHTTPCacheManager', 'ActionsTool', 'UIDCatalog',
    'ReferenceCatalog',]
ID_TO_SKIP = ['Members', ]


def export_plone20(self):

    global COUNTER
    global TMPDIR
    global ID_TO_SKIP

    COUNTER = 1
    TODAY = datetime.today()
    TMPDIR = HOMEDIR+'/content_'+self.getId()+'_'+TODAY.strftime('%Y-%m-%d-%H-%M-%S')
    
    id_to_skip = self.REQUEST.get('id_to_skip', None)
    if id_to_skip is not None:
        ID_TO_SKIP += id_to_skip.split(',')

    if os.path.isdir(TMPDIR):
        shutil.rmtree(TMPDIR)
    else:
        os.mkdir(TMPDIR)

    write(walk(self))

    # TODO: we should return something more useful
    return 'SUCCESS :: '+self.absolute_url()+'\n'



def walk(folder):
    for item_id in folder.objectIds():
        item = folder[item_id]
        if item.__class__.__name__ in CLASSNAME_TO_SKIP or \
           item.getId() in ID_TO_SKIP:
            continue
        if item.__class__.__name__ in CLASSNAME_TO_SKIP_LAUD:
            print '>> SKIPPING :: ['+item.__class__.__name__+'] '+item.absolute_url()
            continue
        yield item
        if getattr(item, 'objectIds', None) and \
           item.objectIds():
            for subitem in walk(item):
                yield subitem
        
def write(items):
    global COUNTER

    for item in items:
        if item.__class__.__name__ not in CLASSNAME_TO_WAPPER_MAP.keys():
            import pdb; pdb.set_trace()
            raise Exception, 'No wrapper defined for "'+item.__class__.__name__+ \
                                                  '" ('+item.absolute_url()+').'
        write_to_jsonfile(CLASSNAME_TO_WAPPER_MAP[item.__class__.__name__](item))
        COUNTER += 1

def write_to_jsonfile(item):
    global COUNTER

    SUB_TMPDIR = os.path.join(TMPDIR, str(COUNTER/1000)) # 1000 files per folder, so we dont reach some fs limit
    if not os.path.isdir(SUB_TMPDIR):
        os.mkdir(SUB_TMPDIR)

    # we store data fields in separate files
    datafield_counter = 1
    if '__datafields__' in item.keys():
        for datafield in item['__datafields__']:
            datafield_filepath = os.path.join(SUB_TMPDIR, str(COUNTER)+'.json-file-'+str(datafield_counter))
            f = open(datafield_filepath, 'wb')
            f.write(item[datafield])
            item[datafield] = os.path.join(str(COUNTER/1000), str(COUNTER)+'.json-file-'+str(datafield_counter))
            f.close()
            datafield_counter += 1
        _empty = item.pop(u'__datafields__')

    f = open(os.path.join(SUB_TMPDIR, str(COUNTER)+'.json'), 'wb')
    simplejson.dump(item, f, indent=4)
    f.close()

def getPermissionMapping(acperm):
    result = {}
    for entry in acperm:
        result[entry[0]] = entry[1]
    return result




class BaseWrapper(dict):
    """Wraps the dublin core metadata and pass it as tranmogrifier friendly style
"""
    
    def __init__(self, obj):
        self.obj = obj
        
        self.portal = getToolByName(obj, 'portal_url').getPortalObject()
        self.portal_utils = getToolByName(obj, 'plone_utils')
        self.charset = self.portal.portal_properties.site_properties.default_charset

        if not self.charset: # newer seen it missing ... but users can change it
            self.charset = 'utf-8'

        self['__datafields__'] = []
        self['_path'] = '/'.join(self.obj.getPhysicalPath())

        self['_type'] = self.obj.__class__.__name__
        
        self['id'] = obj.getId()
        self['title'] = obj.title.decode(self.charset, 'ignore')
        self['description'] = obj.description.decode(self.charset, 'ignore')

        # workflow history
        if hasattr(obj, 'workflow_history'):
            workflow_history = obj.workflow_history.data
            for w in workflow_history:
                for i, w2 in enumerate(workflow_history[w]):
                    workflow_history[w][i]['time'] = str(workflow_history[w][i]['time'])
                    workflow_history[w][i]['comments'] = workflow_history[w][i]['comments'].decode(self.charset, 'ignore')
            self['_workflow_history'] = workflow_history

        # default view
        _browser = '/'.join(self.portal_utils.browserDefault(aq_base(obj))[1])
        if _browser not in ['folder_listing']:
            self['_layout'] = ''
            self['_defaultpage'] = _browser
        #elif obj.getId() != 'index_html':
        # self['_layout'] = _browser
        # self['_defaultpage'] = ''

        # format
        self['_content_type'] = obj.Format()
        
        # properties
        self['_properties'] = []
        if getattr(aq_base(obj), 'propertyIds', False):
            obj_base = aq_base(obj)
            for pid in obj_base.propertyIds():
                val = obj_base.getProperty(pid)
                typ = obj_base.getPropertyType(pid)
                if typ == 'string':
                    if getattr(val, 'decode', False):
                        try:
                            val = val.decode(self.charset, 'ignore')
                        except UnicodeEncodeError:
                            val = unicode(val)
                    else:
                        val = unicode(val)
                self['_properties'].append((pid, val,
                                       obj_base.getPropertyType(pid)))

        # local roles
        self['_ac_local_roles'] = {}
        if getattr(obj, '__ac_local_roles__', False):
            for key, val in obj.__ac_local_roles__.items():
                if key is not None:
                    self['_ac_local_roles'][key] = val

        self['_userdefined_roles'] = ()
        if getattr(aq_base(obj), 'userdefined_roles', False):
            self['_userdefined_roles'] = obj.userdefined_roles()

        self['_ac_inherited_permissions'] = {}
        if getattr(aq_base(obj), 'ac_inherited_permissions', False):
            oldmap = getPermissionMapping(obj.ac_inherited_permissions(1))
            for key, values in oldmap.items():
                old_p = Permission(key, values, obj)
                self['_ac_inherited_permissions'][key] = old_p.getRoles()


        if getattr(aq_base(obj), 'getWrappedOwner', False):
            self['_owner'] = (1, obj.getWrappedOwner().getId())
        else:
            # fallback
            # not very nice but at least it works
            # trying to get/set the owner via getOwner(), changeOwnership(...)
            # did not work, at least not with plone 1.x, at 1.0.1, zope 2.6.2
            self['_owner'] = (0, obj.getOwner(info = 1).getId())

class DocumentWrapper(BaseWrapper):

    def __init__(self, obj):
        super(DocumentWrapper, self).__init__(obj)
        self['text'] = obj.text.decode(self.charset, 'ignore')

class LinkWrapper(BaseWrapper):

    def __init__(self, obj):
        super(LinkWrapper, self).__init__(obj)
        self['remote_url'] = obj.remote_url

class NewsItemWrapper(DocumentWrapper):

    def __init__(self, obj):
        super(NewsItemWrapper, self).__init__(obj)
        self['text_format'] = obj.text_format

class ListCriteriaWrapper(BaseWrapper):

    def __init__(self, obj):
        super(ListCriteriaWrapper, self).__init__(obj)
        self['field'] = obj.field
        self['value'] = obj.value
        self['operator'] = obj.operator

class StringCriteriaWrapper(BaseWrapper):

    def __init__(self, obj):
        super(StringCriteriaWrapper, self).__init__(obj)
        self['field'] = obj.field
        self['value'] = obj.value

class SortCriteriaWrapper(BaseWrapper):
    
    def __init__(self, obj):
        super(SortCriteriaWrapper, self).__init__(obj)
        self['index'] = obj.index
        self['reversed'] = obj.reversed

class DateCriteriaWrapper(BaseWrapper):

    def __init__(self, obj):
        super(DateCriteriaWrapper, self).__init__(obj)
        self['field'] = obj.field
        self['value'] = obj.value
        self['operation'] = obj.operation
        self['daterange'] = obj.daterange

class FileWrapper(BaseWrapper):

    def __init__(self, obj):
        super(FileWrapper, self).__init__(obj)
        self['__datafields__'].append('_datafield_file')
        data = str(obj.data)
        if len(data) != obj.getSize():
            raise Exception, 'Problem while extracting data for File content type at '+obj.absolute_url()
        self['_datafield_file'] = data
    
class ImageWrapper(BaseWrapper):

    def __init__(self, obj):
        super(ImageWrapper, self).__init__(obj)
        self['__datafields__'].append('_datafield_image')
        data = str(obj.data)
        if len(data) != obj.getSize():
            raise Exception, 'Problem while extracting data for Image content type at '+obj.absolute_url()
        self['_datafield_image'] = data
    
class EventWrapper(BaseWrapper):
    
    def __init__(self, obj):
        super(EventWrapper, self).__init__(obj)
        self['effective_date'] = str(obj.effective_date)
        self['expiration_date'] = str(obj.expiration_date)
        self['start_date'] = str(obj.start_date)
        self['end_date'] = str(obj.end_date)
        self['location'] = obj.location.decode(self.charset, 'ignore')
        self['contact_name'] = obj.contact_name.decode(self.charset, 'ignore')
        self['contact_email'] = obj.contact_email
        self['contact_phone'] = obj.contact_phone
        self['event_url'] = obj.event_url

class ArchetypesWrapper(BaseWrapper):

    def __init__(self, obj):
        super(ArchetypesWrapper, self).__init__(obj)

        fields = obj.schema.fields()
        for field in fields:
            type_ = field.__class__.__name__
            if type_ in ['StringField', 'BooleanField', 'LinesField', 'IntegerField', 'TextField',
                         'SimpleDataGridField', 'FloatField', 'FixedPointField']:
                try:
                    value = field.get(obj)
                except:
                    try:
                        value = field.getRaw(obj)
                    except:
                        if field.getStorage().__class__.__name__ == 'PostgreSQLStorage':
                            continue
                        else:
                            import pdb; pdb.set_trace()
                if callable(value) is True:
                    value = value()
                if value:
                    self[unicode(field.__name__)] = value
            elif type_ in ['TALESString', 'ZPTField']:
                value = field.getRaw(obj)
                if value:
                    self[unicode(field.__name__)] = value
            elif type_ in ['DateTimeField']:
                value = str(field.get(obj))
                if value:
                    self[unicode(field.__name__)] = value
            elif type_ in ['ReferenceField']:
                value = field.get(obj)
                if value:
                    if field.multiValued:
                        self[unicode(field.__name__)] = ['/'+i.absolute_url() for i in value]
                    else:
                        self[unicode(field.__name__)] = value.absolute_url()
            elif type_ in ['ImageField', 'FileField']:
                fieldname = unicode('_data_'+field.__name__)
                value = field.get(obj)
                value2 = value
                if type(value) is not str:
                    value = str(value.data)
                if value:
                    size = value2.getSize()
                    self['__datafields__'].append(fieldname)
                    self[fieldname] = {
                        'data': value,
                        'size': size,}
            elif type_ in ['ComputedField']:
                pass
            else:
                raise 'Unknown field type for ArchetypesWrapper.'

    def _guessFilename(self, data, fname='', mimetype='', default=''):
        """
Use the mimetype to guess the extension of the file/datafield if none exists.
This is not a 100% correct, but does not really matter.
In most cases it is nice that a word document has the doc extension, or that a picture has jpeg or bmp.
It is a bit more human readable. When the extension is wrong it can just be ignored by the import anyway.
"""
        if not fname:
            return fname
        obj = self.obj
        mimetool = getToolByName(obj, 'mimetypes_registry')
        imimetype = mimetool.lookupExtension(fname)
        if mimetype and (imimetype is None): # no valid extension on fname
            # find extensions for mimetype
            classification = mimetool.classify(data, mimetype=mimetype)
            extensions = getattr(classification, 'extensions', default)
            extension = extensions[0] # just take the first one ... :-s
            fname = '%s.%s' % (fname, extension)
        return fname

class ArticleWrapper(NewsItemWrapper):

    def __init__(self, obj):
        super(ArticleWrapper, self).__init__(obj)
        try:
            self['cooked_text'] = obj.cooked_text.decode(self.charset)
        except:
            self['cooked_text'] = obj.cooked_text.decode('latin-1')
    
        self['attachments_ids'] = obj.attachments_ids
        self['images_ids'] = obj.images_ids

class ZPhotoWrapper(BaseWrapper):

    def __init__(self, obj):
        super(ZPhotoWrapper, self).__init__(obj)
        self['show_exif'] = obj.show_exif
        self['exif'] = obj.exif
        self['iptc'] = obj.iptc
        self['path'] = obj.path
        self['dir'] = obj.dir
        self['filename'] = obj.filename
        #self['_thumbs'] = obj._thumbs
        self['dict_info'] = obj.dict_info
        self['format'] = obj.format
        self['tmpdir'] = obj.tmpdir
        self['backup'] = obj.backup
        
class ZPhotoSlidesWrapper(BaseWrapper):

    def __init__(self, obj):
      super(ZPhotoSlidesWrapper, self).__init__(obj)
      try:
        self['update_date'] = str(obj.update_date)
        self['show_postcard'] = obj.show_postcard
        self['show_ARpostcard'] = obj.show_ARpostcard
        self['show_rating'] = obj.show_rating
        self['size'] = obj.size
        self['max_size'] = obj.max_size
        self['sort_field'] = obj.sort_field
        self['allow_export'] = obj.allow_export
        self['show_export'] = obj.show_export
        #self['visits_log'] = obj.visits_log
        self['non_hidden_pic'] = obj.non_hidden_pic
        self['list_non_hidden_pic'] = obj.list_non_hidden_pic
        self['rows'] = obj.rows
        self['column'] = obj.column
        self['zphoto_header'] = obj.zphoto_header
        self['list_photo'] = obj.list_photo
        self['zphoto_footer'] = obj.zphoto_footer
        self['symbolic_photo'] = obj.symbolic_photo
        self['keywords'] = obj.keywords
        self['first_big'] = obj.first_big
        self['show_automatic_slide_show'] = obj.show_automatic_slide_show
        self['show_viewed'] = obj.show_viewed
        self['show_exif'] = obj.show_exif
        self['photo_space'] = obj.photo_space
        self['last_modif'] = str(obj.last_modif)
        self['show_iptc'] = obj.show_iptc
        self['formats_available'] = obj.formats_available
        self['default_photo_size'] = obj.default_photo_size
        self['formats'] = obj.formats
        self['actual_css'] = obj.actual_css
        self['thumb_width'] = obj.thumb_width
        self['thumb_height'] = obj.thumb_height
        #self['list_rating'] = obj.list_rating
        self['photo_folder'] = obj.photo_folder
        self['tmpdir'] = obj.tmpdir
        self['lib'] = obj.lib
        self['convert'] = obj.convert
        self['use_http_cache'] = obj.use_http_cache
      except Exception, e:
        import pdb; pdb.set_trace()


class LocalFSWrapper(BaseWrapper):

    def __init__(self, obj):
        super(LocalFSWrapper, self).__init__(obj)
        self['basepath'] = obj.basepath


# TODO: should be also possible to set it with through parameters
CLASSNAME_TO_WAPPER_MAP = {
    'LargePloneFolder': BaseWrapper,
    'Folder': BaseWrapper,
    'PloneSite': BaseWrapper,
    'PloneFolder': BaseWrapper,
    'Document': DocumentWrapper,
    'File': FileWrapper,
    'Image': ImageWrapper,
    'Link': LinkWrapper,
    'Event': EventWrapper,
    'NewsItem': NewsItemWrapper,
    'Favorite': LinkWrapper,
    'Topic': BaseWrapper,
    'ListCriterion': ListCriteriaWrapper,
    'SimpleStringCriterion': StringCriteriaWrapper,
    'SortCriterion': SortCriteriaWrapper,
    'FriendlyDateCriterion': DateCriteriaWrapper,

    # custom ones
    'I18NFolder': BaseWrapper,
    'PloneArticle': ArticleWrapper,
    'ZPhotoSlides': ZPhotoSlidesWrapper,
    'ZPhoto': ZPhotoWrapper,
    'PloneLocalFolderNG': ArchetypesWrapper,
    'LocalFS': LocalFSWrapper,

}
Something went wrong with that request. Please try again.