<commit_before><?php

namespace Coyote\Console\Commands;

use Coyote\Services\Markdown\Transformer;
use Illuminate\Console\Command;
use DB;

ini_set('memory_limit', '1G');

class Markdown extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'coyote:markdown {--model=} {--id=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Coyote markdown converter';

    /**
     * @var Transformer
     */
    private $transformer;

    public function handle()
    {
        $this->transformer = new Transformer();

        $model = $this->option('model');
        $id = $this->option('id');

        try {
            $this->$model($id);
        } catch (\Exception $e) {
            $this->error(sprintf('[%d] [%s] %s', $e->getLine(), $e->getFile(), $e->getMessage()));
        }
    }

    private function microblog($id = null)
    {
        $result = DB::connection('mysql')
            ->table('microblog')
            ->when($id, function ($builder) use ($id) {
                return $builder->where('microblog_id', $id);
            });

        $result = $result->get(['microblog_id', 'microblog_text']);
        $bar = $this->output->createProgressBar(count($result));

        foreach ($result as $row) {
            DB::table('microblogs')
                ->where('id', $row->microblog_id)
                ->update(['text' => $this->transformer->transform($row->microblog_text)]);

            $bar->advance();
        }

        $bar->finish();
    }

    private function post($id = null)
    {
        $count = DB::connection('mysql')->table('post')->count();
        $bar = $this->output->createProgressBar($count);

        $sql = DB::connection('mysql')
            ->table('post')
            ->select(['post.post_id', 'text_content AS post_content'])
            ->join('post_text', 'text_id', '=', 'post_text')
            ->orderBy('post_id', 'DESC')
            ->when($id, function ($builder) use ($id) {
                return $builder->where('post_id', $id);
            });

        $this->transformer->quote = DB::connection('mysql')
            ->table('post')
            ->select(DB::raw('post_id, IF(user_id > 1, user_name, post_username) AS name'))
            ->leftJoin('user', 'user_id', '=', 'post_user')
            ->lists('name', 'post_id');

        $sql->chunk(50000, function ($sql) use ($bar) {
            foreach ($sql as $row) {
                DB::table('posts')
                    ->where('id', $row->post_id)
                    ->update(['text' => $this->transformer->transform($row->post_content)]);

                $bar->advance();
            }
        });

        $bar->finish();
    }

    private function postComment($id)
    {
        $count = DB::connection('mysql')->table('post_comment')->count();
        $bar = $this->output->createProgressBar($count);

        DB::connection('mysql')
            ->table('post_comment')
            ->select(['comment_id', 'comment_text'])
            ->when($id, function ($builder) use ($id) {
                return $builder->where('comment_id', $id);
            })
            ->orderBy('comment_id', 'DESC')
            ->chunk(
                100000,
                function ($sql) use ($bar) {
                    foreach ($sql as $row) {
                        DB::table('post_comments')
                            ->where('id', $row->comment_id)
                            ->update(['text' => $this->transformer->transform($row->comment_text)]);

                        $bar->advance();
                    }
                }
            );<commit_msg>formatowanie<commit_after><?php

namespace Coyote\Console\Commands;

use Coyote\Services\Markdown\Transformer;
use Illuminate\Console\Command;
use DB;

ini_set('memory_limit', '1G');

class Markdown extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'coyote:markdown {--model=} {--id=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Coyote markdown converter';

    /**
     * @var Transformer
     */
    private $transformer;

    public function handle()
    {
        $this->transformer = new Transformer();

        $model = $this->option('model');
        $id = $this->option('id');

        try {
            $this->$model($id);
        } catch (\Exception $e) {
            $this->error(sprintf('[%d] [%s] %s', $e->getLine(), $e->getFile(), $e->getMessage()));
        }
    }

    private function microblog($id = null)
    {
        $result = DB::connection('mysql')
            ->table('microblog')
            ->when($id, function ($builder) use ($id) {
                return $builder->where('microblog_id', $id);
            });

        $result = $result->get(['microblog_id', 'microblog_text']);
        $bar = $this->output->createProgressBar(count($result));

        foreach ($result as $row) {
            DB::table('microblogs')
                ->where('id', $row->microblog_id)
                ->update(['text' => $this->transformer->transform($row->microblog_text)]);

            $bar->advance();
        }

        $bar->finish();
    }

    private function post($id = null)
    {
        $count = DB::connection('mysql')->table('post')->count();
        $bar = $this->output->createProgressBar($count);

        $this->transformer->quote = DB::connection('mysql')
            ->table('post')
            ->select(DB::raw('post_id, IF(user_id > 1, user_name, post_username) AS name'))
            ->leftJoin('user', 'user_id', '=', 'post_user')
            ->lists('name', 'post_id');

        DB::connection('mysql')
            ->table('post')
            ->select(['post.post_id', 'text_content AS post_content'])
            ->join('post_text', 'text_id', '=', 'post_text')
            ->orderBy('post_id', 'DESC')
            ->when($id, function ($builder) use ($id) {
                return $builder->where('post_id', $id);
            })
            ->chunk(
                50000,
                function ($sql) use ($bar) {
                    foreach ($sql as $row) {
                        DB::table('posts')
                            ->where('id', $row->post_id)
                            ->update(['text' => $this->transformer->transform($row->post_content)]);

                        $bar->advance();
                    }
                }
            );

        $bar->finish();
    }

    private function postComment($id)
    {
        $count = DB::connection('mysql')->table('post_comment')->count();
        $bar = $this->output->createProgressBar($count);

        DB::connection('mysql')
            ->table('post_comment')
            ->select(['comment_id', 'comment_text'])
            ->when($id, function ($builder) use ($id) {
                return $builder->where('comment_id', $id);
            })
            ->orderBy('comment_id', 'DESC')
            ->chunk(
                100000,
                function ($sql) use ($bar) {
                    foreach ($sql as $row) {
                        DB::table('post_comments')
                            ->where('id', $row->comment_id)
                            ->update(['text' => $this->transformer->transform($row->comment_text)]);

                        $bar->advance();
                    }
                }
            );
{'fraction_non_alphanumeric': 0.18022328548644337, 'fraction_numerical': 0.0034556087187666137, 'mean_word_length': 1.7543923865300146, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 139, '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': '2210190', 'n_tokens_mistral': 2184, 'n_tokens_neox': 2084, 'n_words': 386}