<commit_before>class WikiPageController < BasePageController
  append_before_filter :fetch_wiki

  ##
  ## ACCESS: no restriction
  ##

  def create
    @page_class = WikiPage
    if params[:cancel]
      return redirect_to(create_page_url(nil, :group => params[:group]))
    elsif request.post?
      begin
        @page = create_new_page!(@page_class)
        @page.update_attribute(:data, Wiki.create(:user => current_user))
        return redirect_to(page_url(@page, :action => 'edit'))
      rescue Exception => exc
        @page = exc.record
        flash_message_now :exception => exc
      end
    end
    render :template => 'base_page/create'
  end

  ##
  ## ACCESS: public or :view
  ##

  def show
    if @wiki.body.empty?
      redirect_to page_url(@page,:action=>'edit')
    elsif @upart and !@upart.viewed? and @wiki.version > 1
      @last_seen = @wiki.first_since( @upart.viewed_at )
    end
  end

  def version
    @version = @wiki.versions.find_by_version(params[:id])
  end

  def versions
  end
  
  def diff
    old_id, new_id = params[:id].split('-')
    @old = @wiki.versions.find_by_version(old_id)
    @new = @wiki.versions.find_by_version(new_id)
    @old_markup = @old.body_html || ''
    @new_markup = @new.body_html || ''
    @difftext = HTMLDiff.diff( @old_markup , @new_markup)

    # output diff html only for ajax requests
    render :text => @difftext if request.xhr?
  end

  def print
    render :layout => "printer-friendly"
  end

  ##
  ## ACCESS: :edit
  ##

 def edit
    @stylesheet = 'wiki_edit'
    @javascript = 'wiki_edit'
    if params[:cancel]
      cancel
    elsif params[:break_lock]
      @wiki.unlock
      lock
      @wiki.body = params[:wiki][:body]
    elsif request.post? and params[:save]
      save
    elsif request.get?
      lock
    end
  end

  # TODO: make post only    
  def break_lock
    @wiki.unlock
    redirect_to page_url(@page, :action => 'edit')
  end
  
  # xhr only
  def show_image_popup
    @images = Asset.visible_to(current_user, @page.group).media_type(:image).most_recent.find(:all, :limit=>20)
    render(:update) do |page| 
      page.replace 'image_popup', :partial => 'image_popup'
    end
  end

  # upload image via xhr
  # response goes to an iframe, so requires responds_to_parent
  def upload
    debugger
    asset = Asset.build params[:asset]
    asset.parent_page = @page
    asset.save
    @images = Asset.visible_to(current_user, @page.group).media_type(:image).most_recent.find(:all, :limit=>20)
    responds_to_parent do<commit_msg>remove debugging from wiki controller<commit_after>class WikiPageController < BasePageController
  append_before_filter :fetch_wiki

  ##
  ## ACCESS: no restriction
  ##

  def create
    @page_class = WikiPage
    if params[:cancel]
      return redirect_to(create_page_url(nil, :group => params[:group]))
    elsif request.post?
      begin
        @page = create_new_page!(@page_class)
        @page.update_attribute(:data, Wiki.create(:user => current_user))
        return redirect_to(page_url(@page, :action => 'edit'))
      rescue Exception => exc
        @page = exc.record
        flash_message_now :exception => exc
      end
    end
    render :template => 'base_page/create'
  end

  ##
  ## ACCESS: public or :view
  ##

  def show
    if @wiki.body.empty?
      redirect_to page_url(@page,:action=>'edit')
    elsif @upart and !@upart.viewed? and @wiki.version > 1
      @last_seen = @wiki.first_since( @upart.viewed_at )
    end
  end

  def version
    @version = @wiki.versions.find_by_version(params[:id])
  end

  def versions
  end
  
  def diff
    old_id, new_id = params[:id].split('-')
    @old = @wiki.versions.find_by_version(old_id)
    @new = @wiki.versions.find_by_version(new_id)
    @old_markup = @old.body_html || ''
    @new_markup = @new.body_html || ''
    @difftext = HTMLDiff.diff( @old_markup , @new_markup)

    # output diff html only for ajax requests
    render :text => @difftext if request.xhr?
  end

  def print
    render :layout => "printer-friendly"
  end

  ##
  ## ACCESS: :edit
  ##

 def edit
    @stylesheet = 'wiki_edit'
    @javascript = 'wiki_edit'
    if params[:cancel]
      cancel
    elsif params[:break_lock]
      @wiki.unlock
      lock
      @wiki.body = params[:wiki][:body]
    elsif request.post? and params[:save]
      save
    elsif request.get?
      lock
    end
  end

  # TODO: make post only    
  def break_lock
    @wiki.unlock
    redirect_to page_url(@page, :action => 'edit')
  end
  
  # xhr only
  def show_image_popup
    @images = Asset.visible_to(current_user, @page.group).media_type(:image).most_recent.find(:all, :limit=>20)
    render(:update) do |page| 
      page.replace 'image_popup', :partial => 'image_popup'
    end
  end

  # upload image via xhr
  # response goes to an iframe, so requires responds_to_parent
  def upload
    asset = Asset.build params[:asset]
    asset.parent_page = @page
    asset.save
    @images = Asset.visible_to(current_user, @page.group).media_type(:image).most_recent.find(:all, :limit=>20)
    responds_to_parent do
{'fraction_non_alphanumeric': 0.11970860405591652, 'fraction_numerical': 0.0019688915140775743, 'mean_word_length': 3.0, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3431862', 'n_tokens_mistral': 1749, 'n_tokens_neox': 1694, 'n_words': 388}