1 |
efrain |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Utility class for handling metadata
|
|
|
4 |
*/
|
|
|
5 |
abstract class H5PMetadata {
|
|
|
6 |
|
|
|
7 |
private static $fields = array(
|
|
|
8 |
'title' => array(
|
|
|
9 |
'type' => 'text',
|
|
|
10 |
'maxLength' => 255
|
|
|
11 |
),
|
|
|
12 |
'a11yTitle' => array(
|
|
|
13 |
'type' => 'text',
|
|
|
14 |
'maxLength' => 255,
|
|
|
15 |
),
|
|
|
16 |
'authors' => array(
|
|
|
17 |
'type' => 'json'
|
|
|
18 |
),
|
|
|
19 |
'changes' => array(
|
|
|
20 |
'type' => 'json'
|
|
|
21 |
),
|
|
|
22 |
'source' => array(
|
|
|
23 |
'type' => 'text',
|
|
|
24 |
'maxLength' => 255
|
|
|
25 |
),
|
|
|
26 |
'license' => array(
|
|
|
27 |
'type' => 'text',
|
|
|
28 |
'maxLength' => 32
|
|
|
29 |
),
|
|
|
30 |
'licenseVersion' => array(
|
|
|
31 |
'type' => 'text',
|
|
|
32 |
'maxLength' => 10
|
|
|
33 |
),
|
|
|
34 |
'licenseExtras' => array(
|
|
|
35 |
'type' => 'text',
|
|
|
36 |
'maxLength' => 5000
|
|
|
37 |
),
|
|
|
38 |
'authorComments' => array(
|
|
|
39 |
'type' => 'text',
|
|
|
40 |
'maxLength' => 5000
|
|
|
41 |
),
|
|
|
42 |
'yearFrom' => array(
|
|
|
43 |
'type' => 'int'
|
|
|
44 |
),
|
|
|
45 |
'yearTo' => array(
|
|
|
46 |
'type' => 'int'
|
|
|
47 |
),
|
|
|
48 |
'defaultLanguage' => array(
|
|
|
49 |
'type' => 'text',
|
|
|
50 |
'maxLength' => 32,
|
|
|
51 |
)
|
|
|
52 |
);
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* JSON encode metadata
|
|
|
56 |
*
|
|
|
57 |
* @param object $content
|
|
|
58 |
* @return string
|
|
|
59 |
*/
|
|
|
60 |
public static function toJSON($content) {
|
|
|
61 |
// Note: deliberatly creating JSON string "manually" to improve performance
|
|
|
62 |
return
|
|
|
63 |
'{"title":' . (isset($content->title) ? json_encode($content->title) : 'null') .
|
|
|
64 |
',"a11yTitle":' . (isset($content->a11y_title) ? $content->a11y_title : 'null') .
|
|
|
65 |
',"authors":' . (isset($content->authors) ? $content->authors : 'null') .
|
|
|
66 |
',"source":' . (isset($content->source) ? '"' . $content->source . '"' : 'null') .
|
|
|
67 |
',"license":' . (isset($content->license) ? '"' . $content->license . '"' : 'null') .
|
|
|
68 |
',"licenseVersion":' . (isset($content->license_version) ? '"' . $content->license_version . '"' : 'null') .
|
|
|
69 |
',"licenseExtras":' . (isset($content->license_extras) ? json_encode($content->license_extras) : 'null') .
|
|
|
70 |
',"yearFrom":' . (isset($content->year_from) ? $content->year_from : 'null') .
|
|
|
71 |
',"yearTo":' . (isset($content->year_to) ? $content->year_to : 'null') .
|
|
|
72 |
',"changes":' . (isset($content->changes) ? $content->changes : 'null') .
|
|
|
73 |
',"defaultLanguage":' . (isset($content->default_language) ? '"' . $content->default_language . '"' : 'null') .
|
|
|
74 |
',"authorComments":' . (isset($content->author_comments) ? json_encode($content->author_comments) : 'null') . '}';
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Make the metadata into an associative array keyed by the property names
|
|
|
79 |
* @param mixed $metadata Array or object containing metadata
|
|
|
80 |
* @param bool $include_title
|
|
|
81 |
* @param bool $include_missing For metadata fields not being set, skip 'em.
|
|
|
82 |
* Relevant for content upgrade
|
|
|
83 |
* @param array $types
|
|
|
84 |
* @return array
|
|
|
85 |
*/
|
|
|
86 |
public static function toDBArray($metadata, $include_title = true, $include_missing = true, &$types = array()) {
|
|
|
87 |
$fields = array();
|
|
|
88 |
|
|
|
89 |
if (!is_array($metadata)) {
|
|
|
90 |
$metadata = (array) $metadata;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
foreach (self::$fields as $key => $config) {
|
|
|
94 |
|
|
|
95 |
// Ignore title?
|
|
|
96 |
if ($key === 'title' && !$include_title) {
|
|
|
97 |
continue;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
$exists = array_key_exists($key, $metadata);
|
|
|
101 |
|
|
|
102 |
// Don't include missing fields
|
|
|
103 |
if (!$include_missing && !$exists) {
|
|
|
104 |
continue;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
$value = $exists ? $metadata[$key] : null;
|
|
|
108 |
|
|
|
109 |
// lowerCamelCase to snake_case
|
|
|
110 |
$db_field_name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $key));
|
|
|
111 |
|
|
|
112 |
switch ($config['type']) {
|
|
|
113 |
case 'text':
|
|
|
114 |
if ($value !== null && strlen($value) > $config['maxLength']) {
|
|
|
115 |
$value = mb_substr($value, 0, $config['maxLength']);
|
|
|
116 |
}
|
|
|
117 |
$types[] = '%s';
|
|
|
118 |
break;
|
|
|
119 |
|
|
|
120 |
case 'int':
|
|
|
121 |
$value = ($value !== null) ? intval($value) : null;
|
|
|
122 |
$types[] = '%d';
|
|
|
123 |
break;
|
|
|
124 |
|
|
|
125 |
case 'json':
|
|
|
126 |
$value = ($value !== null) ? json_encode($value) : null;
|
|
|
127 |
$types[] = '%s';
|
|
|
128 |
break;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
$fields[$db_field_name] = $value;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
return $fields;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* The metadataSettings field in libraryJson uses 1 for true and 0 for false.
|
|
|
139 |
* Here we are converting these to booleans, and also doing JSON encoding.
|
|
|
140 |
* This is invoked before the library data is beeing inserted/updated to DB.
|
|
|
141 |
*
|
|
|
142 |
* @param array $metadataSettings
|
|
|
143 |
* @return string
|
|
|
144 |
*/
|
|
|
145 |
public static function boolifyAndEncodeSettings($metadataSettings) {
|
|
|
146 |
// Convert metadataSettings values to boolean
|
|
|
147 |
if (isset($metadataSettings['disable'])) {
|
|
|
148 |
$metadataSettings['disable'] = $metadataSettings['disable'] === 1;
|
|
|
149 |
}
|
|
|
150 |
if (isset($metadataSettings['disableExtraTitleField'])) {
|
|
|
151 |
$metadataSettings['disableExtraTitleField'] = $metadataSettings['disableExtraTitleField'] === 1;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
return json_encode($metadataSettings);
|
|
|
155 |
}
|
|
|
156 |
}
|