<commit_before><?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Dates_Extension extends ExtensionBase
{
		
	private $minYear = 9999999;
	private $maxYear = 0;

	private $date_mappings = array(
		"dc.created" => "Created",
		"dc.published" => "Published",
		"dc.available" => "Available",
		"dc.dateAccepted" => "Accepted",
		"dc.dateSubmitted" => "Submitted",
		"dc.issued" => "Issued",
		"dc.valid" => "Valid",
	);

	function __construct($ro_pointer)
	{
		parent::__construct($ro_pointer);
	}		
		
	function processTemporal()
	{
		
		$this->minYear = 9999999;
		$this->maxYear = 0;
		$temporalArray = array();
		$sxml = $this->ro->getSimpleXML();
		$sxml->registerXPathNamespace("ro", RIFCS_NAMESPACE);
		$temporals = $sxml->xpath('//ro:date');
		foreach ($temporals AS $temporal)
		{
			$type = (string)$temporal["type"];
			$value = $this->getWTCdate((string)$temporal);
			if($value)
			$temporalArray[] = array('type'=>$type,'value'=>$value);
		}
		return $temporalArray;
	}
	
	function getEarliestAsYear()
	{
		//TODO: write the function :-)
		return $this->minYear;
	}

	function getLatestAsYear()
	{
		//TODO: write the function :-)
		return $this->maxYear;
	}

	function getWTCdate($value)
	{
		utc_timezone();
		// "Year and only year" (i.e. 1960) will be treated as HH SS by default
		if (strlen($value) == 4)
		{
			// Assume this is a year:
			$value = "Jan 1 " . $value;
		}

		if (($timestamp = strtotime($value)) === false) {
	    	return false;
		} else {
	     	$date = getDate($timestamp);
	     	if($date['year'] > $this->maxYear)
	     		$this->maxYear = $date['year'];
	     	if($date['year'] < $this->minYear)
	     		$this->minYear = $date['year'];
	     	return date('Y-m-d\TH:i:s\Z', $timestamp);
		}

		reset_timezone();
	}


	function extractDatesForDisplay($sxml)
	{

		/* Attempt to find a "nice" display for dates elements ('whole of collection dates') */
		$sxml->registerXPathNamespace("ro", RIFCS_NAMESPACE);
		$dates = $sxml->xpath('//ro:dates');
		foreach ($dates AS $date)
		{
			// Determine the prefix for this date:
			$friendly_date = "";
			if (isset($date["type"]))
			{
				if (isset($this->date_mappings[(string) $date["type"]]))
				{
					$friendly_date_prefix = $this->date_mappings[(string) $date["type"]] . ": ";
				}
				else
				{
					$friendly_date_prefix = (string) $date["type"] . ": ";
				}
			}

			$to = array();
			$from = array();
			$other = array();

			/* Witchcraft to determine a nicely displayed date */
			$friendly_date = $this->formatDates($date->{'date'});

			// Add to the extRif
			if ($friendly_date)
			{
				$date->addChild("extRif:friendly_date", $friendly_date_prefix . $friendly_date, EXTRIF_NAMESPACE);
			}
		}

		/* Attempt to find a "nice" display for temporal coverage dates */
		$sxml->registerXPathNamespace("ro", RIFCS_NAMESPACE);
		$temporals = $sxml->xpath('//ro:coverage/ro:temporal');
		foreach ($temporals AS $date)
		{
			// Determine the prefix for this date:
			$friendly_date = "";

			/* Witchcraft to determine a nicely displayed date */
			$friendly_date = $this->formatDates($date->{'date'}, true);

			// Add to the extRif
			if ($friendly_date)
			{
				$date->addChild("extRif:friendly_date", $friendly_date, EXTRIF_NAMESPACE);
			}
		}

		/* Attempt to find a "nice" display for existenceDates */
		$sxml->registerXPathNamespace("ro", RIFCS_NAMESPACE);
		$existenceDates = $sxml->xpath('//ro:existenceDates');
		foreach ($existenceDates AS $date)
		{
			$start = '';
			$end = '';
			if ($date->startDate)
			{
				if (!($start = $this->nicifyDate($date->startDate)))
				{
					$start = $date->startDate;
				}			
			}
			if ($date->endDate)
			{
				if (!($end = $this->nicifyDate($date->endDate)))
				{
					$end = $date->endDate;
				}			
			}

			if ($start && $end)
			{
				$friendly_date = $start . " - " . $end;
			}
			else
			{
				$friendly_date = $start . $end;
			}

			$date->addChild("extRif:friendly_date", $friendly_date, EXTRIF_NAMESPACE);
		}

		return $sxml;<commit_msg>Resolve issue with 4-digit existenceDates being parsed as times (Resolves CC-673)<commit_after><?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Dates_Extension extends ExtensionBase
{
		
	private $minYear = 9999999;
	private $maxYear = 0;

	private $date_mappings = array(
		"dc.created" => "Created",
		"dc.published" => "Published",
		"dc.available" => "Available",
		"dc.dateAccepted" => "Accepted",
		"dc.dateSubmitted" => "Submitted",
		"dc.issued" => "Issued",
		"dc.valid" => "Valid",
	);

	function __construct($ro_pointer)
	{
		parent::__construct($ro_pointer);
	}		
		
	function processTemporal()
	{
		
		$this->minYear = 9999999;
		$this->maxYear = 0;
		$temporalArray = array();
		$sxml = $this->ro->getSimpleXML();
		$sxml->registerXPathNamespace("ro", RIFCS_NAMESPACE);
		$temporals = $sxml->xpath('//ro:date');
		foreach ($temporals AS $temporal)
		{
			$type = (string)$temporal["type"];
			$value = $this->getWTCdate((string)$temporal);
			if($value)
			$temporalArray[] = array('type'=>$type,'value'=>$value);
		}
		return $temporalArray;
	}
	
	function getEarliestAsYear()
	{
		//TODO: write the function :-)
		return $this->minYear;
	}

	function getLatestAsYear()
	{
		//TODO: write the function :-)
		return $this->maxYear;
	}

	function getWTCdate($value)
	{
		utc_timezone();
		// "Year and only year" (i.e. 1960) will be treated as HH SS by default
		if (strlen($value) == 4)
		{
			// Assume this is a year:
			$value = "Jan 1 " . $value;
		}

		if (($timestamp = strtotime($value)) === false) {
	    	return false;
		} else {
	     	$date = getDate($timestamp);
	     	if($date['year'] > $this->maxYear)
	     		$this->maxYear = $date['year'];
	     	if($date['year'] < $this->minYear)
	     		$this->minYear = $date['year'];
	     	return date('Y-m-d\TH:i:s\Z', $timestamp);
		}

		reset_timezone();
	}


	function extractDatesForDisplay($sxml)
	{

		/* Attempt to find a "nice" display for dates elements ('whole of collection dates') */
		$sxml->registerXPathNamespace("ro", RIFCS_NAMESPACE);
		$dates = $sxml->xpath('//ro:dates');
		foreach ($dates AS $date)
		{
			// Determine the prefix for this date:
			$friendly_date = "";
			if (isset($date["type"]))
			{
				if (isset($this->date_mappings[(string) $date["type"]]))
				{
					$friendly_date_prefix = $this->date_mappings[(string) $date["type"]] . ": ";
				}
				else
				{
					$friendly_date_prefix = (string) $date["type"] . ": ";
				}
			}

			$to = array();
			$from = array();
			$other = array();

			/* Witchcraft to determine a nicely displayed date */
			$friendly_date = $this->formatDates($date->{'date'});

			// Add to the extRif
			if ($friendly_date)
			{
				$date->addChild("extRif:friendly_date", $friendly_date_prefix . $friendly_date, EXTRIF_NAMESPACE);
			}
		}

		/* Attempt to find a "nice" display for temporal coverage dates */
		$sxml->registerXPathNamespace("ro", RIFCS_NAMESPACE);
		$temporals = $sxml->xpath('//ro:coverage/ro:temporal');
		foreach ($temporals AS $date)
		{
			// Determine the prefix for this date:
			$friendly_date = "";

			/* Witchcraft to determine a nicely displayed date */
			$friendly_date = $this->formatDates($date->{'date'}, true);

			// Add to the extRif
			if ($friendly_date)
			{
				$date->addChild("extRif:friendly_date", $friendly_date, EXTRIF_NAMESPACE);
			}
		}

		/* Attempt to find a "nice" display for existenceDates */
		$sxml->registerXPathNamespace("ro", RIFCS_NAMESPACE);
		$existenceDates = $sxml->xpath('//ro:existenceDates');
		foreach ($existenceDates AS $date)
		{
			$start = '';
			$end = '';
			if ($date->startDate)
			{
				if (!($start = $this->nicifyDate($this->getWTCdate($date->startDate))))
				{
					$start = $date->startDate;
				}			
			}
			if ($date->endDate)
			{
				if (!($end = $this->nicifyDate($this->getWTCdate($date->endDate))))
				{
					$end = $date->endDate;
				}			
			}

			if ($start && $end)
			{
				$friendly_date = $start . " - " . $end;
			}
			else
			{
				$friendly_date = $start . $end;
			}

			$date->addChild("extRif:friendly_date", $friendly_date, EXTRIF_NAMESPACE);
		}

		return $sxml;
{'fraction_non_alphanumeric': 0.18221895664952242, 'fraction_numerical': 0.005878030859662013, 'mean_word_length': 3.3143159006867404, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 22}, 'pii_count': 0, 'substrings_counts': 4, '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': '3227627', 'n_tokens_mistral': 3466, 'n_tokens_neox': 2898, 'n_words': 587}