ContributionTool.py 26.9 KB
Newer Older
1
# -*- coding: utf-8 -*-
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
##############################################################################
#
# Copyright (c) 2007 Nexedi SARL and Contributors. All Rights Reserved.
#                    Jean-Paul Smets <jp@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

30
import cStringIO
31
import re
32
import socket
Jean-Paul Smets's avatar
Jean-Paul Smets committed
33
import urllib2, urllib
34 35 36
import urlparse
from cgi import parse_header
import os
37

Bartek Górny's avatar
Bartek Górny committed
38
from AccessControl import ClassSecurityInfo, getSecurityManager
39
from Products.ERP5Type.Globals import InitializeClass, DTMLFile
40
from Products.CMFCore.utils import _checkPermission
41 42
from Products.ERP5Type.Tool.BaseTool import BaseTool
from Products.ERP5Type import Permissions
43
from Products.ERP5Type.Utils import reencodeUrlEscapes
44
from Products.ERP5 import _dtmldir
Nicolas Delaby's avatar
Nicolas Delaby committed
45
from Products.ERP5.Document.Url import no_crawl_protocol_list
Ivan Tyagov's avatar
Ivan Tyagov committed
46
from AccessControl import Unauthorized
Jean-Paul Smets's avatar
Jean-Paul Smets committed
47

48
from DateTime import DateTime
Nicolas Delaby's avatar
Nicolas Delaby committed
49
import warnings
50

51 52 53 54 55 56
# Install openers
import ContributionOpener
opener = urllib2.build_opener(ContributionOpener.DirectoryFileHandler)
urllib2.install_opener(opener)

# Global parameters
57
TEMP_NEW_OBJECT_KEY = '_v_new_object'
58
MAX_REPEAT = 10
59 60

_marker = []  # Create a new marker object.
61 62 63 64

class ContributionTool(BaseTool):
  """
    ContributionTool provides an abstraction layer to unify the contribution
65
    of documents into an ERP5 Site.
66

67 68
    ContributionTool needs to be configured in portal_types (allowed contents) so
    that it can store Text, Spreadsheet, PDF, etc. 
69

70 71 72
    The main method of ContributionTool is newContent. This method can
    be provided various parameters from which the portal type and document
    metadata can be derived. 
73 74

    Configuration Scripts:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
75

Nicolas Delaby's avatar
Nicolas Delaby committed
76
      - ContributionTool_getPropertyDictFromFilename: receives file name and a 
77 78
        dict derived from filename by regular expression, and does any necesary
        operations (e.g. mapping document type id onto a real portal_type).
Jean-Paul Smets's avatar
Jean-Paul Smets committed
79 80 81 82 83 84

    Problems which are not solved

      - handling of relative links in HTML contents (or others...)
        some text rewriting is necessary.

85 86 87 88 89 90
  """
  title = 'Contribution Tool'
  id = 'portal_contributions'
  meta_type = 'ERP5 Contribution Tool'
  portal_type = 'Contribution Tool'

Nicolas Delaby's avatar
Nicolas Delaby committed
91
  
Jean-Paul Smets's avatar
Jean-Paul Smets committed
92

93 94 95 96 97 98 99
  # Declarative Security
  security = ClassSecurityInfo()

  security.declareProtected(Permissions.ManagePortal, 'manage_overview' )
  manage_overview = DTMLFile( 'explainContributionTool', _dtmldir )

  security.declareProtected(Permissions.AddPortalContent, 'newContent')
Nicolas Delaby's avatar
Nicolas Delaby committed
100
  def newContent(self, **kw):
101 102 103 104 105 106
    """
      The newContent method is overriden to implement smart content
      creation by detecting the portal type based on whatever information
      was provided and finding out the most appropriate module to store
      the content.

Nicolas Delaby's avatar
Nicolas Delaby committed
107
      explicit named parameters was:
108
        id - id of document
Nicolas Delaby's avatar
Nicolas Delaby committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        portal_type - explicit portal_type parameter, must be honoured
        url - Identifier of external resource. Content will be downloaded
              from it
        container - if specified, it is possible to define
                    where to contribute the content. Else, ContributionTool
                    tries to guess.
        container_path - if specified, defines the container path
                         and has precedence over container
        discover_metadata - Enable metadata extraction and discovery
                            (default True)
        temp_object - build tempObject or not (default False)
        user_login - is the name under which the content will be created
                     XXX - this is a security hole which needs to be fixed by
                     making sure only Manager can use this parameter
        data - Binary representation of content
        filename - explicit filename of content
125
    """
