<commit_before><?php
/**
 * Created by PhpStorm.
 * User: exodus4d
 * Date: 06.06.15
 * Time: 03:34
 */

namespace Controller\Api;

use Controller;
use Controller\Ccp\Universe;
use lib\Config;
use Model;

/**
 * Routes controller
 * Class Route
 * @package Controller\Api
 */
class Route extends Controller\AccessController {

    const ROUTE_SEARCH_DEPTH_DEFAULT = 1;

    /**
     * ESI route search can handle max 100 custom connections
     * -> each connection has a A->B and B->A entry. So we have 50 "real connections"
     */
    const MAX_CONNECTION_COUNT  = 100;

    /**
     * cache time for static jump data (e.g. K-Space stargates)
     * @var int
     */
    private $staticJumpDataCacheTime = 86400;

    /**
     * cache time for dynamic jump data (e.g. W-Space systems, Jumpbridges. ...)
     * @var int
     */
    private $dynamicJumpDataCacheTime = 10;

    /**
     * array system information grouped by systemId
     * @var array
     */
    private $nameArray = [];

    /**
     * array neighbour systems grouped by systemName
     * @var array
     */
    private $jumpArray = [];

    /**
     * array with systemName => systemId matching
     * @var array
     */
    private $idArray = [];

    /**
     * template for routeData payload
     * @var array
     */
    private $defaultRouteData = [
        'routePossible' => false,
        'routeJumps' => 0,
        'maxDepth' => self::ROUTE_SEARCH_DEPTH_DEFAULT,
        'depthSearched' => 0,
        'searchType' => '',
        'route' => [],
        'error' => ''
    ];

    /**
     * reset all jump data
     */
    protected function resetJumpData(){
        $this->nameArray = [];
        $this->jumpArray = [];
        $this->idArray = [];
    }

    /**
     * set static system jump data for this instance
     * the data is fixed and should not change
     * -> jump data includes JUST "static" connections (Stargates)
     * -> this data is equal for EACH route search (does not depend on map data)
     */
    private function setStaticJumpData(){
        $query = "SELECT * FROM system_neighbour";
        $rows = $this->getDB()->exec($query, null, $this->staticJumpDataCacheTime);

        if(count($rows) > 0){
            $this->updateJumpData($rows);
        }
    }

