<commit_before><?php
/*
 *  Copyright (c) 2012-2015  <NAME> <<EMAIL>>,
 *                           <NAME> <<EMAIL>>,
 *                           AIP E-Science (www.aip.de)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// @TODO: LIMITATIONS: Parameters by reference are not yet supported!!
// @TODO: LIMITATIONS: Parameters by posting files are not yet supported!!

abstract class Uws_Model_UwsAbstract extends Daiquiri_Model_Abstract {

    protected static $status = array('PENDING', 'QUEUED', 'EXECUTING', 'COMPLETED', 'ERROR', 'ABORTED', 'UNKNOWN', 'HELD', 'SUSPENDED', 'ARCHIVED');

    public function __construct() {
        parent::__construct();
    }

    public function index($requestParams) {
        $xmlDoc = new DOMDocument('1.0', "UTF-8");
        $xmlDoc->formatOutput = true;

        //build job list for current user
        $jobs = $this->getJobList($requestParams);

        //@TODO: REMOVE THIS DEBUG FLAG!
        $jobs->validateSchema = false;

        $jobs->toXML($xmlDoc);

        return array(
            'status' => 'ok',
            'body' => $xmlDoc->saveXML(),
            'headers' => array('Content-Type' => 'application/xml; charset=utf-8')
        );
    }

    public function get($requestParams) {
        // is this job pending?
        $job = $this->getPendingJob($requestParams['wild0']);

        if ($job === false) {
            // get job information for this id
            $job = $this->getJob($requestParams);
        }

        if ($job === false) {
            throw new Daiquiri_Exception_NotFound();
        }

        // get the wildcard parameters
        $params = array();
        foreach ($requestParams as $key => $value) {
            if (strstr($key, "wild") !== false) {
                $params[$key] = $value;
            }
        }

        if (count($params) == 1) {
            //this is just a get on the object
            $xmlDoc = new DOMDocument('1.0', "UTF-8");
            $xmlDoc->formatOutput = true;

            //@TODO: REMOVE THIS DEBUG FLAG!
            $job->validateSchema = false;

            $job->toXML($xmlDoc);

            return array(
                'status' => 'ok',
                'body' => $xmlDoc->saveXML(),
                'headers' => array('Content-Type' => 'application/xml; charset=utf-8')
            );

        } else {
            $action = $params['wild1'];<commit_msg>Added WAIT-blocking behaviour, including additional PHASE-filter<commit_after><?php
/*
 *  Copyright (c) 2012-2015  <NAME> <<EMAIL>>,
 *                           <NAME> <<EMAIL>>,
 *                           AIP E-Science (www.aip.de)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// @TODO: LIMITATIONS: Parameters by reference are not yet supported!!
// @TODO: LIMITATIONS: Parameters by posting files are not yet supported!!

abstract class Uws_Model_UwsAbstract extends Daiquiri_Model_Abstract {

    protected static $status = array('PENDING', 'QUEUED', 'EXECUTING', 'COMPLETED', 'ERROR', 'ABORTED', 'UNKNOWN', 'HELD', 'SUSPENDED', 'ARCHIVED');
    private static $status_active = array('PENDING', 'QUEUED', 'EXECUTING');

    public function __construct() {
        parent::__construct();
    }

    public function index($requestParams) {
        $xmlDoc = new DOMDocument('1.0', "UTF-8");
        $xmlDoc->formatOutput = true;

        //build job list for current user
        $jobs = $this->getJobList($requestParams);

        //@TODO: REMOVE THIS DEBUG FLAG!
        $jobs->validateSchema = false;

        $jobs->toXML($xmlDoc);

        return array(
            'status' => 'ok',
            'body' => $xmlDoc->saveXML(),
            'headers' => array('Content-Type' => 'application/xml; charset=utf-8')
        );
    }

    public function get($requestParams) {
        $waittime_max = 60; // maximum time the server will wait before continuing
        $sleeptime = 1; // time interval between repeated queries to the database

        // is this job pending?
        $job = $this->getPendingJob($requestParams['wild0']);

        if ($job === false) {
            // get job information for this id
            $job = $this->getJob($requestParams);
        }

        if ($job === false) {
            throw new Daiquiri_Exception_NotFound();
        }

        // get the wildcard parameters and possible WAIT-parameters
        $params = array();
        $waittime = 0;
        $waitphase = NULL;
        foreach ($requestParams as $key => $value) {
            if (strstr($key, "wild") !== false) {
                $params[$key] = $value;
            }
            if ($key == "WAIT") {
                $waittime = $requestParams["WAIT"];
                // validate that it is an integer
                if (isset($waittime) && ctype_digit($waittime)) {
                    $waittime = intval($waittime);
                } else if ($waittime === '-1') {
                    $waittime = $waittime_max;
                } else {
                    // create error or just ignore
                    $waittime = 0;
                }
            }
            if ($key == "PHASE") {
                $waitphase = $requestParams["PHASE"];
            }
        }

        if (count($params) == 1) {
            //this is just a get on the object

            // take care of WAIT
            // if no wait-phase is given, then set it to the current phase of the job
            if (is_null($waitphase)) {
                $waitphase = $job->phase;
            }
            // check if it's an active phase, otherwise don't wait at all
            // e.g. {url/jobid}?WAIT=-1 on aborted job should return immediately
            if ( !in_array($waitphase, self::$status_active) ) {
                $waittime = 0;
            }

            // wait until phase-change of job or waittime is over
            $start = microtime(true);
            $end = $start;
            while ($job->phase == $waitphase && $end-$start < $waittime) {
                $job = $this->getPendingJob($requestParams['wild0']);

                if ($job === false) {
                    // get job information for this id
                    $job = $this->getJob($requestParams);
                }
                if ($job === false) {
                    // should never happen, but who knows
                    throw new Daiquiri_Exception_NotFound();
                }

                sleep($sleeptime);
                $end = microtime(true);
            }

            $xmlDoc = new DOMDocument('1.0', "UTF-8");
            $xmlDoc->formatOutput = true;

            //@TODO: REMOVE THIS DEBUG FLAG!
            $job->validateSchema = false;

            $job->toXML($xmlDoc);

            return array(
                'status' => 'ok',
                'body' => $xmlDoc->saveXML(),
                'headers' => array('Content-Type' => 'application/xml; charset=utf-8')
            );

        } else {
            $action = $params['wild1'];
{'fraction_non_alphanumeric': 0.1250761916372059, 'fraction_numerical': 0.005973424356942582, 'mean_word_length': 1.877586811645037, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6182106', 'n_tokens_mistral': 2329, 'n_tokens_neox': 2219, 'n_words': 705}