Nicolas Delaby's avatar
Nicolas Delaby committed
126 127 128 129 130 131 132 133 134 135
    # Useful for metadata discovery, keep it as it as been provided
    input_parameter_dict = kw.copy()
    # But file and data are exceptions.
    # They are potentialy too big to be keept into memory.
    # We want to keep only one reference of thoses values
    # on futur created document only !
    if 'file' in input_parameter_dict:
      del input_parameter_dict['file']
    if 'data' in input_parameter_dict:
      del input_parameter_dict['data']
136 137 138 139 140
    if 'container' in input_parameter_dict:
      # Container is a persistent object
      # keep only its path in container_path key
      container = input_parameter_dict.pop('container')
      input_parameter_dict['container_path'] = container.getPath()
Nicolas Delaby's avatar
Nicolas Delaby committed
141 142 143 144 145 146
    # pop: remove keys which are not document properties
    url = kw.pop('url', None)
    container = kw.pop('container', None)
    container_path = kw.pop('container_path', None)
    discover_metadata = kw.pop('discover_metadata', True)
    user_login = kw.pop('user_login', None)
147
    document_id = kw.pop('id', None)
Nicolas Delaby's avatar
Nicolas Delaby committed
148 149 150 151 152 153 154
    # check file_name argument for backward compatibility.
    if 'file_name' in kw:
      if 'filename' not in kw:
        kw['filename'] = kw['file_name']
      del(kw['file_name'])
    filename = kw.get('filename', None)
    temp_object = kw.get('temp_object', False)
155

156
    document = None
Nicolas Delaby's avatar
Nicolas Delaby committed
157
    portal = self.getPortalObject()
158 159 160 161
    if container is None and container_path:
      # Get persistent object from its path.
      # Container may disappear, be smoother by passing default value
      container = portal.restrictedTraverse(container_path, None)
Nicolas Delaby's avatar
Nicolas Delaby committed
162
    # Try to find the filename
163
    if not url:
164
      # check if file was provided
Nicolas Delaby's avatar
Nicolas Delaby committed
165 166 167
      file_object = kw.get('file')
      if file_object is not None:
        if not filename:
168
          filename = getattr(file_object, 'filename', None)
169 170 171 172
      else:
        # some channels supply data and file-name separately
        # this is the case for example for email ingestion
        # in this case, we build a file wrapper for it
173 174 175 176 177
        try:
          data = kw.pop('data')
        except KeyError:
          raise ValueError('data must be provided')
        if data is not None:
Nicolas Delaby's avatar
Nicolas Delaby committed
178 179 180 181
          file_object = cStringIO.StringIO()
          file_object.write(data)
          file_object.seek(0)
          kw['file'] = file_object
182
      content_type = kw.pop('content_type', None)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
183
    else:
Nicolas Delaby's avatar
Nicolas Delaby committed
184
      file_object, filename, content_type = self._openURL(url)
185
      content_type = kw.pop('content_type', None) or content_type
Nicolas Delaby's avatar
Nicolas Delaby committed
186
      kw['file'] = file_object
187

188 189 190
    if not filename:
      raise ValueError('filename must be provided')

191 192 193 194
    if not content_type:
      # fallback to a default content_type according provided
      # filename
      content_type = self.guessMimeTypeFromFilename(filename)
195 196 197 198 199 200 201 202 203 204 205
    if content_type:
      kw['content_type'] = content_type

    portal_type = kw.pop('portal_type', None)
    if not portal_type:
      if container is None or container.isModuleType():
        # Guess it with help of portal_contribution_registry
        portal_type = portal.portal_contribution_registry.findPortalTypeName(
          filename=filename, content_type=content_type)
      else:
        portal_type = 'Embedded File'
206

207
    if container is not None:
208 209 210
      # Simplify things here and return a document immediately
      # XXX Nicolas: This will break support of WebDAV
      # if _setObject is not called
211
      document = container.newContent(document_id, portal_type, **kw)
