<commit_before>use v6;

use Session;
use Cache;

class November does Session does Cache {

    use CGI;
    use Tags;
    use HTML::Template;
    use Dispatcher;
    use Utils;
    use Config;
    use November::Storage::File;
    use Text::Markup::Wiki::MediaWiki;

    has November::Storage $.storage;
    has CGI     $.cgi;
    has Config  $.config;

    # RAKUDO: BUILD not implemented yet
    method init {
        $!storage = November::Storage::File.new(
            storage_root => $.config.server_root ~ 'data/'
        );
        $!storage.init(); # Want ze BUILD submethod
    }

    method handle_request(CGI $cgi) {
        $!cgi = $cgi;

        my $d = Dispatcher.new( default => { self.error_page } );

        $d.add: [
            [''],                          { self.view_page },
            ['view', /^ ( \w | '%' | '-' )+ $/], { self.view_page(~$^page) },
            ['edit', /^ ( \w | '%' | '-' )+ $/], { self.edit_page(~$^page) },
            ['in'],                        { self.log_in },
            ['out'],                       { self.log_out },
            ['recent'],                    { self.list_recent_changes },
            ['all'],                       { self.list_all_pages },
        ];

        my @chunks = $cgi.uri.chunks.list;
        $d.dispatch(@chunks);
    }

    method view_page($page is rw='Main_Page') {
        $page .= subst('%20', '_', :g);

        unless $.storage.wiki_page_exists($page) {
            self.not_found($page);
            return;
        }

        # TODO: we need plugin system (see topics in mail-list)
        my $t = Tags.new;

        my $title = $page.trans( ['_'] => [' '] );

        my $content;
        my $cached_page = self.get-cache-entry( $page );
        if ( $cached_page ) {
            $content = $cached_page;
        }
        else {
            my $markup = $.config.markup;

            $content = $markup.format($.storage.read_page( $page ),
                         link_maker    => { self.make_link($^p, $^t) },
                         extlink_maker => { self.make_extlink($^p, $^t)
                         });

            self.set-cache-entry( $page, $content );
        }

        self.response( 'view.tmpl',
            {
            TITLE    => $title,
            PAGE     => $page,
            CONTENT  => $content,
            PAGETAGS => $t.page_tags($page),
            RECENTLY => self.get_changes( page => $page, :limit(8) ),

            TAGS     => $t.all_tags,
            }
        );

    }

    method edit_page($page is rw) {
        $page .= subst('%20', '_', :g);
        my $sessions = self.read_sessions();

        return self.not_authorized() unless self.logged_in();

        my $already_exists
                        = $.storage.wiki_page_exists($page);
        my $action      = $already_exists ?? 'Editing' !! 'Creating';
        my $old_content = $already_exists ?? $.storage.read_page($page) !! '';
        my $title = $action ~ ' ' ~ $page.trans( ['_'] => [' '] );

        # The 'edit' action handles both showing the form and accepting the
        # POST data. The difference is the presence of the 'articletext'
        # parameter -- if there is one, the action is considered a save.
        if $.cgi.params<articletext> || $.cgi.params<tags> {
            my $summary    = $.cgi.params<summary>;
            my $new_text   = $.cgi.params<articletext>;
            my $tags       = $.cgi.params<tags>;
            my $session_id = $.cgi.cookie<session_id>;
            my $author     = $sessions{$session_id}<user_name>;

            if $.cgi.params<preview> {
                # It's only a preview, should just send it back formatted
                return self.show_preview( $page, $summary, $new_text, $tags );
            }

            $.storage.save_page($page, $new_text, $author, $summary);
            self.remove-cache-entry( $page );

            # TODO: we need plugin system (see topics in mail-list)
            my $t = Tags.new();
            $t.update_tags($page, $tags);

            $.cgi.redirect('/view/' ~ $page );
            return;
        }

        # TODO: we need plugin system (see topics in mail-list)
        my $t = Tags.new;
        self.response( 'edit.tmpl',
            {
            PAGE     => $page,
            TITLE    => $title,
            CONTENT  => $old_content,
            PAGETAGS => $t.read_page_tags($page),
            }
        );
    }

