<commit_before># encoding: utf-8

class Admin::PagesController < Admin::AdminController

  before_action :require_news_pages, only: [:news]
  before_action :find_page, only: [
    :show, :edit, :preview, :update, :destroy, :reorder,
    :delete_comment,
    :import_xml
  ]
  before_action :find_categories
  before_action :find_news_pages, only: [:news, :new_news]

  def index
    @root_pages = Page.roots.in_locale(@locale).visible
    respond_to do |format|
      format.html
      format.xml do
        render xml: @root_pages.to_xml(pages: true)
      end
    end
  end

  def news
    @archive_finder = Page.where(parent_page_id: @news_pages)
                          .visible
                          .order('published_at DESC')
                          .in_locale(@locale)
                          .archive_finder

    if params[:year] && params[:month]
      @year, @month = params[:year].to_i, params[:month].to_i
    else
      @year, @month = @archive_finder.latest_year_and_month
    end

    @pages = @archive_finder.by_year_and_month(@year, @month)
  end

  def reorder_pages
    pages = params[:ids].map{|id| Page.find(id)}
    PagesCore::CacheSweeper.once do
      pages.each_with_index do |page, index|
        page.update(position: (index + 1))
      end
    end
    if request.xhr?
      render :text => 'ok'
    end
  end

  def show
    edit
    render :action => :edit
  end

  def import_xml
    if request.post? && params[:xmlfile]
      @created_page = PagesCore::Serializations::PageXmlImporter.new(@page, params[:xmlfile].read).import!
    end
  end<commit_msg>Don't crash on the News tab if pages haven't been added yet.<commit_after># encoding: utf-8

class Admin::PagesController < Admin::AdminController

  before_action :require_news_pages, only: [:news]
  before_action :find_page, only: [
    :show, :edit, :preview, :update, :destroy, :reorder,
    :delete_comment,
    :import_xml
  ]
  before_action :find_categories
  before_action :find_news_pages, only: [:news, :new_news]

  def index
    @root_pages = Page.roots.in_locale(@locale).visible
    respond_to do |format|
      format.html
      format.xml do
        render xml: @root_pages.to_xml(pages: true)
      end
    end
  end

  def news
    @archive_finder = Page.where(parent_page_id: @news_pages)
                          .visible
                          .order('published_at DESC')
                          .in_locale(@locale)
                          .archive_finder

    if params[:year] && params[:month]
      @year, @month = params[:year].to_i, params[:month].to_i
    else
      @year, @month = @archive_finder.latest_year_and_month
      unless @year && @month
        @year, @month = Time.now.year, Time.now.month
      end
    end

    @pages = @archive_finder.by_year_and_month(@year, @month)
  end

  def reorder_pages
    pages = params[:ids].map{|id| Page.find(id)}
    PagesCore::CacheSweeper.once do
      pages.each_with_index do |page, index|
        page.update(position: (index + 1))
      end
    end
    if request.xhr?
      render :text => 'ok'
    end
  end

  def show
    edit
    render :action => :edit
  end

  def import_xml
    if request.post? && params[:xmlfile]
      @created_page = PagesCore::Serializations::PageXmlImporter.new(@page, params[:xmlfile].read).import!
    end
  end
{'fraction_non_alphanumeric': 0.1123798076923077, 'fraction_numerical': 0.001201923076923077, 'mean_word_length': 2.560427807486631, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5513731', 'n_tokens_mistral': 1092, 'n_tokens_neox': 1063, 'n_words': 240}