-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Sitemap rake task to detect and fix issues
It is possible that, due to exceptions or other unexpected errors, the lft/rgt values and/or the page depth on the pages are not correct. In that case these tasks can help to detect and fix issues in the page sitemap.
- Loading branch information
1 parent
55914c5
commit 2058639
Showing
1 changed file
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
class SiteMapIssueFinder | ||
attr_reader :save | ||
attr_accessor :depth_issue_page_ids, :lft_rgt_issue_page_ids | ||
|
||
def initialize(save: false) | ||
@save = save | ||
@depth_issue_page_ids = Set.new | ||
@lft_rgt_issue_page_ids = Set.new | ||
end | ||
|
||
def depth_issues | ||
puts "\nFind Page Depth Issues" | ||
puts "======================\n" | ||
|
||
Alchemy::Page.each_with_level(Alchemy::Page.root.self_and_descendants) do |page, level| | ||
not_the_same_level = level != page.depth | ||
print not_the_same_level ? "F" : "." | ||
if not_the_same_level | ||
depth_issue_page_ids << page.id | ||
page.update_column(:depth, level) if save | ||
end | ||
end | ||
|
||
print_conclusion(depth_issue_page_ids, "depth") | ||
end | ||
|
||
def lft_rgt_issues | ||
puts "\nFind Page Left/Right Value Issues" | ||
puts "=================================\n" | ||
|
||
Alchemy::Page.roots.each do |root_page| | ||
traverse_children(root_page, root_page.lft) | ||
end | ||
|
||
print_conclusion(lft_rgt_issue_page_ids, "left/right value") | ||
end | ||
|
||
private | ||
|
||
def print_conclusion(page_ids, field_description) | ||
puts "\n\n" | ||
if page_ids.length > 0 | ||
if save | ||
puts "Found and corrected #{field_description} for #{page_ids.length} page(s)!" | ||
else | ||
puts "Following Pages have an incorrect #{field_description}: #{page_ids.join(", ")}" | ||
end | ||
else | ||
puts "All pages have the correct #{field_description}!\n" | ||
end | ||
puts "\n" | ||
end | ||
|
||
def fix_page_field(page, field, counter) | ||
wrong_counter = page[field] != counter | ||
print wrong_counter ? "F" : "." | ||
if wrong_counter | ||
lft_rgt_issue_page_ids << page.id | ||
page.update_column(field, counter) if save | ||
end | ||
end | ||
|
||
## | ||
# @param [Alchemy::Page] page | ||
# @param [Integer] counter | ||
# @return [Integer] | ||
def traverse_children(page, counter) | ||
page.children.order(lft: :asc).each do |child_page| | ||
counter += 1 | ||
fix_page_field(child_page, :lft, counter) | ||
counter = traverse_children(child_page, counter) unless child_page.leaf? | ||
|
||
counter += 1 | ||
fix_page_field(child_page, :rgt, counter) | ||
end | ||
|
||
counter | ||
end | ||
end | ||
|
||
namespace :alchemy do | ||
namespace :sitemap do | ||
desc "Get an overview over all sitemap issues" | ||
task anomalies: [:environment] do | ||
finder = SiteMapIssueFinder.new | ||
finder.lft_rgt_issues | ||
finder.depth_issues | ||
end | ||
|
||
desc "Fix nested set sitemap issues" | ||
task fix: [:environment] do | ||
finder = SiteMapIssueFinder.new(save: true) | ||
finder.lft_rgt_issues | ||
finder.depth_issues | ||
end | ||
end | ||
end |