212 213 214 215 216 217 218 219
      if discover_metadata:
        document.activate(after_path_and_method_id=(document.getPath(),
            ('convertToBaseFormat', 'Document_tryToConvertToBaseFormat')))\
              .discoverMetadata(filename=filename, 
                                user_login=user_login,
                                input_parameter_dict=input_parameter_dict)
      return document

220
    # If the portal_type was provided, we can go faster
221
    if portal_type:
222 223
      # We know the portal_type, let us find the default module
      # and use it as container
224
      try:
Nicolas Delaby's avatar
Nicolas Delaby committed
225
        container = portal.getDefaultModule(portal_type)
226
      except ValueError:
227
        pass
Nicolas Delaby's avatar
Nicolas Delaby committed
228

229 230
    #
    # Check if same file is already exists. if it exists, then update it.
231
    #
Nicolas Delaby's avatar
Nicolas Delaby committed
232 233 234 235 236
    property_dict = self.getMatchedFilenamePatternDict(filename)
    reference = property_dict.get('reference', None)
    version  = property_dict.get('version', None)
    language  = property_dict.get('language', None)
    if portal_type and reference and version and language:
237
      portal_catalog = portal.portal_catalog
Nicolas Delaby's avatar
Nicolas Delaby committed
238
      document = portal_catalog.getResultValue(portal_type=portal_type,
Nicolas Delaby's avatar
Nicolas Delaby committed
239 240 241
                                               reference=reference,
                                               version=version,
                                               language=language)
242

Nicolas Delaby's avatar
Nicolas Delaby committed
243 244 245 246 247 248
      if document is not None:
        # document is already uploaded. So overrides file.
        if not _checkPermission(Permissions.ModifyPortalContent, document):
          raise Unauthorized, "[DMS] You are not allowed to update the existing document which has the same coordinates (id %s)" % document.getId()
        document.edit(file=kw['file'])
        return document
249 250 251
    # Temp objects use the standard newContent from Folder
    if temp_object:
      # For temp_object creation, use the standard method
252
      return BaseTool.newContent(self, portal_type=portal_type, **kw)
253

254
    # Then put the file inside ourselves for a short while
255
    document = self._setObject(document_id, None, portal_type=portal_type,
Nicolas Delaby's avatar
Nicolas Delaby committed
256
                               user_login=user_login, container=container,
257
                               discover_metadata=discover_metadata,
Nicolas Delaby's avatar
Nicolas Delaby committed
258 259
                               filename=filename,
                               input_parameter_dict=input_parameter_dict
260
                               )
261
    object_id = document.getId()
262
    document = self._getOb(object_id) # Call _getOb to purge cache
263

Nicolas Delaby's avatar
Nicolas Delaby committed
264
    kw['filename'] = filename # Override filename property
265
    # Then edit the document contents (so that upload can happen)
266
    document._edit(**kw)
267 268
    if url:
      document.fromURL(url)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
269

270
    # Allow reindexing, reindex it and return the document
Romain Courteaud's avatar
Romain Courteaud committed
271
    try:
272
      del document.isIndexable
Romain Courteaud's avatar
Romain Courteaud committed
273 274 275
    except AttributeError:
      # Document does not have such attribute
      pass
276
    document.reindexObject()
277 278
    return document

279
  security.declareProtected( Permissions.AddPortalContent, 'newXML' )
280 281 282 283 284 285 286
  def newXML(self, xml):
    """
      Create a new content based on XML data. This is intended for contributing
      to ERP5 from another application.
    """
    pass

Nicolas Delaby's avatar
Nicolas Delaby committed
287 288 289
  security.declareProtected(Permissions.ModifyPortalContent,
                            'getMatchedFilenamePatternDict')
  def getMatchedFilenamePatternDict(self, filename):
290
    """
291
      Get matched group dict of file name parsing regular expression.
292
    """
293
    property_dict = {}
294

Nicolas Delaby's avatar
Nicolas Delaby committed
295
    if filename is None:
296 297
      return property_dict

Nicolas Delaby's avatar
Nicolas Delaby committed
298 299
    regex_text = self.portal_preferences.\
                                getPreferredDocumentFilenameRegularExpression()
300
    if regex_text in ('', None):
301 302
      return property_dict

303 304 305
    if regex_text:
      pattern = re.compile(regex_text)
      if pattern is not None:
306
        try:
