commit 7cc6dc1a4e55d82703bc959bb330fbd60002a00d
Author: vofp <vof@onid.orst.edu>
Date:   Wed Jun 5 06:18:09 2013 -0700

    Added Tags to wiki pages

diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb
index 96f9c95..a427f19 100644
--- a/app/controllers/wiki_controller.rb
+++ b/app/controllers/wiki_controller.rb
@@ -49,6 +49,11 @@ class WikiController < ApplicationController
     @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
   end
 
+  def tag_index
+    @tags = Tagging.all(:order => 'context',:conditions => ["taggable_type = 'Wiki'"])
+    @tags_by_context = @tags.group_by(&:context)
+  end
+
   # display a page (in editing mode if it doesn't exist)
   def show
     page_title = params[:id]
@@ -91,6 +96,11 @@ class WikiController < ApplicationController
     @content.text = initial_page_content(@page) if @content.text.blank?
     # don't keep previous comment
     @content.comments = nil
+    tags = Tagging.find_by_wiki_id(@page.id)
+    @tag_string = ""
+    tags.each{ |tag|
+      @tag_string += tag.context + ", " 
+    }
 
     # To prevent StaleObjectError exception when reverting to a previous version
     @content.lock_version = @page.content.lock_version
@@ -108,7 +118,7 @@ class WikiController < ApplicationController
     # don't keep previous comment
     @content.comments = nil
 
-    if !@page.new_record? && params[:content].present? && @content.text == params[:content][:text]
+    if !@page.new_record? && params[:content].present? && @content.text == params[:content][:text] && @tag_string == params[:tags][:tags]
       attachments = Attachment.attach_files(@page, params[:attachments])
       render_attachment_warning_if_needed(@page)
       # don't save if text wasn't changed
@@ -121,6 +131,31 @@ class WikiController < ApplicationController
     # if page is new @page.save will also save content, but not if page isn't a new record
     if (@page.new_record? ? @page.save : @content.save)
       attachments = Attachment.attach_files(@page, params[:attachments])
+      tag_change = false
+      added_tags = []
+      params[:tags][:tags].split(",").each{ |tag|
+        tag = tag.strip.downcase
+        next if tag == ''
+        t = Tagging.find_or_new_tag(tag,"Wiki",@page.id)
+        if t.new_record?
+          t.taggable_id = @page.id
+          t.tagger_id = User.current.id
+          t.tagger_type = "User"
+          t.taggable_type = "Wiki"
+          t.context = tag
+          t.save
+          tag_change = true
+        end
+        added_tags << tag
+      }
+      # Delete Tags that got removed. 
+      tags = Tagging.find_by_wiki_id(@page.id)
+      tags.each{ |tag|
+        if (!added_tags.include?(tag.context))
+          tag.delete
+          tag_change = true
+        end
+      }
       render_attachment_warning_if_needed(@page)
       call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
       redirect_to :action => 'show', :project_id => @project, :id => @page.title
@@ -128,6 +163,7 @@ class WikiController < ApplicationController
       render :action => 'edit'
     end
 
+
   rescue ActiveRecord::StaleObjectError
     # Optimistic locking exception
     flash.now[:error] = l(:notice_locking_conflict)
@@ -202,6 +238,10 @@ class WikiController < ApplicationController
         return
       end
     end
+    tags = Tagging.find_by_wiki_id(@page.id)
+    tags.each{ |tag|
+      tag.delete
+    }
     @page.destroy
     redirect_to :action => 'index', :project_id => @project
   end
diff --git a/app/models/tagging.rb b/app/models/tagging.rb
new file mode 100644
index 0000000..bb5e151
--- /dev/null
+++ b/app/models/tagging.rb
@@ -0,0 +1,11 @@
+
+class Tagging < ActiveRecord::Base
+	def self.find_or_new_tag(context, taggable_type, wiki_id)
+		tag = Tagging.first(:conditions => ["context = ? AND taggable_type = ? AND taggable_id = ?", context, taggable_type, wiki_id])
+		tag || Tagging.new()
+	end
+
+	def self.find_by_wiki_id(wiki_id)
+		Tagging.find(:all, :conditions => ["taggable_id = ? AND taggable_type = 'Wiki'", wiki_id])
+	end
+end
\ No newline at end of file
diff --git a/app/views/wiki/_sidebar.rhtml b/app/views/wiki/_sidebar.rhtml
index 9a24963..0b9002e 100644
--- a/app/views/wiki/_sidebar.rhtml
+++ b/app/views/wiki/_sidebar.rhtml
@@ -7,3 +7,4 @@
 <%= link_to l(:field_start_page), {:action => 'show', :id => nil} %><br />
 <%= link_to l(:label_index_by_title), {:action => 'index'} %><br />
 <%= link_to l(:label_index_by_date), {:action => 'date_index'} %><br />
