Commit 732bd131 authored by Dan Poltawski's avatar Dan Poltawski
Browse files

Merge branch 'MDL-55528-master' of git://github.com/andrewnicols/moodle

parents f7c7799c 357712ea
......@@ -230,6 +230,19 @@ if ($hassiteconfig) {
300, PARAM_INT, 10));
$ADMIN->add('mediaplayers', $temp);
// Convert plugins.
$ADMIN->add('modules', new admin_category('fileconverterplugins', new lang_string('type_fileconverter_plural', 'plugin')));
$temp = new admin_settingpage('managefileconverterplugins', new lang_string('type_fileconverter', 'plugin'));
$temp->add(new admin_setting_manage_fileconverter_plugins());
$ADMIN->add('fileconverterplugins', $temp);
$plugins = core_plugin_manager::instance()->get_plugins_of_type('fileconverter');
core_collator::asort_objects_by_property($plugins, 'displayname');
foreach ($plugins as $plugin) {
/** @var \core\plugininfo\media $plugin */
$plugin->load_settings($ADMIN, 'fileconverterplugins', $hassiteconfig);
}
$plugins = core_plugin_manager::instance()->get_plugins_of_type('media');
core_collator::asort_objects_by_property($plugins, 'displayname');
foreach ($plugins as $plugin) {
......
......@@ -12,7 +12,6 @@ $temp->add(new admin_setting_configexecutable('pathtodu', new lang_string('patht
$temp->add(new admin_setting_configexecutable('aspellpath', new lang_string('aspellpath', 'admin'), new lang_string('edhelpaspellpath'), ''));
$temp->add(new admin_setting_configexecutable('pathtodot', new lang_string('pathtodot', 'admin'), new lang_string('pathtodot_help', 'admin'), ''));
$temp->add(new admin_setting_configexecutable('pathtogs', new lang_string('pathtogs', 'admin'), new lang_string('pathtogs_help', 'admin'), '/usr/bin/gs'));
$temp->add(new admin_setting_configexecutable('pathtounoconv', new lang_string('pathtounoconv', 'admin'), new lang_string('pathtounoconv_help', 'admin'), '/usr/bin/unoconv'));
$ADMIN->add('server', $temp);
......
<table class="admintable generaltable">
<thead>
<tr>
<th class="header">{{#str}}name, moodle{{/str}}</th>
{{#infocolumnname}}
<th class="header">{{infocolumnname}}</th>
{{/infocolumnname}}
<th class="header">{{#str}}order, moodle{{/str}}</th>
<th class="header">{{#str}}isenabled, plugin{{/str}}</th>
<th class="header">{{#str}}settings, moodle{{/str}}</th>
<th class="header">{{#str}}uninstall, plugin{{/str}}</th>
</tr>
</thead>
<tbody>
{{#plugins}}
<tr>
<td>{{plugin}}</td>
{{#infocolumnname}}
<td>
{{info}}
</td>
{{/infocolumnname}}
<td class="text-nowrap">
{{#moveuplink}}
<a href="{{{moveuplink}}}">
{{#pix}}t/up, moodle, {{#str}} up, moodle {{/str}}{{/pix}}
</a>
{{/moveuplink}}
{{^moveuplink}}
{{#pix}}spacer, moodle{{/pix}}
{{/moveuplink}}
{{#movedownlink}}
<a href="{{{movedownlink}}}">
{{#pix}}t/down, moodle, {{#str}} down, moodle {{/str}}{{/pix}}
</a>
{{/movedownlink}}
{{^movedownlink}}
{{#pix}}spacer, moodle{{/pix}}
{{/movedownlink}}
</td>
<td>
<a href="{{{togglelink}}}">
{{#toggletarget}}
{{#pix}}i/show, moodle, {{#str}} enable, moodle {{/str}}{{/pix}}
{{/toggletarget}}
{{^toggletarget}}
{{#pix}}i/hide, moodle, {{#str}} disable, moodle {{/str}}{{/pix}}
{{/toggletarget}}
</a>
</td>
<td>
{{#settingslink}}
<a href="{{{settingslink}}}">{{#str}}settings,plugin{{/str}}</a>
{{/settingslink}}
</td>
<td>
{{#uninstalllink}}
<a href="{{{uninstalllink}}}">{{#str}}uninstall,plugin{{/str}}</a>
{{/uninstalllink}}
</td>
</tr>
{{/plugins}}
</tbody>
</table>
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Generic plugin config manipulation script.
*
* @package admin
* @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('NO_OUTPUT_BUFFERING', true);
require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');
$action = required_param('action', PARAM_ALPHANUMEXT);
$plugin = required_param('plugin', PARAM_PLUGIN);
$type = required_param('type', PARAM_PLUGIN);
$PAGE->set_url('/admin/updatesetting.php');
$PAGE->set_context(context_system::instance());
require_login();
require_capability('moodle/site:config', context_system::instance());
require_sesskey();
$plugintypeclass = "\\core\\plugininfo\\{$type}";
$plugins = \core_plugin_manager::instance()->get_plugins_of_type($type);
$sortorder = array_values($plugintypeclass::get_enabled_plugins());
$return = $plugintypeclass::get_manage_url();
if (!array_key_exists($plugin, $plugins)) {
redirect($return);
}
switch ($action) {
case 'disable':
$plugins[$plugin]->set_enabled(false);
break;
case 'enable':
$plugins[$plugin]->set_enabled(true);
break;
case 'up':
if (($pos = array_search($plugin, $sortorder)) > 0) {
$tmp = $sortorder[$pos - 1];
$sortorder[$pos - 1] = $sortorder[$pos];
$sortorder[$pos] = $tmp;
$plugintypeclass::set_enabled_plugins($sortorder);
}
break;
case 'down':
if ((($pos = array_search($plugin, $sortorder)) !== false) && ($pos < count($sortorder) - 1)) {
$tmp = $sortorder[$pos + 1];
$sortorder[$pos + 1] = $sortorder[$pos];
$sortorder[$pos] = $tmp;
$plugintypeclass::set_enabled_plugins($sortorder);
}
break;
}
redirect($return);
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Classes for converting files between different file formats.
*
* @package core_files
* @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_files;
defined('MOODLE_INTERNAL') || die();
use stored_file;
/**
* Class representing a conversion currently in progress.
*
* @package core_files
* @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class conversion extends \core\persistent {
/**
* Status value representing a conversion waiting to start.
*/
const STATUS_PENDING = 0;
/**
* Status value representing a conversion in progress.
*/
const STATUS_IN_PROGRESS = 1;
/**
* Status value representing a successful conversion.
*/
const STATUS_COMPLETE = 2;
/**
* Status value representing a failed conversion.
*/
const STATUS_FAILED = -1;
/**
* Table name for this persistent.
*/
const TABLE = 'file_conversion';
protected static function define_properties() {
return array(
'sourcefileid' => [
'type' => PARAM_INT,
],
'targetformat' => [
'type' => PARAM_ALPHANUMEXT,
],
'status' => [
'type' => PARAM_INT,
'choices' => [
self::STATUS_PENDING,
self::STATUS_IN_PROGRESS,
self::STATUS_COMPLETE,
self::STATUS_FAILED,
],
'default' => self::STATUS_PENDING,
],
'statusmessage' => [
'type' => PARAM_RAW,
'null' => NULL_ALLOWED,
'default' => null,
],
'converter' => [
'type' => PARAM_RAW,
'null' => NULL_ALLOWED,
'default' => null,
],
'destfileid' => [
'type' => PARAM_INT,
'null' => NULL_ALLOWED,
'default' => null,
],
'data' => [
'type' => PARAM_RAW,
'null' => NULL_ALLOWED,
'default' => null,
],
);
}
/**
* Fetch all conversions relating to the specified file.
*
* Only conversions which have a valid file are returned.
*
* @param stored_file $file The source file being converted
* @param string $format The targetforamt to filter to
* @return conversion[]
*/
public static function get_conversions_for_file(stored_file $file, $format) {
global $DB;
$instances = [];
// Conversion records are intended for tracking a conversion in progress or recently completed.
// The record is removed periodically, but the destination file is not.
// We need to fetch all conversion records which match the source file and target, and also all source and
// destination files which do not have a conversion record.
$sqlfields = self::get_sql_fields('c', 'conversion');
// Fetch actual conversions which relate to the specified source file, and have a matching conversion record,
// and either have a valid destination file which still exists, or do not have a destination file at all.
$sql = "SELECT {$sqlfields}
FROM {" . self::TABLE . "} c
INNER JOIN {files} conversionsourcefile ON conversionsourcefile.id = c.sourcefileid
LEFT JOIN {files} conversiondestfile ON conversiondestfile.id = c.destfileid
WHERE
conversionsourcefile.contenthash = :ccontenthash
AND c.targetformat = :cformat
AND (
c.destfileid IS NULL OR conversiondestfile.id IS NOT NULL
)";
// Fetch a empty conversion record for each source/destination combination that we find to match where the
// destination file is in the correct filearea/filepath/filename combination to meet the requirements.
// This ensures that existing conversions are used where possible, even if there is no 'conversion' record for
// them.
$sql .= "
UNION ALL
SELECT
NULL AS conversionid,
orphanedsourcefile.id AS conversionsourcefileid,
:oformat AS conversiontargetformat,
2 AS conversionstatus,
NULL AS conversionstatusmessage,
NULL AS conversionconverter,
orphaneddestfile.id AS conversiondestfileid,
NULL AS conversiondata,
0 AS conversiontimecreated,
0 AS conversiontimemodified,
0 AS conversionusermodified
FROM {files} orphanedsourcefile
INNER JOIN {files} orphaneddestfile ON (
orphaneddestfile.filename = orphanedsourcefile.contenthash
AND orphaneddestfile.component = 'core'
AND orphaneddestfile.filearea = 'documentconversion'
AND orphaneddestfile.filepath = :ofilepath
)
LEFT JOIN {" . self::TABLE . "} orphanedconversion ON orphanedconversion.destfileid = orphaneddestfile.id
WHERE
orphanedconversion.id IS NULL
AND
orphanedsourcefile.id = :osourcefileid
";
$records = $DB->get_records_sql($sql, [
'ccontenthash' => $file->get_contenthash(),
'osourcefileid' => $file->get_id(),
'cfilepath' => "/{$format}/",
'ofilepath' => "/{$format}/",
'cformat' => $format,
'oformat' => $format,
]);
foreach ($records as $record) {
$data = self::extract_record($record, 'conversion');
$newrecord = new static(0, $data);
$instances[] = $newrecord;
}
return $instances;
}
/**
* Remove all old conversion records.
*/
public static function remove_old_conversion_records() {
global $DB;
$DB->delete_records_select(self::TABLE, 'timemodified <= :weekagosecs', [
'weekagosecs' => time() - WEEKSECS,
]);
}
/**
* Set the source file id for the conversion.
*
* @param stored_file $file The file to convert
* @return $this
*/
public function set_sourcefile(stored_file $file) {
$this->raw_set('sourcefileid', $file->get_id());
return $this;
}
/**
* Fetch the source file.
*
* @return stored_file|false
*/
public function get_sourcefile() {
$fs = get_file_storage();
return $fs->get_file_by_id($this->get('sourcefileid'));
}
/**
* Set the destination file for this conversion.
*
* @param string $filepath The path to the converted file
* @return $this
*/
public function store_destfile_from_path($filepath) {
if ($record = $this->get_file_record()) {
$fs = get_file_storage();
$existing = $fs->get_file(
$record['contextid'],
$record['component'],
$record['filearea'],
$record['itemid'],
$record['filepath'],
$record['filename']
);
if ($existing) {
$existing->delete();
}
$file = $fs->create_file_from_pathname($record, $filepath);
$this->raw_set('destfileid', $file->get_id());
}
return $this;
}
/**
* Set the destination file for this conversion.
*
* @param string $content The content of the converted file
* @return $this
*/
public function store_destfile_from_string($content) {
if ($record = $this->get_file_record()) {
$fs = get_file_storage();
$existing = $fs->get_file(
$record['contextid'],
$record['component'],
$record['filearea'],
$record['itemid'],
$record['filepath'],
$record['filename']
);
if ($existing) {
$existing->delete();
}
$file = $fs->create_file_from_string($record, $content);
$this->raw_set('destfileid', $file->get_id());
}
return $this;
}
/**
* Get the destination file.
*
* @return stored_file|this
*/
public function get_destfile() {
$fs = get_file_storage();
return $fs->get_file_by_id($this->get('destfileid'));
}
/**
* Helper to ensure that the returned status is always an int.
*
* @return int
*/
protected function get_status() {
return (int) $this->raw_get('status');
}
/**
* Get an instance of the current converter.
*
* @return converter_interface|false
*/
public function get_converter_instance() {
$currentconverter = $this->get('converter');
if ($currentconverter && class_exists($currentconverter)) {
return new $currentconverter();
} else {
return false;
}
}
/**
* Transform data into a storable format.
*
* @param stdClass $data The data to be stored
* @return $this
*/
protected function set_data($data) {
$this->raw_set('data', json_encode($data));
return $this;
}
/**
* Transform data into a storable format.
*
* @return stdClass The stored data
*/
protected function get_data() {
$data = $this->raw_get('data');
if (!empty($data)) {
return json_decode($data);
}
return (object) [];
}
/**
* Return the file record base for use in the files table.
*
* @return array
*/
protected function get_file_record() {
$file = $this->get_sourcefile();
if (!$file) {
// If the source file was removed before we completed, we must return early.
return false;
}
return [
'contextid' => \context_system::instance()->id,
'component' => 'core',
'filearea' => 'documentconversion',
'itemid' => 0,
'filepath' => "/" . $this->get('targetformat') . "/",
'filename' => $file->get_contenthash(),
];
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Class for converting files between different file formats using unoconv.
*
* @package core_files
* @copyright 2017 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_files;
defined('MOODLE_INTERNAL') || die();
use stored_file;
/**
* Class for converting files between different formats using unoconv.
*
* @package core_files
* @copyright 2017 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class converter {
/**
* Get a list of enabled plugins and classes.
*
* @return array
*/
protected function get_enabled_plugins() {
$plugins = \core\plugininfo\fileconverter::get_enabled_plugins();
$pluginclasses = [];
foreach ($plugins as $plugin) {
$pluginclasses[$plugin] = \core\plugininfo\fileconverter::get_classname($plugin);
}
return $pluginclasses;
}
/**
* Return the file_storage API.
*
* This allows for mocking of the file_storage API.
*
* @return file_storage
*/
protected function get_file_storage() {
return get_file_storage();
}
/**
* Start the conversion for a stored_file into a new format.
*
* @param stored_file $file The file to convert
* @param string $format The desired target file format (file extension)
* @param boolean $forcerefresh If true, the file will be converted every time (not cached).
* @return conversion
*/
public function start_conversion(stored_file $file, $format, $forcerefresh = false) {
$conversions = conversion::get_conversions_for_file($file, $format);
if ($forcerefresh || count($conversions) > 1) {
while ($conversion = array_shift($conversions)) {
if ($conversion->get('id')) {
$conversion->delete();
}
}
}