From d9a3fcabf23eeabc78c30732e57e346b87585aff Mon Sep 17 00:00:00 2001 From: Abishek Das Date: Wed, 1 Nov 2023 19:01:00 +0530 Subject: [PATCH] refactor: Utilize existing Category record using WikiDiscouragedArticleManager The WikiDiscouragedArticleManager has been created to leverage the existing Category table for storing and updating Wiki Edu discouraged articles. This change eliminates the need for creating a new record type in the database, ensuring the utilization of the pre-existing Category record for managing discouraged articles. The updates will occur through the DailyUpdate mechanism. --- lib/wiki_discouraged_article_manager.rb | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 lib/wiki_discouraged_article_manager.rb diff --git a/lib/wiki_discouraged_article_manager.rb b/lib/wiki_discouraged_article_manager.rb new file mode 100644 index 00000000000..d758627db98 --- /dev/null +++ b/lib/wiki_discouraged_article_manager.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true +require_dependency "#{Rails.root}/lib/importers/category_importer" + +class WikiDiscouragedArticleManager + def retrieve_wiki_edu_discouraged_articles + category_titles = [ENV['blocked_assignment_category'], + ENV['warning_assignment_category']] + + category_titles.each do |category_title| + discouraged_articles = CategoryImporter.new(Wiki.default_wiki) + .page_titles_for_category(category_title) + update_or_save_discouraged_articles(discouraged_articles, category_title) + end + end + + private + + def update_or_save_discouraged_articles(discouraged_articles, category_title) + existing_article = Category.find_by(source: category_title) + + if existing_article + # Update the existing article + existing_article.update(article_titles: discouraged_articles) + else + # Create a new entry in the database + article_details = { + article_titles: discouraged_articles, + name: 'Wiki_Edu_Discouraged_Articles', + source: category_title, + wiki_id: en_wiki.id + } + Category.create(article_details) + end + end + + def en_wiki + Wiki.find_by(language: 'en', project: 'wikipedia') + end +end