TestDownloadBase.php 3.25 KB
<?php

require_once "TestAbstract.php";

abstract class TestDownloadBase extends TestAbstract
{
	abstract protected function checkResultInfo($info);

	protected function checkRESTResult($result) {
		if (empty($result)) {
			return array(
				'success' => FALSE,
				'message' => 'Empty result',
			);
		}
		$json_res = json_decode($result);
		if (!$json_res) {
			return array(
				'success' => FALSE,
				'message' => 'Result is not in JSON format',
			);
		}
		$info = $this->getResultInfo($json_res);
		if (!$info['success']) {
			return $info;
		}
		return $this->checkResultInfo($info);
	}

	protected function checkSOAPResult($result) {
		$info = $this->getResultInfo($result);
		if (!$info['success']) {
			return $info;
		}
		return $this->checkResultInfo($info);
	}

	private function gz_get_contents($path) {
		$gzip = file_get_contents($path);
		$rest = substr($gzip, -4);
		$unpacked = unpack("V", $rest);
		$uncompressedSize = end($unpacked);

		// read the gzipped content, specifying the exact length
		$handle = gzopen($path, "rb");
		$contents = gzread($handle, $uncompressedSize);
		gzclose($handle);

		return $contents;
	}

	private function isCompressed($path) {
		return (substr($path, -3) == ".gz");
	}

	private function isVOTable($path, $compressed) {
		if ($compressed) {
			$path = substr($path, 0, strlen($path)-3);
		}
		return (substr($path, -4) == ".vot");
	}

	private function getResultInfo($obj) {
		if (empty($obj) || empty($obj->status)) {
			return array(
				'success' => FALSE,
				'message' => 'Bad result format',
			);
		}

		if ($obj->status != 'done') {
			return array(
				'success' => FALSE,
				'message' => 'Status is not done ('.$obj->status.')',
			);
		}

		if (empty($obj->dataFileURLs)) {
			return array(
				'success' => FALSE,
				'message' => 'Missing data file URL in result',
			);
		}

		$result = array(
			'success' => TRUE,
		);

		$result['compressed'] = $this->isCompressed($obj->dataFileURLs);

		if ($result['compressed']) {
			$data = $this->gz_get_contents($obj->dataFileURLs);
		}
		else {
			$data = file_get_contents($obj->dataFileURLs);
		}
		if (!$data) {
			return array(
				'success' => FALSE,
				'message' => 'Cannot load data result file',
			);
		}

		$result['isVOTable'] = $this->isVOTable($obj->dataFileURLs, $result['compressed']);

		$request_info = array();

		foreach (explode(PHP_EOL, $data) as $line) {
			$pattern = $result['isVOTable'] ? "REQUEST_" : "# REQUEST_";
			if (strpos($line, $pattern) === 0) {
				$sep_pos = strpos($line, ":");
				if ($result['isVOTable'])
					$key = trim(substr($line, 0, $sep_pos));
				else
					$key = trim(substr($line, 2, $sep_pos-2));
				$value = trim(substr($line, $sep_pos+1));
				$request_info[$key] = $value;
			}
		}

		$result['parameters'] = array();
		if (!empty($request_info['REQUEST_OUTPUT_PARAMS'])) {
			$result['parameters'] = explode(',',$request_info['REQUEST_OUTPUT_PARAMS']);
		}

		$result['structure']  = !empty($request_info['REQUEST_STRUCTURE']) ? $request_info['REQUEST_STRUCTURE'] : '';
		$result['timeFormat'] = !empty($request_info['REQUEST_TIME_FORMAT']) ? $request_info['REQUEST_TIME_FORMAT'] : '';
		$result['sampling']   = !empty($request_info['REQUEST_TIME_RESOLUTION']) ? doubleval($request_info['REQUEST_TIME_RESOLUTION']) : NULL;

		return $result;
	}
}

?>