Browse Source

repo.php: split code into class RepositoryDir

master
parent
commit
aaba36ebc9
  1. 1
      modulekit.php
  2. 53
      repo.php
  3. 48
      src/RepositoryDir.php

1
modulekit.php

@ -16,6 +16,7 @@ $include = array(
'src/ip-location.php',
'src/wikipedia.php',
'src/ImageLoader.php',
'src/RepositoryDir.php',
),
'css' => array(
'style.css',

53
repo.php

@ -19,73 +19,44 @@ if (!isset($_REQUEST['repo'])) {
$c = 0;
foreach ($repositories as $repoId => $repoData) {
print $c++ ? ',' : '';
$d = array();
foreach (array('name') as $k) {
if (array_key_exists($k, $repoData)) {
$d[$k] = $repoData[$k];
}
}
$repo = new RepositoryDir($repoId, $repoData);
print $c++ ? ',' : '';
print json_encode($repoId) . ':';
print json_encode($d, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_FORCE_OBJECT);
print json_encode($repo->info(), JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_FORCE_OBJECT);
}
print '}';
exit(0);
}
$repo = $_REQUEST['repo'];
if (!array_key_exists($repo, $repositories)) {
$repoId = $_REQUEST['repo'];
if (!array_key_exists($repoId, $repositories)) {
Header("HTTP/1.1 404 Repository not found");
exit(0);
}
$path = $repositories[$repo]['path'];
function newestTimestamp ($path) {
$ts = 0;
$d = opendir($path);
while ($f = readdir($d)) {
$t = filemtime("{$path}/{$f}");
if ($t > $ts) {
$ts = $t;
}
}
closedir($d);
return $ts;
}
$repo = new RepositoryDir($repoId, $repositories[$repoId]);
$cacheDir = null;
$ts = newestTimestamp($path);
$ts = $repo->newestTimestamp($path);
if (isset($config['cache'])) {
$cacheDir = "{$config['cache']}/repo";
@mkdir($cacheDir);
$cacheTs = filemtime("{$cacheDir}/{$repo}.json");
$cacheTs = filemtime("{$cacheDir}/{$repoId}.json");
if ($cacheTs === $ts) {
Header("Content-Type: application/json; charset=utf-8");
readfile("{$cacheDir}/{$repo}.json");
readfile("{$cacheDir}/{$repoId}.json");
exit(0);
}
}
$data = array();
$d = opendir($path);
while ($f = readdir($d)) {
if (preg_match("/^([0-9a-zA-Z_\-]+)\.json$/", $f, $m) && $f !== 'package.json') {
$d1 = json_decode(file_get_contents("{$path}/{$f}"), true);
$data[$m[1]] = jsonMultilineStringsJoin($d1, array('exclude' => array(array('const'))));
}
}
closedir($d);
$data = $repo->data();
$ret = json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
Header("Content-Type: application/json; charset=utf-8");
print $ret;
file_put_contents("{$cacheDir}/{$repo}.json", $ret);
touch("{$cacheDir}/{$repo}.json", $ts);
file_put_contents("{$cacheDir}/{$repoId}.json", $ret);
touch("{$cacheDir}/{$repoId}.json", $ts);

48
src/RepositoryDir.php

@ -0,0 +1,48 @@
<?php
class RepositoryDir {
function __construct ($id, $def) {
$this->def = $def;
$this->path = $def['path'];
}
function info () {
$ret = array();
foreach (array('name') as $k) {
if (array_key_exists($k, $this->def)) {
$ret[$k] = $this->def[$k];
}
}
return $ret;
}
function newestTimestamp () {
$ts = 0;
$d = opendir($this->path);
while ($f = readdir($d)) {
$t = filemtime("{$this->path}/{$f}");
if ($t > $ts) {
$ts = $t;
}
}
closedir($d);
return $ts;
}
function data () {
$data = array();
$d = opendir($this->path);
while ($f = readdir($d)) {
if (preg_match("/^([0-9a-zA-Z_\-]+)\.json$/", $f, $m) && $f !== 'package.json') {
$d1 = json_decode(file_get_contents("{$this->path}/{$f}"), true);
$data[$m[1]] = jsonMultilineStringsJoin($d1, array('exclude' => array(array('const'))));
}
}
closedir($d);
return $data;
}
}
Loading…
Cancel
Save