<commit_before><?php

namespace SilverStripeAustralia\S3Publisher;

use Convert;
use Director;

/**
 * A static publish queue compatible extension which publishes the website to a bucket.
 */
class Publisher extends \DataExtension {

	/**
	 * @var BucketWebsite
	 */
	private $bucket;

	public function __construct(BucketWebsite $bucket) {
		$this->bucket = $bucket;
		parent::__construct();
	}

	public function publishPages(array $urls) {
		$result = array();

		foreach($urls as $url) {
			try {
				$response = Director::test($url);
			} catch(\SS_HTTPResponse_Exception $e) {
				$response = $e->getResponse();
			}

			$data = array(
				'statuscode' => $response->getStatusCode(),
				'redirect' => null
			);

			if(substr($response->getStatusCode(), 0, 1) == '3') {
				$data['redirect'] = $response->getHeader('Location');

				$body = sprintf(
					'<meta http-equiv="refresh" content="0; URL=%s">',
					Convert::raw2htmlatt(Director::absoluteURL($data['redirect']))
				);
			} else {
				$body = $response->getBody();
			}

			$model = $this->bucket->upload($this->getKeyForURL($url), $body, array(
				'ContentType' => $response->getHeader('Content-Type')
			));

			$result[$url] = $data + array('path' => $model['ObjectURL']);
		}

		return $result;
	}

	private function getKeyForURL($url) {
		$parts = @parse_url($url);
		$path = isset($parts['path']) ? $parts['path'] : '';

		if(stripos($path, BASE_URL) === 0) {
			$path = substr($path, strlen(BASE_URL));
		}

		$path = trim($path, '/');
		return $path ? "$path.html" : 'index.html';
	}

}
<commit_msg>Simplify URL parsing code.<commit_after><?php

namespace SilverStripeAustralia\S3Publisher;

use Convert;
use Director;

/**
 * A static publish queue compatible extension which publishes the website to a bucket.
 */
class Publisher extends \DataExtension {

	/**
	 * @var BucketWebsite
	 */
	private $bucket;

	public function __construct(BucketWebsite $bucket) {
		$this->bucket = $bucket;
		parent::__construct();
	}

	public function publishPages(array $urls) {
		$result = array();

		foreach($urls as $url) {
			try {
				$response = Director::test($url);
			} catch(\SS_HTTPResponse_Exception $e) {
				$response = $e->getResponse();
			}

			$data = array(
				'statuscode' => $response->getStatusCode(),
				'redirect' => null
			);

			if(substr($response->getStatusCode(), 0, 1) == '3') {
				$data['redirect'] = $response->getHeader('Location');

				$body = sprintf(
					'<meta http-equiv="refresh" content="0; URL=%s">',
					Convert::raw2htmlatt(Director::absoluteURL($data['redirect']))
				);
			} else {
				$body = $response->getBody();
			}

			$model = $this->bucket->upload($this->getKeyForURL($url), $body, array(
				'ContentType' => $response->getHeader('Content-Type')
			));

			$result[$url] = $data + array('path' => $model['ObjectURL']);
		}

		return $result;
	}

	private function getKeyForURL($url) {
		$path = parse_url($url, PHP_URL_PATH);

		if(stripos($path, BASE_URL) === 0) {
			$path = substr($path, strlen(BASE_URL));
		}

		return ($path = trim($path, '/')) ? "$path.html" : 'index.html';
	}

}

{'fraction_non_alphanumeric': 0.1888853196273691, 'fraction_numerical': 0.004497269514937359, 'mean_word_length': 3.8962264150943398, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, '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': '4287677', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1026, 'n_words': 210}