Changeset 398

Show
Ignore:
Timestamp:
04/30/07 23:48:06 (1 year ago)
Author:
miya
Message:

Add Extract::Pod.
Add non-xmlrpc wiki post support.
Thanks to franck.
Override File::Extract to use File::MMagic instead of File::MMagic::XS

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • SVN-TracWiki/examples/config.yaml

    r396 r398  
    44 
    55trac: 
    6   endpoint: http://trac.mizzy.org/public/login/xmlrpc 
     6  xmlrpc_endpoint: http://trac.mizzy.org/public/login/xmlrpc 
    77  realm: trac authorized area 
    88  username: foo 
  • SVN-TracWiki/lib/SVN/TracWiki.pm

    r396 r398  
    77use SVN::TracWiki::ConfigLoader; 
    88use Path::Class; 
    9 use MIME::Types; 
    109use RPC::XML; 
    1110use RPC::XML::Client; 
    1211use File::Extract; 
    1312use UNIVERSAL::can; 
     13use File::MMagic; 
    1414 
    1515$RPC::XML::ENCODING = 'UTF-8'; 
     
    3434 
    3535    my $self = bless $opts, $class; 
     36    $self->{mime_types} = []; 
    3637 
    3738    my $config_loader = SVN::TracWiki::ConfigLoader->new; 
     
    4344    my $self = shift; 
    4445 
     46 
    4547    my $e = File::Extract->new; 
    46     $e->magic->add_file_ext('xls', 'application/excel'); 
    4748    $self->{extractor} = $e; 
     49    $e->magic->addFileExts('xls', 'application/excel'); 
    4850 
    4951    $self->load_plugins; 
    5052 
    5153    my @files = $self->get_files; 
    52  
    5354 
    5455    my $temp_dir = $self->{config}->{svn}->{temp_dir}; 
    5556    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); 
    5959        next unless $r; 
     60 
    6061        my $text = $r->text; 
     62        next unless $text; 
    6163 
    6264        Encode::_utf8_off($text) if Encode::is_utf8($text); 
    6365        next if $r->mime_type eq 'text/plain' or $r->mime_type eq 'text/html'; 
    6466 
    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"; 
    6669        $self->publish_to_wiki($file, $text); 
    6770    } 
     
    7275sub load_plugins { 
    7376    my $self = shift; 
     77 
     78    my $e = $self->{extractor}; 
    7479 
    7580    my @plugins; 
     
    7883        next unless $name; 
    7984 
    80         my $e         = $self->{extractor}; 
    81         my $ext       = $class->ext; 
     85        my $ext_ref   = $class->ext; 
    8286        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 
    8493        $e->register_processor($class); 
    8594 
     
    106115        my $output_path = file( File::Spec->catfile( $temp_dir, $file ) ); 
    107116        $output_path->parent->mkpath; 
     117        $output_path->cleanup; 
    108118        system "$svnlook cat $repos $file > $output_path"; 
    109119        push @files, $file; 
     
    128138    my ( $self, $page, $text ) = @_; 
    129139 
     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 
     147sub send_via_xmlrpc { 
     148    my ($self, $page, $text) = @_; 
     149 
    130150    my $conf = $self->{config}->{trac}; 
    131     my $ua = RPC::XML::Client->new($conf->{endpoint}); 
     151    my $ua = RPC::XML::Client->new($conf->{xmlrpc_endpoint}); 
    132152    $ua->credentials($conf->{realm}, $conf->{username}, $conf->{password}); 
    133153 
    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   
    144164    elsif ( !ref $res ) { 
    145         die $res; 
    146     } 
     165        warn $res; 
     166    } 
     167
     168 
     169sub 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 
     216BEGIN { 
     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; 
    147241} 
    148242 
  • SVN-TracWiki/lib/SVN/TracWiki/Plugin/Extract.pm

    r396 r398  
    55use base qw( File::Extract SVN::TracWiki::Plugin Class::Data::Inheritable ); 
    66 
     7sub ext { } 
    78sub mime_type { } 
    8 sub extract {} 
     9sub extract { } 
     10sub format { } 
    911 
    1012sub strip_html { 
  • SVN-TracWiki/lib/SVN/TracWiki/Plugin/Extract/PowerPoint.pm

    r396 r398  
    77use Carp; 
    88 
    9 sub ext { 'ppt' }; 
    10 sub mime_type { 'application/powerpoint' }; 
     9sub ext { 'ppt' } 
     10sub mime_type { 'application/powerpoint' } 
     11sub format { 'text' } 
    1112 
    1213sub extract {