Edgewall Software

Ticket #8861: 8861-wiki-replace-r8952.2.patch

File 8861-wiki-replace-r8952.2.patch, 7.8 KB (added by rblank, 2 years ago)

Don't deprecate wiki import, and only import from files with wiki load.

  • trac/admin/tests/console-tests.txt

    diff --git a/trac/admin/tests/console-tests.txt b/trac/admin/tests/console-tests.txt
    a b  
    6262wiki export          Export wiki page to file or stdout 
    6363wiki import          Import wiki page from file or stdin 
    6464wiki list            List wiki pages 
    65 wiki load            Import all wiki pages from directory 
     65wiki load            Import wiki pages from files 
    6666wiki remove          Remove wiki page 
     67wiki replace         Replace the content of wiki pages from files 
    6768wiki upgrade         Upgrade default wiki pages to current version 
    6869===== test_attachment_list_empty ===== 
    6970 
  • trac/wiki/admin.py

    diff --git a/trac/wiki/admin.py b/trac/wiki/admin.py
    a b  
    2121from trac.core import * 
    2222from trac.wiki import model 
    2323from trac.wiki.api import WikiSystem 
     24from trac.util import read_file 
    2425from trac.util.compat import any 
    2526from trac.util.datefmt import format_datetime, utc 
    2627from trac.util.text import to_unicode, unicode_quote, unicode_unquote, \ 
     
    4849        yield ('wiki import', '<page> [file]', 
    4950               'Import wiki page from file or stdin', 
    5051               self._complete_import_export, self._do_import) 
    51         yield ('wiki dump', '<directory> [name] [...]', 
     52        yield ('wiki dump', '<directory> [page] [...]', 
    5253               """Export wiki pages to files named by title 
    5354                
    5455               Individual wiki page names can be specified after the directory. 
     
    5657               that prefix should be dumped. If no name is specified, all wiki 
    5758               pages are dumped.""", 
    5859               self._complete_dump, self._do_dump) 
    59         yield ('wiki load', '<directory>', 
    60                'Import all wiki pages from directory', 
    61                self._complete_load, self._do_load) 
     60        yield ('wiki load', '<path> [...]', 
     61               """Import wiki pages from files 
     62                
     63               If a given path is a file, it is imported as a page with the 
     64               name of the file. If a path is a directory, all files in that 
     65               directory are imported.""", 
     66               self._complete_load_replace, self._do_load) 
     67        yield ('wiki replace', '<path> [...]', 
     68               """Replace the content of wiki pages from files 
     69                
     70               This command replaces the content of the last version of one 
     71               or more wiki pages with new content. The previous content is 
     72               lost, and no new entry is created in the page history. The 
     73               metadata of the page (time, author) is not changed either. 
     74                
     75               If a given path is a file, it is imported as a page with the 
     76               name of the file. If a path is a directory, all files in that 
     77               directory are imported.""", 
     78               self._complete_load_replace, self._do_replace) 
    6279        yield ('wiki upgrade', '', 
    6380               'Upgrade default wiki pages to current version', 
    6481               None, self._do_upgrade) 
     
    85102            finally: 
    86103                f.close() 
    87104     
    88     def import_page(self, filename, title, db=None, create_only=[]): 
    89         if not os.path.isfile(filename): 
    90             raise AdminCommandError(_("'%(name)s' is not a file", 
    91                                       name=filename)) 
    92          
    93         f = open(filename, 'r') 
    94         try: 
    95             data = to_unicode(f.read(), 'utf-8') 
    96         finally: 
    97             f.close() 
     105    def import_page(self, filename, title, db=None, create_only=[], 
     106                    replace=False): 
     107        if filename: 
     108            if not os.path.isfile(filename): 
     109                raise AdminCommandError(_("'%(name)s' is not a file", 
     110                                          name=filename)) 
     111            data = read_file(filename) 
     112        else: 
     113            data = sys.stdin.read() 
     114        data = to_unicode(data, 'utf-8') 
    98115         
    99116        # Make sure we don't insert the exact same page twice 
    100117        handle_ta = not db 
     
    111128        if old and data == old[0][0]: 
    112129            printout('  %s already up to date.' % title) 
    113130            return False 
     131        if replace and not old: 
     132            printout("  Not replacing %s, page doesn't exist." % title) 
     133            return False 
    114134         
    115         cursor.execute("INSERT INTO wiki(version,name,time,author,ipnr,text) " 
    116                        " SELECT 1+COALESCE(max(version),0),%s,%s," 
    117                        " 'trac','127.0.0.1',%s FROM wiki " 
    118                        " WHERE name=%s", 
    119                        (title, int(time.time()), data, title)) 
     135        if replace: 
     136            cursor.execute("UPDATE wiki SET text=%s WHERE name=%s " 
     137                           "  AND version=(SELECT max(version) FROM wiki " 
     138                           "               WHERE name=%s)", 
     139                           (data, title, title)) 
     140        else: 
     141            cursor.execute("INSERT INTO wiki(version,name,time,author,ipnr," 
     142                           "                 text) " 
     143                           "SELECT 1+COALESCE(max(version),0),%s,%s," 
     144                           "       'trac','127.0.0.1',%s FROM wiki " 
     145                           "WHERE name=%s", 
     146                           (title, int(time.time()), data, title)) 
    120147        if not old: 
    121148            WikiSystem(self.env).pages.invalidate(db) 
    122149        if handle_ta: 
    123150            db.commit() 
    124151        return True 
    125152 
    126     def load_pages(self, dir, db=None, ignore=[], create_only=[]): 
     153    def load_pages(self, dir, db=None, ignore=[], create_only=[], 
     154                   replace=False): 
    127155        cons_charset = getattr(sys.stdout, 'encoding', None) or 'utf-8' 
    128156        for page in os.listdir(dir): 
    129157            if page in ignore: 
     
    131159            filename = os.path.join(dir, page) 
    132160            page = unicode_unquote(page.encode('utf-8')) 
    133161            if os.path.isfile(filename): 
    134                 if self.import_page(filename, page, db, create_only): 
     162                if self.import_page(filename, page, db, create_only, replace): 
    135163                    printout(_("  %(page)s imported from %(filename)s", 
    136164                               filename=filename, page=page)) 
    137165     
     
    147175     
    148176    def _complete_dump(self, args): 
    149177        if len(args) == 1: 
    150             return get_dir_list(args[-1], True) 
    151         elif len(args) == 2: 
     178            return get_dir_list(args[-1], dirs_only=True) 
     179        elif len(args) >= 2: 
    152180            return self.get_wiki_list() 
    153181     
    154     def _complete_load(self, args): 
    155         if len(args) == 1: 
    156             return get_dir_list(args[-1], True) 
     182    def _complete_load_replace(self, args): 
     183        if len(args) >= 1: 
     184            return get_dir_list(args[-1]) 
    157185     
    158186    def _do_list(self): 
    159187        db = self.env.get_db_cnx() 
     
    206234                printout(' %s => %s' % (p, dst)) 
    207235                self.export_page(p, dst, cursor) 
    208236     
    209     def _do_load(self, directory): 
     237    def _load_or_replace(self, paths, replace): 
    210238        db = self.env.get_db_cnx() 
    211         self.load_pages(directory, db) 
     239        for path in paths: 
     240            if os.path.isdir(path): 
     241                self.load_pages(path, db, replace=replace) 
     242            else: 
     243                page = os.path.basename(path) 
     244                page = unicode_unquote(page.encode('utf-8')) 
     245                if self.import_page(path, page, db, replace=replace): 
     246                    printout(_("  %(page)s imported from %(filename)s", 
     247                               filename=path, page=page)) 
    212248        db.commit() 
    213249     
     250    def _do_load(self, *paths): 
     251        self._load_or_replace(paths, replace=False) 
     252     
     253    def _do_replace(self, *paths): 
     254        self._load_or_replace(paths, replace=True) 
     255     
    214256    def _do_upgrade(self): 
    215257        db = self.env.get_db_cnx() 
    216258        self.load_pages(pkg_resources.resource_filename('trac.wiki',