comparison MoinMoin/wikiutil.py @ 1122:2be8ec7ba817

Now the interwiki code caches the modification time of the interwiki files.
author Alexander Schremmer <alex AT alexanderweb DOT de>
date Thu, 03 Aug 2006 18:20:48 +0200
parents 2aa53ed0afa0
children a7f8dceb4410
comparison
equal deleted inserted replaced
1121:91ffa85ac616 1122:2be8ec7ba817
501 501
502 502
503 ############################################################################# 503 #############################################################################
504 ### InterWiki 504 ### InterWiki
505 ############################################################################# 505 #############################################################################
506 INTERWIKI_PAGE = "InterWikiMap"
507
508 def generate_file_list(request):
509 """ generates a list of all files. for internal use. """
510
511 # order is important here, the local intermap file takes
512 # precedence over the shared one, and is thus read AFTER
513 # the shared one
514 intermap_files = request.cfg.shared_intermap
515 if not isinstance(intermap_files, list):
516 intermap_files = [intermap_files]
517 else:
518 intermap_files = intermap_files[:]
519 intermap_files.append(os.path.join(request.cfg.data_dir, "intermap.txt"))
520 request.cfg.shared_intermap_files = [filename for filename in intermap_files
521 if filename and os.path.isfile(filename)]
522
523
524 def get_max_mtime(file_list, page):
525 """ Returns the highest modification time of the files in file_list and the
526 page page. """
527 return max([os.stat(filename).st_mtime for filename in file_list] +
528 [version2timestamp(page.mtime_usecs())])
529
530
506 def load_wikimap(request): 531 def load_wikimap(request):
507 """ load interwiki map (once, and only on demand) """ 532 """ load interwiki map (once, and only on demand) """
533 from MoinMoin.Page import Page
508 534
509 now = int(time.time()) 535 now = int(time.time())
536 if getattr(request.cfg, "shared_intermap_files", None) is None:
537 generate_file_list(request)
510 538
511 try: 539 try:
512 _interwiki_list = request.cfg._interwiki_list 540 _interwiki_list = request.cfg._interwiki_list
513 if request.cfg._interwiki_ts + (3*60) < now: # 3 minutes caching time 541 old_mtime = request.cfg._interwiki_mtime
514 raise AttributeError # refresh cache 542 if request.cfg._interwiki_ts + (1*60) < now: # 1 minutes caching time
543 max_mtime = get_max_mtime(request.cfg.shared_intermap_files, Page(request, INTERWIKI_PAGE))
544 if max_mtime > old_mtime:
545 raise AttributeError # refresh cache
546 else:
547 request.cfg._interwiki_ts = now
515 except AttributeError: 548 except AttributeError:
516 from MoinMoin.Page import Page
517
518 _interwiki_list = {} 549 _interwiki_list = {}
519 lines = [] 550 lines = []
520 551
521 # order is important here, the local intermap file takes 552 for filename in request.cfg.shared_intermap_files:
522 # precedence over the shared one, and is thus read AFTER 553 f = open(filename, "r")
523 # the shared one 554 lines.extend(f.readlines())
524 intermap_files = request.cfg.shared_intermap 555 f.close()
525 if not isinstance(intermap_files, list):
526 intermap_files = [intermap_files]
527 intermap_files.append(os.path.join(request.cfg.data_dir, "intermap.txt"))
528
529 for filename in intermap_files:
530 if filename and os.path.isfile(filename):
531 f = open(filename, "r")
532 lines.extend(f.readlines())
533 f.close()
534 556
535 # add the contents of the InterWikiMap page 557 # add the contents of the InterWikiMap page
536 lines += Page(request, "InterWikiMap").get_raw_body().splitlines() 558 lines += Page(request, INTERWIKI_PAGE).get_raw_body().splitlines()
537 559
538 for line in lines: 560 for line in lines:
539 if not line or line[0] == '#': continue 561 if not line or line[0] == '#': continue
540 try: 562 try:
541 line = "%s %s/InterWiki" % (line, request.getScriptname()) 563 line = "%s %s/InterWiki" % (line, request.getScriptname())
553 _interwiki_list[request.cfg.interwikiname] = request.getScriptname() + '/' 575 _interwiki_list[request.cfg.interwikiname] = request.getScriptname() + '/'
554 576
555 # save for later 577 # save for later
556 request.cfg._interwiki_list = _interwiki_list 578 request.cfg._interwiki_list = _interwiki_list
557 request.cfg._interwiki_ts = now 579 request.cfg._interwiki_ts = now
580 request.cfg._interwiki_mtime = get_max_mtime(request.cfg.shared_intermap_files, Page(request, INTERWIKI_PAGE))
558 581
559 return _interwiki_list 582 return _interwiki_list
560 583
561 def split_wiki(wikiurl): 584 def split_wiki(wikiurl):
562 """ Split a wiki url, e.g: 585 """ Split a wiki url, e.g: