<commit_before><?php

/**
 * Copyright 2013 <NAME> <<EMAIL>>
 *
 * 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.
 */

namespace SURFnet\janus\export;

use PDO;

class Export
{
    /** @var PDO */
    private $db;

    public function __construct(PDO $db)
    {
        $this->db = $db;
    }

    private function validateType($type)
    {
        $validTypes = array(
            "saml20-idp",
            "saml20-sp"
        );

        if (!in_array($type, $validTypes)) {
            throw new ExportException("invalid type");
        }
    }

    private function validateState($state)
    {
        $validStates = array(
            "testaccepted",
            "prodaccepted"
        );

        if (!in_array($state, $validStates)) {
            throw new ExportException("invalid state");
        }
    }

    /**
     * Fetch all entities of a certain type with a state
     */
    public function getEntities($type, $state)
    {
        $this->validateType($type);
        $this->validateState($state);

        $sql = <<< EOF
SELECT
    e.eid, e.revisionid
FROM
    janus__entity e
WHERE
    active = "yes" AND state=:state AND type=:type AND revisionid = (SELECT
            MAX(revisionid)
        FROM
            janus__entity
        WHERE
            eid = e.eid)
EOF;

        $sth = $this->db->prepare($sql);
        $sth->bindValue(":state", $state, PDO::PARAM_STR);
        $sth->bindValue(":type", $type, PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetchAll(PDO::FETCH_ASSOC);

        $entities = array();

        foreach ($result as $e) {
            $entities[] = $this->getEntity($type, $e['eid'], $e['revisionid']);
        }

        return $entities;
    }

    private function getAllowedEntities($eid, $revisionid)
    {
        $sql = <<< EOF
    SELECT<commit_msg>#7 Fixed getEntities query<commit_after><?php

/**
 * Copyright 2013 <NAME> <<EMAIL>>
 *
 * 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.
 */

namespace SURFnet\janus\export;

use PDO;

class Export
{
    /** @var PDO */
    private $db;

    public function __construct(PDO $db)
    {
        $this->db = $db;
    }

    private function validateType($type)
    {
        $validTypes = array(
            "saml20-idp",
            "saml20-sp"
        );

        if (!in_array($type, $validTypes)) {
            throw new ExportException("invalid type");
        }
    }

    private function validateState($state)
    {
        $validStates = array(
            "testaccepted",
            "prodaccepted"
        );

        if (!in_array($state, $validStates)) {
            throw new ExportException("invalid state");
        }
    }

    /**
     * Fetch all entities of a certain type with a state
     */
    public function getEntities($type, $state)
    {
        $this->validateType($type);
        $this->validateState($state);

        $sql = <<< EOF
SELECT
    C.id, C.revisionNr
FROM
    janus__connection AS C
INNER JOIN
    janus__connectionRevision AS CR ON
        CR.eid = C.id AND
        CR.revisionid = C.revisionNr
WHERE
    CR.active = "yes" AND CR.state=:state AND C.type=:type
EOF;

        $sth = $this->db->prepare($sql);
        $sth->bindValue(":state", $state, PDO::PARAM_STR);
        $sth->bindValue(":type", $type, PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetchAll(PDO::FETCH_ASSOC);

        $entities = array();

        foreach ($result as $e) {
            $entities[] = $this->getEntity($type, $e['id'], $e['revisionNr']);
        }

        return $entities;
    }

    private function getAllowedEntities($eid, $revisionid)
    {
        $sql = <<< EOF
    SELECT
{'fraction_non_alphanumeric': 0.12569832402234637, 'fraction_numerical': 0.0053717232488182205, 'mean_word_length': 2.2758620689655173, 'pattern_counts': {'":': 4, '<': 23, '<?xml version=': 0, '>': 29, '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': '4993624', 'n_tokens_mistral': 1435, 'n_tokens_neox': 1394, 'n_words': 401}