~ubuntu-branches/ubuntu/utopic/critcl/utopic

« back to all changes in this revision

Viewing changes to lib/wikit/cache.tcl

  • Committer: Package Import Robot
  • Author(s): Andrew Shadura
  • Date: 2013-05-11 00:08:06 UTC
  • Revision ID: package-import@ubuntu.com-20130511000806-7hq1zc3fnn0gat79
Tags: upstream-3.1.9
ImportĀ upstreamĀ versionĀ 3.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Wikit Cache - Caches page titles in an array, to speed access
 
2
#
 
3
# BuildTitleCache - traverse pages and cache their titles
 
4
# AdjustTitleCache - change a title in the cache
 
5
# LookupPage - look up a title in the cache
 
6
 
 
7
package provide Wikit::Cache 1.0
 
8
package require Wikit::Db
 
9
 
 
10
namespace eval Wikit {
 
11
  namespace export BuildTitleCache AdjustTitleCache
 
12
  
 
13
  variable titleCache   ;# an array mapping name to id
 
14
  
 
15
  # AdjustTitleCache for renamed page
 
16
  proc AdjustTitleCache {name newName id {db wdb}} {
 
17
    variable titleCache
 
18
    set name [string tolower $name]
 
19
    if {[info exists titleCache($name)]} {
 
20
      unset titleCache($db,$name)
 
21
    }
 
22
    set titleCache($db,[string tolower $newName]) $id
 
23
  }
 
24
  
 
25
  # BuildTitleCache by traversing all pages and caching their title
 
26
  proc BuildTitleCache {{db wdb}} {
 
27
    variable titleCache
 
28
    mk::loop c $db.pages {
 
29
      set title [mk::get $c name]
 
30
      if {[info exists titleCache($db,[string tolower $title])]} {
 
31
        #puts stderr "duplicate page! [mk::cursor position c] -> $title"
 
32
        #puts stderr [mk::get $c page]
 
33
      } else {
 
34
        set titleCache($db,[string tolower $title]) [mk::cursor position c]
 
35
      }
 
36
    }
 
37
  }
 
38
  
 
39
  # LookupPage - find a name in the titleCache
 
40
  proc LookupPage {name {db wdb}} {
 
41
    variable titleCache
 
42
    
 
43
    set lcname [string tolower $name]
 
44
    if {[info exists titleCache($db,$lcname)]} {
 
45
      set n $titleCache($db,$lcname)
 
46
    } else {
 
47
      set n [mk::select $db.pages -count 1 name $name]
 
48
      set titleCache($db,$lcname) $n
 
49
    }
 
50
    
 
51
    if {$n == ""} {
 
52
      set n [mk::view size $db.pages]
 
53
      mk::set $db.pages!$n name $name
 
54
      set titleCache($db,$lcname) $n
 
55
      DoCommit $db
 
56
    }
 
57
    
 
58
    return $n
 
59
  }
 
60
}