Nicolas Delaby's avatar
Nicolas Delaby committed
307
          property_dict = pattern.match(filename).groupdict()
308 309
        except AttributeError: # no match
          pass
310 311
    return property_dict

Nicolas Delaby's avatar
Nicolas Delaby committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325
  # backward compatibility
  security.declareProtected(Permissions.ModifyPortalContent,
                            'getMatchedFileNamePatternDict')
  def getMatchedFileNamePatternDict(self, filename):
    """
    (deprecated) use getMatchedFilenamePatternDict() instead.
    """
    warnings.warn('getMatchedFileNamePatternDict() is deprecated. '
                  'use getMatchedFilenamePatternDict() instead.')
    return self.getMatchedFilenamePatternDict(filename)

  security.declareProtected(Permissions.ModifyPortalContent,
                            'getPropertyDictFromFilename')
  def getPropertyDictFromFilename(self, filename):
326 327 328 329
    """
      Gets properties from filename. File name is parsed with a regular expression
      set in preferences. The regexp should contain named groups.
    """
Nicolas Delaby's avatar
Nicolas Delaby committed
330
    if filename is None:
331
      return {}
Nicolas Delaby's avatar
Nicolas Delaby committed
332
    property_dict = self.getMatchedFilenamePatternDict(filename)
333 334
    try:
      method = self._getTypeBasedMethod('getPropertyDictFromFilename',
Nicolas Delaby's avatar
Nicolas Delaby committed
335
             fallback_script_id='ContributionTool_getPropertyDictFromFilename')
336 337 338
    except AttributeError: # Try to use previous naming convention
      method = self._getTypeBasedMethod('getPropertyDictFromFileName',
             fallback_script_id='ContributionTool_getPropertyDictFromFileName')
Nicolas Delaby's avatar
Nicolas Delaby committed
339
    property_dict = method(filename, property_dict)
340 341
    return property_dict

Nicolas Delaby's avatar
Nicolas Delaby committed
342 343 344 345 346 347 348 349 350 351 352
  # backward compatibility
  security.declareProtected(Permissions.ModifyPortalContent,
                            'getPropertyDictFromFileName')
  def getPropertyDictFromFileName(self, filename):
    """
    (deprecated) use getPropertyDictFromFilename() instead.
    """
    warnings.warn('getPropertyDictFromFileName() is deprecated. '
                  'use getPropertyDictFromFilename() instead.')
    return self.getPropertyDictFromFilename(filename)

353
  # WebDAV virtual folder support
Nicolas Delaby's avatar
Nicolas Delaby committed
354 355 356
  def _setObject(self, id, ob, portal_type=None, user_login=None,
                 container=None, discover_metadata=True, filename=None,
                 input_parameter_dict=None):
357
    """
358
      portal_contribution_registry will find appropriate portal type
Nicolas Delaby's avatar
Nicolas Delaby committed
359
      name by filename and content itself.
360 361 362 363 364

      The ContributionTool instance must be configured in such
      way that _verifyObjectPaste will return TRUE.

    """
365 366 367 368 369
    # _setObject is called by constructInstance at a time
    # when the object has no portal_type defined yet. It
    # will be removed later on. We can safely store the
    # document inside us at this stage. Else we
    # must find out where to store it.
370
    if ob is not None:
371 372 373 374 375
      # Called from webdav API
      # Object is already created by PUT_factory
      # fill the volatile cache _v_document_cache
      # then return the document
      document = ob
376
    else:
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
      if not portal_type:
        document = BaseTool.newContent(self, id=id,
                                      portal_type=portal_type,
                                      is_indexable=0)
      elif ob is None:
        # We give the system a last chance to analyse the
        # portal_type based on the document content
        # (ex. a Memo is a kind of Text which can be identified
        # by the fact it includes some specific content)

        # Now we know the portal_type, let us find the module
        # to which we should move the document to
        if container is None:
          module = self.getDefaultModule(portal_type)
        else:
          module = container
        # There is no preexisting document - we can therefore
        # set the new object
        new_content_kw = {'portal_type': portal_type,
                          'is_indexable': False}
        if id is not None:
          new_content_kw['id'] = id
        document = module.newContent(**new_content_kw)
        # We can now discover metadata
        if discover_metadata:
          # Metadata disovery is done as an activity by default
          # If we need to discoverMetadata synchronously, it must
          # be for user interface and should thus be handled by
          # ZODB scripts
          document.activate(after_path_and_method_id=(document.getPath(),
407 408
            ('convertToBaseFormat', 'Document_tryToConvertToBaseFormat',
             'immediateReindexObject', 'recursiveImmediateReindexObject')))\
