From 3a78ef3dbf09131d73cb3c6478b5f0192349d85d Mon Sep 17 00:00:00 2001 From: Jacques Distler Date: Sat, 6 Dec 2008 16:11:47 -0600 Subject: [PATCH] Delete Orphan Pages in Category If a Web has categories defined, you can delete orphaned pages in a given category (in addition to being able to delete all orphaned pages). --- app/controllers/admin_controller.rb | 17 +++++++++++++++++ app/controllers/web_sweeper.rb | 5 +++++ app/models/page_set.rb | 6 ++++++ app/models/wiki.rb | 6 ++++++ app/views/admin/edit_web.rhtml | 28 +++++++++++++++++++++++++--- config/routes.rb | 1 + 6 files changed, 60 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index 34c5231a..6a8d2b52 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -104,6 +104,23 @@ class AdminController < ApplicationController end end + def remove_orphaned_pages_in_category + unless (request.post? || ENV["RAILS_ENV"] == "test") + headers['Allow'] = 'POST' + render(:status => 405, :text => 'You must use an HTTP POST', :layout => 'error') + return + end + if wiki.authenticate(params['system_password_orphaned_in_category']) + category = params['category'] + wiki.remove_orphaned_pages_in_category(@web_name, category) + flash[:info] = "Orphaned pages in category \"#{category}\" removed" + redirect_to :controller => 'wiki', :web => @web_name, :action => 'list' + else + flash[:error] = password_error(params['system_password_orphaned_in_category']) + redirect_to :controller => 'admin', :web => @web_name, :action => 'edit_web' + end + end + def delete_web unless (request.post? || ENV["RAILS_ENV"] == "test") headers['Allow'] = 'POST' diff --git a/app/controllers/web_sweeper.rb b/app/controllers/web_sweeper.rb index b9731cd1..817d792b 100644 --- a/app/controllers/web_sweeper.rb +++ b/app/controllers/web_sweeper.rb @@ -15,4 +15,9 @@ class WebSweeper < ActionController::Caching::Sweeper def after_remove_orphaned_pages(web) expire_cached_summary_pages(web) end + + def after_remove_orphaned_pages_in_category(web) + expire_cached_summary_pages(web) + end + end diff --git a/app/models/page_set.rb b/app/models/page_set.rb index ac2c7caf..d22a6246 100644 --- a/app/models/page_set.rb +++ b/app/models/page_set.rb @@ -73,6 +73,12 @@ class PageSet < Array } end + def pages_in_category(category) + self.select { |page| + WikiReference.pages_in_category(web, category).map.include?(page.name) + } + end + # Returns all the wiki words in this page set for which # there are no pages in this page set's web def wanted_pages diff --git a/app/models/wiki.rb b/app/models/wiki.rb index e5a21d9e..35de15d5 100644 --- a/app/models/wiki.rb +++ b/app/models/wiki.rb @@ -54,6 +54,12 @@ class Wiki web.remove_pages(web.select.orphaned_pages) end + def remove_orphaned_pages_in_category(web_address,category) + web = Web.find_by_address(web_address) + pages_in_category = PageSet.new(web, web.select.pages_in_category(category)) + web.remove_pages(pages_in_category.orphaned_pages) + end + def revise_page(web_address, page_name, content, revised_at, author, renderer) page = read_page(web_address, page_name) page.revise(content, revised_at, author, renderer) diff --git a/app/views/admin/edit_web.rhtml b/app/views/admin/edit_web.rhtml index 393f1d1b..20630650 100644 --- a/app/views/admin/edit_web.rhtml +++ b/app/views/admin/edit_web.rhtml @@ -118,24 +118,46 @@ 'accept-charset' => 'utf-8' }) do %>

- Clean up by entering system password + Clean up this Web by entering the system password and

<% end %> +<% categories = WikiReference.list_categories(@web).sort + if categories.length > 0 %> +<% form_tag({:controller => 'admin', :web => @web.address, :action => 'remove_orphaned_pages_in_category'}, + { :id => 'remove_orphaned_pages_in_category', + :onsubmit => "return checkSystemPassword(document.getElementById('system_password_orphaned_in_category').value)", + 'accept-charset' => 'utf-8' }) do +%> +

+ Clean up selected category: + + Enter system password + + and + +

+<% end %> +<% end %> + +
<% form_tag({:controller => 'admin', :web => @web.address, :action => 'delete_web'}, { :id => 'delete_web', :onsubmit => "return checkSystemPassword(document.getElementById('system_password_delete_web').value)", 'accept-charset' => 'utf-8' }) do %> -

- Delete this Web, and all its pages. +

+ Delete this Web, and all its pages. Enter system password and

<% end %> +
<%= javascript_include_tag 'edit_web' %> diff --git a/config/routes.rb b/config/routes.rb index 6a70ede9..7a245e95 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,6 +16,7 @@ ActionController::Routing::Routes.draw do |map| connect_to_web map, ':web/edit_web', :controller => 'admin', :action => 'edit_web' connect_to_web map, ':web/remove_orphaned_pages', :controller => 'admin', :action => 'remove_orphaned_pages' + connect_to_web map, ':web/remove_orphaned_pages_in_category', :controller => 'admin', :action => 'remove_orphaned_pages_in_category' connect_to_web map, ':web/files/:id', :controller => 'file', :action => 'file', :requirements => {:id => /[-._\w]+/}, :id => nil connect_to_web map, ':web/import/:id', :controller => 'file', :action => 'import' connect_to_web map, ':web/login', :controller => 'wiki', :action => 'login'