Ticket #8861: 8861-wiki-replace-r8952.2.patch
| File 8861-wiki-replace-r8952.2.patch, 7.8 KB (added by rblank, 2 years ago) |
|---|
-
trac/admin/tests/console-tests.txt
diff --git a/trac/admin/tests/console-tests.txt b/trac/admin/tests/console-tests.txt
a b 62 62 wiki export Export wiki page to file or stdout 63 63 wiki import Import wiki page from file or stdin 64 64 wiki list List wiki pages 65 wiki load Import all wiki pages from directory65 wiki load Import wiki pages from files 66 66 wiki remove Remove wiki page 67 wiki replace Replace the content of wiki pages from files 67 68 wiki upgrade Upgrade default wiki pages to current version 68 69 ===== test_attachment_list_empty ===== 69 70 -
trac/wiki/admin.py
diff --git a/trac/wiki/admin.py b/trac/wiki/admin.py
a b 21 21 from trac.core import * 22 22 from trac.wiki import model 23 23 from trac.wiki.api import WikiSystem 24 from trac.util import read_file 24 25 from trac.util.compat import any 25 26 from trac.util.datefmt import format_datetime, utc 26 27 from trac.util.text import to_unicode, unicode_quote, unicode_unquote, \ … … 48 49 yield ('wiki import', '<page> [file]', 49 50 'Import wiki page from file or stdin', 50 51 self._complete_import_export, self._do_import) 51 yield ('wiki dump', '<directory> [ name] [...]',52 yield ('wiki dump', '<directory> [page] [...]', 52 53 """Export wiki pages to files named by title 53 54 54 55 Individual wiki page names can be specified after the directory. … … 56 57 that prefix should be dumped. If no name is specified, all wiki 57 58 pages are dumped.""", 58 59 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) 62 79 yield ('wiki upgrade', '', 63 80 'Upgrade default wiki pages to current version', 64 81 None, self._do_upgrade) … … 85 102 finally: 86 103 f.close() 87 104 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') 98 115 99 116 # Make sure we don't insert the exact same page twice 100 117 handle_ta = not db … … 111 128 if old and data == old[0][0]: 112 129 printout(' %s already up to date.' % title) 113 130 return False 131 if replace and not old: 132 printout(" Not replacing %s, page doesn't exist." % title) 133 return False 114 134 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)) 120 147 if not old: 121 148 WikiSystem(self.env).pages.invalidate(db) 122 149 if handle_ta: 123 150 db.commit() 124 151 return True 125 152 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): 127 155 cons_charset = getattr(sys.stdout, 'encoding', None) or 'utf-8' 128 156 for page in os.listdir(dir): 129 157 if page in ignore: … … 131 159 filename = os.path.join(dir, page) 132 160 page = unicode_unquote(page.encode('utf-8')) 133 161 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): 135 163 printout(_(" %(page)s imported from %(filename)s", 136 164 filename=filename, page=page)) 137 165 … … 147 175 148 176 def _complete_dump(self, args): 149 177 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: 152 180 return self.get_wiki_list() 153 181 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]) 157 185 158 186 def _do_list(self): 159 187 db = self.env.get_db_cnx() … … 206 234 printout(' %s => %s' % (p, dst)) 207 235 self.export_page(p, dst, cursor) 208 236 209 def _ do_load(self, directory):237 def _load_or_replace(self, paths, replace): 210 238 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)) 212 248 db.commit() 213 249 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 214 256 def _do_upgrade(self): 215 257 db = self.env.get_db_cnx() 216 258 self.load_pages(pkg_resources.resource_filename('trac.wiki',