409 410 411 412 413 414 415 416 417 418
          .discoverMetadata(filename=filename,
                            user_login=user_login,
                            input_parameter_dict=input_parameter_dict)
    # Keep the document close to us - this is only useful for
    # file upload from webdav
    volatile_cache = getattr(self, '_v_document_cache', None)
    if volatile_cache is None:
      self._v_document_cache = {}
      volatile_cache = self._v_document_cache
    volatile_cache[document.getId()] = document.getRelativeUrl()
419 420
    # Return document to newContent method
    return document
421

422 423 424 425 426
  def _getOb(self, id, default=_marker):
    """
    Check for volatile temp object info first
    and try to find it
    """
427 428
    # Use the document cache if possible and return result immediately
    # this is only useful for webdav
Nicolas Delaby's avatar
Nicolas Delaby committed
429 430 431
    volatile_cache = getattr(self, '_v_document_cache', None)
    if volatile_cache is not None:
      document_url = volatile_cache.get(id)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
432
      if document_url is not None:
Nicolas Delaby's avatar
Nicolas Delaby committed
433
        del volatile_cache[id]
Jean-Paul Smets's avatar
Jean-Paul Smets committed
434 435
        return self.getPortalObject().unrestrictedTraverse(document_url)

436 437 438 439 440 441 442 443 444 445 446 447
    # Try first to return the real object inside
    # This is much safer than trying to access objects displayed by listDAVObjects
    # because the behaviour of catalog is unpredicatble if a string is passed
    # for a UID. For example 
    #   select path from catalog where uid = "001193.html";
    # will return the same as
    #   select path from catalog where uid = 1193;
    # This was the source of an error in which the contribution tool
    # was creating a web page and was returning a Base Category
    # when
    #   o = folder._getOb(id)
    # was called in DocumentConstructor
448 449 450 451 452 453 454
    if default is _marker:
      result = BaseTool._getOb(self, id)
    else:
      result = BaseTool._getOb(self, id, default=default)
    if result is not None:
      # if result is None, ignore it at this stage
      # we can be more lucky with portal_catalog
455 456 457
      return result

    # Return an object listed by listDAVObjects
458 459 460
    # ids are concatenation of uid + '-' + standard file name of documents
    # get the uid
    uid = str(id).split('-', 1)[0]
461 462
    object = self.getPortalObject().portal_catalog.unrestrictedGetResultValue(uid=uid)
    if object is not None:
463
      return object.getObject() # Make sure this does not break security. XXX
464 465
    if default is not _marker:
      return default
466 467 468
    # Raise an AttributeError the same way as in OFS.ObjectManager._getOb
    raise AttributeError, id

469

Bartek Górny's avatar
Bartek Górny committed
470
  def listDAVObjects(self):
471 472 473
    """
      Get all contents contributed by the current user. This is
      delegated to a script in order to help customisation.
474
    XXX Killer feature, it is not scalable
475 476 477 478 479 480 481 482 483 484 485 486 487
    """
    method = getattr(self, 'ContributionTool_getMyContentList', None)
    if method is not None:
      object_list = method()
    else:
      sm = getSecurityManager()
      user = sm.getUser()
      object_list = self.portal_catalog(portal_type=self.getPortalMyDocumentTypeList(),
                                        owner=str(user))

    def wrapper(o_list):
      for o in o_list:
        o = o.getObject()
Nicolas Delaby's avatar
Nicolas Delaby committed
488
        id = '%s-%s' % (o.getUid(), o.getStandardFilename(),)
489
        yield o.asContext(id=id)
490 491

    return wrapper(object_list)
Bartek Górny's avatar
Bartek Górny committed
492

Jean-Paul Smets's avatar
Jean-Paul Smets committed
493
  security.declareProtected(Permissions.AddPortalContent, 'crawlContent')
494
  def crawlContent(self, content, container=None):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
495 496 497 498 499 500
    """
      Analyses content and download linked pages

      XXX: missing is the conversion of content local href to something
      valid.
    """