+<%= link_to l(:label_index_by_tag), {:action => 'tag_index'} %><br />
diff --git a/app/views/wiki/edit.rhtml b/app/views/wiki/edit.rhtml
index d858dcf..392c6dc 100644
--- a/app/views/wiki/edit.rhtml
+++ b/app/views/wiki/edit.rhtml
@@ -6,6 +6,7 @@
 
 <p><%= f.text_area :text, :cols => 100, :rows => 25, :class => 'wiki-edit', :accesskey => accesskey(:edit) %></p>
 <p><label><%= l(:field_comments) %></label><br /><%= f.text_field :comments, :size => 120 %></p>
+<p><label><%= l(:label_tag) %></label><br /><%= text_field :tags, :tags, :value => @tag_string , :size => 120 %></p>
 <p><label><%=l(:label_attachment_plural)%></label><br /><%= render :partial => 'attachments/form' %></p>
 
 <p><%= submit_tag l(:button_save) %>
diff --git a/app/views/wiki/tag_index.html.erb b/app/views/wiki/tag_index.html.erb
new file mode 100644
index 0000000..c450753
--- /dev/null
+++ b/app/views/wiki/tag_index.html.erb
@@ -0,0 +1,34 @@
+<div class="contextual">
+  <%= watcher_link(@wiki, User.current) %>
+</div>
+
+<h2><%= l(:label_index_by_tag) %></h2>
+
+<% if @tags.empty? %>
+<p class="nodata"><%= l(:label_no_data) %></p>
+<% end %>
+
+<% @tags_by_context.keys.sort.each do |context| %>
+<h3><%= context %></h3>
+<ul>
+<% @tags_by_context[context].each do |tag| %>
+	<% page = WikiPage.find(tag.taggable_id) %>
+    <li><%= link_to h(page.pretty_title), :action => 'show', :id => page.title, :project_id => page.project %></li>
+<% end %>
+</ul>
+<% end %>
+
+<% content_for :sidebar do %>
+  <%= render :partial => 'sidebar' %>
+<% end %>
+
+<% unless @tags.empty? %>
+<% other_formats_links do |f| %>
+	<%= f.link_to 'Atom', :url => {:controller => 'activities', :action => 'index', :id => @project, :show_wiki_edits => 1, :key => User.current.rss_key} %>
+	<%= f.link_to('HTML', :url => {:action => 'export'}) if User.current.allowed_to?(:export_wiki_pages, @project) %>
+<% end %>
+<% end %>
+
+<% content_for :header_tags do %>
+<%= auto_discovery_link_tag(:atom, :controller => 'activities', :action => 'index', :id => @project, :show_wiki_edits => 1, :format => 'atom', :key => User.current.rss_key) %>
+<% end %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 22e7775..7c9cb0e 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -674,6 +674,7 @@ en:
   label_wiki_page_plural: Wiki pages
   label_index_by_title: Index by title
   label_index_by_date: Index by date
+  label_index_by_tag: Index by tag
   label_current_version: Current version
   label_preview: Preview
   label_feed_plural: Feeds
diff --git a/config/routes.rb b/config/routes.rb
index 5e6716c..8a58897 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -176,7 +176,8 @@ ActionController::Routing::Routes.draw do |map|
       :add_attachment => :post
     }, :collection => {
       :export => :get,
-      :date_index => :get
+      :date_index => :get,
+      :tag_index => :get
     }
 
   end
diff --git a/lib/redmine.rb b/lib/redmine.rb
index 8ddf66a..1429820 100644
--- a/lib/redmine.rb
+++ b/lib/redmine.rb
@@ -130,7 +130,7 @@ Redmine::AccessControl.map do |map|
     map.permission :manage_wiki, {:wikis => [:edit, :destroy]}, :require => :member
     map.permission :rename_wiki_pages, {:wiki => :rename}, :require => :member
     map.permission :delete_wiki_pages, {:wiki => :destroy}, :require => :member
-    map.permission :view_wiki_pages, :wiki => [:index, :show, :special, :date_index]
+    map.permission :view_wiki_pages, :wiki => [:index, :show, :special, :date_index, :tag_index]
     map.permission :export_wiki_pages, :wiki => [:export]
     map.permission :view_wiki_edits, :wiki => [:history, :diff, :annotate]
     map.permission :edit_wiki_pages, :wiki => [:edit, :update, :preview, :add_attachment]
@@ -337,6 +337,12 @@ Redmine::MenuManager.map :project_menu do |menu|
               :param => :project_id,
               :if => Proc.new { |p| p.wiki && !p.wiki.new_record? }
             })
+  menu.push(:wiki_by_tag, { :controller => 'wiki', :action => 'tag_index'}, {
+              :caption => :label_index_by_tag,
+              :parent => :wiki,
+              :param => :project_id,
+              :if => Proc.new { |p| p.wiki && !p.wiki.new_record? }
+            })
   menu.push(:boards, { :controller => 'boards', :action => 'index', :id => nil }, {
               :param => :project_id,
               :caption => :label_board_plural,
