From: Sven D. <sdo...@nt...> - 2004-06-18 14:03:49
|
Author: sdowideit Date: 2004-06-18 07:02:53 -0700 (Fri, 18 Jun 2004) New Revision: 1535 Added: twiki/trunk/templates/attachtables.tmpl Modified: twiki/trunk/lib/TWiki/Attach.pm twiki/trunk/lib/TWiki/UI/Upload.pm Log: Codev/TemplatesForMetaDataPresentation CrawfordCurrie! Modified: twiki/trunk/lib/TWiki/Attach.pm =================================================================== --- twiki/trunk/lib/TWiki/Attach.pm 2004-06-18 06:54:26 UTC (rev 1534) +++ twiki/trunk/lib/TWiki/Attach.pm 2004-06-18 14:02:53 UTC (rev 1535) @@ -19,6 +19,8 @@ # - Installation instructions in $dataDir/TWiki/TWikiDocumentation.txt # - Customize variables in TWiki.cfg when installing TWiki. +use strict; + =begin twiki ---+ TWiki::Attach Module @@ -29,67 +31,205 @@ package TWiki::Attach; -use vars qw( - $viewableAttachmentCount $noviewableAttachmentCount $attachmentCount - ); +use vars qw( %templateVars ); # ====================== =pod ---++ sub renderMetaData ( $web, $topic, $meta, $args, $isTopRev ) -Not yet documented. +Generate a table of attachments suitable for the bottom of a topic +view, using templates for the header, footer and each row. +| =$web= | the web | +| =$topic= | the topic | +| =$meta= | meta-data hash for the topic | +| =$attrs= | hash of attachment arguments | +| $isTopTopicRev | 1 if this topic is being rendered at the most recent revision | =cut sub renderMetaData { - my( $web, $topic, $meta, $args, $isTopRev ) = @_; - - my $metaText = ""; - - my $showAttr = ""; - my $showAll = &TWiki::extractNameValuePair( $args, "all" ); - if( $showAll ) { - $showAttr = "h"; - } - - $viewableAttachmentCount = 0; - $noviewableAttachmentCount = 0; - $attachmentCount = 0; + my( $web, $topic, $meta, $attrs, $isTopTopicRev ) = @_; - my $header = "<div class=\"TWikiAttachments\">\n"; - $header .= "<p><\/p>\n"; # prefix empty line - $header .= "| *[[$TWiki::twikiWebname.FileAttachment][Attachment]]* | *Action* | *Size* | *Date* | *Who* | *Comment* |"; - if( $showAttr ) { - $header .= " *[[$TWiki::twikiWebname.FileAttribute][Attribute]]* |"; - } - $header .= "\n"; - - my @attachments = $meta->find( "FILEATTACHMENT" ); + my $showAll = TWiki::extractNameValuePair( $attrs, "all" ); + my $showAttr = $showAll ? "h" : ""; + my $a = ( $showAttr ) ? ":A" : ""; + + my @attachments = $meta->find( "FILEATTACHMENT" ); + + my $rows = ""; + my $row = _getTemplate("ATTACH:files:row$a"); foreach my $attachment ( @attachments ) { - $metaText .= formatAttachments( $web, $topic, $showAttr, $isTopRev, %$attachment ); + my $attrAttr = $attachment->{attr}; + + if( ! $attrAttr || ( $showAttr && $attrAttr =~ /^[$showAttr]*$/ )) { + $rows .= _formatRow( $web, + $topic, + $attachment->{name}, + $attachment->{version}, + $isTopTopicRev, + $attachment->{date}, + $attachment->{user}, + $attachment->{comment}, + $attachment, + $row ); + } } - + my $text = ""; - if( $showAll || $viewableAttachmentCount ) { - $text = "$header$metaText<\/div>"; + + if( $showAll || $rows ne "" ) { + my $header = _getTemplate("ATTACH:files:header$a"); + my $footer = _getTemplate("ATTACH:files:footer$a"); + + $text = "$header$rows$footer"; } - return $text; } +# PRIVATE get a template, reading the attachment tables template +# if not already defined. +sub _getTemplate { + my $template = shift; + if ( ! defined( $templateVars{$template} )) { + TWiki::Store::readTemplate("attachtables"); + } + + return TWiki::Store::handleTmplP($template); +} + +#========================= +=pod + +---++ sub formatVersions ( $theWeb, $theTopic, $attachment, $attrs ) + +Generate a version history table for a single attachment +| =$web= | the web | +| =$topic= | the topic | +| =$attachment= | basename of attachment | +| =$attrs= | Hash of meta-data attributes | + +=cut + +sub formatVersions { + my( $web, $topic, $attachment, $attrs ) = @_; + + my $latestRev = TWiki::Store::getRevisionNumber( $web, $topic, $attachment ); + $latestRev =~ m/\.(.*)/o; + my $maxRevNum = $1; + + my $header = _getTemplate("ATTACH:versions:header"); + my $footer = _getTemplate("ATTACH:versions:footer"); + my $row = _getTemplate("ATTACH:versions:row"); + + my $rows =""; + + for( my $version = $maxRevNum; $version >= 1; $version-- ) { + my $rev = "1.$version"; + + my( $date, $userName, $minorRev, $comment ) = + TWiki::Store::getRevisionInfo( $web, $topic, $rev, $attachment ); + $rows .= _formatRow( $web, $topic, + $attachment, + $rev, + ( $rev eq $latestRev), + $date, + $userName, + $comment, + $attrs, + $row ); + } + + return "$header$rows$footer"; +} + +#========================= +=pod + +---++ sub _formatRow ( $web, $topic, $file, $rev, $topRev, $date, $userName, $comment, $attrs, $tmpl ) + +Format a single row in an attachment table by expanding a template. +| =$web= | the web | +| =$topic= | the topic | +| =$file= | the attachment file name | +| =$rev= | the required revision; required to be a full (major.minor) revision number | +| =$topRev= | boolean indicating if this revision is the most recent revision | +| =$date= | date of _this revision_ of the attachment | +| =$userName= | user (not wikiname) who uploaded this revision | +| =$comment= | comment against this revision | +| =$attrs= | reference to a hash of other meta-data attributes for the attachment | + +=cut + +sub _formatRow { + my ( $web, $topic, $file, $rev, $topRev, + $date, $userName, $comment, $attrs, $tmpl ) = @_; + + my $row = $tmpl; + + $row =~ s/%A_REV%/$rev/go; + + if ( $row =~ /%A_ICON%/o ) { + my $fileIcon = _filenameToIcon( $file ); + $row =~ s/%A_ICON%/$fileIcon/go; + } + + if ( $row =~ /%A_URL%/o ) { + my $url; + + if ( $topRev || $rev eq "1.1" ) { + # I18N: To support attachments via UTF-8 URLs to attachment + # directories/files that use non-UTF-8 character sets, go through viewfile. + # If using %PUBURL%, must URL-encode explicitly to site character set. + $url = TWiki::handleNativeUrlEncode + ( "%PUBURLPATH%/$web/$topic/$file" ); + } else { + $url = "%SCRIPTURLPATH%/viewfile%SCRIPTSUFFIX%/". + "$web/$topic?rev=$rev&filename=$file"; + } + $row =~ s/%A_URL%/$url/go; + } + + if ( $row =~ /%A_SIZE%/o && $attrs ) { + my $attrSize = $attrs->{size}; + $attrSize = 100 if( $attrSize < 100 ); + $attrSize = sprintf( "%1.1f K", $attrSize / 1024 ); + $row =~ s/%A_SIZE%/$attrSize/go; + } + + $comment = " " unless ( $comment ); + $row =~ s/%A_COMMENT%/$comment/go; + + if ( $row =~ /%A_ATTRS%/o && $attrs ) { + my $attrAttr = $attrs->{attr}; + $attrAttr = $attrAttr || " "; + $row =~ s/%A_ATTRS%/$attrAttr/go; + } + + $row =~ s/%A_FILE%/$file/go; + + $date = TWiki::formatTime( $date ); + $row =~ s/%A_DATE%/$date/go; + + my $wikiUserName = TWiki::userToWikiName( $userName ); + $row =~ s/%A_USER%/$wikiUserName/go; + + return $row; +} + # ========================= =pod ----++ sub filenameToIcon ( $fileName ) +---++ sub _filenameToIcon ( $fileName ) -Not yet documented. +PRIVATE Produce an image tailored to the type of the file, guessed from +it's extension. =cut -sub filenameToIcon +sub _filenameToIcon { my( $fileName ) = @_; @@ -109,64 +249,88 @@ return "<img src=\"$iconUrl/else.gif\" width=\"16\" height=\"16\" align=\"top\" alt=\"\" border=\"0\" />"; } +# ========================= =pod ----++ sub formatAttachments ( $theWeb, $theTopic, $showAttr, $isTopRev, %attachment ) +---++ sub removeFile () -This routine creates attachment links as part of attachment table etc; within -topic text, attachment links are created using %ATTACHURL% and %ATTACHURLPATH%. +Remove attachment macro for specified file from topic +return "", or error string =cut -sub formatAttachments +sub removeFile { - my ( $theWeb, $theTopic, $showAttr, $isTopRev, %attachment ) = @_; + my $theFile = $_[1]; + my $error = ""; + + # %FILEATTACHMENT{[\s]*"$theFile"[^}]*}% + if( ! ( $_[0] =~ s/%FILEATTACHMENT{[\s]*"$theFile"[^}]*}%//) ) { + $error = "Failed to remove attachment $theFile"; + } + return $error; +} - my $row = ""; +# ========================= +=pod - my ( $file, $attrVersion, $attrPath, $attrSize, $attrDate, $attrUser, $attrComment, $attrAttr ) = - TWiki::Attach::extractFileAttachmentArgs( %attachment ); +---++ sub updateProperties ( $fileName, $hideFile, $fileComment, $meta ) - $attachmentCount++; - if ( ! $attrAttr || ( $showAttr && $attrAttr =~ /^[$showAttr]*$/ ) ) { - $viewableAttachmentCount++; - my $fileIcon = TWiki::Attach::filenameToIcon( $file ); +Not yet documented. - # I18N: To support attachments via UTF-8 URLs to attachment - # directories/files that use non-UTF-8 character sets, go through viewfile. - # If using %PUBURL%, must URL-encode explicitly to site character set. - my $fileUrl = "%SCRIPTURLPATH%/viewfile%SCRIPTSUFFIX%/$theWeb/$theTopic?rev=$attrVersion&filename=$file"; - # Go direct to file where possible, for efficiency - if( $isTopRev || $attrVersion eq "1.1" ) { - $fileUrl = TWiki::handleNativeUrlEncode( "%PUBURLPATH%/$theWeb/$theTopic/$file" ); - } +=cut - $attrSize = 100 if( $attrSize < 100 ); - $attrSize = sprintf( "%1.1f K", $attrSize / 1024 ); - $attrComment = $attrComment || " "; - $row .= "| $fileIcon <a href=\"$fileUrl\">$file</a> " - . "| <a href=\"%SCRIPTURL%/attach%SCRIPTSUFFIX%/$theWeb/$theTopic?filename=$file&revInfo=1\"" - . " title=\"change, update, previous revisions, move, delete...\">manage</a> " - . "| $attrSize | $attrDate | $attrUser | $attrComment |"; - if ( $showAttr ) { - $attrAttr = $attrAttr || " "; - $row .= " $attrAttr |"; - } - $row .= "\n"; - } else { - $noviewableAttachmentCount++; - } +sub updateProperties +{ + my( $fileName, $hideFile, $fileComment, $meta ) = @_; - return $row; + my %fileAttachment = $meta->findOne( "FILEATTACHMENT", $fileName ); + $fileAttachment{"attr"} = ( $hideFile ) ? "h" : ""; + $fileAttachment{"comment"} = $fileComment; + $meta->put( "FILEATTACHMENT", %fileAttachment ); + # FIXME warning if no entry? } +# ========================= +=pod +---++ sub updateAttachment ( $fileVersion, $fileName, $filePath, $fileSize, $fileDate, $fileUser, $fileComment, $hideFile, $meta ) + +Add/update attachment for a topic +$text is full set of attachments, new attachments will be added to the end. + +=cut + +sub updateAttachment +{ + my ( $fileVersion, $fileName, $filePath, $fileSize, $fileDate, $fileUser, $fileComment, $hideFile, $meta ) = @_; + + my $tmpAttr = ( $hideFile ) ? "h" : ""; + + my( $theFile, $theVersion, $thePath, $theSize, $theDate, $theUser, + $theComment, $theAttr ) = @_; + + my @attrs = ( + "name" => $fileName, + "version" => $fileVersion, + "path" => $filePath, + "size" => $fileSize, + "date" => $fileDate, + "user" => $fileUser, + "comment" => $fileComment, + "attr" => $tmpAttr + ); + + $meta->put( "FILEATTACHMENT", @attrs ); +} + #========================= =pod ---++ sub migrateFormatForTopic ( $theWeb, $theTopic, $doLogToStdOut ) Not yet documented. +CODE_SMELL: Is this really necessary? migrateFormatForTopic? =cut @@ -195,22 +359,29 @@ } } -# Get file attachment attributes for old html -# format. # ========================= =pod ---++ sub getOldAttachAttr ( $atext ) -Not yet documented. +Get file attachment attributes for old html +format. +CODE_SMELL: Is this really necessary? getOldAttachAttr? =cut sub getOldAttachAttr { my( $atext ) = @_; - my $fileName="", $filePath="", $fileSize="", $fileDate="", $fileUser="", $fileComment=""; - my $before="", $item="", $after=""; + my $fileName=""; + my $filePath=""; + my $fileSize=""; + my $fileDate=""; + my $fileUser=""; + my $fileComment=""; + my $before=""; + my $item=""; + my $after=""; ( $before, $fileName, $after ) = split( /<(?:\/)*TwkFileName>/, $atext ); if( ! $fileName ) { $fileName = ""; } @@ -243,14 +414,14 @@ return ( $fileName, $filePath, $fileSize, $fileDate, $fileUser, $fileComment ); } -# Migrate old HTML format, to %FILEATTACHMENT ... format -# for one piece of text # ========================= =pod ---++ sub migrateToFileAttachmentMacro ( $meta, $text ) -Not yet documented. +Migrate old HTML format, to %FILEATTACHMENT ... format +for one piece of text +CODE_SMELL: Is this really necessary? migrateToFileAttachmentMacro? =cut @@ -271,9 +442,17 @@ getOldAttachAttr( $line ); if( $fileName ) { - my @args = formFileAttachmentArgs( $fileName, "", $filePath, $fileSize, - $fileDate, $fileUser, $fileComment, "" ); - $meta->put( "FILEATTACHMENT", @args ); + my @attrs = ( + "name" => $fileName, + "version" => "", + "path" => $filePath, + "size" => $fileSize, + "date" => $fileDate, + "user" => $fileUser, + "comment" => $fileComment, + "attr" => "" + ); + $meta->put( "FILEATTACHMENT", @attrs ); } } } else { @@ -301,7 +480,7 @@ ---++ sub upgradeFrom1v0beta ( $meta ) -Not yet documented. +CODE_SMELL: Is this really necessary? upgradeFrom1v0beta? =cut @@ -310,7 +489,7 @@ my( $meta ) = @_; my @attach = $meta->find( "FILEATTACHMENT" ); - foreach $att ( @attach ) { + foreach my $att ( @attach ) { my $date = $att->{"date"}; if( $date =~ /-/ ) { $date =~ s/ / /go; @@ -321,154 +500,4 @@ } } - - -# ========================= -=pod - ----++ sub formFileAttachmentArgs () - -Not yet documented. - -=cut - -sub formFileAttachmentArgs -{ - my( $theFile, $theVersion, $thePath, $theSize, $theDate, $theUser, - $theComment, $theAttr ) = @_; - - my @args = ( - "name" => $theFile, - "version" => $theVersion, - "path" => $thePath, - "size" => $theSize, - "date" => $theDate, - "user" => $theUser, - "comment" => $theComment, - "attr" => $theAttr ); - - return @args; -} - - - -# ========================= -# Includes required formatting and conversion -=pod - ----++ sub extractFileAttachmentArgs ( %attributes ) - -Not yet documented. - -=cut - -sub extractFileAttachmentArgs -{ - my( %attributes ) = @_; - - my $file = $attributes{"name"}; - my $attrVersion = $attributes{"version"}; - my $attrPath = $attributes{"path"}; - my $attrSize = $attributes{"size"}; - my $attrDate = $attributes{"date"}; - my $attrUser = $attributes{"user"}; - my $attrComment = $attributes{"comment"}; - my $attrAttr = $attributes{"attr"}; - - $attrDate = &TWiki::formatTime( $attrDate ); - - $attrUser = &TWiki::userToWikiName( $attrUser ); - - return ( $file, $attrVersion, $attrPath, $attrSize, $attrDate, $attrUser, - $attrComment, $attrAttr ); -} - -# FIXME - could be used more? -# ========================== -=pod - ----++ sub extractArgsForFile ( $theText, $theFile ) - -Not yet documented. - -=cut - -sub extractArgsForFile -{ - my ( $theText, $theFile ) = @_; - - if ( $theText =~ /%FILEATTACHMENT{[\s]*("$theFile" [^}]*)}%/o ) { - return extractFileAttachmentArgs( $1 ); - } else { - return ""; - } -} - - -# ========================= -# Remove attachment macro for specified file from topic -# return "", or error string -=pod - ----++ sub removeFile () - -Not yet documented. - -=cut - -sub removeFile -{ - my $theFile = $_[1]; - my $error = ""; - - # %FILEATTACHMENT{[\s]*"$theFile"[^}]*}% - if( ! ( $_[0] =~ s/%FILEATTACHMENT{[\s]*"$theFile"[^}]*}%//) ) { - $error = "Failed to remove attachment $theFile"; - } - return $error; -} - -# ========================= -=pod - ----++ sub updateProperties ( $fileName, $hideFile, $fileComment, $meta ) - -Not yet documented. - -=cut - -sub updateProperties -{ - my( $fileName, $hideFile, $fileComment, $meta ) = @_; - - my %fileAttachment = $meta->findOne( "FILEATTACHMENT", $fileName ); - $fileAttachment{"attr"} = ( $hideFile ) ? "h" : ""; - $fileAttachment{"comment"} = $fileComment; - $meta->put( "FILEATTACHMENT", %fileAttachment ); - # FIXME warning if no entry? -} - -# ========================= -# Add/update attachment for a topic -# $text is full set of attachments, new attachments will be added to the end. -=pod - ----++ sub updateAttachment ( $fileVersion, $fileName, $filePath, $fileSize, $fileDate, $fileUser, $fileComment, $hideFile, $meta ) - -Not yet documented. - -=cut - -sub updateAttachment -{ - my ( $fileVersion, $fileName, $filePath, $fileSize, $fileDate, $fileUser, $fileComment, $hideFile, $meta ) = @_; - - my $tmpAttr = ( $hideFile ) ? "h" : ""; - - my @args = formFileAttachmentArgs( - $fileName, $fileVersion, $filePath, $fileSize, $fileDate, $fileUser, - $fileComment, $tmpAttr ); - $meta->put( "FILEATTACHMENT", @args ); -} - 1; Modified: twiki/trunk/lib/TWiki/UI/Upload.pm =================================================================== --- twiki/trunk/lib/TWiki/UI/Upload.pm 2004-06-18 06:54:26 UTC (rev 1534) +++ twiki/trunk/lib/TWiki/UI/Upload.pm 2004-06-18 14:02:53 UTC (rev 1535) @@ -66,10 +66,6 @@ $isHideChecked = "checked"; } - if ( $fileName ) { - $atext = _listVersions( $webName, $topic, $fileName ); - } - # why log attach before post is called? # FIXME: Move down, log only if successful (or with error msg?) # Attach is a read function, only has potential for a change @@ -86,6 +82,10 @@ } else { $tmpl = TWiki::Store::readTemplate( "attachnew", $skin ); } + if ( $fileName ) { + # must come after templates have been read + $atext .= TWiki::Attach::formatVersions( $webName, $topic, $fileName, %args ); + } $tmpl =~ s/%ATTACHTABLE%/$atext/go; $tmpl =~ s/%FILEUSER%/$fileWikiUser/go; $tmpl = &TWiki::handleCommonTags( $tmpl, $topic ); @@ -100,31 +100,6 @@ print $tmpl; } -sub _listVersions { - my( $web, $topic, $attachment ) = @_; - - my $latestRev = TWiki::Store::getRevisionNumber( $web, $topic, $attachment ); - $latestRev =~ /\.(.*)/; - my $maxRevNum = $1; - my $found = 0; - my $result = "\n| *Version* | *Action* | *Date* | *Who* | *Comment* |\n"; - - for( my $version = $maxRevNum; $version >= 1; $version-- ) { - my $rev = "1.$version"; - - my( $date, $userName, $dummy, $comment ) = - TWiki::Store::getRevisionInfo( $web, $topic, $rev, $attachment ); - $date = TWiki::formatTime( $date ); - my $wikiUserName = &TWiki::userToWikiName( $userName ); - - my $viewAction = "<a href=\"%SCRIPTURLPATH%/viewfile%SCRIPTSUFFIX%/%WEB%/%TOPIC%?rev=$rev&filename=$attachment\">view</a>"; - $result .= "| 1.$version | $viewAction | $date | $wikiUserName | $comment |\n"; - } - - $result = "$result"; - return $result; -} - # ========================= # code fragment to extract pixel size from images # taken from http://www.tardis.ed.ac.uk/~ark/wwwis/ Added: twiki/trunk/templates/attachtables.tmpl =================================================================== --- twiki/trunk/templates/attachtables.tmpl 2004-06-18 06:54:26 UTC (rev 1534) +++ twiki/trunk/templates/attachtables.tmpl 2004-06-18 14:02:53 UTC (rev 1535) @@ -0,0 +1,54 @@ +<!-- Macros for definition of attachment tables --> + +<!-- Header definition for topic attachments and top table in attach screen --> +%TMPL:DEF{ATTACH:files:header(basic)}% +<div class="TWikiAttachments"><p></p> +| *[[%TWIKIWEB%.FileAttachment][Attachment]]* | *Action* | *Size* | *Date* | *Who* | *Comment* |%TMPL:END% + +<!-- A basic row definition, for attachment table and attach screen --> +%TMPL:DEF{ATTACH:files:row(basic)}% +| %A_ICON% <a href="%A_URL%">%A_FILE%</a> | <a href="%SCRIPTURL%/attach%SCRIPTSUFFIX%/%WEB%/%TOPIC%?filename=%A_FILE%&revInfo=1" title="change, update, previous revisions, move, delete...">manage</a> | %A_SIZE% | %A_DATE% | %A_USER% | %A_COMMENT% |%TMPL:END% + +<!-- Header for topic attachment table --> +%TMPL:DEF{ATTACH:files:header}% +%TMPL:P{ATTACH:files:header(basic)}% +%TMPL:END% + +<!-- Row for topic attachment table --> +%TMPL:DEF{ATTACH:files:row}% +%TMPL:P{ATTACH:files:row(basic)}% +%TMPL:END% + +<!-- Footer for topic attachment table --> +%TMPL:DEF{ATTACH:files:footer}% +</div> +%TMPL:END% + +<!-- Header for table at top of attach screen --> +%TMPL:DEF{ATTACH:files:header:A}% +%TMPL:P{ATTACH:files:header(basic)}% *[[%TWIKIWEB%.FileAttribute][Attribute]]* | +%TMPL:END% + +<!-- Row for table at top of attach screen --> +%TMPL:DEF{ATTACH:files:row:A}% +%TMPL:P{ATTACH:files:row(basic)}% %A_ATTRS% | +%TMPL:END% + +<!-- Footer for table at top of attach screen --> +%TMPL:DEF{ATTACH:files:footer:A}% +</div> +%TMPL:END% + +<!-- Header for versions table in attach screen --> +%TMPL:DEF{ATTACH:versions:header}% +| *Version* | *Action* | *Date* | *Who* | *Comment* | +%TMPL:END% + +<!-- Row for versions table in attach screen --> +%TMPL:DEF{ATTACH:versions:row}% +| %A_REV% | <a href="%A_URL%">view</a> | %A_DATE% | %A_USER% | %A_COMMENT% | +%TMPL:END% + +<!-- Footer for versions table in attach screen --> +%TMPL:DEF{ATTACH:versions:footer}% +%TMPL:END% |