Nicolas Delaby's avatar
Nicolas Delaby committed
501 502
    portal = self.getPortalObject()
    url_registry_tool = portal.portal_url_registry
Jean-Paul Smets's avatar
Jean-Paul Smets committed
503
    depth = content.getCrawlingDepth()
504 505 506 507 508 509 510 511
    if depth < 0:
      # Do nothing if crawling depth is reached
      # (this is not a duplicate code but a way to prevent
      # calling isIndexContent unnecessarily)
      return
    if not content.isIndexContent(): # Decrement depth only if it is a content document
      depth = depth - 1
    if depth < 0:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
512 513
      # Do nothing if crawling depth is reached
      return
Nicolas Delaby's avatar
Nicolas Delaby committed
514
    url_list = content.getContentNormalisedURLList()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
515
    for url in set(url_list):
516
      # LOG('trying to crawl', 0, url)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
517
      # Some url protocols should not be crawled
Nicolas Delaby's avatar
Nicolas Delaby committed
518
      if urlparse.urlsplit(url)[0] in no_crawl_protocol_list:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
519
        continue
520 521 522 523
      if container is None:
        #if content.getParentValue()
        # in place of not ?
        container = content.getParentValue()
Nicolas Delaby's avatar
Nicolas Delaby committed
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
      try:
        url_registry_tool.getReferenceFromURL(url, context=container)
      except KeyError:
        pass
      else:
        # url already crawled
        continue
      # XXX - This call is not working due to missing group_method_id
      # therefore, multiple call happen in parallel and eventually fail
      # (the same URL is created multiple times)
      # LOG('activate newContentFromURL', 0, url)
      self.activate(activity="SQLQueue").newContentFromURL(
                                  container_path=container.getRelativeUrl(),
                                  url=url, crawling_depth=depth)
      # Url is not known yet but register right now to avoid
      # creation of duplicated crawled content
      # An activity will later setup the good reference for it.
      url_registry_tool.registerURL(url, None, context=container)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
542 543

  security.declareProtected(Permissions.AddPortalContent, 'updateContentFromURL')
544 545
  def updateContentFromURL(self, content, repeat=MAX_REPEAT, crawling_depth=0,
                           repeat_interval=1, batch_mode=True):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
546 547 548
    """
      Updates an existing content.
    """
549 550 551 552 553 554 555 556 557
    # First, test if the document is updatable according to
    # its workflow states (if it has a workflow associated with)
    if content.isUpdatable():
      # Step 0: update crawling_depth if required
      if crawling_depth > content.getCrawlingDepth():
        content._setCrawlingDepth(crawling_depth)
      # Step 1: download new content
      try:
        url = content.asURL()
Nicolas Delaby's avatar
Nicolas Delaby committed
558
        file_object, filename, content_type = self._openURL(url)
559
      except urllib2.URLError, error:
560
        if repeat == 0 or not batch_mode:
561
          # XXX - Call the extendBadURLList method,--NOT Implemented--
Jérome Perrin's avatar
Jérome Perrin committed
562
          raise
563
        content.activate(at_date=DateTime() + repeat_interval).updateContentFromURL(repeat=repeat - 1)
564 565
        return

Nicolas Delaby's avatar
Nicolas Delaby committed
566 567
      content._edit(file=file_object, content_type=content_type)
                              # Please make sure that if content is the same
568 569 570
                              # we do not update it
                              # This feature must be implemented by Base or File
                              # not here (look at _edit in Base)
Nicolas Delaby's avatar
Nicolas Delaby committed
571 572 573 574 575 576 577
      # Step 2: convert to base format
      if content.isSupportBaseDataConversion():
        content.activate().Document_tryToConvertToBaseFormat()
      # Step 3: run discoverMetadata
      content.activate(after_path_and_method_id=(content.getPath(),
            ('convertToBaseFormat', 'Document_tryToConvertToBaseFormat'))) \
          .discoverMetadata(filename=filename)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
578
      # Step 4: activate populate (unless interaction workflow does it)
579
      content.activate().populateContent()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
580
      # Step 5: activate crawlContent
581 582 583
      depth = content.getCrawlingDepth()
      if depth > 0:
        content.activate().crawlContent()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
584 585

  security.declareProtected(Permissions.AddPortalContent, 'newContentFromURL')
