Commit 5a83c883 authored by Richard Mansfield's avatar Richard Mansfield

Import/export changes from Penny, Nigel collected from importexport branch

parent 5777b4aa
......@@ -39,6 +39,27 @@ define('SECTION_PLUGINNAME', $pluginname);
define('SECTION_PAGE', 'pluginconfig');
safe_require($plugintype, $pluginname);
if ($sesskey = param_alphanum('sesskey', '')) {
if ($sesskey != $USER->get('sesskey')) {
throw new UserException('Invalid sesskey');
}
}
$enable = param_integer('enable', 0);
$disable = param_integer('disable', 0);
if ($disable && !call_static_method(generate_class_name($plugintype, $pluginname), 'can_be_disabled')) {
throw new UserException("Plugin $plugintype $pluginname cannot be disabled");
}
if ($enable || $disable) {
if ($plugintype == 'blocktype') {
$pluginname = blocktype_namespaced_to_single($pluginname);
}
set_field($plugintype . '_installed', 'active', $enable, 'name', $pluginname);
$SESSION->add_ok_msg(get_string('plugin' . (($enable) ? 'enabled' : 'disabled')));
redirect('/admin/extensions/plugins.php');
}
if ($plugintype == 'artefact') {
$type = param_alpha('type');
$classname = generate_artefact_class_name($type);
......
......@@ -48,13 +48,17 @@ foreach (plugin_types() as $plugin) {
}
foreach (array_keys($plugins) as $plugin) {
if (table_exists(new XMLDBTable($plugin . '_installed'))) {
if ($installed = get_records_array($plugin . '_installed')) {
if ($installed = plugins_installed($plugin, true)) {
foreach ($installed as $i) {
$key = $i->name;
if ($plugin == 'blocktype') {
$key = blocktype_single_to_namespaced($i->name, $i->artefactplugin);
}
$plugins[$plugin]['installed'][$key] = array();
safe_require($plugin, $key);
$plugins[$plugin]['installed'][$key] = array(
'active' => $i->active,
'disableable' => call_static_method(generate_class_name($plugin, $key), 'can_be_disabled'),
);
if ($plugin == 'artefact') {
$plugins[$plugin]['installed'][$key]['types'] = array();
safe_require('artefact', $key);
......
......@@ -272,7 +272,7 @@ function user_authorise($token, $useragent) {
function send_content_intent($username) {
global $REMOTEWWWROOT;
require_once('import.php');
require_once(get_config('docroot') . 'import/lib.php');
list ($user, $authinstance) = find_remote_user($username, $REMOTEWWWROOT);
if (!$user) {
......@@ -289,16 +289,7 @@ function send_content_intent($username) {
throw $e;
}
// generate a token, insert it into the queue table
$queue = new StdClass;
$queue->token = generate_token();
$queue->host = $REMOTEWWWROOT;
$queue->usr = $user->id;
$queue->queue = (int)!(Importer::import_immediately_allowed());
$queue->ready = 0;
$queue->expirytime = db_format_timestamp(time()+(60*60*24));
insert_record('import_queue', $queue);
$queue = PluginImport::create_new_queue($user->id, null, $REMOTEWWWROOT, 0);
return array(
'sendtype' => (($queue->queue) ? 'queue' : 'immediate'),
......@@ -308,7 +299,7 @@ function send_content_intent($username) {
function send_content_ready($token, $username, $format, $importdata, $fetchnow=false) {
global $REMOTEWWWROOT;
require_once('import.php');
require_once(get_config('docroot') . 'import/lib.php');
list ($user, $authinstance) = find_remote_user($username, $REMOTEWWWROOT);
if (!$user) {
......@@ -327,7 +318,7 @@ function send_content_ready($token, $username, $format, $importdata, $fetchnow=f
$queue->format = $format;
$class = null;
try {
$class = Importer::class_from_format($format);
$class = PluginImport::class_from_format($format);
} catch (Exception $e) {
throw new ImportException('Invalid format $format');
}
......@@ -352,11 +343,12 @@ function send_content_ready($token, $username, $format, $importdata, $fetchnow=f
update_record('import_queue', $queue);
$result = new StdClass;
if ($fetchnow && Importer::import_immediately_allowed()) {
if ($fetchnow && PluginImport::import_immediately_allowed()) {
// either immediately spawn a curl request to go fetch the file
$importer = Importer::create_importer($queue->id, $queue);
$importer = PluginImport::create_importer($queue->id, $queue);
$importer->prepare();
$importer->process();
$importer->cleanup();
delete_records('import_queue', 'id', $queue->id);
$result->status = true;
$result->type = 'complete';
......
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage artefact-blog-export-html
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
class HtmlExportBlog extends HtmlExportArtefactPlugin {
private $blogcount;
public function dump_export_data() {
if ($blogs = get_column('artefact', 'id', 'owner', $this->exporter->get('user')->get('id'), 'artefacttype', 'blog')) {
foreach ($blogs as $blogid) {
$blog = artefact_instance_from_id($blogid);
// Create directory for storing the blog
$dirname = preg_replace('#[^a-zA-Z0-9_-]+#', '-', $blog->get('title'));
if (!check_dir_exists($this->fileroot . $dirname)) {
throw new SystemException("Couldn't create blog directory {$this->fileroot}{$dirname}");
}
$smarty = $this->exporter->get_smarty('../../../');
$smarty->assign('title', $blog->get('title'));
$rendered = $blog->render_self(array());
$smarty->assign('rendered_blog', $rendered['html']);
$content = $smarty->fetch('export:html/blog:index.tpl');
if (false === file_put_contents($this->fileroot . $dirname . '/index.html', $content)) {
throw new SystemException("Unable to create index.html for blog $blogid");
}
}
}
$this->blogcount = count($blogs);
}
public function get_summary() {
return array(
'title' => 'Blogs',
'description' => "<p>You have {$this->blogcount} blogs</p>",
);
}
public function get_summary_weight() {
return 10;
}
}
?>
{include file="export:html:header.tpl"}
<h2>Blog: {$title|escape}</h2>
{$rendered_blog}
{include file="export:html:footer.tpl"}
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage artefact-blog-export-leap
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
class LeapExportElementBlogpost extends LeapExportElement {
public function add_links() {
parent::add_links();
// add on attachments
if (!$attachments = $this->artefact->get_attached_files()) {
return;
}
foreach ($attachments as &$attachment) {
$f = artefact_instance_from_id($attachment->id);
$this->add_artefact_link($f, 'has_attachment');
}
}
public function replace_content_placeholders($content) {
$content = parent::replace_content_placeholders($content, 'ARTEFACT(DL|VIEW)LINK');
return $content;
}
public function assign_smarty_vars() {
parent::assign_smarty_vars();
$this->smarty->assign('contenttype', 'xhtml');
}
public function get_content() {
//$rendered = $this->artefact->render_base(array('icons' => false));
//$rendered = $rendered['html'];
//return $this->replace_content_placeholders(clean_html($rendered));
// Probably need replace content placeholders!
return clean_html($this->artefact->get('description'));
}
public function get_categories() {
if (!$this->artefact->get('published')) {
return array(
array(
'scheme' => 'readiness',
'term' => 'Unready',
)
);
}
return array();
}
}
class LeapExportElementBlog extends LeapExportElement {
public function get_leap_type() {
return 'selection';
}
public function get_categories() {
return array(
array(
'scheme' => 'selection_type',
'term' => 'Blog',
)
);
}
public function assign_smarty_vars() {
parent::assign_smarty_vars();
$this->smarty->assign('contenttype', 'xhtml');
}
public function get_content() {
return clean_html($this->artefact->get('description'));
}
}
This diff is collapsed.
......@@ -447,8 +447,8 @@ class ArtefactTypeBlogPost extends ArtefactType {
foreach ($attachments as &$attachment) {
$f = artefact_instance_from_id($attachment->id);
$attachment->size = $f->describe_size();
$attachment->iconpath = $f->get_icon(array('id' => $attachment->id, 'viewid' => $options['viewid']));
$attachment->viewpath = get_config('wwwroot') . 'view/artefact.php?artefact=' . $attachment->id . '&view=' . $options['viewid'];
$attachment->iconpath = $f->get_icon(array('id' => $attachment->id, 'viewid' => isset($options['viewid']) ? $options['viewid'] : 0));
$attachment->viewpath = get_config('wwwroot') . 'view/artefact.php?artefact=' . $attachment->id . '&view=' . (isset($options['viewid']) ? $options['viewid'] : 0);
$attachment->downloadpath = get_config('wwwroot') . 'artefact/file/download.php?file=' . $attachment->id;
if (isset($options['viewid'])) {
$attachment->downloadpath .= '&id=' . $options['viewid'];
......
......@@ -10,7 +10,7 @@
<tr><th colspan="2">{str tag=attachedfiles section=artefact.blog}</th></tr>
{foreach from=$attachments item=item}
<tr class="r{cycle values=1,0}">
<td style="width: 22px;"><img src="{$item->iconpath|escape}" alt=""></td>
{if $icons}<td style="width: 22px;"><img src="{$item->iconpath|escape}" alt=""></td>{/if}
<td><a href="{$item->viewpath|escape}">{$item->title|escape}</a> ({$item->size|escape}) - <strong><a href="{$item->downloadpath|escape}">{str tag=Download section=artefact.file}</a></strong>
<br><strong>{$item->description|escape}</strong></td>
</tr>
......
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage artefact-file-export-html
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
/**
* TODO:
* - handle filenames with slashes
* - handle exporting when there's no files/folders
*/
class HtmlExportFile extends HtmlExportArtefactPlugin {
private $artefactdata;
public function dump_export_data() {
// Get all folders/files
$this->artefactdata = get_records_select_assoc(
'artefact',
"owner = ? AND
artefacttype IN ('" . join("','", PluginArtefactFile::get_artefact_types()) . "')",
array($this->exporter->get('user')->get('id')),
'',
'id, artefacttype, parent, title'
);
$this->populate_profileicons();
$this->create_index_for_directory($this->fileroot, 0, null);
$this->populate_filedir($this->fileroot, 0, null);
}
public function get_summary() {
$filecount = count(array_filter($this->artefactdata, create_function('$a', 'return $a->artefacttype != "folder";')));
$foldercount = count(array_filter($this->artefactdata, create_function('$a', 'return $a->artefacttype == "folder";')));
return array(
'title' => 'Files',
'description' => "<p>You have {$filecount} files in {$foldercount} folders. <a href=\"files/file/index.html\">Browse</a>.</p>",
);
}
/**
* Puts all profile icons in the static/profileicons/ directory
*/
private function populate_profileicons() {
$madeprofileiconsdir = false;
$profileiconsdir = $this->exporter->get('exportdir') . '/' . $this->exporter->get('rootdir') . '/static/profileicons/';
$removekeys = array();
foreach (array_keys($this->artefactdata) as $key) {
$artefactdata = $this->artefactdata[$key];
if ($artefactdata->artefacttype == 'profileicon') {
$removekeys[] = $key;
if (!$madeprofileiconsdir) {
check_dir_exists($profileiconsdir);
}
// TODO: sanitise path for /'s
$artefact = artefact_instance_from_id($artefactdata->id);
if (!copy($artefact->get_path(), $profileiconsdir . $artefactdata->title)) {
throw new SystemException("Unable to copy profile icon $artefactdata->title into export");
}
// Make sure we grab a nicely resized version too
$maxdimension = 200;
$resizedpath = get_dataroot_image_path('artefact/file/profileicons/', $artefactdata->id, $maxdimension);
if (!copy($resizedpath, $profileiconsdir . $maxdimension . 'px-' . $artefactdata->title)) {
throw new SystemException("Unable to copy resized profile icon {$maxdimension}px-$artefactdata->title into export");
}
}
}
}
/**
* Given a filesystem directory and the id of an artefact, fill the
* filesystem directory with the files and folders that Mahara considers
* are inside the artefact.
*
* This method is recursive, creating the file/directory structure for all
* directories under the one passed.
*
* This method also creates index.htmls in each directory created.
*
* @param string $filesystemdirectory The file system directory to populate
* @param int $level How deep the directory is
* @param int $parentid The folder to start from - can be null
*/
private function populate_filedir($filesystemdirectory, $level, $parentid) {
foreach ($this->artefactdata as $artefactdata) {
if ($artefactdata->parent == $parentid) {
if ($artefactdata->artefacttype == 'folder') {
$directory = $filesystemdirectory . $artefactdata->title . '/';
check_dir_exists($directory);
$this->create_index_for_directory($directory, $level + 1, $artefactdata);
$this->populate_filedir($directory, $level + 1, $artefactdata->id);
}
else {
$artefact = artefact_instance_from_id($artefactdata->id);
if (!copy($artefact->get_path(), $filesystemdirectory . $artefactdata->title)) {
throw new SystemException("HtmlExportFile::populate_filedir: unable to copy artefact $artefactdata->id's file");
}
}
}
}
}
/**
* Given a filesystem directory and the artefact data corresponding to that
* directory, creates an index.html for it.
*
* @param string $filesystemdirectory The file system directory to make the
* index.html inside
* @param int $level How deep this directory index is
* @param object $artefactdata Artefact data relating to the folder
* represented by this directory
*/
private function create_index_for_directory($filesystemdirectory, $level, $artefactdata=null) {
$smarty = $this->exporter->get_smarty(str_repeat('../', $level + 2));
if ($artefactdata) {
$smarty->assign('folder', ArtefactTypeFileBase::get_full_path($artefactdata->id, $this->artefactdata));
}
else {
$smarty->assign('folder', '/');
}
$id = ($artefactdata) ? $artefactdata->id : 'null';
$smarty->assign('folders', array_filter($this->artefactdata, create_function('$a', 'return $a->parent == ' . $id . ' && $a->artefacttype == "folder";')));
$smarty->assign('files', array_filter($this->artefactdata, create_function('$a', 'return $a->parent == ' . $id . ' && $a->artefacttype != "folder";')));
$content = $smarty->fetch('export:html/file:index.tpl');
if (false === file_put_contents($filesystemdirectory . 'index.html', $content)) {
throw new SystemException("HtmlExportFile::create_index_for_directory: unable to create index.html for directory $id");
}
}
public function get_summary_weight() {
return 20;
}
}
?>
{include file="export:html:header.tpl"}
<h2>Index of {$folder}</h2>
{if $folder != '/'}<p><a href="../index.html">Parent Folder</a>{/if}
{if $files || $folders}
<ul>
{foreach from=$folders item=folder}
<li>Folder: <a href="{$folder->title|rawurlencode|escape}/index.html">{$folder->title|escape}</a></li>
{/foreach}
{foreach from=$files item=file}
<li>File: <a href="{$file->title|rawurlencode|escape}">{$file->title|escape}</a></li>
{/foreach}
</ul>
{else}
<p>This folder is empty.</p>
{/if}
{include file="export:html:footer.tpl"}
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage artefact-file-export-leap
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
class LeapExportElementFile extends LeapExportElement {
private $filename;
public function add_links() {
parent::add_links();
// check for blog posts this file may be attached to
if (!$posts = get_records_array('artefact_blog_blogpost_file',
'file', $this->artefact->get('id'))) {
return;
}
foreach ($posts as &$p) {
$post = artefact_instance_from_id($p->blogpost);
$this->add_artefact_link($post, 'is_attachment_of');
}
}
public function get_leap_type() {
return 'resource';
}
public function get_categories() {
return array(
array(
'scheme' => 'resource_type',
'term' => 'Offline',
'label' => 'File',
)
);
}
public function assign_smarty_vars() {
parent::assign_smarty_vars();
$this->smarty->assign('summary', $this->artefact->get('description'));
$this->smarty->assign('contenttype', $this->artefact->get('filetype'));
$this->smarty->assign('contentsrc', $this->exporter->get('filedir') . $this->filename);
}
public function add_attachments() {
$this->filename = $this->exporter->add_attachment($this->artefact->get_path(), $this->artefact->get('title'));
}
public function get_content() {
return '';
}
}
class LeapExportElementFolder extends LeapExportElement {
public function get_leap_type() {
return 'selection';
}
public function get_categories() {
return array(
array(
'scheme' => 'selection_type',
'term' => 'Folder',
)
);
}
public function get_content() {
return hsc($this->artefact->get('description'));
}
}
class LeapExportElementImage extends LeapExportElementFile { }