    /**
     * set/add dynamic system jump data for specific "mapId"´s
     * -> this data is dynamic and could change on any map change
     * -> (e.g. new system added, connection added/updated, ...)
     * @param array $mapIds
     * @param array $filterData
     * @throws \Exception
     */
    private function setDynamicJumpData($mapIds = [], $filterData = []){
        // make sure, mapIds are integers (protect against SQL injections)
        $mapIds = array_unique( array_map('intval', $mapIds), SORT_NUMERIC);

        if( !empty($mapIds) ){
            // map filter ---------------------------------------------------------------------------------------------
            $whereMapIdsQuery = (count($mapIds) == 1) ? " = " . reset($mapIds) : " IN (" . implode(', ', $mapIds) . ")";

            // connection filter --------------------------------------------------------------------------------------
            $whereQuery = "";
            $includeScopes = [];
            $includeTypes = [];
            $excludeTypes = [];
            $includeEOL = true;

            if( $filterData['stargates'] === true){
                // include "stargates" for search
                $includeScopes[] = 'stargate';
                $includeTypes[] = 'stargate';

            }

            if( $filterData['jumpbridges'] === true ){
                // add jumpbridge connections for search
                $includeScopes[] = 'jumpbridge';
                $includeTypes[] = 'jumpbridge';
            }

            if( $filterData['wormholes'] === true ){
                // add wormhole connections for search
                $includeScopes[] = 'wh';
                $includeTypes[] = 'wh_fresh';


                if( $filterData['wormholesReduced'] === true ){
                    $includeTypes[] = 'wh_reduced';
                }

                if( $filterData['wormholesCritical'] === true ){
                    $includeTypes[] = 'wh_critical';
                }

                if( $filterData['wormholesFrigate'] !== true ){
                    $excludeTypes[] = 'frigate';
                }

                if( $filterData['wormholesEOL'] === false ){
                    $includeEOL = false;
                }
            }

            // search connections -------------------------------------------------------------------------------------

            if( !empty($includeScopes) ){
                $whereQuery .= " `connection`.`scope` IN ('" . implode("', '", $includeScopes) . "') AND ";

                if( !empty($excludeTypes) ){
                    $whereQuery .= " `connection`.`type` NOT REGEXP '" . implode("|", $excludeTypes) . "' AND ";
                }

                if( !empty($includeTypes) ){
                    $whereQuery .= " `connection`.`type` REGEXP '" . implode("|", $includeTypes) . "' AND ";
                }

                if(!$includeEOL){
                    $whereQuery .= " `connection`.`eolUpdated` IS NULL AND ";
                }

                $query = "SELECT
                        `system_src`.`regionId` regionId,
                        `system_src`.`constellationId` constellationId,
                        `system_src`.`name` systemName,
                        `system_src`.`systemId` systemId,
                        (
                          SELECT
                            GROUP_CONCAT( NULLIF(`system_tar`.`systemId`, NULL) SEPARATOR ':')
                          FROM
                            `connection` INNER JOIN
                            `system` system_tar ON
                              `system_tar`.`id` = `connection`.`source` OR
                              `system_tar`.`id` = `connection`.`target`
                          WHERE
                            `connection`.`mapId` " . $whereMapIdsQuery . " AND
                            `connection`.`active` = 1 AND
                            (
                              `connection`.`source` = `system_src`.`id` OR
                              `connection`.`target` = `system_src`.`id`
                            ) AND
                            " . $whereQuery . "
                            `system_tar`.`id` != `system_src`.`id` AND
                            `system_tar`.`active` = 1
                        ) jumpNodes,
                        `system_src`.`trueSec` trueSec
                    FROM
                        `system` `system_src` INNER JOIN
                        `map` ON
                          `map`.`id` = `system_src`.`mapId`
                    WHERE
                        `system_src`.`mapId` " . $whereMapIdsQuery . " AND
                        `system_src`.`active` = 1 AND<commit_msg>- fixed broken "route search" after last commit :)<commit_after><?php
/**
 * Created by PhpStorm.
 * User: exodus4d
 * Date: 06.06.15
 * Time: 03:34
 */

namespace Controller\Api;

use Controller;
use Controller\Ccp\Universe;
use lib\Config;
use Model;

/**
 * Routes controller
 * Class Route
 * @package Controller\Api
 */
class Route extends Controller\AccessController {

    const ROUTE_SEARCH_DEPTH_DEFAULT = 1;

    /**
     * ESI route search can handle max 100 custom connections
     * -> each connection has a A->B and B->A entry. So we have 50 "real connections"
     */
    const MAX_CONNECTION_COUNT  = 100;

    /**
     * cache time for static jump data (e.g. K-Space stargates)
     * @var int
     */
    private $staticJumpDataCacheTime = 86400;

    /**
     * cache time for dynamic jump data (e.g. W-Space systems, Jumpbridges. ...)
     * @var int
     */
    private $dynamicJumpDataCacheTime = 10;

    /**
     * array system information grouped by systemId
     * @var array
     */
    private $nameArray = [];

    /**
     * array neighbour systems grouped by systemName
     * @var array
     */
    private $jumpArray = [];

    /**
     * array with systemName => systemId matching
     * @var array
     */
    private $idArray = [];

    /**
     * template for routeData payload
     * @var array
     */
    private $defaultRouteData = [
        'routePossible' => false,
        'routeJumps' => 0,
        'maxDepth' => self::ROUTE_SEARCH_DEPTH_DEFAULT,
        'depthSearched' => 0,
        'searchType' => '',
        'route' => [],
        'error' => ''
    ];

    /**
     * reset all jump data
     */
    protected function resetJumpData(){
        $this->nameArray = [];
        $this->jumpArray = [];
        $this->idArray = [];
    }

    /**
     * set static system jump data for this instance
     * the data is fixed and should not change
     * -> jump data includes JUST "static" connections (Stargates)
     * -> this data is equal for EACH route search (does not depend on map data)
     */
    private function setStaticJumpData(){
        $query = "SELECT * FROM system_neighbour";
        $rows = $this->getDB()->exec($query, null, $this->staticJumpDataCacheTime);

        if(count($rows) > 0){
            $this->updateJumpData($rows);
        }
    }

    /**
     * set/add dynamic system jump data for specific "mapId"´s
     * -> this data is dynamic and could change on any map change
     * -> (e.g. new system added, connection added/updated, ...)
     * @param array $mapIds
     * @param array $filterData
     * @throws \Exception
     */
    private function setDynamicJumpData($mapIds = [], $filterData = []){
        // make sure, mapIds are integers (protect against SQL injections)
        $mapIds = array_unique( array_map('intval', $mapIds), SORT_NUMERIC);

        if( !empty($mapIds) ){
            // map filter ---------------------------------------------------------------------------------------------
            $whereMapIdsQuery = (count($mapIds) == 1) ? " = " . reset($mapIds) : " IN (" . implode(', ', $mapIds) . ")";

            // connection filter --------------------------------------------------------------------------------------
            $whereQuery = "";
            $includeScopes = [];
            $includeTypes = [];
            $excludeTypes = [];
            $includeEOL = true;

            if( $filterData['stargates'] === true){
                // include "stargates" for search
                $includeScopes[] = 'stargate';
                $includeTypes[] = 'stargate';

            }

            if( $filterData['jumpbridges'] === true ){
                // add jumpbridge connections for search
                $includeScopes[] = 'jumpbridge';
                $includeTypes[] = 'jumpbridge';
            }

            if( $filterData['wormholes'] === true ){
                // add wormhole connections for search
                $includeScopes[] = 'wh';
                $includeTypes[] = 'wh_fresh';


                if( $filterData['wormholesReduced'] === true ){
                    $includeTypes[] = 'wh_reduced';
                }

                if( $filterData['wormholesCritical'] === true ){
                    $includeTypes[] = 'wh_critical';
                }

                if( $filterData['wormholesFrigate'] !== true ){
                    $excludeTypes[] = 'frigate';
                }

                if( $filterData['wormholesEOL'] === false ){
                    $includeEOL = false;
                }
            }

            // search connections -------------------------------------------------------------------------------------

            if( !empty($includeScopes) ){
                $whereQuery .= " `connection`.`scope` IN ('" . implode("', '", $includeScopes) . "') AND ";

                if( !empty($excludeTypes) ){
                    $whereQuery .= " `connection`.`type` NOT REGEXP '" . implode("|", $excludeTypes) . "' AND ";
                }

                if( !empty($includeTypes) ){
                    $whereQuery .= " `connection`.`type` REGEXP '" . implode("|", $includeTypes) . "' AND ";
                }

                if(!$includeEOL){
                    $whereQuery .= " `connection`.`eolUpdated` IS NULL AND ";
                }

                $query = "SELECT
                        `system_src`.`systemId` systemId,
                        (
                          SELECT
                            GROUP_CONCAT( NULLIF(`system_tar`.`systemId`, NULL) SEPARATOR ':')
                          FROM
                            `connection` INNER JOIN
                            `system` system_tar ON
                              `system_tar`.`id` = `connection`.`source` OR
                              `system_tar`.`id` = `connection`.`target`
                          WHERE
                            `connection`.`mapId` " . $whereMapIdsQuery . " AND
                            `connection`.`active` = 1 AND
                            (
                              `connection`.`source` = `system_src`.`id` OR
                              `connection`.`target` = `system_src`.`id`
                            ) AND
                            " . $whereQuery . "
                            `system_tar`.`id` != `system_src`.`id` AND
                            `system_tar`.`active` = 1
                        ) jumpNodes
                    FROM
                        `system` `system_src` INNER JOIN
                        `map` ON
                          `map`.`id` = `system_src`.`mapId`
                    WHERE
                        `system_src`.`mapId` " . $whereMapIdsQuery . " AND
                        `system_src`.`active` = 1 AND
{'fraction_non_alphanumeric': 0.1633262881714161, 'fraction_numerical': 0.004955907003862692, 'mean_word_length': 1.2954165272666445, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, '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': '1987075', 'n_tokens_mistral': 3742, 'n_tokens_neox': 3456, 'n_words': 839}