? templates/attachtables.tmpl Index: lib/TWiki/Attach.pm =================================================================== RCS file: /cvsroot/twiki/twiki/lib/TWiki/Attach.pm,v retrieving revision 1.37 diff -u -r1.37 Attach.pm --- lib/TWiki/Attach.pm 19 Apr 2004 07:54:20 -0000 1.37 +++ lib/TWiki/Attach.pm 28 Apr 2004 10:57:14 -0000 @@ -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 $header = "
\n"; - $header .= "

<\/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( $web, $topic, $meta, $attrs, $isTopTopicRev ) = @_; + + 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,57 +249,80 @@ return "\"\""; } +# ========================= =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 ); - - # 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" ); - } +Not yet documented. - $attrSize = 100 if( $attrSize < 100 ); - $attrSize = sprintf( "%1.1f K", $attrSize / 1024 ); - $attrComment = $attrComment || " "; - $row .= "| $fileIcon $file " - . "| manage " - . "| $attrSize | $attrDate | $attrUser | $attrComment |"; - if ( $showAttr ) { - $attrAttr = $attrAttr || "   "; - $row .= " $attrAttr |"; - } - $row .= "\n"; - } else { - $noviewableAttachmentCount++; - } +=cut - return $row; +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? } +# ========================= +=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 @@ -167,6 +330,7 @@ ---++ 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; @@ -319,156 +498,6 @@ $att->{"date"} = $date; $att->{"user"} = &TWiki::wikiToUserName( $att->{"user"} ); } -} - - - -# ========================= -=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; Index: lib/TWiki/UI/Upload.pm =================================================================== RCS file: /cvsroot/twiki/twiki/lib/TWiki/UI/Upload.pm,v retrieving revision 1.4 diff -u -r1.4 Upload.pm --- lib/TWiki/UI/Upload.pm 25 Apr 2004 17:18:40 -0000 1.4 +++ lib/TWiki/UI/Upload.pm 28 Apr 2004 10:57:16 -0000 @@ -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 ); @@ -98,31 +98,6 @@ $tmpl =~ s/( ?) *<\/?(nop|noautolink)\/?>\n?/$1/gois; # remove and tags TWiki::writeHeader( TWiki::getCgiQuery() ); 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 = "view"; - $result .= "| 1.$version | $viewAction | $date | $wikiUserName | $comment |\n"; - } - - $result = "$result"; - return $result; } # =========================