From 0c5e812ba7c43856b5193f03b75a3f7a3d51d686 Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Fri, 4 Mar 2011 15:44:20 +0800 Subject: [PATCH] MDL-26392, nwiki and creole format wiki pages should support file attachments --- mod/wiki/edit_form.php | 28 +++--- mod/wiki/lang/en/wiki.php | 8 +- mod/wiki/locallib.php | 116 +++++++------------------ mod/wiki/pagelib.php | 14 +-- mod/wiki/parser/markups/wikimarkup.php | 21 ++++- mod/wiki/parser/parser.php | 4 +- 6 files changed, 82 insertions(+), 109 deletions(-) diff --git a/mod/wiki/edit_form.php b/mod/wiki/edit_form.php index 17f6d14e3d5..054555901f9 100644 --- a/mod/wiki/edit_form.php +++ b/mod/wiki/edit_form.php @@ -80,20 +80,20 @@ class mod_wiki_edit_form extends moodleform { $mform->addElement('hidden', 'contentformat'); $mform->setDefault('contentformat', $format); -// if ($format != 'html') { -// //uploads -// $mform->addElement('header', 'attachments_tags', get_string('attachments', 'wiki')); -// $mform->addElement('filemanager', 'attachments', get_string('attachments', 'wiki'), null, page_wiki_edit::$attachmentoptions); -// $fileinfo = array( -// 'contextid'=>$contextid, -// 'component'=>'mod_wiki', -// 'filearea'=>$filearea, -// 'itemid'=>$fileitemid, -// ); -// -// $mform->addElement('wikifiletable', 'deleteuploads', get_string('wikifiletable', 'wiki'), null, $fileinfo, $format); -// $mform->addElement('submit', 'editoption', get_string('upload', 'wiki'), array('id' => 'tags')); -// } + if ($format != 'html') { + //uploads + $mform->addElement('header', 'attachments_tags', get_string('uploadtitle', 'wiki')); + $mform->addElement('filemanager', 'attachments', '', null, page_wiki_edit::$attachmentoptions); + $fileinfo = array( + 'contextid'=>$contextid, + 'component'=>'mod_wiki', + 'filearea'=>$filearea, + 'itemid'=>$fileitemid, + ); + + //$mform->addElement('wikifiletable', 'deleteuploads', get_string('wikifiletable', 'wiki'), null, $fileinfo, $format); + //$mform->addElement('submit', 'editoption', get_string('upload', 'wiki'), array('id' => 'tags')); + } if (!empty($CFG->usetags)) { $mform->addElement('header', 'tagshdr', get_string('tags', 'tag')); diff --git a/mod/wiki/lang/en/wiki.php b/mod/wiki/lang/en/wiki.php index 5a2069cae2b..a12e3d594d2 100644 --- a/mod/wiki/lang/en/wiki.php +++ b/mod/wiki/lang/en/wiki.php @@ -68,12 +68,16 @@ To create a new page, type the new page name enclosed in double square brackets, $string['formatcreole'] = 'Creole format'; $string['formatcreole_help'] = 'Creole is a common wiki markup language with an edit toolbar for inserting appropriate markup. -To create a new page, type the new page name enclosed in double square brackets, for example [[Page 2]].'; +To create a new page, type the new page name enclosed in double square brackets, for example [[Page 2]]. + +To insert image, attach the image files in file manager, then reference the file name using creole markup language.'; $string['formatcreole_link'] = 'mod/wiki/creole'; $string['formatnwiki'] = 'NWiki format'; $string['formatnwiki_help'] = 'Nwiki is the Mediawiki-like markup language used in the contributed Nwiki module. -To create a new page, type the new page name enclosed in double square brackets, for example [[Page 2]].'; +To create a new page, type the new page name enclosed in double square brackets, for example [[Page 2]]. + +To insert image, attach the image files in file manager, then reference the file name using nwiki markup language.'; $string['formatnwiki_link'] = 'mod/wiki/nwiki'; $string['history'] = 'History'; $string['history_help'] = 'The history lists links to previous versions of the page.'; diff --git a/mod/wiki/locallib.php b/mod/wiki/locallib.php index 91feed29b3a..5a284dbcc8c 100644 --- a/mod/wiki/locallib.php +++ b/mod/wiki/locallib.php @@ -578,7 +578,23 @@ function wiki_parse_content($markup, $pagecontent, $options = array()) { $cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid); $context = get_context_instance(CONTEXT_MODULE, $cm->id); - $parser_options = array('link_callback' => '/mod/wiki/locallib.php:wiki_parser_link', 'link_callback_args' => array('swid' => $options['swid']), 'table_callback' => '/mod/wiki/locallib.php:wiki_parser_table', 'real_path_callback' => '/mod/wiki/locallib.php:wiki_parser_real_path', 'real_path_callback_args' => array('context' => $context, 'component' => 'mod_wiki', 'filearea' => 'attachments', 'pageid' => $options['pageid']), 'pageid' => $options['pageid'], 'pretty_print' => (isset($options['pretty_print']) && $options['pretty_print']), 'printable' => (isset($options['printable']) && $options['printable'])); + $parser_options = array('link_callback' => '/mod/wiki/locallib.php:wiki_parser_link', + 'link_callback_args' => array('swid' => $options['swid']), + 'table_callback' => '/mod/wiki/locallib.php:wiki_parser_table', + 'real_path_callback' => '/mod/wiki/locallib.php:wiki_parser_real_path', + 'real_path_callback_args' => array( + 'context' => $context, + 'component' => 'mod_wiki', + 'filearea' => 'attachments', + 'subwikiid'=> $subwiki->id, + 'pageid' => $options['pageid'] + ), + 'preview' => !empty($options['preview']) ? true : false, + 'itemid' => !empty($options['itemid']) ? $options['itemid'] : null, + 'pageid' => $options['pageid'], + 'pretty_print' => (isset($options['pretty_print']) && $options['pretty_print']), + 'printable' => (isset($options['printable']) && $options['printable']) + ); return wiki_parser_proxy::parse($pagecontent, $markup, $parser_options); } @@ -661,7 +677,7 @@ function wiki_parser_table($table) { /** * Returns an absolute path link, unless there is no such link. * - * @param string url Link's URL + * @param string url Link's URL or filename * @param stdClass context filearea params * @param string filearea * @param int fileareaid @@ -669,13 +685,26 @@ function wiki_parser_table($table) { * @return File full path */ -function wiki_parser_real_path($url, $context, $filearea, $fileareaid) { +function wiki_parser_real_path($url, $context, $component, $filearea, $swid) { global $CFG; if (preg_match("/^(?:http|ftp)s?\:\/\//", $url)) { return $url; } else { - return "{$CFG->wwwroot}/pluginfile.php/{$context->id}/$filearea/$fileareaid/$url"; + if ($context->contextlevel == CONTEXT_USER) { + // when preview wiki page, files are in draft area + $file = 'draftfile.php'; + } else if ($context->contextlevel == CONTEXT_MODULE) { + $file = 'pluginfile.php'; + } else { + $file = 'brokenfile.php#'; + } + if (!$CFG->slasharguments) { + $file = $file . '?file='; + } + $baseurl = "$CFG->wwwroot/$file/{$context->id}/$component/$filearea/$swid/"; + // it is a file in current file area + return $baseurl . $url; } } @@ -995,85 +1024,6 @@ function wiki_delete_old_locks() { $DB->delete_records_select('wiki_locks', "lockedat < ?", array(time() - 3600)); } -/** - * File processing - */ - -/** - * Uploads files to permanent disk space. - * - * @param int draftitemid Draft space ID - * @param int contextid - * - * @return array of files that have not been inserted. - */ - -function wiki_process_attachments($draftitemid, $deleteuploads, $contextid, $filearea, $itemid, $options = null) { - global $CFG, $USER; - - if (empty($options)) { - $options = page_wiki_edit::$attachmentoptions; - } - - $errors = array(); - - $usercontext = get_context_instance(CONTEXT_USER, $USER->id); - $fs = get_file_storage(); - - $oldfiles = $fs->get_area_files($contextid, 'mod_wiki', 'attachments', $itemid, 'id'); - - foreach ($oldfiles as $file) { - if (in_array($file->get_pathnamehash(), $deleteuploads)) { - $file->delete(); - } - } - - $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id'); - $oldfiles = $fs->get_area_files($contextid, 'mod_wiki', 'attachments', $itemid, 'id'); - - $file_record = array('contextid' => $contextid, 'component' => 'mod_wiki', 'filearea' => 'attachments', 'itemid' => $itemid); - //more or less a merge... - $newhashes = array(); - foreach ($draftfiles as $file) { - $newhash = sha1("/$contextid/mod_wiki/attachments/$itemid" . $file->get_filepath() . $file->get_filename()); - $newhashes[$newhash] = $file; - } - - $filecount = 0; - foreach ($oldfiles as $file) { - $oldhash = $file->get_pathnamehash(); - if (!$file->is_directory() && isset($newhashes[$oldhash])) { - //repeated file: ERROR!!! - unset($newhashes[$oldhash]); - $errors[] = $file; - } - - if (!$file->is_directory()) { - $filecount++; - } - } - - foreach ($newhashes as $file) { - if ($file->get_filepath() !== '/' or $file->is_directory()) { - continue; - } - - if ($options['maxfiles'] and $options['maxfiles'] <= $filecount) { - break; - } - - if (!$file->is_directory()) { - $filecount++; - $fs->create_file_from_storedfile($file_record, $file); - } - } - - //delete all draft files - $fs->delete_area_files($usercontext->id, 'user', 'draft', $draftitemid); - - return $errors; -} - function wiki_get_comment($commentid){ global $DB; return $DB->get_record('comments', array('id' => $commentid)); diff --git a/mod/wiki/pagelib.php b/mod/wiki/pagelib.php index f49545eab25..d438765d581 100644 --- a/mod/wiki/pagelib.php +++ b/mod/wiki/pagelib.php @@ -367,7 +367,7 @@ class page_wiki_edit extends page_wiki { function __construct($wiki, $subwiki, $cm) { global $CFG, $PAGE; parent::__construct($wiki, $subwiki, $cm); - self::$attachmentoptions = array('subdirs' => false, 'maxfiles' => - 1, 'maxbytes' => $CFG->maxbytes, 'accepted_types' => '*'); + self::$attachmentoptions = array('subdirs' => false, 'maxfiles' => - 1, 'maxbytes' => $CFG->maxbytes, 'accepted_types' => 'image'); $PAGE->requires->js_init_call('M.mod_wiki.renew_lock', null, true); $PAGE->requires->yui2_lib('connection'); } @@ -540,9 +540,9 @@ class page_wiki_edit extends page_wiki { $data = file_prepare_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $context, 'mod_wiki', 'attachments', $this->subwiki->id); break; default: - //$draftitemid = file_get_submitted_draft_itemid('attachments'); - //file_prepare_draft_area($draftitemid, $context->id, 'mod_wiki', 'attachments', $this->subwiki->id); - //$data->attachments = $draftitemid; + $draftitemid = file_get_submitted_draft_itemid('attachments'); + file_prepare_draft_area($draftitemid, $context->id, 'mod_wiki', 'attachments', $this->subwiki->id); + $data->attachments = $draftitemid; } if ($version->contentformat != 'html') { @@ -588,7 +588,6 @@ class page_wiki_edit extends page_wiki { if ($this->upload) { file_save_draft_area_files($this->attachments, $context->id, 'mod_wiki', 'attachments', $this->subwiki->id); return null; - //return wiki_process_attachments($this->attachments, $this->deleteuploads, $context->id, 'mod_wiki', 'attachments', $this->subwiki->id); } } } @@ -1034,12 +1033,13 @@ class page_wiki_preview extends page_wiki_edit { $form = new mod_wiki_edit_form($url, $params); - $options = array('swid' => $this->page->subwikiid, 'pageid' => $this->page->id, 'pretty_print' => true); + $options = array('swid' => $this->page->subwikiid, 'pageid' => $this->page->id, 'pretty_print' => true, 'preview'=>true); if ($data = $form->get_data()) { if (isset($data->newcontent)) { // wiki fromat $text = $data->newcontent; + $options['itemid'] = $data->attachments; } else { // html format $text = $data->newcontent_editor['text']; @@ -1047,7 +1047,7 @@ class page_wiki_preview extends page_wiki_edit { $parseroutput = wiki_parse_content($data->contentformat, $text, $options); $this->set_newcontent($text); echo $OUTPUT->notification(get_string('previewwarning', 'wiki'), 'notifyproblem wiki_info'); - $content = format_text($parseroutput['parsed_text'], FORMAT_HTML, array('overflowdiv'=>true)); + $content = format_text($parseroutput['parsed_text'], FORMAT_HTML, array('overflowdiv'=>true, 'filter'=>false)); echo $OUTPUT->box($content, 'generalbox wiki_previewbox'); $content = $this->newcontent; } diff --git a/mod/wiki/parser/markups/wikimarkup.php b/mod/wiki/parser/markups/wikimarkup.php index 30976db0c6a..a748710074e 100644 --- a/mod/wiki/parser/markups/wikimarkup.php +++ b/mod/wiki/parser/markups/wikimarkup.php @@ -25,6 +25,10 @@ abstract class wiki_markup_parser extends generic_parser { //header & ToC protected $toc = array(); + /** @var boolean parse content for preview*/ + protected $preview = false; + /** @var int if preview, itemid will be required to generate file url */ + protected $itemid = null; /** * function wiki_parser_link_callback($link = "") @@ -143,6 +147,10 @@ abstract class wiki_markup_parser extends generic_parser { $this->printable = true; } break; + case 'preview': + $this->preview = $options['preview']; + $this->itemid = $options['itemid']; + break; } } } @@ -324,7 +332,18 @@ abstract class wiki_markup_parser extends generic_parser { } protected function real_path($url) { - $callbackargs = array_merge(array($url), $this->realpathcallbackargs); + global $USER; + if ($this->preview) { + // when preview wiki page, images are in draft area + $callbackargs = array(); + $callbackargs[] = $url; + $callbackargs[] = get_context_instance(CONTEXT_USER, $USER->id); + $callbackargs[] = 'user'; + $callbackargs[] = 'draft'; + $callbackargs[] = $this->itemid; + } else { + $callbackargs = array_merge(array($url), $this->realpathcallbackargs); + } return call_user_func_array($this->realpathcallback, $callbackargs); } diff --git a/mod/wiki/parser/parser.php b/mod/wiki/parser/parser.php index b8af04df213..122061ccd05 100644 --- a/mod/wiki/parser/parser.php +++ b/mod/wiki/parser/parser.php @@ -139,8 +139,8 @@ abstract class generic_parser { /** * Initialize options */ - - protected function set_options($options) {} + protected function set_options($options) { + } /** * Block processing function & callbacks -- 2.43.0