<commit_before><?php
namespace RelatedContentByTags;

class Extension extends \Bolt\BaseExtension
{

    function info()
    {

        $data = array(
            'name' => "Related Content By Tags",
            'description' => "Retrieves a list of similar content based on tags.",
            'author' => "<NAME>",
            'link' => "http://bolt.cm",
            'version' => "0.2",
            'required_bolt_version' => "2.0",
            'highest_bolt_version' => "2.0",
            'type' => "General",
            'first_releasedate' => "2013-10-25",
            'latest_releasedate' => "2014-08-12",
            'dependencies' => "",
            'priority' => 10
        );

        return $data;

    }

    public function getName()
    {
        return "Related Content by Tags";
    }


    function initialize()
    {

        $this->addTwigFunction('relatedcontentbytags', 'relatedContentByTags');

        // Always make sure a default config.yml is available.
        if( !isset($this->config) ) {
            $this->config = array();
        }

        if ( !isset($this->config['limit']) ) {
            $this->config['limit'] = 10;
        }

        if ( !isset($this->config['points']) ) {
            $this->config['points'] = array();
        }

        if ( !isset($this->config['points']['tag']) ) {
            $this->config['points']['tag'] = 10;
        }

        if ( !isset($this->config['points']['type']) ) {
            $this->config['points']['type'] = 10;
        }

    }

