diff --git a/shiftPres.php b/shiftPres.php new file mode 100644 index 0000000..2ce3022 --- /dev/null +++ b/shiftPres.php @@ -0,0 +1,126 @@ +CET = new DateTimeZone('Europe/Berlin'); + $this->vor = $vor; + $this->vorMd = file($vor, FILE_IGNORE_NEW_LINES); + $vorBase = basename($vor, '.md'); + $this->vorT = $vorBase . '.txt'; + $this->vorTxt = []; + $this->pastT = $past; + $this->pastMd = file($this->pastT, FILE_IGNORE_NEW_LINES); + $this->exit = 0; // Default return value + } + + // Save "vor.txt" and "past.md" before leaving + public function save() { + // Backup files first + copy($this->vorT, $this->vorT . '.bak'); + copy($this->pastT, $this->pastT . '.bak'); + file_put_contents($this->vorT, implode(PHP_EOL, $this->vorTxt) . PHP_EOL); + file_put_contents($this->pastT, implode(PHP_EOL, $this->pastMd) . PHP_EOL); + // Signal to calling bash script to put files into git + $this->exit = 1; + } + + // Shift from "vor.md" to "past.md", creating "vor.txt" along the way + public function shift() { + $yester = (new DateTime('now', $this->CET))->modify('- 1 day'); + $yester->setTime(0, 0); + // For correct insertion into past array + $past = $this->findPast(); + if ($past < 0) { + printStderr("Can't locate insert position in $this->pastT, exiting"); + } + + // Loop through "vor.md" and check if yesterday is 4th Tuesday => shift to "past.md" + // Otherwise copy to "vor.txt", including custom date if found (as in: not 4th Tuesday) + foreach ($this->vorMd as $line) { + $fields = array_filter(explode('|', $line)); + $ff = explode(', ', $fields[1]); + // Does 1st field contain date? + if (count($ff) > 1) { + $dat = trim($ff[1]); + // If so, in valid format + if (preg_match(self::PREG, $dat)) { + $dp = new DateTime($dat, $this->CET); + $dp->setTime(0, 0); + // if we are not invoked on the morning of the 4th Wednesday, copy to "vor.txt" + if ($dp != $yester) { + // If manually set date doesn't equal fourth Tuesday, copy + if ($dp != $this->get4Tuesday($dat)) { + $str0 = '|' . implode($fields, '|') . '|'; + } else { + // Otherwise, only copy text + $str0 = '|' . implode(array_slice($fields, 1), '|') . '|'; + } + $this->vorTxt[] = $str0; + } else { + // Shift to "past.md" + array_splice($this->pastMd, $past, 0, $line); + } + } + } + } + } + + // Check if "vor.txt" and "past.md" need to be saved (length of "vor.md" and "vor.txt" differ => line shifted) + public function checkSave() { + // Take header of "vor.md" into account + return count($this->vorTxt) != count($this->vorMd)-2; + } + + public function getExit() { + return $this->exit; + } + + // Find first entry in PAST + private function findPast() { + for ($i = 0; $i < count($this->pastMd); $i++) { + $fields = array_filter(explode('|', $this->pastMd[$i])); + $ff = explode(', ', $fields[1]); + if (count($ff) > 1) { + $dat = trim($ff[1]); + if (preg_match(self::PREG, $dat)) { + return $i; + } + } + } + return -1; + } + + private function get4Tuesday($dat) { + // This is for PHP >= 8.1 as the old strftime will be deprecated by then, so use datefmt_format_object if we are running on PHP beyond version 7 + $date_time = new DateTime($dat, $this->CET); + $first_day = $date_time->modify('first day of this month'); + $ts = $first_day->getTimeStamp(); + if (intval(explode(".", phpversion())[0]) >= 7) { + $date_time->setTimestamp(strtotime('fourth tuesday of this month', $ts)); + $tuesday = datefmt_format_object($date_time, 'd.M.yyyy ', 'de_DE.utf8'); + } else { + $tuesday = strftime('%d.%m.%Y ', strtotime('fourth tuesday of this month', $ts)); + } + return new DateTime($tuesday); + } +} + +setlocale(LC_TIME, 'de_DE.utf8'); +date_default_timezone_set('Europe/Berlin'); +$shift = new ShiftPres(); +$shift->shift(); +if ($shift->checkSave()) { + $shift->save(); +} +// Signal to calling bash script +exit($shift->getExit()); +?>