    method show_preview( $page is rw, $summary, $new_text, $tags ) {
        $page .= subst('%20', '_', :g);
        my $title = $page.trans( ['_'] => [' '] );

        my $markup = $.config.markup;

        my $content = $markup.format($new_text,
                     link_maker    => { self.make_link($^p, $^t) },
                     extlink_maker => { self.make_extlink($^p, $^t)
                     });<commit_msg>Workaround for gh-16<commit_after>use v6;

use Session;
use Cache;

class November does Session does Cache {

    use CGI;
    use Tags;
    use HTML::Template;
    use Dispatcher;
    use Utils;
    use Config;
    use November::Storage::File;
    use Text::Markup::Wiki::MediaWiki;

    has November::Storage $.storage;
    has CGI     $.cgi;
    has Config  $.config;

    # RAKUDO: BUILD not implemented yet
    method init {
        $!storage = November::Storage::File.new(
            storage_root => $.config.server_root ~ 'data/'
        );
        $!storage.init(); # Want ze BUILD submethod
    }

    method handle_request(CGI $cgi) {
        $!cgi = $cgi;

        my $d = Dispatcher.new( default => { self.error_page } );

        $d.add: [
            [''],                          { self.view_page },
            ['view', /^ ( \w | '%' | '-' )+ $/], { self.view_page(~$^page) },
            ['edit', /^ ( \w | '%' | '-' )+ $/], { self.edit_page(~$^page) },
            ['in'],                        { self.log_in },
            ['out'],                       { self.log_out },
            ['recent'],                    { self.list_recent_changes },
            ['all'],                       { self.list_all_pages },
        ];

        my @chunks = $cgi.uri.chunks.list;
        $d.dispatch(@chunks);
    }

    method view_page($page is rw='Main_Page') {
        $page .= subst('%20', '_', :g);

        unless $.storage.wiki_page_exists($page) {
            self.not_found($page);
            return;
        }

        # TODO: we need plugin system (see topics in mail-list)
        my $t = Tags.new;

        my $title = $page.trans( ['_'] => [' '] );

        my $content;
        my $cached_page = self.get-cache-entry( $page );
        if ( $cached_page ) {
            $content = $cached_page;
        }
        else {
            my $markup = $.config.markup;

            $content = $markup.format(
                        # MediaWiki markup can't handle trailing spaces, gh-16
                        $.storage.read_page( $page ).subst(/[\s|\n]+$/, ''),
                        link_maker    => { self.make_link($^p, $^t) },
                        extlink_maker => { self.make_extlink($^p, $^t)}
            );

            self.set-cache-entry( $page, $content );
        }

        self.response( 'view.tmpl',
            {
            TITLE    => $title,
            PAGE     => $page,
            CONTENT  => $content,
            PAGETAGS => $t.page_tags($page),
            RECENTLY => self.get_changes( page => $page, :limit(8) ),

            TAGS     => $t.all_tags,
            }
        );

    }

    method edit_page($page is rw) {
        $page .= subst('%20', '_', :g);
        my $sessions = self.read_sessions();

        return self.not_authorized() unless self.logged_in();

        my $already_exists
                        = $.storage.wiki_page_exists($page);
        my $action      = $already_exists ?? 'Editing' !! 'Creating';
        my $old_content = $already_exists ?? $.storage.read_page($page) !! '';
        my $title = $action ~ ' ' ~ $page.trans( ['_'] => [' '] );

        # The 'edit' action handles both showing the form and accepting the
        # POST data. The difference is the presence of the 'articletext'
        # parameter -- if there is one, the action is considered a save.
        if $.cgi.params<articletext> || $.cgi.params<tags> {
            my $summary    = $.cgi.params<summary>;
            my $new_text   = $.cgi.params<articletext>;
            my $tags       = $.cgi.params<tags>;
            my $session_id = $.cgi.cookie<session_id>;
            my $author     = $sessions{$session_id}<user_name>;

            if $.cgi.params<preview> {
                # It's only a preview, should just send it back formatted
                return self.show_preview( $page, $summary, $new_text, $tags );
            }

            $.storage.save_page($page, $new_text, $author, $summary);
            self.remove-cache-entry( $page );

            # TODO: we need plugin system (see topics in mail-list)
            my $t = Tags.new();
            $t.update_tags($page, $tags);

            $.cgi.redirect('/view/' ~ $page );
            return;
        }

        # TODO: we need plugin system (see topics in mail-list)
        my $t = Tags.new;
        self.response( 'edit.tmpl',
            {
            PAGE     => $page,
            TITLE    => $title,
            CONTENT  => $old_content,
            PAGETAGS => $t.read_page_tags($page),
            }
        );
    }

    method show_preview( $page is rw, $summary, $new_text, $tags ) {
        $page .= subst('%20', '_', :g);
        my $title = $page.trans( ['_'] => [' '] );

        my $markup = $.config.markup;

        my $content = $markup.format(
                    # MediaWiki markup can't handle trailing spaces, gh-16
                    $new_text.subst(/[\s|\n]+$/, ''),
                    link_maker    => { self.make_link($^p, $^t) },
                    extlink_maker => { self.make_extlink($^p, $^t)}
        );
{'fraction_non_alphanumeric': 0.15579308258545932, 'fraction_numerical': 0.0022184128264596146, 'mean_word_length': 1.5301020408163266, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 59, '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': '1724867', 'n_tokens_mistral': 3139, 'n_tokens_neox': 2970, 'n_words': 663}