Did some minor refactoring of "minimum version support" to ease implementation of this in other systems.

namespaces
Frode Petterson 2014-08-26 11:26:41 +02:00
parent 96b75f7d73
commit 4955e9da59
1 changed files with 64 additions and 41 deletions

View File

@ -585,7 +585,7 @@ class H5PValidator {
$this->h5pF->setErrorMessage($this->h5pF->t('Missing required library @library', array('@library' => H5PCore::libraryToString($library)))); $this->h5pF->setErrorMessage($this->h5pF->t('Missing required library @library', array('@library' => H5PCore::libraryToString($library))));
} }
if (!$this->h5pF->mayUpdateLibraries()) { if (!$this->h5pF->mayUpdateLibraries()) {
$this->h5pF->setInfoMessage($this->h5pF->t("Note that the libraries may exist in the file you uploaded, but you're not allowed to upload new libraries. Contact the site administrator about this.")); $this->h5pF->setInfoMessage($this->h5pF->t("Note that the libraries may exist in the file you uploaded, but you're not allowed to upload new libraries. Contact the site administrator about this."));
} }
} }
$valid = empty($missingLibraries) && $valid; $valid = empty($missingLibraries) && $valid;
@ -1864,42 +1864,36 @@ class H5PCore {
return; return;
} }
// Read and parse library-support.json $minVersions = getMinimumVersionsSupported(realpath(dirname(__FILE__)) . '/library-support.json');
$jsonString = file_get_contents(realpath(dirname(__FILE__)).'/library-support.json'); if ($minVersions === NULL) {
return;
}
if($jsonString !== FALSE) { // Get all libraries installed, check if any of them is not supported:
// Get all libraries installed, check if any of them is not supported: $libraries = $this->h5pF->loadLibraries();
$libraries = $this->h5pF->loadLibraries(); $unsupportedLibraries = array();
$unsupportedLibraries = array();
$minimumLibraryVersions = array();
$librariesSupported = json_decode($jsonString, true); // Iterate over all installed libraries
foreach ($librariesSupported as $library) { foreach ($libraries as $library_name => $versions) {
$minimumLibraryVersions[$library['machineName']]['versions'] = $library['minimumVersions']; if (!isset($minVersions[$library_name])) {
$minimumLibraryVersions[$library['machineName']]['downloadUrl'] = $library['downloadUrl']; continue;
} }
$min = $minVersions[$library_name];
// Iterate over all installed libraries // For each version of this library, check if it is supported
foreach ($libraries as $machine_name => $libraryList) { foreach ($versions as $library) {
// Check if there are any minimum version requirements for this library if (!$this->isLibraryVersionSupported($library, $min->versions)) {
if (isset($minimumLibraryVersions[$machine_name])) { // Current version of this library is not supported
$minimumVersions = $minimumLibraryVersions[$machine_name]['versions']; $unsupportedLibraries[] = array (
// For each version of this library, check if it is supported 'name' => $library_name,
foreach ($libraryList as $library) { 'downloadUrl' => $min->downloadUrl,
if (!self::isLibraryVersionSupported($library, $minimumVersions)) { 'currentVersion' => array (
// Current version of this library is not supported 'major' => $library->major_version,
$unsupportedLibraries[] = array ( 'minor' => $library->minor_version,
'name' => $machine_name, 'patch' => $library->patch_version,
'downloadUrl' => $minimumLibraryVersions[$machine_name]['downloadUrl'], ),
'currentVersion' => array ( 'minimumVersions' => $min->versions,
'major' => $library->major_version, );
'minor' => $library->minor_version,
'patch' => $library->patch_version,
),
'minimumVersions' => $minimumVersions,
);
}
}
} }
} }
@ -1907,6 +1901,35 @@ class H5PCore {
} }
} }
/**
* Returns a list of the minimum version of libraries that are supported.
* This is needed because some old libraries are no longer supported by core.
*
* TODO: Make it possible for the systems to cache this list between requests.
*
* @param string $path to json file
* @return array indexed using library names
*/
public function getMinimumVersionsSupported($path) {
$minSupported = array();
// Get list of minimum version for libraries. Some old libraries are no longer supported.
$libraries = file_get_contents($path);
if ($libraries !== FALSE) {
$libraries = json_decode($libraries);
if ($libraries !== NULL) {
foreach ($libraries as $library) {
$minSupported[$library->machineName] = (object) array(
'versions' => $library->minimumVersions,
'downloadUrl' => $library->downloadUrl
);
}
}
}
return empty($minSupported) ? NULL : $minSupported;
}
/** /**
* Check if a specific version of a library is supported * Check if a specific version of a library is supported
* *
@ -1914,22 +1937,22 @@ class H5PCore {
* @param array An array containing versions * @param array An array containing versions
* @return boolean TRUE if supported, otherwise FALSE * @return boolean TRUE if supported, otherwise FALSE
*/ */
private static function isLibraryVersionSupported ($library, $minimumVersions) { public function isLibraryVersionSupported ($library, $minimumVersions) {
$major_supported = $minor_supported = $patch_supported = false; $major_supported = $minor_supported = $patch_supported = false;
foreach ($minimumVersions as $minimumVersion) { foreach ($minimumVersions as $minimumVersion) {
// A library is supported if: // A library is supported if:
// --- major is higher than any minimumversion // --- major is higher than any minimumversion
// --- minor is higher than any minimumversion for a given major // --- minor is higher than any minimumversion for a given major
// --- major and minor equals and patch is >= supported // --- major and minor equals and patch is >= supported
$major_supported |= ($library->major_version > $minimumVersion['major']); $major_supported |= ($library->major_version > $minimumVersion->major);
if ($library->major_version == $minimumVersion['major']) { if ($library->major_version == $minimumVersion->major) {
$minor_supported |= ($library->minor_version > $minimumVersion['minor']); $minor_supported |= ($library->minor_version > $minimumVersion->minor);
} }
if ($library->major_version == $minimumVersion['major'] && if ($library->major_version == $minimumVersion->major &&
$library->minor_version == $minimumVersion['minor']) { $library->minor_version == $minimumVersion->minor) {
$patch_supported |= ($library->patch_version >= $minimumVersion['patch']); $patch_supported |= ($library->patch_version >= $minimumVersion->patch);
} }
} }