Commit 9bba0204 authored by Sean Madsen's avatar Sean Madsen

#50 - Update model and views to follow new yaml schema for book config

parent 09a0092f
......@@ -58,30 +58,19 @@ class Language {
* Data passed into the language constructor.
*/
private function setupVersions($yaml) {
$latestBranch = isset($yaml['latest']) ? $yaml['latest'] : NULL;
$stableBranch = isset($yaml['stable']) ? $yaml['stable'] : NULL;
$history = isset($yaml['history']) ? $yaml['history'] : array();
if ($latestBranch && $stableBranch) {
if ($latestBranch == $stableBranch) {
$this->addVersion('latest', $latestBranch,
array('stable'));
}
else {
$this->addVersion('latest', $latestBranch);
$this->addVersion('stable', $stableBranch);
}
}
elseif ($latestBranch) {
$this->addVersion('latest', $latestBranch);
}
elseif ($stableBranch) {
$this->addVersion('stable', $latestBranch);
}
foreach ($history as $item) {
$this->addVersion($item);
// Add versions defined in yaml
$versions = $yaml['versions'] ?? [];
foreach ($versions as $slug => $version) {
$name = $version['name'] ?? 'Latest';
$path = $version['path'] ?? $slug;
$branch = $version['branch'] ?? 'master';
$redirects = $version['redirects'] ?? [];
$this->versions[] = new Version($slug, $name, $path, $branch, $redirects);
}
// If no versions were defined, then add one version (with default values)
if (count($this->versions) == 0) {
$this->addVersion('latest', 'master');
$this->versions[] = new Version();
}
}
......@@ -113,21 +102,6 @@ class Language {
}
}
/**
* Adds a new version to this branch
*
* @param string $name
* (e.g. "latest", "master", "4.7", etc.)
* @param string $branch
* (e.g "master", "4.7", etc)
* @param array $aliases
* Array of strings containing names which can also be used to reference
* the version.
*/
public function addVersion($name, $branch = NULL, $aliases = array()) {
$this->versions[] = new Version($name, $branch, $aliases);
}
/**
* Check all versions within this language to make sure there are no
* collisions between name/branch/aliases across different versions.
......
......@@ -8,7 +8,24 @@ class Version {
/**
* @var string
* Version name (e.g. "4.6" or "latest"). This is what readers see.
* The machine readable name of this version. For example, in a book with
* multiple versions, the slug should be defined to correspond to the
* version numbers of the product (i.e. "4.7", or "4.6"). The slug can be
* "master" if a book is only using one version.
*/
public $slug;
/**
* @var string
* This is the URL component for the published book. If it's not defined
* in the book's yaml file, then we use $slug as the path component.
*/
public $path;
/**
* @var string
* A human-readable name of this version (e.g. "4.7 / Current" or "latest").
* This is what readers see.
* Sometimes it's the same as name of the branch, but not always. It can
* contain pretty much whatever characters you want.
*/
......@@ -25,16 +42,16 @@ class Version {
/**
* @var array
* An array (without keys) of strings which represent aliases to this
* version of the book. For each alias, we will create symbolic links so
* that a reader can also access this version of the book at a URL with that
* alias.
* An array (without keys) of strings which represent redirects to this
* version of the book.
* (e.g. ["latest", "current"])
* If a reader requests a page with one of these redirects in place of the
* $path, then the app will redirect them to the proper page.
*/
public $aliases;
public $redirects;
/**
* Defines a new "version" of a book, with aliases.
*
* A version has one and only
* one "branch", meaning the git branch used for the version. A version also
* can have many "aliases", which are other descriptors like "stable" that we
......@@ -43,63 +60,63 @@ class Version {
* links to this directory for each of the aliases. So if the branch is
* "master" and we have an alias called "stable", then the book will be
* accessible at "master" via the directory and at "stable" via the symlink.
*
* If the constructor receives different $name and $branch values, it will
* automatically add an alias for $name.
*
* @param string $slug
* See Version::slug
* @param string $name
* e.g. "latest", "master", "4.7"
*
* See Version::name
* @param null $path
* See Version::path
* @param string $branch
* e.g "master", "4.7"
*
* @param array $aliases
* Array of strings containing names which can also be used to reference
* this version.
* See Version::branch
* @param array $redirects
* See Version::redirects
*/
public function __construct($name, $branch = NULL, $aliases = array()) {
public function __construct($slug = 'latest', $name = 'Latest', $path = NULL, $branch = 'master', $redirects = []) {
$this->slug = $slug;
$this->name = $name;
$this->branch = $branch ?: $name;
$this->setupAliases($aliases);
$this->path = $path ?? $slug;
$this->branch = $branch;
$this->setupRedirects($redirects);
}
/**
* @param $aliases array|string
* @param $redirects array|string
* e.g. "latest"
*/
private function setupAliases($aliases) {
private function setupRedirects($redirects) {
// wrap $aliases in array, if necessary
if (!is_array($aliases)) {
$aliases = array($aliases);
if (!is_array($redirects)) {
$redirects = array($redirects);
}
// Add an alias for $name if necessary
if ($this->name != $this->branch && !isset($aliases[$this->name])) {
$aliases[] = $this->name;
}
// Remove alias for $path if it exists
unset($redirects[$this->path]);
// Remove alias for $branch if it exists
unset($aliases[$this->branch]);
// Add alias for $branch (e.g. so urls with "master" will work correctly)
$redirects[] = $this->branch;
// Make sure each alias is URL-safe
foreach ($aliases as &$alias) {
$alias = StringTools::urlSafe($alias);
foreach ($redirects as &$redirect) {
$redirect = StringTools::urlSafe($redirect);
}
$this->aliases = array_unique($aliases);
// Make sure we don't have any duplicate branches
$this->redirects = array_unique($redirects);
}
/**
* Gives an array of all unique strings that can be used to describe this
* version, including branch, name, and any aliases.
* version, including the path plus any aliases.
*
* @return array
* Array of strings (without keys)
*/
public function allDescriptors() {
$result = $this->aliases;
$result[] = $this->name;
$result[] = $this->branch;
$result = $this->redirects;
$result[] = $this->path;
return array_unique($result);
}
......@@ -114,7 +131,7 @@ class Version {
*/
public function validate() {
if (preg_match("#/#", $this->branch)) {
throw new Exception("Branch name can not contain a forward slash.");
throw new \Exception("Branch name can not contain a forward slash.");
}
}
......
......@@ -12,7 +12,7 @@
{% set default_language = book.getDefaultLanguage() %}
{% set default_version = default_language.getDefaultVersion() %}
{% set default_url = "/" ~ book.slug ~ "/" ~ default_language.code ~ "/"
~ default_version.name %}
~ default_version.path %}
{% set default_title = default_language.nativeName() ~ " / " ~ default_version.name %}
<li>Default edition: <a href="{{ default_url }}">{{ default_title }}</a></li>
<li>Category: {{ book.category }}</li>
......@@ -32,7 +32,7 @@
</tr>
{% for language in book.languages %}
{% for version in language.versions %}
{% set url = "/" ~ book.slug ~ "/" ~ language.code ~ "/" ~ version.name %}
{% set url = "/" ~ book.slug ~ "/" ~ language.code ~ "/" ~ version.path %}
<tr>
<td class="language">
<span class="native-name">{{ language.nativeName }}</span>
......
......@@ -5,13 +5,13 @@
<div>
{% set language = book.getDefaultLanguage %}
{% set version = language.getDefaultVersion %}
{% set url = "/" ~ book.slug ~ "/" ~ language.code ~ "/" ~ version.name %}
{% set url = "/" ~ book.slug ~ "/" ~ language.code ~ "/" ~ version.path %}
<a href="{{ url }}">
<span class="name">
{{ book.name }}
</span>
<span class="edition">
({{ language.nativeName() }} / {{ version.name }})
({{ language.nativeName() }} &ndash; {{ version.name }})
</span>
</a>
{% set countOtherEditions = book.countEditions - 1 %}
......
......@@ -343,9 +343,9 @@ class Publisher {
$purgeExisting->run();
// Add new symlinks
foreach ($version->aliases as $alias) {
$this->fs->symlink($publishPath, "$path/$alias");
$msg = sprintf("Adding alias '%s' for %s.", $alias, $version->name);
foreach ($version->redirects as $redirect) {
$this->fs->symlink($publishPath, "$path/$redirect");
$msg = sprintf("Adding alias '%s' for %s.", $redirect, $version->name);
$this->addMessage('INFO', $msg);
}
}
......@@ -378,8 +378,8 @@ class Publisher {
$this->addMessage('INFO', "Using book: " . $book->name);
$aliasText = "";
if ($version->aliases) {
$aliasList = implode('", "', $version->aliases);
if ($version->redirects) {
$aliasList = implode('", "', $version->redirects);
$aliasText = sprintf("with aliases: %s", $aliasList);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment