Renamed humanId => slug.

Moved most of the slug generating code out of the framework and into core.
Remove old export files.
Use old export file until new slug is generated.
Added support for converting more special chars.
redesign-copyrights
Frode Petterson 2015-03-13 15:17:19 +01:00
parent c7a7a99590
commit 6541faf8ba
1 changed files with 60 additions and 18 deletions

View File

@ -496,12 +496,12 @@ interface H5PFrameworkInterface {
public function setOption($name, $value); public function setOption($name, $value);
/** /**
* This will set the filtered parameters for the given content. * This will update selected fields on the given content.
* *
* @param int $content_id * @param int $id Content identifier
* @param string $parameters filtered * @param array $fields Content fields, e.g. filtered or slug.
*/ */
public function setFilteredParameters($content_id, $parameters = ''); public function updateContentFields($id, $fields);
/** /**
* Will clear filtered params for all the content that uses the specified * Will clear filtered params for all the content that uses the specified
@ -529,11 +529,12 @@ interface H5PFrameworkInterface {
public function getNumContent($libraryId); public function getNumContent($libraryId);
/** /**
* Generate an easy human readable identifier for the given content. * Determines if content slug is used.
* *
* @param array $content assoc * @param string $slug
* @return boolean
*/ */
public function generateHumanReadableContentIdentifier($content); public function isContentSlugAvailable($slug);
} }
/** /**
@ -1476,7 +1477,7 @@ Class H5PExport {
public function createExportFile($content) { public function createExportFile($content) {
$h5pDir = $this->h5pF->getH5pPath() . DIRECTORY_SEPARATOR; $h5pDir = $this->h5pF->getH5pPath() . DIRECTORY_SEPARATOR;
$tempPath = $h5pDir . 'temp' . DIRECTORY_SEPARATOR . $content['id']; $tempPath = $h5pDir . 'temp' . DIRECTORY_SEPARATOR . $content['id'];
$zipPath = $h5pDir . 'exports' . DIRECTORY_SEPARATOR . $content['humanId'] . '.h5p'; $zipPath = $h5pDir . 'exports' . DIRECTORY_SEPARATOR . $content['slug'] . '.h5p';
// Temp dir to put the h5p files in // Temp dir to put the h5p files in
@mkdir($tempPath, 0777, TRUE); @mkdir($tempPath, 0777, TRUE);
@ -1698,7 +1699,7 @@ class H5PCore {
* @return Object NULL on failure. * @return Object NULL on failure.
*/ */
public function filterParameters(&$content) { public function filterParameters(&$content) {
if (isset($content['filtered']) && $content['filtered'] !== '' && $content['humanId']) { if (isset($content['filtered']) && $content['filtered'] !== '') {
return $content['filtered']; return $content['filtered'];
} }
@ -1720,8 +1721,14 @@ class H5PCore {
$this->h5pF->deleteLibraryUsage($content['id']); $this->h5pF->deleteLibraryUsage($content['id']);
$this->h5pF->saveLibraryUsage($content['id'], $content['dependencies']); $this->h5pF->saveLibraryUsage($content['id'], $content['dependencies']);
if (!$content['humanId']) { if (!$content['slug']) {
$content['humanId'] = $this->h5pF->generateHumanReadableContentIdentifier($content); $content['slug'] = $this->generateContentSlug($content);
// Remove old export file
$oldExport = $this->h5pF->getH5pPath() . '/exports/' . $content['id'] . '.h5p';
if (file_exists($oldExport)) {
unlink($oldExport);
}
} }
if ($this->exportEnabled) { if ($this->exportEnabled) {
@ -1733,11 +1740,41 @@ class H5PCore {
} }
// Cache. // Cache.
$this->h5pF->setFilteredParameters($content['id'], $params, $content['humanId']); $this->h5pF->updateContentFields($content['id'], array(
'filtered' => $params,
'slug' => $content['slug']
));
} }
return $params; return $params;
} }
/**
* Generate content slug
*
* @param array $content object
* @return string unique content slug
*/
private function generateContentSlug($content) {
$slug = H5PCore::slugify($content['title']);
$available = NULL;
while (!$available) {
if ($available === FALSE) {
// If not available, add number suffix.
$matches = array();
if (preg_match('/(.+-)([0-9]+)$/', $slug, $matches)) {
$slug = $matches[1] . (intval($matches[2]) + 1);
}
else {
$slug .= '-2';
}
}
$available = $this->h5pF->isContentSlugAvailable($slug);
}
return $slug;
}
/** /**
* Find the files required for this content to work. * Find the files required for this content to work.
* *
@ -2300,20 +2337,20 @@ class H5PCore {
} }
/** /**
* Convert strings of text to a simple kebab case. * Convert strings of text into simple kebab case slugs.
* Very useful for readable urls etc. * Very useful for readable urls etc.
* *
* @param string $input * @param string $input
* @return string * @return string
*/ */
public static function kebabize($input) { public static function slugify($input) {
// Down low // Down low
$input = strtolower($input); $input = strtolower($input);
// Replace common chars // Replace common chars
$input = str_replace( $input = str_replace(
array('æ', 'ø', 'ö', 'ó', 'ô', 'œ', 'å', 'ä', 'á', 'à', 'â', 'ç', 'é', 'è', 'ê', 'ë', ', 'î', 'ï', 'ú', 'ñ', 'ü', 'ù', 'û', '), array('æ', 'ø', 'ö', 'ó', 'ô', 'Ò', 'Õ', 'Ý', 'ý', 'ÿ', 'ā', 'ă', 'ą', 'œ', 'å', 'ä', 'á', 'à', 'â', 'ã', 'ç', 'ć', 'ĉ', 'ċ', 'č', 'é', 'è', 'ê', 'ë', ', ', 'î', 'ï', 'ú', 'ñ', 'ü', 'ù', 'û', ', 'ď', 'đ', 'ē', 'ĕ', 'ė', 'ę', 'ě', 'ĝ', 'ğ', 'ġ', 'ģ', 'ĥ', 'ħ', 'ĩ', 'ī', 'ĭ', 'į', 'ı', 'ij', 'ĵ', 'ķ', 'ĺ', 'ļ', 'ľ', 'ŀ', 'ł', 'ń', 'ņ', 'ň', 'ʼn', 'ō', 'ŏ', 'ő', 'ŕ', 'ŗ', 'ř', 'ś', 'ŝ', 'ş', 'š', 'ţ', 'ť', 'ŧ', 'ũ', 'ū', 'ŭ', 'ů', 'ű', 'ų', 'ŵ', 'ŷ', 'ź', 'ż', 'ž', 'ſ', 'ƒ', 'ơ', 'ư', 'ǎ', 'ǐ', 'ǒ', 'ǔ', 'ǖ', 'ǘ', 'ǚ', 'ǜ', 'ǻ', 'ǽ', 'ǿ'),
array('ae', 'oe', 'o', 'o', 'o', 'oe', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'u', 'n', 'u', 'u', 'u', 'es'), array('ae', 'oe', 'o', 'o', 'o', 'oe', 'o', 'o', 'y', 'y', 'y', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'c', 'c', 'c', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'u', 'n', 'u', 'u', 'u', 'es', 'd', 'd', 'e', 'e', 'e', 'e', 'e', 'g', 'g', 'g', 'g', 'h', 'h', 'i', 'i', 'i', 'i', 'i', 'ij', 'j', 'k', 'l', 'l', 'l', 'l', 'l', 'n', 'n', 'n', 'n', 'o', 'o', 'o', 'r', 'r', 'r', 's', 's', 's', 's', 't', 't', 't', 'u', 'u', 'u', 'u', 'u', 'u', 'w', 'y', 'z', 'z', 'z', 's', 'f', 'o', 'u', 'a', 'i', 'o', 'u', 'u', 'u', 'u', 'u', 'a', 'ae', 'oe'),
$input); $input);
// Replace everything else // Replace everything else
@ -2323,10 +2360,15 @@ class H5PCore {
$input = preg_replace('/-{2,}/', '-', $input); $input = preg_replace('/-{2,}/', '-', $input);
// Prevent hyphen in beginning or end // Prevent hyphen in beginning or end
$input = preg_replace('/^-|-$/', '', $input); $input = trim($input, '-');
// Prevent to long slug
if (strlen($input) > 91) {
$inputsubstr($input, 0, 92);
}
// Prevent empty slug
if ($input === '') { if ($input === '') {
// Use default string
$input = 'interactive'; $input = 'interactive';
} }