univ-pau-ics/build/service/lib/parse.php

79 lines
1.7 KiB
PHP
Raw Normal View History

2017-09-12 07:49:38 +00:00
<?php
// Absolute root dir
$ABSROOT = dirname(__DIR__);
require_once "$ABSROOT/lib/months.php";
/* [1] Check arguments
=========================================================*/
/* (1) If not enough -> abort */
if( $argc <= 2 )
exit(1);
/* (2) If @type incorrect */
if( !in_array($argv[1], ['cursus', 'period']) )
die(" * incorrect file type\n");
/* (3) Check given file */
if( !file_exists($argv[2]) )
die(" * file does not exist\n");
/* [2] Try to access file's content
=========================================================*/
/* (1) Read file */
$raw = file_get_contents($argv[2]);
/* (2) Manage error */
if( !is_string($raw) )
die(" * Cannot read file\n");
/* (3) Split by lines */
$raw = explode("\n", $raw);
/* [3] Parse data
=========================================================*/
/* (1) Prepare the output list */
$list = [];
/* (2) Cursur parser */
if( $argv[1] == 'cursus' ){
foreach($raw as $line)
if( preg_match('@"grDiplome","(?:&lt;)?(.+)(?:&gt;)?","T(.+)"@i', $line, $m) )
$list[ html_entity_decode($m[1]) ] = "T${m[2]}";
/* (3) Period parser */
}else{
$first = null;
foreach($raw as $line){
if( preg_match('@"T\d+","du \d+ au (\d+) ([a-z]+) (\d+)","T\d+S(.+)"@i', $line, $m) ){
/* (3.1) Translate month */
$month = $monthList[$m[2]];
/* (3.2) Get date of monday */
$list[ "S${m[4]}" ] = date('d-m-Y', strtotime("${m[1]}-$month-${m[3]} - 5 days"));
}
}
}
/* [4] Write data back
=========================================================*/
if( !file_put_contents("$ABSROOT/../../tmp/${argv[1]}.json", json_encode($list)) )
die(" * Cannot write parsed data back\n");
exit(0);