dtext links: allow searching for forum posts linking to specific tag.

This commit is contained in:
evazion
2019-10-27 01:05:54 -05:00
parent d946a84480
commit d617b20b49
7 changed files with 55 additions and 20 deletions

View File

@@ -3,7 +3,10 @@ class DtextLink < ApplicationRecord
enum link_type: [:wiki_link, :external_link]
before_validation :normalize_link_target
validates :link_target, uniqueness: { scope: [:model_type, :model_id] }
# validates :link_target, uniqueness: { scope: [:model_type, :model_id] }
scope :wiki_page, -> { where(model_type: "WikiPage") }
scope :forum_post, -> { where(model_type: "ForumPost") }
def self.new_from_dtext(dtext)
links = []
@@ -21,7 +24,7 @@ class DtextLink < ApplicationRecord
def self.model_matches(params)
return all if params.blank?
where(model_id: WikiPage.search(params).reorder(nil))
where(model_type: "WikiPage", model_id: WikiPage.search(params).reorder(nil))
end
def self.linked_wiki_exists(exists = true)
@@ -63,5 +66,9 @@ class DtextLink < ApplicationRecord
if wiki_link?
self.link_target = WikiPage.normalize_title(link_target)
end
# postgres will raise an error if the link is more than 2712 bytes long
# because it can't index values that take up more than 1/3 of an 8kb page.
self.link_target = self.link_target.truncate(2048, omission: "")
end
end

View File

@@ -5,11 +5,14 @@ class ForumPost < ApplicationRecord
belongs_to_creator
belongs_to_updater
belongs_to :topic, :class_name => "ForumTopic"
has_many :dtext_links, as: :model, dependent: :destroy
has_many :votes, class_name: "ForumPostVote"
has_one :tag_alias
has_one :tag_implication
has_one :bulk_update_request
before_validation :initialize_is_deleted, :on => :create
before_save :update_dtext_links, if: :dtext_links_changed?
after_create :update_topic_updated_at_on_create
after_update :update_topic_updated_at_on_update_for_original_posts
after_destroy :update_topic_updated_at_on_destroy
@@ -50,6 +53,10 @@ class ForumPost < ApplicationRecord
q = q.search_attributes(params, :creator, :updater, :topic_id, :is_deleted, :body)
q = q.text_attribute_matches(:body, params[:body_matches], index_column: :text_index)
if params[:linked_to].present?
q = q.where(id: DtextLink.forum_post.wiki_link.where(link_target: params[:linked_to]).select(:model_id))
end
if params[:topic_title_matches].present?
q = q.topic_title_matches(params[:topic_title_matches])
end
@@ -141,6 +148,14 @@ class ForumPost < ApplicationRecord
update_topic_updated_at_on_undelete
end
def dtext_links_changed?
body_changed? && DText.dtext_links_differ?(body, body_was)
end
def update_dtext_links
self.dtext_links = DtextLink.new_from_dtext(body)
end
def update_topic_updated_at_on_delete
max = ForumPost.where(:topic_id => topic.id, :is_deleted => false).order("updated_at desc").first
if max

View File

@@ -77,7 +77,7 @@ class WikiPage < ApplicationRecord
end
if params[:linked_to].present?
q = q.where(id: DtextLink.wiki_link.where(link_target: params[:linked_to]).select(:model_id))
q = q.where(id: DtextLink.wiki_page.wiki_link.where(link_target: params[:linked_to]).select(:model_id))
end
if params[:hide_deleted].to_s.truthy?

View File

