Skip to content

Commit

Permalink
Dynamic server branding support
Browse files Browse the repository at this point in the history
The Online server product name is now
dynamically pulled from the server, once a
valid URL is provided. To avoids using
a hard-coded name. After the initial
discovery of the server capabilities, the
product-name is cached.

However we allow for changing the server
(or, upon updating the server the product
name may have been changed) by clearing
the cache from the plugin settings page.
Simply loading the settings page will
invalidate the cache, if a URL is already
set and is valid, and will pull the new
name.

The relevant strings displayed in the UI,
including errors, have been updated to use
the product name provided by the server
with proper fallback.
  • Loading branch information
Ashod committed Sep 13, 2020
1 parent fc22536 commit eb4970f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 6 deletions.
70 changes: 69 additions & 1 deletion classes/collabora.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,12 @@ private function get_file_mimetype() {
private function get_discovery_xml() {
$baseurl = trim(get_config('mod_collabora', 'url'));
if (!$baseurl) {
throw new \moodle_exception('collaboraurlnotset', 'mod_collabora');
// Get the product name, if it's already cached.
$cache = \cache::make('mod_collabora', 'capabilities');
if (!$productname = $cache->get('productname')) {
$productname = get_string('default_server_name', 'mod_collabora');
}
throw new \moodle_exception('collaboraurlnotset', 'mod_collabora', '', $productname);
}
$cache = \cache::make('mod_collabora', 'discovery');
if (!$xml = $cache->get($baseurl)) {
Expand Down Expand Up @@ -312,6 +317,69 @@ private function get_url_from_mimetype($discoveryxml, $mimetype) {
return (string)$url;
}

/**
* Get the capabilities JSON file from the collabora server.
* @return string
*/
private function get_capabilities_json() {
$cache = \cache::make('mod_collabora', 'capabilities');
if (!$json = $cache->get('json')) {
// Pull it from the discovery.xml.
$discoveryxml = $this->get_discovery_xml();
$xml = new \SimpleXMLElement($discoveryxml);
$app = $xml->xpath("//app[@name='Capabilities']");
if (!$app) {
throw new \moodle_exception('discovery_error_no_caps_url', 'mod_collabora');
}
$action = $app[0]->action;
$url = isset($action['urlsrc']) ? $action['urlsrc'] : '';
if (!$url) {
throw new \moodle_exception('discovery_error_no_caps_url', 'mod_collabora');
}

$curl = new \curl();
$json = $curl->get($url);
$cache->set('json', $json); // Cache the Capabilities JSON file.
}

return $json;
}

/**
* Get the product name from the collabora server.
* @return string
*/
public function get_product_name() {
$cache = \cache::make('mod_collabora', 'capabilities');
if (!$productname = $cache->get('productname')) {
$caps = $this->get_capabilities_json();
$json = json_decode($caps);
if (isset($json->{'productName'})) {
$productname = $json->{'productName'};
$cache->set('productname', $productname);
}
}

return $productname;
}

/**
* Resets the caches. Useful when the server is changed.
* @return string
*/
public function reset_caches() {
$cache = \cache::make('mod_collabora', 'capabilities');
$cache->delete('json');
$cache->delete('productname');

// Clear the discovery.xml cache.
$baseurl = trim(get_config('mod_collabora', 'url'));
if ($baseurl) {
$cache = \cache::make('mod_collabora', 'discovery');
$cache->delete($baseurl);
}
}

/**
* Get the URL of the handler, base on the mimetype of the existing file.
* @return string
Expand Down
5 changes: 4 additions & 1 deletion db/caches.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@
'discovery' => [
'mode' => cache_store::MODE_APPLICATION,
],
];
'capabilities' => [
'mode' => cache_store::MODE_APPLICATION,
],
];
7 changes: 5 additions & 2 deletions lang/en/collabora.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
defined('MOODLE_INTERNAL') || die();

$string['cachedef_discovery'] = 'Collabora discovery XML file';
$string['discovery_error_no_caps_url'] = 'The discovery.xml file does not contain the Capabilities URL';
$string['cachedef_capabilities'] = 'Capabilities file and fields';
$string['collabora:addinstance'] = 'Add collaborative document to a course';
$string['collabora:editlocked'] = 'Edit a locked collaborative document';
$string['collabora:lock'] = 'Lock/unlock a collaborative document';
$string['collabora:view'] = 'View a collaborative document';
$string['collaboraurl'] = 'Collabora URL';
$string['collaboraurlnotset'] = 'Collabora URL is not configured for this site';
$string['default_server_name'] = 'Collabora Online';
$string['collaboraurl'] = '{$a} URL';
$string['collaboraurlnotset'] = '{$a} URL is not configured for this site';
$string['current'] = 'Current tab';
$string['defaultdisplay'] = 'Default display';
$string['defaultdisplaydescription'] = 'Default display description';
Expand Down
29 changes: 28 additions & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,35 @@
defined('MOODLE_INTERNAL') || die();

if ($hassiteconfig) {

$productname = '';
try {
// If we have a URL, fetch the product name from the server.
// And if we had it, re-fetch it, in case it's changed.
if (get_config('mod_collabora', 'url')) {
$collabora = new \mod_collabora\collabora(0, 0, -1, 0);
// Remove from the cache to force fetching.
$collabora->reset_caches();
$productname = $collabora->get_product_name();
}
} catch (Exception $e) {
// For some reason we couldn't discover the server name.
// Possibly the server doesn't have the capabilities file.
$productname = '';
}
finally {
// Fallback.
if (empty($productname)) {
$cache = \cache::make('mod_collabora', 'capabilities');
if (!$productname = $cache->get('productname')) {
// If it's not already cached, default, as we are aren't configured yet.
$productname = get_string('default_server_name', 'mod_collabora');
}
}
}

$settings->add(new admin_setting_configtext('mod_collabora/url',
new lang_string('collaboraurl', 'mod_collabora'), '', '', PARAM_URL));
new lang_string('collaboraurl', 'mod_collabora', $productname), '', '', PARAM_URL));

$settings->add(new admin_setting_configselect('mod_collabora/defaultformat',
new lang_string('defaultformat', 'mod_collabora'), '',
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2020090100;
$plugin->version = 2020091200;
$plugin->requires = 2018051700; // M3.5.
$plugin->component = 'mod_collabora';
$plugin->maturity = MATURITY_STABLE;
Expand Down

0 comments on commit eb4970f

Please sign in to comment.