def getSingleWikiWordPacket(wikiDocument, word, writeVersionData=True, formatVer=1): """ Helper function for the trashcan to create a trash bag packet for a single wiki word (including versions). Returns it as a utf8-encoded bytestring. """ for tryNumber in range(35): stream = StringIO() stream.write(BOM_UTF8) separator = u"-----%s-----" % createRandomString(25) exportFile = _SeparatorWatchUtf8Writer(stream, separator) wikiPageWriter = MultiPageTextWikiPageWriter(wikiDocument, exportFile, writeVersionData, formatVer) exportFile.write(u"Multipage text format %i\n" % formatVer) # Separator line exportFile.write(u"Separator: %s\n" % separator) try: wikiPageWriter.exportWikiWord(word) exportFile.checkAndClearBuffer() return stream.getvalue() except _SeparatorFoundException: continue else: raise ExportException(_(u"No usable separator found"))
def recoveryExport(self, wikiDocument, exportDest, progressHandler): """ Export in recovery mode wikiDocument -- WikiDocument object exportDest -- Path to destination directory or file to export to """ self.wikiDocument = wikiDocument self.exportDest = exportDest self.exportFile = None self.rawExportFile = None self.firstSeparatorCallDone = False self.formatVer = 1 try: for tryNumber in range(35): self.separator = u"-----%s-----" % createRandomString(25) try: self.rawExportFile = open(pathEnc(self.exportDest), "w") # Only UTF-8 mode currently self.rawExportFile.write(BOM_UTF8) self.exportFile = _SeparatorWatchUtf8Writer( self.rawExportFile, self.separator, "replace") # self.wikiPageWriter = MultiPageTextWikiPageWriter( # self.wikiDocument, self.exportFile, # self.writeVersionData, self.formatVer) # Identifier line with file format self.exportFile.write(u"Multipage text format %i\n" % self.formatVer) # Separator line self.exportFile.write(u"Separator: %s\n" % self.separator) self._recoveryExportDatablocks() self._recoveryExportWikiWords() break except _SeparatorFoundException: if self.exportFile is not None: self.exportFile.flush() self.exportFile = None if self.rawExportFile is not None: self.rawExportFile.close() self.rawExportFile = None else: raise ExportException(_(u"No usable separator found")) finally: if self.exportFile is not None: self.exportFile.flush() self.exportFile = None if self.rawExportFile is not None: self.rawExportFile.close() self.rawExportFile = None
def export(self, wikiDocument, wordList, exportType, exportDest, compatFilenames, addOpt, progressHandler): """ Run export operation. wikiDocument -- WikiDocument object wordList -- Sequence of wiki words to export exportType -- string tag to identify how to export exportDest -- Path to destination directory or file to export to compatFilenames -- Should the filenames be encoded to be lowest level compatible addOpt -- additional options returned by getAddOpt() """ self.wikiDocument = wikiDocument self.wordList = wordList self.exportDest = exportDest self.addOpt = addOpt self.exportFile = None self.rawExportFile = None self.firstSeparatorCallDone = False self.formatVer = min(addOpt[0], 1) self.writeWikiFuncPages = addOpt[1] and (self.formatVer > 0) self.writeSavedSearches = addOpt[2] and (self.formatVer > 0) self.writeVersionData = addOpt[3] and (self.formatVer > 0) try: for tryNumber in range(35): self.separator = u"-----%s-----" % createRandomString(25) try: self.rawExportFile = open(pathEnc(self.exportDest), "w") # Only UTF-8 mode currently self.rawExportFile.write(BOM_UTF8) self.exportFile = _SeparatorWatchUtf8Writer( self.rawExportFile, self.separator, "replace") self.wikiPageWriter = MultiPageTextWikiPageWriter( self.wikiDocument, self.exportFile, self.writeVersionData, self.formatVer) # Identifier line with file format self.exportFile.write(u"Multipage text format %i\n" % self.formatVer) # Separator line self.exportFile.write(u"Separator: %s\n" % self.separator) # Write wiki-bound functional pages if self.writeWikiFuncPages: # Only wiki related functional pages wikiFuncTags = [ft for ft in DocPages.getFuncTags() if ft.startswith("wiki/")] for ft in wikiFuncTags: self.exportFile.writeSeparator() self.exportFile.write(u"funcpage/%s\n" % ft) page = self.wikiDocument.getFuncPage(ft) self.exportFile.write(page.getLiveText()) # Write saved searches if self.writeSavedSearches: # Wiki-wide searches wikiData = self.wikiDocument.getWikiData() unifNames = wikiData.getDataBlockUnifNamesStartingWith( u"savedsearch/") for un in unifNames: self.exportFile.writeSeparator() self.exportFile.write(un + u"\n") datablock = wikiData.retrieveDataBlock(un) self.exportFile.write(base64BlockEncode(datablock)) # Page searches unifNames = wikiData.getDataBlockUnifNamesStartingWith( u"savedpagesearch/") for un in unifNames: self.exportFile.writeSeparator() self._writeHintedDatablock(un, False) locale.setlocale(locale.LC_ALL, '') # Write actual wiki words for word in self.wordList: self.wikiPageWriter.exportWikiWord(word) self.exportFile.checkAndClearBuffer() break except _SeparatorFoundException: if self.exportFile is not None: self.exportFile.flush() self.exportFile = None if self.rawExportFile is not None: self.rawExportFile.close() self.rawExportFile = None continue except Exception, e: traceback.print_exc() raise ExportException(unicode(e)) else:
except _SeparatorFoundException: if self.exportFile is not None: self.exportFile.flush() self.exportFile = None if self.rawExportFile is not None: self.rawExportFile.close() self.rawExportFile = None continue except Exception, e: traceback.print_exc() raise ExportException(unicode(e)) else: raise ExportException(_(u"No usable separator found")) finally: if self.exportFile is not None: self.exportFile.flush() self.exportFile = None if self.rawExportFile is not None: self.rawExportFile.close() self.rawExportFile = None def _getDatablocksClass(self, unifName): """ """ if unifName.startswith(u"wiki/"): return 1 | 8 # as text, not hinted, prepend "funcpage/"