<commit_before><?php
/*
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, version 2.0 (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

/**
 * RESTo PostgreSQL filters functions
 */
class Functions_filters {
    
    /*
     * Non search filters are excluded from search
     */
    private $excludedFilters = array(
        'count',
        'startIndex',
        'startPage',
        'language',
        'geo:name',
        'geo:lat', // linked to geo:lon
        'geo:radius' // linked to geo:lon
    );
    
    /**
     * Constructor
     */
    public function __construct() {}
    
    /**
     * Return an array of database column names
     * 
     * @param RestoModel $model
     * 
     * @return array
     */
    public function getSQLFields($model) {

        /*
         * Get Controller database fields
         */
        $columns = array();
        foreach (array_keys($model->properties) as $key) {

            /*
             * Avoid null value and excluded fields
             */
            if (!isset($model->properties[$key]) || isset($model->properties[$key]['notDisplayed'])) {
                continue;
            }
            
            $value = is_array($model->properties[$key]) ? $model->properties[$key]['name'] : $model->properties[$key];
            
            /*
             * Force geometry element to be retrieved as GeoJSON
             * Retrieve also BoundinBox in EPSG:4326
             */
            if ($key === 'geometry') {
                $columns[] = 'ST_AsGeoJSON(' . $value . ') AS ' . $key;
                $columns[] = 'Box2D(' . $value . ') AS bbox4326';
            }
            else if ($model->getDbType($key) === 'date') {
                $columns[] = 'to_char(' . $value . ', \'YYYY-MM-DD"T"HH24:MI:SS"Z"\') AS "' . $key . '"';
            }
            /*
             * Other fields are retrieved normally
             */
            else {
                $columns[] = $value . ' AS "' . $key . '"';
            }
        }

        return $columns;
        
    }

    
    /**
     * Return search filters based on model and input search parameters
     *
     * @param RestoUser $user
     * @param RestoModel $model
     * @param Array $params
     * @return boolean
     */
    public function prepareFilters($user, $model, $params) {
       
        $filters = array();
        
        /**
         * Append filter for contextual search
         */
        $filterCS = $this->prepareFilterQuery_contextualSearch($user, $model);
        if (isset($filterCS) && $filterCS !== '') {
            $filters[] = $filterCS;
        }

        /*
         * Process each input search filter excepted excluded filters
         */
        foreach (array_keys($model->searchFilters) as $filterName) {
            
            if (!isset($params[$filterName]) || $params[$filterName] === '') {
                continue;
            }
            
            /*
             * First check if filter is valid and as an associated column within database
             */
            if (!in_array($filterName, $this->excludedFilters) && $model->getDbKey($model->searchFilters[$filterName]['key'])) {
                $filter = $this->prepareFilterQuery($model, $params, $filterName);
                if (isset($filter) && $filter !== '') {
                    $filters[] = $filter;
                }
            }
            /*
             * Check for included function
             */
            else if (isset($model->searchFilters[$filterName]['function'])) {
                $function = $model->searchFilters[$filterName]['function'];
                $filters[] = $model->$function($params[$filterName]);
            }
        }
        
        return $filters;
        
    }

    /**
     * Filter search result on metadataVisibility attribute using
     * the grantedvisibility list from user profile
     * 
     * @param RestoUser $user
     * @param RestoModel $model
     * @return string
     */
    private function prepareFilterQuery_contextualSearch($user, $model) {
        if ($user->profile['groupname'] !== 'admin') {
            $grantedVisibility = '\'PUBLIC\'';
            if (isset($user->profile['grantedvisibility'])) {
                $visibilities = str_getcsv($user->profile['grantedvisibility']);
                foreach ($visibilities as &$v) {
                    $grantedVisibility = $grantedVisibility . ', \'' . $v . '\'';
                }
            }
            $filter = $model->properties['metadataVisibility']['name'] . ' in (' . $grantedVisibility . ')';
            return $filter;
        }
        return null;
    }<commit_msg>[RO] Add user reference to externalized search functions<commit_after><?php
/*
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, version 2.0 (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

/**
 * RESTo PostgreSQL filters functions
 */
class Functions_filters {
    