586 587
  def newContentFromURL(self, url, container_path=None, id=None, repeat=MAX_REPEAT,
                        repeat_interval=1, batch_mode=True, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
588 589 590 591 592 593 594 595 596
    """
      A wrapper method for newContent which provides extra safety
      in case or errors (ie. download, access, conflict, etc.).
      The method is able to handle a certain number of exceptions
      and can postpone itself through an activity based on
      the type of exception (ex. for a 404, postpone 1 day), using
      the at_date parameter and some standard values.

      NOTE: implementation needs to be done.
Nicolas Delaby's avatar
Nicolas Delaby committed
597
      id parameter is ignored
Jean-Paul Smets's avatar
Jean-Paul Smets committed
598
    """
Ivan Tyagov's avatar
Ivan Tyagov committed
599
    document = None
600
    try:
Nicolas Delaby's avatar
Nicolas Delaby committed
601
      document = self.newContent(container_path=container_path, url=url, **kw)
602 603 604 605 606 607
      if document.isIndexContent() and document.getCrawlingDepth() >= 0:
        # If this is an index document, keep on crawling even if crawling_depth is 0
        document.activate().crawlContent()
      elif document.getCrawlingDepth() > 0:
        # If this is an index document, stop crawling if crawling_depth is 0
        document.activate().crawlContent()
608
    except urllib2.HTTPError, error:
609
      if repeat == 0 or not batch_mode:
610 611 612
        # here we must call the extendBadURLList method,--NOT Implemented--
        # which had to add this url to bad URL list, so next time we avoid
        # crawling bad URL
613
        raise
614 615 616 617
      if repeat > 0:
        # Catch any HTTP error
        self.activate(at_date=DateTime() + repeat_interval,
                      activity="SQLQueue").newContentFromURL(
Nicolas Delaby's avatar
Nicolas Delaby committed
618
                        container_path=container_path, url=url,
619 620
                        repeat=repeat - 1,
                        repeat_interval=repeat_interval, **kw)
621
    return document
Jean-Paul Smets's avatar
Jean-Paul Smets committed
622

Nicolas Delaby's avatar
Nicolas Delaby committed
623 624 625
  security.declareProtected(Permissions.AccessContentsInformation,
                            'guessMimeTypeFromFilename')
  def guessMimeTypeFromFilename(self, filename):
626
    """
Nicolas Delaby's avatar
Nicolas Delaby committed
627
      get mime type from file name
628
    """
Nicolas Delaby's avatar
Nicolas Delaby committed
629 630 631 632
    if not filename:
      return
    portal = self.getPortalObject()
    content_type = portal.mimetypes_registry.lookupExtension(filename)
633 634
    if content_type:
      return str(content_type)
Nicolas Delaby's avatar
Nicolas Delaby committed
635 636 637 638 639 640 641 642
    return content_type

  def _openURL(self, url):
    """Download content from url,
    read filename and content_type
    return file_object, filename, content_type tuple
    """
    # Quote path part of url
643
    url = reencodeUrlEscapes(url)
Nicolas Delaby's avatar
Nicolas Delaby committed
644
    # build a new file from the url
645 646
    url_file = urllib2.urlopen(urllib2.Request(url,
                                               headers={'Accept':'*/*'}))
Nicolas Delaby's avatar
Nicolas Delaby committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
    data = url_file.read() # time out must be set or ... too long XXX
    file_object = cStringIO.StringIO()
    file_object.write(data)
    file_object.seek(0)
    # if a content-disposition header is present,
    # try first to read the suggested filename from it.
    header_info = url_file.info()
    content_disposition = header_info.getheader('content-disposition', '')
    filename = parse_header(content_disposition)[1].get('filename')
    if not filename:
      # Now read the filename from url.
      # In case of http redirection, the real url must be read
      # from file object returned by urllib2.urlopen.
      # It can happens when the header 'Location' is present in request.
      # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
      url = url_file.geturl()
      # Create a file name based on the URL and quote it
      filename = urlparse.urlsplit(url)[-3]
      filename = os.path.basename(filename)
      filename = urllib.quote(filename, safe='')
      filename = filename.replace('%', '')
    content_type = header_info.gettype()
    return file_object, filename, content_type
670

Ivan Tyagov's avatar
Ivan Tyagov committed
671
InitializeClass(ContributionTool)