Class | WikiPage
In: | app/models/wiki_page.rb
Parent: | Page

Copyright (c) 2006 Logica

### Methods

active checkedout? checkout comments comments_in_other_wikis_count contributors current_version current_version= harvested? html= last_version make_rel_path_unique max_version_no new_using_template other_pages_with_comments other_pages_with_versions unharvested_versions versions versions_in_other_wikis versions_in_other_wikis_count wikis

### Public Class methods

active()

Example wiki.pages.active

[Source]

         # File app/models/wiki_page.rb, line 181
    181:   def self.active
    182:     find(:all, :conditions=>["status = ? or status = ?", 'New','Updated'])
    183:   end

new_using_template(params)

Method new_using_template requires presentation_name, note, site, user and source_version and returns: page, checkout. For an example see PagesController.new

[Source]

         # File app/models/wiki_page.rb, line 81
     81:   def self.new_using_template(params)
     82:     logger.info("Creating new page using params: #{params.inspect}" )
     83:     sv = Version.find(params[:source_version])
     84:     p = sv.page.clone
     85:     p.site = params[:site]
     86:     p.tool = 'Wiki'
     87:     p.user = params[:user]
     88:     p.presentation_name = params[:presentation_name]
     89:     p.filename = p.presentation_name.downcase.delete('&+-.\"/\[]:;=,').tr(' ','_') + '.html' if !p.presentation_name.blank?
     90:     old_path = File.expand_path(p.filename, File.dirname(sv.path))
     91:     if sv.class.name == UserVersion.name
     92:       p.rel_path = old_path.gsub(sv.wiki.path + '/','')
     93:     else
     94:       p.rel_path = old_path.gsub(sv.baseline_process.path + '/','')
     95:     end
     96:
     97:     # make the rel_path unique, this way we can create multiple pages with the same presentation name
     98:     unique = false
     99:     while !unique
    100:       if Page.exists?(['rel_path = ? and site_id = ?',p.rel_path, p.site.id])
    101:         p.make_rel_path_unique
    102:       else
    103:         unique = true
    104:       end
    105:     end
    106:
    107:     logger.debug("New path is #{p.rel_path}, site path is #{sv.wiki.path}, old_path is #{old_path}")
    108:     h = sv.html
    109:     h = h.gsub(/<meta.*? name="uma\.presentationName".*?>/, '<meta content="' + p.presentation_name + '" name="uma.presentationName">')
    110:     h = h.gsub(TITLE_PATTERN, "<title>#{p.presentation_name}</title>")
    111:     h = h.gsub(TITLE2_PATTERN, 'class="pageTitle">' + p.presentation_name + '<\/td>') # TODO don't think this is working, but there is a workaround
    112:     p.html = h
    113:     Page.enhance_file(p.path)
    114:     if p.save
    115:       logger.info("Page saved, creating co")
    116:         co = Checkout.new(:note => p.note, :page => p, :site => p.site, :source_version => sv, :user => params[:user], :html => h)
    117:         logger.debug("creating co: #{co.inspect}")
    118:         co.save
    119:         Notification.find_or_create(p, params[:user], Page.name)
    120:       return p, co
    121:     else
    122:       logger.info("Failed to save page, returned unsaved page")
    123:       return p, nil
    124:     end
    125:   end

### Public Instance methods

checkedout?()

[Source]

         # File app/models/wiki_page.rb, line 231
    231:   def checkedout?
    232:     !self.checkout.nil?
    233:   end

checkout()

[Source]

        # File app/models/wiki_page.rb, line 56
    56:   def checkout
    57:     Checkout.find(:first, :conditions => ['page_id=?',self.id])
    58:   end

comments()

[Source]

        # File app/models/wiki_page.rb, line 60
    60:   def comments
    61:     Comment.find(:all, :order => 'created_on ASC', :conditions => ['page_id=?',self.id])
    62:   end

comments_in_other_wikis_count()

[Source]

         # File app/models/wiki_page.rb, line 218
    218:   def comments_in_other_wikis_count
    219:     Comment.count(:conditions => ['site_id in (?) and page_id <> ?', WikiPage.find(:all, :conditions => ['rel_path=?', self.rel_path]).collect{|p|p.id}, self.id])
    220:   end

contributors()

[Source]

         # File app/models/wiki_page.rb, line 166
    166:   def contributors
    167:     (self.versions.collect {|v|v.user.name} + self.comments.collect {|c|c.user.name}).uniq
    168:   end

current_version()

Method current_version returns the current version of the page

[Source]

         # File app/models/wiki_page.rb, line 152
    152:   def current_version
    153:     logger.debug("Finding current version of page #{self.presentation_name}")
    154:     version = Version.find(:first ,:conditions => ["page_id=? and current = ?", self.id, true]) #TODO check that true works instead of 1
    155:     version = Version.find(:first ,:order => "version DESC", :conditions => ["page_id=? and version is not null", self.id]) if version.nil?
    156:     version = version.previous_version if !version.nil? && version.checkout #NOTE: version can be nil, it will be nil when creating a new page
    157:     return version
    158:   end

current_version=(v)

Method current_version=(version) makes the version the current version of the page

