Compare commits

..

12 Commits

20 changed files with 106 additions and 241 deletions

View File

@ -1,11 +1,11 @@
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
UPSTREAM=dev74
DEVELOP=dev82
FEATURE=wip82/
RELEASE=rel82-
MAIN=dist82
TAG_SUFFIX=p82
HOTFIX=hotf82-
UPSTREAM=
DEVELOP=dev74
FEATURE=wip74/
RELEASE=rel74-
MAIN=dist74
TAG_SUFFIX=p74
HOTFIX=hotf74-
DIST=
NOAUTO=

View File

@ -4,5 +4,5 @@
RUNPHP=
# Si RUNPHP n'est pas défini, les variables suivantes peuvent être définies
DIST=d12
DIST=d11
#REGISTRY=pubdocker.univ-reunion.fr/dist

View File

@ -19,8 +19,11 @@ git checkout dev82
prel -C
commit="$(git log --grep="Init changelog . version ${version}p82" --format=%H)"
commit="$(git log --grep="Init changelog . version ${version}p82" --format=%H)" &&
echo "commit=$commit"
git checkout dev74
git cherry-pick "$commit"
pp -a
~~~

View File

@ -426,7 +426,7 @@ EOF
$(qvals echo "$(awk <"$changelog" '
BEGIN { p = 0 }
p == 0 && $0 == "" { p = 1; next }
p == 1 { gsub(/\$/, "\\$", $0); print }
p == 1 { print }
')") >CHANGES.md
git add CHANGES.md
EOF

View File

@ -15,12 +15,12 @@
}
},
"require": {
"symfony/yaml": "^7.1",
"symfony/yaml": "^5.0",
"ext-json": "*",
"php": "^8.2"
"php": "^7.4"
},
"require-dev": {
"nulib/tests": "^8.2",
"nulib/tests": "^7.4",
"ext-posix": "*",
"ext-pcntl": "*",
"ext-curl": "*",

View File

@ -177,6 +177,14 @@ class A {
return $pvalue;
}
static final function shift(?array &$dest, int $count=1, $default=null) {
if ($dest === null) return null;
$values = array_slice($dest, 0, $count);
$dest = array_slice($dest, $count);
if ($values === []) return $default;
else return $count == 1? $values[0]: $values;
}
static final function pop(&$dest, $key, $default=null) {
if ($dest === null) return $default;
self::ensure_narray($dest);

View File

@ -86,6 +86,12 @@ class cv {
$b = $tmp;
}
/** cloner une valeur */
static final function clone($value) {
if (is_object($value)) $value = clone $value;
return $value;
}
#############################################################################
/** mettre à jour $dest avec $value si $cond($value) est vrai */
@ -197,19 +203,29 @@ class cv {
}
/**
* retourner [$bool, $string, $array] initialisés chacun en fonction du type
* retourner [$bool, $scalar, $array] initialisés chacun en fonction du type
* de $value.
*
* @throws ValueException si $value n'est d'aucun de ces types
*/
static final function check_bsa($value, ?string $prefix=null, bool $throw_exception=true): array {
$bool = is_bool($value)? $value : null;
$string = is_string($value)? $value : null;
$scalar = !is_bool($value) && is_scalar($value)? $value : null;
$array = is_array($value)? $value : null;
if ($bool === null && $string === null && $array === null && $throw_exception) {
if ($bool === null && $scalar === null && $array === null && $throw_exception) {
throw ValueException::invalid_kind($value, "value", $prefix);
} else {
return [$bool, $string, $array];
return [$bool, $scalar, $array];
}
}
static final function subclass_of($value, $class): bool {
if (is_string($value)) {
return $value === $class || is_subclass_of($value, $class);
} elseif (is_object($value)) {
return $value instanceof $class;
} else {
return false;
}
}

View File

@ -1,6 +1,7 @@
<?php
namespace nulib\db;
use nulib\cl;
use nulib\php\func;
use nulib\ValueException;
use Traversable;
@ -168,6 +169,13 @@ class Capacitor implements ITransactor {
return $this->storage->_delete($this->channel, $filter, $func, $args);
}
function dbUpdate(array $update) {
return $this->storage->db()->exec(cl::merge([
"update",
"table" => $this->getTableName(),
], $update));
}
function close(): void {
$this->storage->close();
}

View File

@ -13,7 +13,9 @@ class CapacitorChannel implements ITransactor {
const TABLE_NAME = null;
const COLUMN_DEFINITIONS = null;
protected function COLUMN_DEFINITIONS(): ?array {
return static::COLUMN_DEFINITIONS;
} const COLUMN_DEFINITIONS = null;
const PRIMARY_KEYS = null;
@ -28,19 +30,17 @@ class CapacitorChannel implements ITransactor {
static function verifix_name(?string &$name, ?string &$tableName=null): void {
if ($name !== null) {
$name = strtolower($name);
if ($tableName === null) {
$tableName = str_replace("-", "_", $name) . "_channel";
}
$tableName ??= str_replace("-", "_", $name) . "_channel";
} else {
$name = static::class;
if ($name === self::class) {
$name = "default";
if ($tableName === null) $tableName = "default_channel";
$tableName ??= "default_channel";
} else {
$name = preg_replace('/^.*\\\\/', "", $name);
$name = preg_replace('/Channel$/', "", $name);
$name = lcfirst($name);
if ($tableName === null) $tableName = str::camel2us($name);
$tableName ??= str::camel2us($name);
$name = strtolower($name);
}
}
@ -63,7 +63,7 @@ class CapacitorChannel implements ITransactor {
$this->useCache = static::USE_CACHE;
$this->setup = false;
$this->created = false;
$columnDefinitions = cl::withn(static::COLUMN_DEFINITIONS);
$columnDefinitions = $this->COLUMN_DEFINITIONS();
$primaryKeys = cl::withn(static::PRIMARY_KEYS);
$migration = cl::withn(static::MIGRATION);
$lastMkey = 1;
@ -482,6 +482,10 @@ class CapacitorChannel implements ITransactor {
return $this->capacitor->delete($filter, $func, $args);
}
function dbUpdate(array $update) {
return $this->capacitor->dbUpdate($update);
}
function close(): void {
$this->capacitor->close();
}

View File

@ -3,7 +3,6 @@ namespace nulib\db;
use nulib\cl;
use nulib\db\_private\_migration;
use nulib\db\cache\cache;
use nulib\php\func;
use nulib\ValueException;
use Traversable;

View File

@ -1,116 +0,0 @@
<?php
namespace nulib\db\cache;
use nulib\cl;
use nulib\db\CapacitorChannel;
use nulib\php\time\DateTime;
use nulib\php\time\Delay;
class CacheChannel extends CapacitorChannel {
/** @var int durée de vie par défaut du cache */
const DURATION = "1D"; // jusqu'au lendemain
const INCLUDES = null;
const EXCLUDES = null;
const COLUMN_DEFINITIONS = [
"group_id" => "varchar(64) not null",
"id" => "varchar(64) not null",
"date_start" => "datetime",
"duration_" => "text",
"primary key (group_id, id)",
];
static function get_cache_ids($id): array {
if (is_array($id)) {
$keys = array_keys($id);
if (array_key_exists("group_id", $id)) $groupIdKey = "group_id";
else $groupIdKey = $keys[1] ?? null;
$groupId = $id[$groupIdKey] ?? "";
if (array_key_exists("id", $id)) $idKey = "id";
else $idKey = $keys[0] ?? null;
$id = $id[$idKey] ?? "";
} else {
$groupId = "";
}
if (preg_match('/^(.*\\\\)?([^\\\\]+)$/', $groupId, $ms)) {
# si le groupe est une classe, faire un hash du package pour limiter la
# longueur du groupe
[$package, $groupId] = [$ms[1], $ms[2]];
$package = substr(md5($package), 0, 4);
$groupId = "${groupId}_$package";
}
return ["group_id" => $groupId, "id" => $id];
}
function __construct(?string $duration=null, ?string $name=null) {
parent::__construct($name);
$this->duration = $duration ?? static::DURATION;
$this->includes = static::INCLUDES;
$this->excludes = static::EXCLUDES;
}
protected string $duration;
protected ?array $includes;
protected ?array $excludes;
function getItemValues($item): ?array {
return cl::merge(self::get_cache_ids($item), [
"item" => null,
]);
}
function onCreate($item, array $values, ?array $alwaysNull, ?string $duration=null): ?array {
$now = new DateTime();
$duration ??= $this->duration;
return [
"date_start" => $now,
"duration" => new Delay($duration, $now),
];
}
function onUpdate($item, array $values, array $pvalues, ?string $duration=null): ?array {
$now = new DateTime();
$duration ??= $this->duration;
return [
"date_start" => $now,
"duration" => new Delay($duration, $now),
];
}
function shouldUpdate($id, bool $noCache=false): bool {
if ($noCache) return true;
$cacheIds = self::get_cache_ids($id);
$groupId = $cacheIds["group_id"];
if ($groupId) {
$includes = $this->includes;
$shouldInclude = $includes !== null && in_array($groupId, $includes);
$excludes = $this->excludes;
$shouldExclude = $excludes !== null && in_array($groupId, $excludes);
if (!$shouldInclude || $shouldExclude) return true;
}
$found = false;
$expired = false;
$this->each($cacheIds,
function($item, $values) use (&$found, &$expired) {
$found = true;
$expired = $values["duration"]->isElapsed();
});
return !$found || $expired;
}
function setCached($id, ?string $duration=null): void {
$cacheIds = self::get_cache_ids($id);
$this->charge($cacheIds, null, [$duration]);
}
function resetCached($id) {
$cacheIds = self::get_cache_ids($id);
$this->delete($cacheIds);
}
}

View File

@ -1,51 +0,0 @@
<?php
namespace nulib\db\cache;
use Closure;
use IteratorAggregate;
use nulib\cl;
use nulib\db\CapacitorChannel;
use Traversable;
class RowsChannel extends CapacitorChannel implements IteratorAggregate {
const COLUMN_DEFINITIONS = [
"key" => "varchar(128) primary key not null",
"all_values" => "mediumtext",
];
function __construct($id, callable $builder, ?string $duration=null) {
$this->cacheIds = $cacheIds = CacheChannel::get_cache_ids($id);
$this->builder = Closure::fromCallable($builder);
$this->duration = $duration;
$name = "{$cacheIds["group_id"]}-{$cacheIds["id"]}";
parent::__construct($name);
}
protected array $cacheIds;
protected Closure $builder;
protected ?string $duration = null;
function getItemValues($item): ?array {
$key = array_keys($item)[0];
$row = $item[$key];
return [
"key" => $key,
"item" => $row,
"all_values" => implode(" ", cl::filter_n(cl::with($row))),
];
}
function getIterator(): Traversable {
$cm = cache::get();
if ($cm->shouldUpdate($this->cacheIds)) {
$this->capacitor->reset();
foreach (($this->builder)() as $key => $row) {
$this->charge([$key => $row]);
}
$cm->setCached($this->cacheIds, $this->duration);
}
return $this->discharge(false);
}
}

View File

@ -1,37 +0,0 @@
<?php
namespace nulib\db\cache;
use nulib\db\Capacitor;
use nulib\db\CapacitorStorage;
use nulib\db\sqlite\SqliteStorage;
class cache {
protected static ?CapacitorStorage $storage = null;
static function set_storage(CapacitorStorage $storage): CapacitorStorage {
return self::$storage = $storage;
}
protected static function get_storage(): CapacitorStorage {
return self::$storage ??= new SqliteStorage("");
}
protected static ?CacheChannel $channel = null;
static function set(?CacheChannel $channel): CacheChannel {
$channel ??= new CacheChannel();
new Capacitor(self::get_storage(), $channel);
return self::$channel = $channel;
}
static function get(): CacheChannel {
if (self::$channel !== null) return self::$channel;
else return self::set(null);
}
static function new(?RowsChannel $channel, $id=null, ?callable $builder=null): RowsChannel {
$channel ??= new RowsChannel($id, $builder);
new Capacitor(self::get_storage(), $channel);
return $channel;
}
}

View File

@ -30,7 +30,11 @@ class Pgsql implements IDatabase {
protected const OPTIONS = [
"persistent" => true,
# XXX désactiver les connexions persistantes par défaut
# pour réactiver par défaut, il faudrait vérifier la connexion à chaque fois
# qu'elle est ouverte avec un "select 1". en effet, l'expérience jusqu'ici
# est que la première connexion après un long timeout échoue
"persistent" => false,
"force_new" => false,
"serial_support" => true,
];

View File

@ -40,4 +40,6 @@ interface IReader extends _IFile {
function unserialize(?array $options=null, bool $close=true, bool $alreadyLocked=false);
function copyTo(IWriter $dest, bool $closeWriter=false, bool $closeReader=true): void;
function setCsvFlavour(?string $flavour): void;
}

View File

@ -299,8 +299,8 @@ class Stream extends AbstractIterator implements IReader, IWriter {
/** retourner le contenu du fichier sous forme de chaine */
function getContents(bool $close=true, bool $alreadyLocked=false): string {
$useLocking = $this->useLocking;
if ($useLocking && !$alreadyLocked) $this->lock(LOCK_SH);
$useLocking = $this->useLocking && !$alreadyLocked;
if ($useLocking) $this->lock(LOCK_SH);
try {
return IOException::ensure_valid(stream_get_contents($this->fd), $this->throwOnError);
} finally {
@ -380,7 +380,9 @@ class Stream extends AbstractIterator implements IReader, IWriter {
/** @throws IOException */
function ftruncate(int $size=0, bool $rewind=true): self {
$fd = $this->getResource();
IOException::ensure_valid(ftruncate($fd, $size), $this->throwOnError);
$r = ftruncate($fd, $size);
$this->stat = null;
IOException::ensure_valid($r, $this->throwOnError);
if ($rewind) rewind($fd);
return $this;
}
@ -390,6 +392,7 @@ class Stream extends AbstractIterator implements IReader, IWriter {
$fd = $this->getResource();
if ($length === null) $r = fwrite($fd, $data);
else $r = fwrite($fd, $data, $length);
$this->stat = null;
return IOException::ensure_valid($r, $this->throwOnError);
}
@ -403,9 +406,13 @@ class Stream extends AbstractIterator implements IReader, IWriter {
$line[] = strval($col);
}
$line = implode($sep, $line);
IOException::ensure_valid(fwrite($fd, "$line\n"), $this->throwOnError);
$r = fwrite($fd, "$line\n");
$this->stat = null;
IOException::ensure_valid($r, $this->throwOnError);
} else {
IOException::ensure_valid(fputcsv($fd, $row, $params[0], $params[1], $params[2]), $this->throwOnError);
$r = fputcsv($fd, $row, $params[0], $params[1], $params[2]);
$this->stat = null;
IOException::ensure_valid($r, $this->throwOnError);
}
}
@ -471,8 +478,8 @@ class Stream extends AbstractIterator implements IReader, IWriter {
}
function putContents(string $contents, bool $close=true, bool $alreadyLocked=false): void {
$useLocking = $this->useLocking;
if ($useLocking && !$alreadyLocked) $this->lock(LOCK_EX);
$useLocking = $this->useLocking && !$alreadyLocked;
if ($useLocking) $this->lock(LOCK_EX);
try {
$this->fwrite($contents);
} finally {

View File

@ -2,7 +2,9 @@
namespace nulib\file\csv;
use nulib\file;
use nulib\file\_IFile;
use nulib\file\FileReader;
use nulib\file\IReader;
use nulib\file\tab\AbstractReader;
use nulib\file\tab\TAbstractReader;
@ -20,7 +22,12 @@ class CsvReader extends AbstractReader {
protected ?string $inputEncoding;
function getIterator() {
$reader = new FileReader(file::fix_dash($this->input));
$input = $this->input;
if ($input instanceof IReader) {
$reader = $input;
} else {
$reader = new FileReader(file::fix_dash($input));
}
$inputEncoding = $this->inputEncoding;
if ($inputEncoding !== null) {
$reader->appendFilter("convert.iconv.$inputEncoding.utf-8");

View File

@ -478,11 +478,17 @@ class func {
}
static function with($func, ?array $args=null, bool $strict=true): self {
if ($func instanceof self) return $func;
$func = self::_with($func, $args, $strict, $reason);
if ($func !== null) return $func;
throw self::not_a_callable($func, $reason);
}
static function withn($func, ?array $args=null, bool $strict=true): ?self {
if ($func === null) return null;
else return self::with($func, $args, $strict);
}
static function ensure($func, ?array $args=null, bool $strict=true): self {
$func = self::with($func, $args, $strict);
if (!$func->isBound()) {

View File

@ -29,7 +29,7 @@ class DateInterval extends \DateInterval {
}
function __construct($duration) {
if (is_int($duration)) $duration = "PT${duration}S";
if (is_numeric($duration)) $duration = "PT${duration}S";
if ($duration instanceof \DateInterval) {
$this->y = $duration->y;
$this->m = $duration->m;

View File

@ -3,6 +3,7 @@ namespace nulib\php\time;
use DateTimeInterface;
use InvalidArgumentException;
use nulib\ValueException;
/**
* Class Delay: une durée jusqu'à un moment destination. le moment destination
@ -115,6 +116,10 @@ class Delay {
$this->repr = $repr;
}
function __clone() {
$this->dest = clone $this->dest;
}
function __serialize(): array {
return [$this->dest, $this->repr];
}
@ -130,7 +135,7 @@ class Delay {
}
function addDuration($duration) {
if (is_int($duration) && $duration < 0) {
if (is_numeric($duration) && $duration < 0) {
$this->dest->sub(DateInterval::with(-$duration));
} else {
$this->dest->add(DateInterval::with($duration));
@ -138,7 +143,7 @@ class Delay {
}
function subDuration($duration) {
if (is_int($duration) && $duration < 0) {
if (is_numeric($duration) && $duration < 0) {
$this->dest->add(DateInterval::with(-$duration));
} else {
$this->dest->sub(DateInterval::with($duration));