Changeset 398
- Timestamp:
- 04/30/07 23:48:06 (1 year ago)
- Files:
-
- SVN-TracWiki/examples/config.yaml (modified) (1 diff)
- SVN-TracWiki/lib/SVN/TracWiki.pm (modified) (7 diffs)
- SVN-TracWiki/lib/SVN/TracWiki/Plugin/Extract.pm (modified) (1 diff)
- SVN-TracWiki/lib/SVN/TracWiki/Plugin/Extract/Pod.pm (added)
- SVN-TracWiki/lib/SVN/TracWiki/Plugin/Extract/PowerPoint.pm (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
SVN-TracWiki/examples/config.yaml
r396 r398 4 4 5 5 trac: 6 endpoint: http://trac.mizzy.org/public/login/xmlrpc6 xmlrpc_endpoint: http://trac.mizzy.org/public/login/xmlrpc 7 7 realm: trac authorized area 8 8 username: foo SVN-TracWiki/lib/SVN/TracWiki.pm
r396 r398 7 7 use SVN::TracWiki::ConfigLoader; 8 8 use Path::Class; 9 use MIME::Types;10 9 use RPC::XML; 11 10 use RPC::XML::Client; 12 11 use File::Extract; 13 12 use UNIVERSAL::can; 13 use File::MMagic; 14 14 15 15 $RPC::XML::ENCODING = 'UTF-8'; … … 34 34 35 35 my $self = bless $opts, $class; 36 $self->{mime_types} = []; 36 37 37 38 my $config_loader = SVN::TracWiki::ConfigLoader->new; … … 43 44 my $self = shift; 44 45 46 45 47 my $e = File::Extract->new; 46 $e->magic->add_file_ext('xls', 'application/excel');47 48 $self->{extractor} = $e; 49 $e->magic->addFileExts('xls', 'application/excel'); 48 50 49 51 $self->load_plugins; 50 52 51 53 my @files = $self->get_files; 52 53 54 54 55 my $temp_dir = $self->{config}->{svn}->{temp_dir}; 55 56 for my $file ( @files ) { 56 my $path = file( File::Spec->catfile( $temp_dir, $file ) ); 57 my $r = $self->{extractor}->extract($path->stringify); 58 57 my $path = File::Spec->catfile( $temp_dir, $file ); 58 my $r = $self->{extractor}->extract($path); 59 59 next unless $r; 60 60 61 my $text = $r->text; 62 next unless $text; 61 63 62 64 Encode::_utf8_off($text) if Encode::is_utf8($text); 63 65 next if $r->mime_type eq 'text/plain' or $r->mime_type eq 'text/html'; 64 66 65 $text = "source:$file\n\n{{{\n" . $text . "\n}}}"; 67 $text = "{{{\n$text\n}}}" if $self->{format_of}->{$r->mime_type} eq 'text'; 68 $text = "source:$file\n\n$text"; 66 69 $self->publish_to_wiki($file, $text); 67 70 } … … 72 75 sub load_plugins { 73 76 my $self = shift; 77 78 my $e = $self->{extractor}; 74 79 75 80 my @plugins; … … 78 83 next unless $name; 79 84 80 my $e = $self->{extractor}; 81 my $ext = $class->ext; 85 my $ext_ref = $class->ext; 82 86 my $mime_type = $class->mime_type; 83 $e->magic->add_file_ext($ext, $mime_type) if $ext; 87 88 $ext_ref = [ $ext_ref ] if ref $ext_ref ne 'ARRAY'; 89 map { $e->magic->addFileExts( $_, $mime_type ) } @$ext_ref; 90 91 $self->{format_of}->{$mime_type} = $class->can('format'); 92 84 93 $e->register_processor($class); 85 94 … … 106 115 my $output_path = file( File::Spec->catfile( $temp_dir, $file ) ); 107 116 $output_path->parent->mkpath; 117 $output_path->cleanup; 108 118 system "$svnlook cat $repos $file > $output_path"; 109 119 push @files, $file; … … 128 138 my ( $self, $page, $text ) = @_; 129 139 140 if ( $self->{config}->{trac}->{xmlrpc_endpoint} ) { 141 $self->send_via_xmlrpc($page, $text); 142 }else{ 143 $self->send_via_basic_auth($page, $text); 144 } 145 } 146 147 sub send_via_xmlrpc { 148 my ($self, $page, $text) = @_; 149 130 150 my $conf = $self->{config}->{trac}; 131 my $ua = RPC::XML::Client->new($conf->{ endpoint});151 my $ua = RPC::XML::Client->new($conf->{xmlrpc_endpoint}); 132 152 $ua->credentials($conf->{realm}, $conf->{username}, $conf->{password}); 133 153 134 my $res = $ua->send_request(135 'wiki.putPage',136 RPC::XML::string->new($page),137 RPC::XML::string->new($text),138 RPC::XML::struct->new,139 );140 141 if ( ref $res eq 'RPC::XML::fault' ) {142 die$res->string;143 }154 my $res = $ua->send_request( 155 'wiki.putPage', 156 RPC::XML::string->new($page), 157 RPC::XML::string->new($text), 158 RPC::XML::struct->new, 159 ); 160 161 if ( ref $res eq 'RPC::XML::fault' ) { 162 warn $res->string; 163 } 144 164 elsif ( !ref $res ) { 145 die $res; 146 } 165 warn $res; 166 } 167 } 168 169 sub send_via_basic_auth { 170 my ($self, $page, $text) = @_; 171 172 my $conf = $self->{config}->{trac}; 173 174 my $ua = LWP::UserAgent->new; 175 $ua->cookie_jar( {} ); 176 177 my $req = HTTP::Request->new( GET => $conf->{trac_url} . "/login/" ); 178 $req->authorization_basic( $conf->{username}, $conf->{password} ); 179 180 my $res = $ua->request( $req ); 181 182 # are we logged in ? 183 if ( !$res->is_success ) { 184 die ( "Can't login on this trac, check your login" ); 185 } 186 my $cookie = $res->request->headers->{ cookie }; 187 $cookie =~ /trac_form_token=(.*);/; 188 my $form_token = $1; 189 190 my $page_rev = 0; 191 my $page_url = $conf->{trac_url} . "/wiki/" . $page; 192 193 $req = HTTP::Request->new( GET => $page_url ); 194 $res = $ua->request( $req ); 195 196 if ( $res->is_success ) { 197 if ( $res->content 198 =~ /<input type="hidden" name="version" value="(\d+?)" \/>/ ) 199 { 200 $page_rev = $1; 201 } 202 } 203 204 $req = HTTP::Request->new( POST => $page_url ); 205 $req->content_type( 'application/x-www-form-urlencoded' ); 206 $req->content( "__FORM_TOKEN=" 207 . $form_token 208 . "&action=edit&version=" 209 . $page_rev 210 . "&text=" 211 . $text 212 . "&save=Submit+changes" ); 213 $res = $ua->request( $req ); 214 } 215 216 BEGIN { 217 no warnings 'redefine'; 218 219 *File::Extract::new = sub { 220 my $class = shift; 221 my %args = @_; 222 223 my $encoding = $args{output_encoding} || 'utf8'; 224 my @encodings = $args{encodings} ? 225 (ref($args{encodings}) eq 'ARRAY' ? @{$args{encodings}} : $args{encodings}) : (); 226 my $self = bless { 227 filters => $args{filters}, 228 processors => $args{processors}, 229 magic => File::MMagic->new, 230 encodings => \@encodings, 231 output_encoding => $encoding 232 }, $class; 233 return $self; 234 }; 235 236 *File::MMagic::checktype_filename = sub { 237 File::MMagic::checktype_byfilename(@_); 238 }; 239 240 *File::MMagic::checktype_filename = *File::MMagic::checktype_byfilename; 147 241 } 148 242 SVN-TracWiki/lib/SVN/TracWiki/Plugin/Extract.pm
r396 r398 5 5 use base qw( File::Extract SVN::TracWiki::Plugin Class::Data::Inheritable ); 6 6 7 sub ext { } 7 8 sub mime_type { } 8 sub extract {} 9 sub extract { } 10 sub format { } 9 11 10 12 sub strip_html { SVN-TracWiki/lib/SVN/TracWiki/Plugin/Extract/PowerPoint.pm
r396 r398 7 7 use Carp; 8 8 9 sub ext { 'ppt' }; 10 sub mime_type { 'application/powerpoint' }; 9 sub ext { 'ppt' } 10 sub mime_type { 'application/powerpoint' } 11 sub format { 'text' } 11 12 12 13 sub extract {
