<commit_before><?php
namespace beatrix\commands;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;


class PageCommand extends Command
{
    protected function configure()
    {
        $this->setName('page')->setDescription('Creates site page');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // defining directories and url
        $curDir = getcwd();
        $docRoot = $this->detectWebRoot();

        /** @var DialogHelper $dialog */
        $dialog = $this->getHelper('dialog');
        $dirname = $dialog->askAndValidate($output, "Enter dir name: ", function ($name) use ($curDir) {
            $name = trim($name);
            if (empty($name)) {
                throw new \InvalidArgumentException("Empty input");
            }
            if (file_exists($curDir . '/' . $name)) {
                throw new \InvalidArgumentException("File {$curDir}/{$name} already exist");
            }
            return $name;
        });

        $title = $dialog->ask($output, "Enter page title: ", "New page");

        $titleEscaped = addslashes($title);

        $targetDir = $curDir . "/" . $dirname;
        $targetFile = $targetDir . "/index.php";

        $url = '/' . trim($dirname, '/\\') . '/';

        if (!file_exists($targetDir)) {
            mkdir($targetDir, 0777, true);
        }
        if (file_exists($targetFile)) {
            $overwrite = $dialog->ask($output, "file [$targetFile] already exists. Overwrite? [y/n] ");
            if (!strcasecmp($overwrite, "y")) {
                exit("Operation cancelled");
            }
        }

        // save index page
        $indexContents = <<<PHP
	<?php
	require(\$_SERVER["DOCUMENT_ROOT"] . "/bitrix/header.php");
	\$APPLICATION->SetTitle("{$titleEscaped}");?>
	<p>Страница {$title}</p>
	<?php require(\$_SERVER["DOCUMENT_ROOT"] . "/bitrix/footer.php"); ?>
PHP;

        $indexWritten = file_put_contents($targetFile, $indexContents);
        $sectionContents = '<?php $sSectionName="' . $titleEscaped . '"; ?>';
        $sectionWritten = file_put_contents($targetDir . '/.section.php', $sectionContents);
        if ($indexWritten === false) {
            $output->writeln("<error>Failed writing index file to [$targetFile]</error>");
        }

        $createMenu = $dialog->ask($output, "Add to menu? [y|n]:", 'n');
        if ($createMenu != 'y') {
            exit("Menu won't be created");
        }
        $menuDir = realpath($targetDir . '/..');

        // search existing or use default menus
        $menuList = array(
            '.top.menu.php',
            '.left.menu.php',
        );
        $menus = glob($menuDir . '/.*.menu.php');
        if (count($menus)) {<commit_msg>Fix page generation template<commit_after><?php
namespace beatrix\commands;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;


class PageCommand extends Command
{
    protected function configure()
    {
        $this->setName('page')->setDescription('Creates site page');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // defining directories and url
        $curDir = getcwd();
        $docRoot = $this->detectWebRoot();

        /** @var DialogHelper $dialog */
        $dialog = $this->getHelper('dialog');
        $dirname = $dialog->askAndValidate($output, "Enter dir name: ", function ($name) use ($curDir) {
            $name = trim($name);
            if (empty($name)) {
                throw new \InvalidArgumentException("Empty input");
            }
            if (file_exists($curDir . '/' . $name)) {
                throw new \InvalidArgumentException("File {$curDir}/{$name} already exist");
            }
            return $name;
        });

        $title = $dialog->ask($output, "Enter page title: ", "New page");

        $titleEscaped = addslashes($title);

        $targetDir = $curDir . "/" . $dirname;
        $targetFile = $targetDir . "/index.php";

        $url = '/' . trim($dirname, '/\\') . '/';

        if (!file_exists($targetDir)) {
            mkdir($targetDir, 0777, true);
        }
        if (file_exists($targetFile)) {
            $overwrite = $dialog->ask($output, "file [$targetFile] already exists. Overwrite? [y/n] ");
            if (!strcasecmp($overwrite, "y")) {
                exit("Operation cancelled");
            }
        }

        // save index page
        $indexContents = <<<PHP
<?php
require(\$_SERVER["DOCUMENT_ROOT"] . "/bitrix/header.php");
\$APPLICATION->SetTitle("{$titleEscaped}");?>
<p>Страница {$title}</p>
<?php require(\$_SERVER["DOCUMENT_ROOT"] . "/bitrix/footer.php"); ?>
PHP;

        $indexWritten = file_put_contents($targetFile, $indexContents);
        $sectionContents = '<?php $sSectionName="' . $titleEscaped . '"; ?>';
        $sectionWritten = file_put_contents($targetDir . '/.section.php', $sectionContents);
        if ($indexWritten === false) {
            $output->writeln("<error>Failed writing index file to [$targetFile]</error>");
        }

        $createMenu = $dialog->ask($output, "Add to menu? [y|n]:", 'n');
        if ($createMenu != 'y') {
            exit("Menu won't be created");
        }
        $menuDir = realpath($targetDir . '/..');

        // search existing or use default menus
        $menuList = array(
            '.top.menu.php',
            '.left.menu.php',
        );
        $menus = glob($menuDir . '/.*.menu.php');
        if (count($menus)) {
{'fraction_non_alphanumeric': 0.1695621959694232, 'fraction_numerical': 0.001389854065323141, 'mean_word_length': 2.376539589442815, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 37, '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': '95860', 'n_tokens_mistral': 1689, 'n_tokens_neox': 1622, 'n_words': 345}