Merge branch 'master' of github.com:h5p/h5p-php-library

namespaces
Frode Petterson 2014-11-02 17:26:36 +01:00
commit 9b236da2d7
1 changed files with 37 additions and 19 deletions

View File

@ -11,9 +11,26 @@ interface H5PFrameworkInterface {
* An associative array containing: * An associative array containing:
* - name: The name of the plattform, for instance "Wordpress" * - name: The name of the plattform, for instance "Wordpress"
* - version: The version of the pattform, for instance "4.0" * - version: The version of the pattform, for instance "4.0"
* - h5pVersion: The version of the H5P plugin/module
*/ */
public function getPlatformInfo(); public function getPlatformInfo();
/**
* Fetches a file from a remote server using HTTP GET
*
* @param $url
* @return string The content (response body). NULL if something went wrong
*/
public function fetchExternalData($url);
/**
* Set the tutorial URL for a library. All versions of the library is set
*
* @param string $machineName
* @param string $tutorialUrl
*/
public function setLibraryTutorialUrl($machineName, $tutorialUrl);
/** /**
* Show the user an error message * Show the user an error message
* *
@ -2157,27 +2174,28 @@ class H5PCore {
} }
/** /**
* Get a list of libraries' metadata from h5p.org. Cache it, and refetch once a week. * Fetch a list of libraries' metadata from h5p.org.
* * Save URL tutorial to database. Each platform implementation
* @return mixed An object of objects keyed by machineName * is responsible for invoking this, eg using cron
*/ */
public function getLibrariesMetadata() { public function fetchLibrariesMetadata($fetchingDisabled = FALSE) {
// Fetch from cache:
$metadata = $this->h5pF->cacheGet('libraries','metadata');
// If not available in cache, or older than a week => refetch!
if ($metadata === NULL || $metadata->lastTimeFetched < (time() - self::SECONDS_IN_WEEK)) {
$platformInfo = $this->h5pF->getPlatformInfo(); $platformInfo = $this->h5pF->getPlatformInfo();
$json = file_get_contents('http://h5p.org/libraries-metadata.json?platform=' . json_encode($platformInfo)); $platformInfo['autoFetchingDisabled'] = $fetchingDisabled;
$platformInfo['uuid'] = $this->h5pF->getOption('h5p_site_uuid', '');
$metadata = new stdClass(); // Adding random string to GET to be sure nothing is cached
$metadata->json = ($json === FALSE ? NULL : json_decode($json)); $random = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 5);
$metadata->lastTimeFetched = time(); $json = $this->h5pF->fetchExternalData('http://h5p.lvh.me/h5p.org/libraries-metadata.json?api=1&platform=' . urlencode(json_encode($platformInfo)) . '&x=' . urlencode($random));
if ($json !== NULL) {
$this->h5pF->cacheSet('libraries','metadata', $metadata); $json = json_decode($json);
if (isset($json->libraries)) {
foreach ($json->libraries as $machineName => $libInfo) {
$this->h5pF->setLibraryTutorialUrl($machineName, $libInfo->tutorialUrl);
}
}
if($platformInfo['uuid'] === '' && isset($json->uuid)) {
$this->h5pF->setOption('h5p_site_uuid', $json->uuid);
}
} }
return $metadata->json;
} }
} }