2018-09-17 11:47:05 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Utility class for handling metadata
|
|
|
|
*/
|
|
|
|
abstract class H5PMetadata {
|
|
|
|
|
2018-09-21 13:08:11 +02:00
|
|
|
private static $fields = array(
|
2018-09-17 12:54:21 +02:00
|
|
|
'title' => array(
|
|
|
|
'type' => 'text',
|
|
|
|
'maxLength' => 255
|
|
|
|
),
|
2020-08-28 10:30:06 +02:00
|
|
|
'a11yTitle' => array(
|
|
|
|
'type' => 'text',
|
|
|
|
'maxLength' => 255,
|
|
|
|
),
|
2018-09-17 11:47:05 +02:00
|
|
|
'authors' => array(
|
|
|
|
'type' => 'json'
|
|
|
|
),
|
|
|
|
'changes' => array(
|
|
|
|
'type' => 'json'
|
|
|
|
),
|
|
|
|
'source' => array(
|
|
|
|
'type' => 'text',
|
|
|
|
'maxLength' => 255
|
|
|
|
),
|
|
|
|
'license' => array(
|
|
|
|
'type' => 'text',
|
|
|
|
'maxLength' => 32
|
|
|
|
),
|
|
|
|
'licenseVersion' => array(
|
|
|
|
'type' => 'text',
|
|
|
|
'maxLength' => 10
|
|
|
|
),
|
|
|
|
'licenseExtras' => array(
|
|
|
|
'type' => 'text',
|
|
|
|
'maxLength' => 5000
|
|
|
|
),
|
|
|
|
'authorComments' => array(
|
|
|
|
'type' => 'text',
|
|
|
|
'maxLength' => 5000
|
|
|
|
),
|
|
|
|
'yearFrom' => array(
|
|
|
|
'type' => 'int'
|
|
|
|
),
|
|
|
|
'yearTo' => array(
|
|
|
|
'type' => 'int'
|
2019-02-21 13:42:00 +01:00
|
|
|
),
|
|
|
|
'defaultLanguage' => array(
|
|
|
|
'type' => 'text',
|
|
|
|
'maxLength' => 32,
|
2018-09-17 11:47:05 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-09-17 13:44:42 +02:00
|
|
|
/**
|
|
|
|
* JSON encode metadata
|
|
|
|
*
|
|
|
|
* @param object $content
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function toJSON($content) {
|
|
|
|
// Note: deliberatly creating JSON string "manually" to improve performance
|
|
|
|
return
|
|
|
|
'{"title":' . (isset($content->title) ? json_encode($content->title) : 'null') .
|
2020-08-28 10:30:06 +02:00
|
|
|
',"a11yTitle":' . (isset($content->a11y_title) ? $content->a11y_title : 'null') .
|
2018-09-17 13:44:42 +02:00
|
|
|
',"authors":' . (isset($content->authors) ? $content->authors : 'null') .
|
|
|
|
',"source":' . (isset($content->source) ? '"' . $content->source . '"' : 'null') .
|
|
|
|
',"license":' . (isset($content->license) ? '"' . $content->license . '"' : 'null') .
|
|
|
|
',"licenseVersion":' . (isset($content->license_version) ? '"' . $content->license_version . '"' : 'null') .
|
|
|
|
',"licenseExtras":' . (isset($content->license_extras) ? json_encode($content->license_extras) : 'null') .
|
|
|
|
',"yearFrom":' . (isset($content->year_from) ? $content->year_from : 'null') .
|
|
|
|
',"yearTo":' . (isset($content->year_to) ? $content->year_to : 'null') .
|
|
|
|
',"changes":' . (isset($content->changes) ? $content->changes : 'null') .
|
2019-03-21 10:30:09 +01:00
|
|
|
',"defaultLanguage":' . (isset($content->default_language) ? '"' . $content->default_language . '"' : 'null') .
|
2018-09-17 13:44:42 +02:00
|
|
|
',"authorComments":' . (isset($content->author_comments) ? json_encode($content->author_comments) : 'null') . '}';
|
|
|
|
}
|
|
|
|
|
2018-09-17 11:47:05 +02:00
|
|
|
/**
|
|
|
|
* Make the metadata into an associative array keyed by the property names
|
|
|
|
* @param mixed $metadata Array or object containing metadata
|
2018-09-17 12:54:21 +02:00
|
|
|
* @param bool $include_title
|
2018-11-01 14:42:12 +01:00
|
|
|
* @param bool $include_missing For metadata fields not being set, skip 'em.
|
|
|
|
* Relevant for content upgrade
|
2018-09-17 12:54:21 +02:00
|
|
|
* @param array $types
|
2018-09-17 11:47:05 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-11-01 14:42:12 +01:00
|
|
|
public static function toDBArray($metadata, $include_title = true, $include_missing = true, &$types = array()) {
|
2018-09-17 11:47:05 +02:00
|
|
|
$fields = array();
|
|
|
|
|
|
|
|
if (!is_array($metadata)) {
|
|
|
|
$metadata = (array) $metadata;
|
|
|
|
}
|
|
|
|
|
2018-09-21 13:08:11 +02:00
|
|
|
foreach (self::$fields as $key => $config) {
|
2018-09-17 12:54:21 +02:00
|
|
|
|
2018-11-01 14:42:12 +01:00
|
|
|
// Ignore title?
|
2018-09-17 12:54:21 +02:00
|
|
|
if ($key === 'title' && !$include_title) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-01 14:42:12 +01:00
|
|
|
$exists = array_key_exists($key, $metadata);
|
|
|
|
|
|
|
|
// Don't include missing fields
|
2018-11-02 10:25:35 +01:00
|
|
|
if (!$include_missing && !$exists) {
|
2018-11-01 14:42:12 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$value = $exists ? $metadata[$key] : null;
|
|
|
|
|
|
|
|
// lowerCamelCase to snake_case
|
|
|
|
$db_field_name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $key));
|
|
|
|
|
|
|
|
switch ($config['type']) {
|
|
|
|
case 'text':
|
|
|
|
if ($value !== null && strlen($value) > $config['maxLength']) {
|
|
|
|
$value = mb_substr($value, 0, $config['maxLength']);
|
|
|
|
}
|
|
|
|
$types[] = '%s';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'int':
|
|
|
|
$value = ($value !== null) ? intval($value) : null;
|
|
|
|
$types[] = '%d';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'json':
|
|
|
|
$value = ($value !== null) ? json_encode($value) : null;
|
|
|
|
$types[] = '%s';
|
|
|
|
break;
|
2018-09-17 11:47:05 +02:00
|
|
|
}
|
2018-11-01 14:42:12 +01:00
|
|
|
|
|
|
|
$fields[$db_field_name] = $value;
|
2018-09-17 11:47:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
2018-09-21 15:09:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The metadataSettings field in libraryJson uses 1 for true and 0 for false.
|
|
|
|
* Here we are converting these to booleans, and also doing JSON encoding.
|
|
|
|
* This is invoked before the library data is beeing inserted/updated to DB.
|
|
|
|
*
|
|
|
|
* @param array $metadataSettings
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function boolifyAndEncodeSettings($metadataSettings) {
|
|
|
|
// Convert metadataSettings values to boolean
|
|
|
|
if (isset($metadataSettings['disable'])) {
|
|
|
|
$metadataSettings['disable'] = $metadataSettings['disable'] === 1;
|
|
|
|
}
|
2018-10-17 13:53:38 +02:00
|
|
|
if (isset($metadataSettings['disableExtraTitleField'])) {
|
2018-09-21 15:09:31 +02:00
|
|
|
$metadataSettings['disableExtraTitleField'] = $metadataSettings['disableExtraTitleField'] === 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_encode($metadataSettings);
|
|
|
|
}
|
2018-09-17 11:47:05 +02:00
|
|
|
}
|