From 888e93a7fd1e4c6d1a0d1a34f52d8366cb3abcd2 Mon Sep 17 00:00:00 2001 From: Jacques Distler Date: Sat, 29 Aug 2009 23:31:39 -0500 Subject: [PATCH 1/5] Streamline Rake Task Refactor the upgrade_instiki rake task. Based on the (very nice) JHerdman's http://github.com/jherdman/instiki/commit/64d305f2a85c1647bf4d2bb80069600df5d60432 but defaults to 'production' environment, instead. Instiki users don't know about production/development/test. Instiki defaults to 'production'. So should its associated rake tasks. --- lib/chunks/engines.rb | 4 ++-- lib/tasks/upgrade_instiki.rake | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/chunks/engines.rb b/lib/chunks/engines.rb index d7324013..a19b0978 100644 --- a/lib/chunks/engines.rb +++ b/lib/chunks/engines.rb @@ -85,7 +85,7 @@ module Engines :html_math_output_mathml => false, :html_math_output_png => true, :html_png_engine => 'blahtex', - :html_png_dir => @content.web.files_path + '/pngs', + :html_png_dir => @content.web.files_path.join('pngs').to_s, :html_png_url => '../files/pngs/', :content_only => true, :author => @content.options[:engine_opts][:author], @@ -99,7 +99,7 @@ module Engines :html_math_output_mathml => false, :html_math_output_png => true, :html_png_engine => 'blahtex', - :html_png_dir => @content.web.files_path + '/pngs', + :html_png_dir => @content.web.files_path.join('pngs').to_s, :html_png_url => '../files/pngs/'}).to_html html.gsub(/\A
\n?(.*?)\n?<\/div>\Z/m, '\1') end diff --git a/lib/tasks/upgrade_instiki.rake b/lib/tasks/upgrade_instiki.rake index be075a5e..ef242c42 100644 --- a/lib/tasks/upgrade_instiki.rake +++ b/lib/tasks/upgrade_instiki.rake @@ -1,15 +1,16 @@ -require 'active_record' - task :upgrade_instiki => :environment do - ActiveRecord::Base.establish_connection(:production) - webs = ActiveRecord::Base.connection.execute( "select * from webs" ) - webs.each do |row| - if File.exists?('public/' + row[4]) - if File.exists?('webs/' + row[4]) - print "Warning! The directory webs/#{row[4]} already exists. Skipping.\n" + RAILS_ENV = 'production' unless ENV['RAILS_ENV'] + puts "Upgrading Instiki in #{RAILS_ENV} environment." + + Web.all.each do |web| + public_path = Rails.root.join("public", web.address) + if public_path.exist? + webs_path = Rails.root.join("webs", web.address) + if webs_path.exist? + puts "Warning! The directory #{webs_path} already exists. Skipping." else - File.rename('public/' + row[4], 'webs/' + row[4]) - print "Moved: #{row[4]}\n" + public_path.rename(webs_path) + puts "Moved #{public_path} to #{webs_path}" end end end From 342298ed0e8c8f2ec70bfe1baa573bf9a34242d7 Mon Sep 17 00:00:00 2001 From: Jacques Distler Date: Thu, 3 Sep 2009 23:09:10 -0500 Subject: [PATCH 2/5] Wikilinks to Published Webs Should be to the published action. This didn't work right for inter-web links. (Reported by Mike Shulman) Also, change some .length's to .size's (for Andrew Stacey) --- app/controllers/wiki_controller.rb | 2 +- app/helpers/application_helper.rb | 4 ++-- app/views/wiki/recently_revised.rhtml | 4 ++-- lib/url_generator.rb | 7 +++++-- lib/wiki_content.rb | 2 +- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index a1110e4b..404a116a 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -98,7 +98,7 @@ class WikiController < ApplicationController diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f6662834..c0602606 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -44,12 +44,12 @@ module ApplicationHelper # Creates a hyperlink to a Wiki page, or to a "new page" form if the page doesn't exist yet def link_to_page(page_name, web = @web, text = nil, options = {}) raise 'Web not defined' if web.nil? - UrlGenerator.new(@controller).make_link(page_name, web, text, + UrlGenerator.new(@controller).make_link(@web, page_name, web, text, options.merge(:base_url => "#{base_url}/#{web.address}")) end def author_link(page, options = {}) - UrlGenerator.new(@controller).make_link(page.author.name, page.web, nil, options) + UrlGenerator.new(@controller).make_link(@web, page.author.name, page.web, nil, options) end # Create a hyperlink to a particular revision of a Wiki page diff --git a/app/views/wiki/recently_revised.rhtml b/app/views/wiki/recently_revised.rhtml index abf49a04..9c604000 100644 --- a/app/views/wiki/recently_revised.rhtml +++ b/app/views/wiki/recently_revised.rhtml @@ -8,9 +8,9 @@ <%- for page in @pages_by_day[day] -%>
  • <%= link_to_existing_page page %> - <%- if page.revisions.length > 1 %> + <%- if page.revisions.size > 1 %> - ( <%= link_to_revision(page, page.revisions.length, text='diff', + ( <%= link_to_revision(page, page.revisions.size, text='diff', mode='diff') %> | <%= link_to_history(page, text='history') %> ) <%- end -%> diff --git a/lib/url_generator.rb b/lib/url_generator.rb index cbfbf110..ba3fab57 100644 --- a/lib/url_generator.rb +++ b/lib/url_generator.rb @@ -10,7 +10,8 @@ class AbstractUrlGenerator # Create a link for the given page (or file) name and link text based # on the render mode in options and whether the page (file) exists # in the web. - def make_link(asked_name, web, text = nil, options = {}) + def make_link(current_web, asked_name, web, text = nil, options = {}) + @web = current_web mode = (options[:mode] || :show).to_sym link_type = (options[:link_type] || :show).to_sym @@ -105,7 +106,9 @@ class UrlGenerator < AbstractUrlGenerator end when :show if known_page - href = @controller.url_for :controller => 'wiki', :web => web_address, :action => 'show', + web = Web.find_by_address(web_address) + action = web.published? && web != @web ? 'published' : 'show' + href = @controller.url_for :controller => 'wiki', :web => web_address, :action => action, :id => name, :only_path => true %{#{text}} else diff --git a/lib/wiki_content.rb b/lib/wiki_content.rb index cc0784dc..8825029f 100644 --- a/lib/wiki_content.rb +++ b/lib/wiki_content.rb @@ -155,7 +155,7 @@ class WikiContent < String def page_link(web_name, name, text, link_type) web = Web.find_by_name(web_name) || Web.find_by_address(web_name) || @web @options[:link_type] = (link_type || :show) - @url_generator.make_link(name, web, text, @options) + @url_generator.make_link(@web, name, web, text, @options) end def build_chunks From 34c4306867f8b9f30a7c2476f50346153f12932f Mon Sep 17 00:00:00 2001 From: Jacques Distler Date: Fri, 4 Sep 2009 00:09:39 -0500 Subject: [PATCH 3/5] More ActiveRecord Association .length -> .size Optimizations --- app/controllers/cache_sweeping_helper.rb | 2 +- app/controllers/wiki_controller.rb | 6 +++--- app/helpers/application_helper.rb | 2 +- app/helpers/wiki_helper.rb | 14 +++++++------- app/views/wiki/history.html.erb | 2 +- app/views/wiki/web_list.rhtml | 2 +- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/controllers/cache_sweeping_helper.rb b/app/controllers/cache_sweeping_helper.rb index f91ec909..20e68b2f 100644 --- a/app/controllers/cache_sweeping_helper.rb +++ b/app/controllers/cache_sweeping_helper.rb @@ -26,7 +26,7 @@ module CacheSweepingHelper end def expire_cached_revisions(page) - page.revisions.length.times do |i| + page.revisions.count.times do |i| revno = i+1 expire_action :controller => 'wiki', :web => page.web.address, :action => 'revision', :id => page.name, :rev => revno diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 404a116a..1490c810 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -19,7 +19,7 @@ class WikiController < ApplicationController redirect_home elsif not @wiki.setup? redirect_to :controller => 'admin', :action => 'create_system' - elsif @wiki.webs.length == 1 + elsif @wiki.webs.size == 1 redirect_home @wiki.webs.values.first.address else redirect_to :action => 'web_list' @@ -345,7 +345,7 @@ class WikiController < ApplicationController if @page @revisions_by_day = Hash.new { |h, day| h[day] = [] } @revision_numbers = Hash.new { |h, id| h[id] = [] } - revision_number = @page.revisions.length + revision_number = @page.revisions.size @page.revisions.reverse.each do |rev| day = Date.new(rev.revised_at.year, rev.revised_at.month, rev.revised_at.day) @revisions_by_day[day] << rev @@ -462,7 +462,7 @@ class WikiController < ApplicationController if params['rev'] @revision_number = params['rev'].to_i else - @revision_number = @page.revisions.length + @revision_number = @page.revisions.size end @revision = @page.revisions[@revision_number - 1] end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c0602606..154d54fd 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -54,7 +54,7 @@ module ApplicationHelper # Create a hyperlink to a particular revision of a Wiki page def link_to_revision(page, revision_number, text = nil, mode = nil, html_options = {}) - revision_number == page.revisions.length ? + revision_number == page.revisions.size ? link_to( text || page.plain_name, {:web => @web.address, :action => 'show', :id => page.name, diff --git a/app/helpers/wiki_helper.rb b/app/helpers/wiki_helper.rb index eeb103f2..e88f9765 100644 --- a/app/helpers/wiki_helper.rb +++ b/app/helpers/wiki_helper.rb @@ -6,7 +6,7 @@ module WikiHelper menu << back_for_revision if @revision_number > 1 menu << current_revision menu << see_or_hide_changes_for_revision if @revision_number > 1 - menu << history if @page.revisions.length > 1 + menu << history if @page.revisions.size > 1 menu << rollback menu end @@ -15,11 +15,11 @@ module WikiHelper menu = [] menu << edit_page menu << edit_web if @page.name == "HomePage" - if @page.revisions.length > 1 + if @page.revisions.size > 1 menu << back_for_page menu << see_or_hide_changes_for_page end - menu << history if @page.revisions.length > 1 + menu << history if @page.revisions.size > 1 menu end @@ -40,11 +40,11 @@ module WikiHelper end def forward - if @revision_number < @page.revisions.length - 1 + if @revision_number < @page.revisions.size - 1 link_to('Forward in time', {:web => @web.address, :action => 'revision', :id => @page.name, :rev => @revision_number + 1}, {:class => 'navlink', :accesskey => 'F', :id => 'to_next_revision', :rel => 'nofollow'}) + - " (#{@revision.page.revisions.length - @revision_number} more) " + " (#{@revision.page.revisions.size - @revision_number} more) " else link_to('Forward in time', {:web => @web.address, :action => 'show', :id => @page.name}, {:class => 'navlink', :accesskey => 'F', :id => 'to_next_revision', :rel => 'nofollow'}) + @@ -62,9 +62,9 @@ module WikiHelper def back_for_page link_to('Back in time', {:web => @web.address, :action => 'revision', :id => @page.name, - :rev => @page.revisions.length - 1}, + :rev => @page.revisions.size - 1}, {:class => 'navlink', :accesskey => 'B', :id => 'to_previous_revision', :rel => 'nofollow'}) + - " (#{@page.revisions.length - 1} #{@page.revisions.length - 1 == 1 ? 'revision' : 'revisions'})" + " (#{@page.revisions.size - 1} #{@page.revisions.size - 1 == 1 ? 'revision' : 'revisions'})" end def current_revision diff --git a/app/views/wiki/history.html.erb b/app/views/wiki/history.html.erb index 8fb08e06..5db42f4d 100644 --- a/app/views/wiki/history.html.erb +++ b/app/views/wiki/history.html.erb @@ -7,7 +7,7 @@ <%- for rev in @revisions_by_day[day] -%>
  • <%= link_to_revision(rev.page, @revision_numbers[rev.id], - text= (rev.page.revisions.length == @revision_numbers[rev.id] ? + text= (rev.page.revisions.size == @revision_numbers[rev.id] ? "Current" : "Revision #{@revision_numbers[rev.id]}" ) ) %> diff --git a/app/views/wiki/web_list.rhtml b/app/views/wiki/web_list.rhtml index 4ba4a523..665be141 100644 --- a/app/views/wiki/web_list.rhtml +++ b/app/views/wiki/web_list.rhtml @@ -15,7 +15,7 @@ <%- end -%>