diff --git a/data/System/RenderPlugin.txt b/data/System/RenderPlugin.txt index 7242f61..1a3edc7 100644 --- a/data/System/RenderPlugin.txt +++ b/data/System/RenderPlugin.txt @@ -19,28 +19,28 @@ One line description, required for extensions repository catalog. This plugin implements three simple REST handlers to get content from Foswiki. For example, these can be used to easily replace a server-side INCLUDE with a -functinal equivalent using an ajax call to one of these REST handlers. +functional equivalent using an ajax call to one of these REST handlers. This comes in handy to asynchronously load content in a tabbed interface, -loading subsequent content for a treeview, a menu widget or tooltips. +loading subsequent content for a treeview, a menu widget, or tooltips. You may also use it to just render a snippet of TopicMarkup on the server and return a HTML preview back to the browser. -The REST handlers of this plugin have been designed rather generic. -So besides some very basic parameter checking, most error handling is up +The REST handlers of this plugin have been designed to be rather generic. +So, besides some very basic parameter checking, most error handling is up to the callback analysing the return value of the REST handler. The -main purpose is best described of being an intermediate transport device. +main purpose is best described as being an intermediate transport device. Compare this to other plugins that implement more specific REST handlers. -As such they are rather tailored to one specific service and thus will be dealing with +As such, they are rather tailored to one specific service and thus will be dealing with error cases on the server side in much more detail. -The advantage of using this plugin's REST handler is, that you don't have +The advantage of using this plugin's REST handler is that you don't have to implement service backends using perl. Instead, you can write TopicFunctions that contain the WikiApplication to be executed when it is called by an appropriate ajax request. This basically is the client side equivalent of -a parametrized =INCLUDE= as it is used on server side WikiApplications (see examples below). +a parametrized =INCLUDE=, as it is used on server side WikiApplications (see examples below). ---++ Syntax The available REST handlers are: @@ -51,9 +51,11 @@ The available REST handlers are: | =tag= | =name=, %BR% =param=, %BR% =*=, %BR% =render= | this is a convenience function to expand one specific \ variable instead of having to build up a snippet of code that you want to =expand= or =render=; \ The =param= parameter specifies the default parameters to this tag; the =named-params= are additional \ - named parameters; the =render= flag indicates if the result is to be expanded only (see =expand=) \ + named parameters; the =render= flag indicates if the result is to be expanded only \ or if it should be renderd into HTML; default is off, that is only expand common tags but don't interpret \ TopicMarkup | +| =template= | =name=, %BR% =expand=, %BR% =render= | this handler loads the template =name= and expands the macro \ + given in =expand=; the =render= flag indicates whether the result is to be rendered in addition to common tags being interpreted | ---++ Usage The REST handlers are called like this: @@ -144,8 +146,9 @@ Okay, your turn to create an example calling SEARCH. | Plugin Author: | Michael Daum | | Copyright ©: | 2006-2009, Michael Daum http://michaeldaumconsulting.com | | License: | GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]]) | -| Plugin Version: | 2.0 | +| Plugin Version: | %$RELEASE% | | Change History: |   | +| 12 May 2009: | added =template= handler | | 24 Apr 2009: | converted to foswiki plugin | | 07 Jan 2009: | added upload rest handler; \ fixes for foswiki and FastCGIContrib | diff --git a/lib/Foswiki/Plugins/RenderPlugin.pm b/lib/Foswiki/Plugins/RenderPlugin.pm index 73c767d..e82b7cd 100644 --- a/lib/Foswiki/Plugins/RenderPlugin.pm +++ b/lib/Foswiki/Plugins/RenderPlugin.pm @@ -22,7 +22,7 @@ use strict; use vars qw( $VERSION $RELEASE $SHORTDESCRIPTION $NO_PREFS_IN_TOPIC ); $VERSION = '$Rev$'; -$RELEASE = '2.0'; +$RELEASE = '3.0'; $SHORTDESCRIPTION = 'Render WikiApplications asynchronously'; $NO_PREFS_IN_TOPIC = 1; @@ -40,9 +40,9 @@ sub initPlugin { my ($topic, $web, $user, $installWeb) = @_; Foswiki::Func::registerRESTHandler('tag', \&restTag); + Foswiki::Func::registerRESTHandler('template', \&restTemplate); Foswiki::Func::registerRESTHandler('expand', \&restExpand); Foswiki::Func::registerRESTHandler('render', \&restRender); - Foswiki::Func::registerRESTHandler('upload', \&restUpload); return 1; } @@ -78,6 +78,40 @@ sub restExpand { return Foswiki::Func::expandCommonVariables($theText, $topic, $web) || ' '; } +############################################################################### +sub restTemplate { + my ($session, $subject, $verb) = @_; + + my $query = Foswiki::Func::getCgiQuery(); + my $theTemplate = $query->param('name'); + return '' unless $theTemplate; + + my $theExpand = $query->param('expand'); + return '' unless $theExpand; + + my $theRender = $query->param('render') || 0; + + $theRender = ($theRender =~ /^\s*(1|on|yes|true)\s*$/) ? 1:0; + my $theTopic = $query->param('topic') || $session->{topicName}; + my $theWeb = $query->param('web') || $session->{webName}; + my ($web, $topic) = Foswiki::Func::normalizeWebTopicName($theWeb, $theTopic); + + Foswiki::Func::loadTemplate($theTemplate); + + require Foswiki::Attrs; + my $attrs = new Foswiki::Attrs($theExpand); + + my $tmpl = $session->templates->tmplP($attrs); + + # and render it + my $result = Foswiki::Func::expandCommonVariables($tmpl, $topic, $web) || ' '; + if ($theRender) { + $result = Foswiki::Func::renderText($result, $web); + } + + return $result; +} + ############################################################################### sub restTag { my ($session, $subject, $verb) = @_; @@ -122,93 +156,4 @@ sub restTag { return $result; } -############################################################################### -sub restUpload { - my ($session, $subject, $verb) = @_; - - my $query = Foswiki::Func::getCgiQuery(); - my $topic = $query->param('topic'); - my $web; - - ($web, $topic) = Foswiki::Func::normalizeWebTopicName("", $topic); - - my $hideFile = $query->param('hidefile') || ''; - my $fileComment = $query->param('filecomment') || ''; - my $createLink = $query->param('createlink') || ''; - my $doPropsOnly = $query->param('changeproperties'); - my $filePath = $query->param('filepath') || ''; - my $fileName = $query->param('filename') || ''; - if ($filePath && ! $fileName) { - $filePath =~ m|([^/\\]*$)|; - $fileName = $1; - } - $fileComment =~ s/\s+/ /go; - $fileComment =~ s/^\s*//o; - $fileComment =~ s/\s*$//o; - $fileName =~ s/\s*$//o; - $filePath =~ s/\s*$//o; - - unless (Foswiki::Func::checkAccessPermission( - 'CHANGE', Foswiki::Func::getWikiName(), undef, $topic, $web)) { - return "Access denied"; - } - - my ($fileSize, $fileDate, $tmpFileName); - - my $stream = $query->upload('filepath') unless $doPropsOnly; - my $origName = $fileName; - - unless($doPropsOnly) { - # SMELL: call to unpublished function - ($fileName, $origName) = - Foswiki::Sandbox::sanitizeAttachmentName($fileName); - - # check if upload has non zero size - if($stream) { - my @stats = stat $stream; - $fileSize = $stats[7]; - $fileDate = $stats[9]; - } - - unless($fileSize && $fileName) { - return "Zero-sized file upload"; - } - - my $maxSize = Foswiki::Func::getPreferencesValue( - 'ATTACHFILESIZELIMIT'); - $maxSize = 0 unless ($maxSize =~ /([0-9]+)/o); - - if ($maxSize && $fileSize > $maxSize * 1024) { - return "Oversized upload"; - } - } - - # SMELL: use of undocumented CGI::tmpFileName - my $tfp = $query->tmpFileName($query->param('filepath')); - my $dontlog = $Foswiki::cfg{Log}{upload}; - $dontlog = $Foswiki::cfg{Log}{upload} unless defined $dontlog; - my $error = Foswiki::Func::saveAttachment( - $web, $topic, $fileName, - { - dontlog => !$dontlog, - comment => $fileComment, - hide => $hideFile, - createlink => $createLink, - stream => $stream, - filepath => $filePath, - filesize => $fileSize, - filedate => $fileDate, - tmpFilename => $tfp, - }); - - close($stream) if $stream; - - return $error if $error; - - # Otherwise allow the rest dispatcher to write a 200 - return - "$origName attached to $web.$topic" . - ($origName ne $fileName ?" as $fileName" : ''); -} - 1;