[Source]

         # File app/models/wiki_page.rb, line 137
    137:   def current_version=(v)
    138:     v2 = self.current_version
    139:     if v2 && v2.current
    140:       v2.current = false
    141:       v2.save!
    142:     end
    143:     if v
    144:       logger.info("Making version #{v.id} the current version")
    145:       v.current = true
    146:       v.save!
    147:     end
    148:     return v
    149:   end

harvested?()

[Source]

        # File app/models/wiki_page.rb, line 72
    72:   def harvested?
    73:     self.unharvested_versions.size == 0
    74:   end

html=(h)

See also Page.html

[Source]

         # File app/models/wiki_page.rb, line 171
    171:   def html=(h)
    172:     logger.debug("Writing html to #{self.path}")
    173:     d = File.dirname(self.path)
    174:     File.makedirs(d) if !File.exists?(d) # In case of a new page being created using a template, the dir possible doesn't exist yet
    175:     f = File.new(self.path, "w")
    176:     f.puts(h)
    177:     f.close
    178:   end

last_version()

Method last_version return the last created version (the version with the highest version number). Versions that are part of a checkout are excluded (version is nil for those versions)

[Source]

         # File app/models/wiki_page.rb, line 162
    162:   def last_version
    163:     return Version.find(:first ,:order => 'version DESC', :conditions => ["page_id=? and version is not null", self.id])
    164:   end

make_rel_path_unique()

Make rel_path unique if it isn‘t unique. Used for creating new pages with the same presentation name. Only used by WikiPage.new_using_template

[Source]

         # File app/models/wiki_page.rb, line 239
    239:   def make_rel_path_unique
    240:        match = /_([\d]+)\.html/.match(self.rel_path)
    241:         if match
    242:           nr = (match[1].to_i + 1)
    243:           self.rel_path = self.rel_path.gsub(match[0], "_#{nr}.html")
    244:         else
    245:           self.rel_path = self.rel_path.gsub('.html', '_1.html')
    246:         end
    247:   end

max_version_no()

[Source]

         # File app/models/wiki_page.rb, line 127
    127:   def max_version_no
    128:     max = Version.maximum('version', :conditions => ['page_id=? and wiki_id = ?',self.id, self.site.id])
    129:     max = 0 if max.nil? # max can be zero when creating new pages, there is no BaselineProcessVersion only a UserVersion
    130:     return max
    131:   end

other_pages_with_comments()

     TODO Bugzilla 231125
     A method like below could be used to create a good index for finding pages

def text

      t = read_attribute('text')
      if t.blank?
        t = self.html.gsub(/<\/?[^>]*>/, '')
        t += self.comments.collect {|c|c.text}
        t += self.versions.collect {|v|v.note}
        write_attribute('text', t)
      end
      t

end

[Source]

         # File app/models/wiki_page.rb, line 198
    198:   def other_pages_with_comments
    199:     returning pages = [] do
    200:       WikiPage.find(:all, :conditions => ['rel_path = ? and id <> ?', self.rel_path, self.id]).each do |p|
    201:         pages << p if Comment.count(:conditions => ['page_id =?', self.id]) > 0
    202:       end
    203:     end
    204:   end

other_pages_with_versions()

[Source]

         # File app/models/wiki_page.rb, line 206
    206:   def other_pages_with_versions
    207:     returning pages = [] do
    208:       WikiPage.find(:all, :conditions => ['rel_path = ? and id <> ?', self.rel_path, self.id]).each do |p|
    209:         pages << p if Version.count(:conditions => ['page_id =? and baseline_process_id is null', self.id]) > 0
    210:       end
    211:     end
    212:   end

unharvested_versions()

change 68 TODO remove

[Source]

        # File app/models/wiki_page.rb, line 65
    65:   def unharvested_versions
    66:     Version.find(:all, :order => 'created_on DESC', :conditions => ['page_id = ? and done=?',self.id, 'N'])
    67:   end

versions()

[Source]

        # File app/models/wiki_page.rb, line 52
    52:   def versions
    53:     (self.user_versions + self.baseline_process_versions).sort_by{|v|v.version}
    54:   end

versions_in_other_wikis()

[Source]

         # File app/models/wiki_page.rb, line 222
    222:   def versions_in_other_wikis
    223:     Version.find(:all, :conditions => ['wiki_id in ? and baseline_process_id is null and page_id <> ?', WikiPage.find(:all, :rel_path => self.rel_path).collect{|p|p.id}, self.id])
    224:   end

versions_in_other_wikis_count()

[Source]

         # File app/models/wiki_page.rb, line 226
    226:   def versions_in_other_wikis_count
    227:     ids = WikiPage.find(:all, :conditions => ['rel_path=?', self.rel_path]).collect{|p|p.id}
    228:     Version.count(:conditions => ['wiki_id in (?) and baseline_process_id is null and page_id <> ?', ids , self.id])
    229:   end

wikis()

[Source]

         # File app/models/wiki_page.rb, line 214
    214:   def wikis
    215:     return WikiPage.find(:all, :conditions => ['rel_path=?', self.rel_path]).collect {|p|p.site}
    216:   end

[Validate]