Skip to content

Commit

Permalink
Merging current changes (#195)
Browse files Browse the repository at this point in the history
* Disable git error messages in log file (#179)

* Remove extractGitVersion, use version.ini instead (#179)

* Omit timestamp in error logger

It's likely added by the operating system.

* Don't show timeline of no historicalDataDir

* Fix undefined variable

* Start Docker image (#192)

* Configuration without id is broken #194

---------

Co-authored-by: Jakob Voss <[email protected]>
  • Loading branch information
pkiraly and nichtich authored Jun 4, 2024
1 parent 5ec0660 commit 9c37ad9
Show file tree
Hide file tree
Showing 20 changed files with 143 additions and 25 deletions.
25 changes: 25 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# site files
configuration.cnf
images/
libs/
cache/
config/
vendor/
_smarty/
logs/

# data files
metadata-qa/

# editor files
.idea/

# development files
.git
.github
.gitignore
docker-compose.yml
Dockerfile
.dockerignore
tests
*.md
31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# includes most required extensions
FROM php:8.1-apache

# install locales, gettext, zip, yaml
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
locales gettext zlib1g-dev libzip-dev libyaml-dev \
&& locale-gen \
en_GB.UTF-8 && locale-gen de_DE.UTF-8 && locale-gen pt_BR.UTF-8 \
&& apt-get --assume-yes autoremove \
&& rm -rf /var/lib/apt/lists/* \
&& pecl install yaml \
&& docker-php-ext-enable yaml \
&& docker-php-ext-install gettext zip

# install composer from its official Docker image
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

# copy application files, install PHP libraries and initialize
USER www-data
WORKDIR /var/www/html

# TODO: chown www-data
COPY . .

RUN composer install --prefer-dist --no-dev \
&& composer clear-cache

RUN mkdir config metadata-qa \
&& echo dir=metadata-qa > configuration.cnf \
&& echo include=config/configuration.cnf >> configuration.cnf \
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ Additional configuration parameters are listed below. Data type and version numb
- `id` (string) the machine name of the data directory. By default, it comes from the URL as the path of the application
(qa-catalogue). With this parameter the administrator can overwrite the path. Note: this parameter was called `db`
previously. For compatibility reason we will support `db` as well for some time.
- `extractGitVersion` (bool, v0.8.0): a flag that enables to display the current git commit hash on the bottom of
the user interface. Default is `true`.

The following parameters can be set either for all catalogues (`parameter=value`) or for each individual catalogue (`parameter[id]=...`).

Expand Down Expand Up @@ -191,7 +189,15 @@ On Apache webserver add these lines to its configuration (`/etc/apache2/sites-av
</Directory>
```

You can access the application at `http://localhost/$APPDIR`.
If you don't want the application to check version number from local git
repository, create a (possibly empty) configuration file `version.ini`.
The file can also be generated with `composer run git-version`.

```bash
echo "catalogue=$CATALOG" >> configuration.cnf
```

Finally you can access the application at `http://localhost/$APPDIR`.

## Customization

Expand Down
1 change: 1 addition & 0 deletions classes/BaseTab.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function __construct(Configuration $configuration, string $id) {
}
if ($this->catalogue->getSchemaType() == 'UNIMARC' || is_null($this->historicalDataDir)) {
$this->display["history"] = false;
$this->display["timeline"] = false;
}
// TODO: disable more tabs when no data is available
}
Expand Down
2 changes: 1 addition & 1 deletion classes/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private function listFiles() {
$categories['Serial scores'] += $this->getSerialScoreHistograms();
$categories['T&T completeness'] += $this->getTtCompletenessHistograms();
$categories['Shelf-Ready completeness'] += $this->getShelfReadyCompletenessHistograms();
if ($this->displayShacl) {
if ($this->configuration->display("shacl")) {
$path = $this->getFilePath(Shacl::$paramsFile);
if (file_exists($path)) {
$shaclParameters = json_decode(file_get_contents($path));
Expand Down
4 changes: 2 additions & 2 deletions classes/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private function listFiles() {
$files = [];
$fileName = $this->grouped ? 'completeness-grouped-marc-elements.csv' : 'marc-elements.csv';
$elementsFile = $this->getFilePath($fileName);
error_log('elementsFile: ' . $elementsFile);
$this->log->info('elementsFile: ' . $elementsFile);
if (file_exists($elementsFile)) {
$raw_files = scandir(sprintf('images/%s', $this->id));
foreach ($raw_files as $file)
Expand All @@ -31,4 +31,4 @@ private function listFiles() {
}
return $files;
}
}
}
2 changes: 1 addition & 1 deletion classes/Timeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ private function getByTypeImages() {
}
return null;
}
}
}
11 changes: 10 additions & 1 deletion classes/Utils/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Utils;

use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\ErrorLogHandler;
use Exception;
Expand Down Expand Up @@ -52,6 +53,11 @@ public static function fromIniFile(string $file, array $defaults=[]) {
$ini['id'] = $ini['db'];
}

// set 'id' from 'dir', if 'id' is not available
if (!isset($ini['id']) && isset($ini['dir'])) {
$ini['id'] = $ini['dir'];
}

// merge in defaults
return new Configuration(array_merge($defaults, $ini));
}
Expand Down Expand Up @@ -189,7 +195,10 @@ public function createLogger($name): Logger {
if ($this->logHandler === "file") {
$logger->pushHandler(new StreamHandler($this->logFile, $this->logLevel));
} else {
$logger->pushHandler(new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, $this->logLevel));
$handler = new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, $this->logLevel);
$formatter = new LineFormatter('%channel%.%level_name%: %message% %context% %extra%');
$handler->setFormatter($formatter);
$logger->pushHandler($handler);
}
return $logger;
}
Expand Down
13 changes: 6 additions & 7 deletions classes/Utils/GitVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class GitVersion {

private static function extractVersion() {
return array_filter([
"version" => trim(`git tag --points-at HEAD | head -1` ?? ""),
"tag" => trim(`git tag --points-at HEAD | head -1` ?? ""),
"branch" => trim(`git rev-parse --abbrev-ref HEAD` ?? ""),
"clean" => trim(`git diff --quiet && echo true` ?? ""),
"commit" => trim(`git rev-parse HEAD` ?? ""),
"dirty" => trim(`git diff --quiet; echo $?` ?? ""),
"commit" => trim(`git rev-parse` ?? ""),
]);
}

Expand All @@ -24,13 +24,12 @@ public static function saveVersion() {
file_put_contents("version.ini", $ini);
}

public static function getVersion(bool $extractGitVersion = true) {
public static function getVersion() {
$version = [];
if (is_dir(".git") && $extractGitVersion) {
$version = self::extractVersion();
}
if (empty($version) && file_exists("version.ini")) {
$version = parse_ini_file("version.ini");
} else if (is_dir(".git")) {
$version = self::extractVersion();
}
return $version;
}
Expand Down
15 changes: 15 additions & 0 deletions classes/catalogue/Bnf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php


class Bnf extends Catalogue {

protected $name = 'bnpt';
protected $label = 'Biblioteca Nacional de Portugal (Portugal National Library)';
protected $url = 'https://www.bnportugal.gov.pt/';
protected $schemaType = "UNIMARC";

function getOpacLink($id, $record) {
return sprintf('http://id.bnportugal.gov.pt/bib/catbnp/%s', trim($id));
}
}

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"git push --follow-tags",
"git checkout develop"
],
"version": [
"git-version": [
"Utils\\GitVersion::saveVersion",
"cat version.ini"
]
Expand Down
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3'
services:
app:
build:
context: "."
ports:
# TODO: solr-port
- "${WEBPORT:-80}:80"
volumes:
- "${CONFIG:-./config}:/var/www/html/config"
- "${BASE_OUTPUT_DIR:-./metadata-qa}:/var/www/html/metadata-qa"
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
require_once 'common-functions.php';

$smarty = createSmarty('templates');
$smarty->assign('clientVersion', Utils\GitVersion::getVersion($configuration->doExtractGitVersion()));
$smarty->assign('clientVersion', Utils\GitVersion::getVersion());
$smarty->assign('templates', $configuration->getTemplates());

$tab = getOrDefault('tab', $configuration->getDefaultTab());
Expand Down
12 changes: 7 additions & 5 deletions templates/common/version.tpl
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<a href="https://github.com/pkiraly/qa-catalogue-web" target="_blank">qa-catalogue-web</a>
{if $clientVersion.version}
{if isset($clientVersion.tag)}
version
<a href="https://github.com/pkiraly/qa-catalogue-web/releases/tag/{$clientVersion.version}" target="_blank">{$clientVersion.version}</a>
{elseif $clientVersion.commit}
<a href="https://github.com/pkiraly/qa-catalogue-web/releases/tag/{$clientVersion.tag}" target="_blank">{$clientVersion.tag}</a>
{elseif isset($clientVersion.commit)}
version
<a href="https://github.com/pkiraly/qa-catalogue-web/commit/{$clientVersion.commit}" target="_blank">{$clientVersion.commit|substr:0:8}</a>@<a href="https://github.com/pkiraly/qa-catalogue-web/tree/{$clientVersion.branch}" target="_blank">{$clientVersion.branch}</a>
{elseif isset($clientVersion.branch)}
branch
<a href="https://github.com/pkiraly/qa-catalogue-web/tree/{$clientVersion.branch}" target="_blank">{$clientVersion.branch}</a>
{/if}
{if !$clientVersion.clean}
{if isset($clientVersion.dirty) && $clientVersion.dirty}
<em>+ local modifications</em>
{/if}

18 changes: 15 additions & 3 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ class ConfigurationTest extends \PHPUnit\Framework\TestCase {

public static function invalidFiles(): array {
return [
["tests/files/invalid1.cnf"],
["tests/files/invalid2.cnf"]
["tests/files/configuration/invalid1.cnf"],
["tests/files/configuration/invalid2.cnf"]
];
}

public function testValidConfiguration() {
$config = Configuration::fromIniFile("tests/files/valid1.cnf");
$config = Configuration::fromIniFile("tests/files/configuration/valid1.cnf");
$this->assertEquals($config->getDir(), 'new-value');
$this->assertEquals($config->getCatalogue(), 'abc');
$this->assertEquals($config->display("foo"), true);
Expand All @@ -22,6 +22,18 @@ public function testValidConfiguration() {
$this->assertEquals($config->display("undefined", true), true);
}

public function testValidConfiguration_embedded() {
$config = Configuration::fromIniFile("tests/files/configuration/valid2.cnf");
$this->assertEquals($config->getDir(), 'qa-catalogue');
$this->assertEquals($config->getId(), 'qa-catalogue');
$this->assertEquals($config->getCatalogue(), 'qa-catalogue');
$this->assertEquals($config->getDefaultTab(), "completeness");
$this->assertEquals($config->getLabel(), "My custom Catalogue");
$this->assertEquals($config->getUrl(), "https://my-catalogue.org");
$this->assertEquals($config->getLinkTemplate(), "https://my-catalogue.org/catalogue/{id}");
$this->assertEquals($config->getLanguage(), "de");
}

/**
* @dataProvider invalidFiles
*/
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions tests/files/configuration/valid2-embedded.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
default-tab=completeness
label=My custom Catalogue
url=https://my-catalogue.org
linkTemplate=https://my-catalogue.org/catalogue/{id}
language=de
2 changes: 2 additions & 0 deletions tests/files/configuration/valid2.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dir=qa-catalogue
include=tests/files/configuration/valid2-embedded.cnf

0 comments on commit 9c37ad9

Please sign in to comment.