<commit_before><?php

namespace Bolt\Extension\SthlmConnection\ManyRedirects;

use Bolt\Application;
use Bolt\BaseExtension;
use Silex\Application as SilexApplication;
use Symfony\Component\HttpFoundation\Request;

class Extension extends BaseExtension {

  public $tableName;

  public function __construct(Application $app) {
    parent::__construct($app);
  }

  public function getName() {
    return "ManyRedirects";
  }

  public function initialize() {
    $self = $this;

    // Register this extension's actions as an early event.
    $this->app->before(function (Request $request) use ($self) {
      if ($self->dbCheck()) {
        return $self->handleRequest($request);
      }
    }, SilexApplication::EARLY_EVENT);
  }

  public function handleRequest(Request $request) {
    $app = $this->app;
    $redirect = new Redirect($app['db'], $this->tableName);

    // Look for a migrated article with this URL path.
    $requested_path = $request->getPathInfo();

    if ($redirect->load($requested_path)) {
      $status_code = $redirect->code;
      $status_code = empty($status_code) ? $this->config['status_code'] : $status_code;
      $status_code = !in_array($status_code, [301, 302]) ? 302 : $status_code;
      $record = $this->getContentRecord($redirect->contentType, $redirect->contentId);
      if ($record) {
        return $app->redirect($app['paths']['rooturl'] . strtolower($record->getReference()), $status_code);
      }
      else {
        return false;
      }
    }
  }

  public function dbCheck() {
    $table = $this->tableName = $this->getTableName();
    $this->app['integritychecker']->registerExtensionTable(
      function ($schema) use ($table) {
        $table = $schema->createTable($table);
        $table->addColumn('id', 'integer', array('autoincrement' => true));
        $table->addColumn('source', 'string', array('length' => 128));
        $table->addColumn('content_type', 'string', array('length' => 128));
        $table->addColumn('content_id', 'integer');
        $table->addColumn('code', 'integer', array('length' => 3, 'default' => null, 'notnull' => false));
        $table->setPrimaryKey(array('id'));
        $table->addUniqueIndex(array('source'));
        $table->addIndex(array('content_type', 'content_id'));
        return $table;
      }
    );

    // The table is not immediately created, so check if exists and return that.<commit_msg>Better performance by ignoring certain known paths.<commit_after><?php

namespace Bolt\Extension\SthlmConnection\ManyRedirects;

use Bolt\Application;
use Bolt\BaseExtension;
use Silex\Application as SilexApplication;
use Symfony\Component\HttpFoundation\Request;

class Extension extends BaseExtension {

  public $tableName;

  public function __construct(Application $app) {
    parent::__construct($app);
  }

  public function getName() {
    return "ManyRedirects";
  }

  public function initialize() {
    $self = $this;

    // Register this extension's actions as an early event.
    $this->app->before(function (Request $request) use ($self) {
      if ($self->dbCheck()) {
        return $self->handleRequest($request);
      }
    }, SilexApplication::EARLY_EVENT);
  }

  public function handleRequest(Request $request) {
    $app = $this->app;
    $redirect = new Redirect($app['db'], $this->tableName);

    // Look for a migrated article with this URL path.
    $requested_path = $request->getPathInfo();

    if ($this->isRedirectable($requested_path) && $redirect->load($requested_path)) {
      $status_code = $redirect->code;
      $status_code = empty($status_code) ? $this->config['status_code'] : $status_code;
      $status_code = !in_array($status_code, [301, 302]) ? 302 : $status_code;
      $record = $this->getContentRecord($redirect->contentType, $redirect->contentId);
      if ($record) {
        return $app->redirect($app['paths']['rooturl'] . strtolower($record->getReference()), $status_code);
      }
      else {
        return false;
      }
    }
  }

  public function isRedirectable($path) {
    $blacklist = [
      '/^\/$/',
      '/^\/bolt.*$/',
    ];
    foreach ($blacklist as $pattern) {
      if (preg_match($pattern, $path)) {
        return false;
      }
    }
    return true;
  }

  public function dbCheck() {
    $table = $this->tableName = $this->getTableName();
    $this->app['integritychecker']->registerExtensionTable(
      function ($schema) use ($table) {
        $table = $schema->createTable($table);
        $table->addColumn('id', 'integer', array('autoincrement' => true));
        $table->addColumn('source', 'string', array('length' => 128));
        $table->addColumn('content_type', 'string', array('length' => 128));
        $table->addColumn('content_id', 'integer');
        $table->addColumn('code', 'integer', array('length' => 3, 'default' => null, 'notnull' => false));
        $table->setPrimaryKey(array('id'));
        $table->addUniqueIndex(array('source'));
        $table->addIndex(array('content_type', 'content_id'));
        return $table;
      }
    );

    // The table is not immediately created, so check if exists and return that.
{'fraction_non_alphanumeric': 0.16413255360623782, 'fraction_numerical': 0.006237816764132553, 'mean_word_length': 3.4890638670166227, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 72, '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': '5234434', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1506, 'n_words': 328}