    /**
     * @author <NAME>
     * @param \Bolt\Content $record   The record to search similar content for.
     * @param array         $options  Options for custom queries.
     *
     * @return array Returns an array with the elements sorted by similarity.
     */
    function relatedContentByTags($record, $options = array())
    {

        $app           = $this->app;
        $limit         = isset($options['limit'])  ? $options['limit']  : $this->config['limit'];
        $tablePrefix   = $app['config']->get('general/database/prefix', 'bolt_');
        $taxonomyTable = sprintf('%staxonomy', $tablePrefix);
        $contenttypes  = $app['config']->get('contenttypes');
        $filter        = isset($options['contenttypes']) ? $options['contenttypes'] : false;

        // if set, filter contenttypes
        if ($filter) {
            $filterContenttypes = array();
            foreach ($filter as $contenttypeName) {
                if (isset($contenttypes[$contenttypeName])) {
                    $filterContenttypes[$contenttypeName] = $contenttypes[$contenttypeName];
                }
            }
            if ($filterContenttypes) {
                $contenttypes = $filterContenttypes;
            }
        }

        // Get all taxonomies that behave like tags and their values from $record.
        $tagsValues     = array();
        $tagsTaxonomies = array();

        // If no taxonomies exist, then no matching items exist
        if (!isset( $record->contenttype['taxonomy'])) {
            return array();
        }
        foreach ( $record->contenttype['taxonomy'] as $key ) {
            if ($app['config']->get('taxonomy/'.$key.'/behaves_like') == 'tags') {
                // only useful if values exist, otherwise just skip this taxonomy
                if ($record->taxonomy[$key]) {
                    $tagsValues[$key] = array_values( $record->taxonomy[$key] );
                    $tagsTaxonomies[] = $key;
                }
            }
        }

        // Make the basic WHERE query for all behaves-like-tags values in $record.
        $queryWhere = array();
        foreach ( $tagsValues as $tagName => $values ) {
            $subqueryWhere = array();

            foreach( $values as $word ) {
                $subqueryWhere[] = sprintf("%s.slug = '%s'", $taxonomyTable, $word);
            }
              $temp  = sprintf( "%s.taxonomytype = '%s'", $taxonomyTable, $tagName );
            $temp .= sprintf( ' AND (%s)', implode(' OR ', $subqueryWhere) );
            $queryWhere[] = $temp;

        }
        $queryWhere = implode(' OR ', $queryWhere);

        // Get all contenttypes (database tables) that have a similar behaves-like-tags taxonomies like $record
        $tables = array();
        foreach ( $contenttypes as $key => $contenttype ) {
            foreach( $contenttype['taxonomy'] as $taxonomyKey ) {
                if (in_array( $taxonomyKey, $tagsTaxonomies )) {
                    $tables[] = $contenttype['slug'];
                    break;
                }
            }
        }

        // Fetch results for every proper contenttype
        $results = array();
        foreach ($tables as $name) {

            $table        = sprintf('%s%s', $tablePrefix, $name);
            $querySelect  = '';<commit_msg>Check for availability of taxonomy<commit_after><?php
namespace RelatedContentByTags;

class Extension extends \Bolt\BaseExtension
{

    function info()
    {

        $data = array(
            'name' => "Related Content By Tags",
            'description' => "Retrieves a list of similar content based on tags.",
            'author' => "<NAME>",
            'link' => "http://bolt.cm",
            'version' => "0.2",
            'required_bolt_version' => "2.0",
            'highest_bolt_version' => "2.0",
            'type' => "General",
            'first_releasedate' => "2013-10-25",
            'latest_releasedate' => "2014-08-12",
            'dependencies' => "",
            'priority' => 10
        );

        return $data;

    }

    public function getName()
    {
        return "Related Content by Tags";
    }


    function initialize()
    {

        $this->addTwigFunction('relatedcontentbytags', 'relatedContentByTags');

        // Always make sure a default config.yml is available.
        if( !isset($this->config) ) {
            $this->config = array();
        }

        if ( !isset($this->config['limit']) ) {
            $this->config['limit'] = 10;
        }

        if ( !isset($this->config['points']) ) {
            $this->config['points'] = array();
        }

        if ( !isset($this->config['points']['tag']) ) {
            $this->config['points']['tag'] = 10;
        }

        if ( !isset($this->config['points']['type']) ) {
            $this->config['points']['type'] = 10;
        }

    }

    /**
     * @author <NAME>
     * @param \Bolt\Content $record   The record to search similar content for.
     * @param array         $options  Options for custom queries.
     *
     * @return array Returns an array with the elements sorted by similarity.
     */
    function relatedContentByTags($record, $options = array())
    {

        $app           = $this->app;
        $limit         = isset($options['limit'])  ? $options['limit']  : $this->config['limit'];
        $tablePrefix   = $app['config']->get('general/database/prefix', 'bolt_');
        $taxonomyTable = sprintf('%staxonomy', $tablePrefix);
        $contenttypes  = $app['config']->get('contenttypes');
        $filter        = isset($options['contenttypes']) ? $options['contenttypes'] : false;

        // if set, filter contenttypes
        if ($filter) {
            $filterContenttypes = array();
            foreach ($filter as $contenttypeName) {
                if (isset($contenttypes[$contenttypeName])) {
                    $filterContenttypes[$contenttypeName] = $contenttypes[$contenttypeName];
                }
            }
            if ($filterContenttypes) {
                $contenttypes = $filterContenttypes;
            }
        }

        // Get all taxonomies that behave like tags and their values from $record.
        $tagsValues     = array();
        $tagsTaxonomies = array();

        // If no taxonomies exist, then no matching items exist
        if (!isset( $record->contenttype['taxonomy'])) {
            return array();
        }
        foreach ( $record->contenttype['taxonomy'] as $key ) {
            if ($app['config']->get('taxonomy/'.$key.'/behaves_like') == 'tags') {
                // only useful if values exist, otherwise just skip this taxonomy
                if ($record->taxonomy[$key]) {
                    $tagsValues[$key] = array_values( $record->taxonomy[$key] );
                    $tagsTaxonomies[] = $key;
                }
            }
        }

        // Make the basic WHERE query for all behaves-like-tags values in $record.
        $queryWhere = array();
        foreach ( $tagsValues as $tagName => $values ) {
            $subqueryWhere = array();

            foreach( $values as $word ) {
                $subqueryWhere[] = sprintf("%s.slug = '%s'", $taxonomyTable, $word);
            }
              $temp  = sprintf( "%s.taxonomytype = '%s'", $taxonomyTable, $tagName );
            $temp .= sprintf( ' AND (%s)', implode(' OR ', $subqueryWhere) );
            $queryWhere[] = $temp;

        }
        $queryWhere = implode(' OR ', $queryWhere);

        // Get all contenttypes (database tables) that have a similar behaves-like-tags taxonomies like $record
        $tables = array();
        foreach ( $contenttypes as $key => $contenttype ) {
            if (empty($contenttype['taxonomy'])) {
                continue;
            }

            foreach( $contenttype['taxonomy'] as $taxonomyKey ) {
                if (in_array( $taxonomyKey, $tagsTaxonomies )) {
                    $tables[] = $contenttype['slug'];
                    break;
                }
            }
        }

        // Fetch results for every proper contenttype
        $results = array();
        foreach ($tables as $name) {

            $table        = sprintf('%s%s', $tablePrefix, $name);
            $querySelect  = '';
{'fraction_non_alphanumeric': 0.15019030963892605, 'fraction_numerical': 0.006172204505709289, 'mean_word_length': 1.8090147356255417, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 75, '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': '6906944', 'n_tokens_mistral': 2718, 'n_tokens_neox': 2601, 'n_words': 625}