    /*
     * Non search filters are excluded from search
     */
    private $excludedFilters = array(
        'count',
        'startIndex',
        'startPage',
        'language',
        'geo:name',
        'geo:lat', // linked to geo:lon
        'geo:radius' // linked to geo:lon
    );
    
    /**
     * Constructor
     */
    public function __construct() {}
    
    /**
     * Return an array of database column names
     * 
     * @param RestoModel $model
     * 
     * @return array
     */
    public function getSQLFields($model) {

        /*
         * Get Controller database fields
         */
        $columns = array();
        foreach (array_keys($model->properties) as $key) {

            /*
             * Avoid null value and excluded fields
             */
            if (!isset($model->properties[$key]) || isset($model->properties[$key]['notDisplayed'])) {
                continue;
            }
            
            $value = is_array($model->properties[$key]) ? $model->properties[$key]['name'] : $model->properties[$key];
            
            /*
             * Force geometry element to be retrieved as GeoJSON
             * Retrieve also BoundinBox in EPSG:4326
             */
            if ($key === 'geometry') {
                $columns[] = 'ST_AsGeoJSON(' . $value . ') AS ' . $key;
                $columns[] = 'Box2D(' . $value . ') AS bbox4326';
            }
            else if ($model->getDbType($key) === 'date') {
                $columns[] = 'to_char(' . $value . ', \'YYYY-MM-DD"T"HH24:MI:SS"Z"\') AS "' . $key . '"';
            }
            /*
             * Other fields are retrieved normally
             */
            else {
                $columns[] = $value . ' AS "' . $key . '"';
            }
        }

        return $columns;
        
    }

    
    /**
     * Return search filters based on model and input search parameters
     *
     * @param RestoUser $user
     * @param RestoModel $model
     * @param Array $params
     * @return boolean
     */
    public function prepareFilters($user, $model, $params) {
       
        $filters = array();
        
        /**
         * Append filter for contextual search
         */
        $filterCS = $this->prepareFilterQuery_contextualSearch($user, $model);
        if (isset($filterCS) && $filterCS !== '') {
            $filters[] = $filterCS;
        }

        /*
         * Process each input search filter excepted excluded filters
         */
        foreach (array_keys($model->searchFilters) as $filterName) {
            
            if (!isset($params[$filterName]) || $params[$filterName] === '') {
                continue;
            }
            
            /*
             * First check if filter is valid and as an associated column within database
             */
            if (!in_array($filterName, $this->excludedFilters) && $model->getDbKey($model->searchFilters[$filterName]['key'])) {
                $filter = $this->prepareFilterQuery($model, $params, $filterName);
                if (isset($filter) && $filter !== '') {
                    $filters[] = $filter;
                }
            }
            /*
             * Check for included function
             */
            else if (isset($model->searchFilters[$filterName]['function'])) {
                $function = $model->searchFilters[$filterName]['function'];
                $filters[] = $model->$function($params[$filterName], $user);
            }
        }
        
        return $filters;
        
    }

    /**
     * Filter search result on metadataVisibility attribute using
     * the grantedvisibility list from user profile
     * 
     * @param RestoUser $user
     * @param RestoModel $model
     * @return string
     */
    private function prepareFilterQuery_contextualSearch($user, $model) {
        if ($user->profile['groupname'] !== 'admin') {
            $grantedVisibility = '\'PUBLIC\'';
            if (isset($user->profile['grantedvisibility'])) {
                $visibilities = str_getcsv($user->profile['grantedvisibility']);
                foreach ($visibilities as &$v) {
                    $grantedVisibility = $grantedVisibility . ', \'' . $v . '\'';
                }
            }
            $filter = $model->properties['metadataVisibility']['name'] . ' in (' . $grantedVisibility . ')';
            return $filter;
        }
        return null;
    }
{'fraction_non_alphanumeric': 0.13215258855585832, 'fraction_numerical': 0.0036979369404437526, 'mean_word_length': 1.6966675413277355, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4225719', 'n_tokens_mistral': 2704, 'n_tokens_neox': 2623, 'n_words': 702}