@@ -1,15 +1,8 @@
<div id="c-dtext-links">
<div id="a-index">
<%= search_form_for(dtext_links_path) do |f| %>
<%= f.simple_fields_for :model do |fa| %>
<%= fa.input :title, label: "Wiki", hint: "Use * for wildcard", input_html: { value: params.dig(:search, :model, :title), "data-autocomplete": "wiki-page" } %>
<% end %>
<%= f.input :link_target_ilike, label: "Link", hint: "Use * for wildcard", input_html: { value: params[:search][:link_target_ilike], data: { autocomplete: "wiki-page" } } %>
<%= f.simple_fields_for :model do |fm| %>
<%= fm.simple_fields_for :tag do |ft| %>
<%= ft.input :category, label: "Wiki Type", collection: TagCategory.canonical_mapping.to_a, include_blank: true, selected: params.dig(:search, :model, :tag, :category) %>
<% end %>
<% end %>
<%= f.input :model_type, label: "Page Type", collection: [["Wiki Page", "WikiPage"], ["Forum Post", "ForumPost"]], include_blank: true, selected: params[:search][:model_type] %>
<%= f.input :link_type, label: "Link Type", collection: [["Wiki", "0"], ["External", "1"]], include_blank: true, selected: params[:search][:link_type] %>
<%= f.input :linked_wiki_exists, label: "Wiki Exists?", collection: ["Yes", "No"], include_blank: true, selected: params[:search][:linked_wiki_exists] %>
<%= f.input :linked_tag_exists, label: "Tag Exists?", collection: ["Yes", "No"], include_blank: true, selected: params[:search][:linked_tag_exists] %>
@@ -19,7 +12,7 @@
<table class="striped autofit">
<thead>
<tr>
<th>Wiki</th>
<th>Page</th>
<th>Link</th>
<th>Type</th>
</tr>
@@ -27,10 +20,17 @@
<tbody>
<% @dtext_links.each do |dtext_link| %>
<tr>
<td class="category-<%= Tag.category_for(dtext_link.model.title) %>">
<%= link_to(dtext_link.model.title, dtext_link.model) %>
<%= link_to("»", dtext_links_path(search: { model: { title: dtext_link.model.title }})) %>
</td>
<% if dtext_link.model_type == "WikiPage" %>
<td class="category-<%= Tag.category_for(dtext_link.model.title) %>">
<%= link_to(dtext_link.model.title, dtext_link.model) %>
<%= link_to("»", dtext_links_path(search: { model_type: "WikiPage", model: { title: dtext_link.model.title }})) %>
</td>
<% elsif dtext_link.model_type == "ForumPost" %>
<td>
<%= link_to("forum ##{dtext_link.model_id}", dtext_link.model) %>
</td>
<% end %>
<td class="col-expand">
<% if dtext_link.external_link? %>
<%= external_link_to(dtext_link.link_target) %>

View File

@@ -6,6 +6,7 @@
<%= f.input :topic_title_matches, label: "Title" %>
<%= f.input :body_matches, label: "Body" %>
<%= f.input :creator_name, label: "Creator", input_html: { "data-autocomplete": "user" } %>
<%= f.input :linked_to, label: "Tag", hint: "Find posts mentioning a tag", input_html: { "data-autocomplete": "tag" } %>
<%= f.input :topic_category_id, label: "Category", collection: ForumTopic::CATEGORIES.invert.to_a, include_blank: true %>
<%= f.submit "Search" %>
<% end %>

View File

@@ -5,11 +5,12 @@
<% if @wiki_page.present? %>
<h4>Options</h4>
<ul class="list-bulleted">
<ul>
<% unless @wiki_page.is_meta_wiki? %>
<li><%= link_to "Tag History", post_versions_path(search: { changed_tags: @wiki_page.title }) %></li>
<% end %>
<li><%= link_to "Wiki History", wiki_page_versions_path(search: { wiki_page_id: @wiki_page.id }) %></li>
<li><%= link_to "Discussions", forum_posts_path(search: { linked_to: @wiki_page.title }) %></li>
<li><%= link_to "What Links Here", wiki_pages_path(search: { linked_to: @wiki_page.title }) %></li>
</ul>
<% end %>

View File

@@ -7,17 +7,28 @@ require_relative "../../config/environment"
# wiki_page.save!(touch: false, validate: false)
# end
def reindex
def reindex_wiki_pages
WikiPage.find_in_batches(batch_size: 500) do |wiki_pages|
WikiPage.transaction do
wiki_pages.each do |wiki_page|
DtextLink.new_from_dtext(wiki_page.body).each do |link|
link.model = wiki_page
link.save!(touch: false, validate: false)
link.save!
end
end
end
end
end
reindex
def reindex_forum_posts
ForumPost.find_in_batches(batch_size: 500) do |forum_posts|
ForumPost.transaction do
forum_posts.each do |forum_post|
DtextLink.new_from_dtext(forum_post.body).each do |link|
link.model = forum_post
link.save!
end
end
end
end
end