[rt-commit] [svn] r563 - kwiki-cli/trunk

jesse at fsck.com jesse at fsck.com
Sat Mar 13 18:44:18 EST 2004


Author: jesse
Date: Sat Mar 13 18:44:17 2004
New Revision: 563

Added:
   kwiki-cli/trunk/kwiki   (contents, props changed)
Log:
initial import of a kwiki cli too

Added: kwiki-cli/trunk/kwiki
==============================================================================
--- (empty file)
+++ kwiki-cli/trunk/kwiki	Sat Mar 13 18:44:17 2004
@@ -0,0 +1,147 @@
+#!/usr/bin/perl -w 
+
+use strict;
+use Data::Dumper;
+use WWW::Mechanize;
+use File::Temp qw/tempfile/;
+use Text::Diff;
+use HTTP::Cookies;
+use Config::Simple;
+
+use vars qw/$WIKI_BASE/;
+
+my $config = read_config( $ENV{'HOME'} . "/.kwikirc" );
+
+my $cookies = HTTP::Cookies->new( hide_cookie2 => 1);
+
+my $wiki = shift(@ARGV);
+
+die "Wiki $wiki not found in .kwikirc" unless ( $config->{$wiki}->{URL} );
+
+my $domain;
+if ($config->{$wiki}->{URL} =~ qr{^https?://(.*?)/(.*)}i) {
+    $domain = $1;
+}
+
+
+$cookies->set_cookie( 0, 'prefs', 'user_name&'. $config->{$wiki}->{Name}, '/', $domain,undef, undef, 0);
+my $mech = WWW::Mechanize->new( cookie_jar => $cookies );
+
+
+my $command = shift @ARGV;
+if ( $command =~ /get/ ) {
+    my $page = get_page( $mech, $config->{$wiki}->{URL}, shift @ARGV );
+    print $page;
+
+}
+elsif ( $command =~ /edit|post|put/ ) {
+    my $page = edit_page( $mech, $config->{$wiki}->{URL}, shift @ARGV );
+}
+
+sub get_page {
+    my $mech = shift;
+    my $wiki = shift;
+    my $page = shift;
+
+    $mech->get( $wiki . "?action=edit&page_id=$page" );
+
+    unless ( $mech->success ) {
+        die "Error talking to $wiki";
+    }
+
+    my $wiki_text;
+    foreach my $form ( @{ $mech->forms } ) {
+        $form->find_input('wiki_text')
+          ? $wiki_text = $form->value('wiki_text')
+          : next;
+    }
+
+    return $wiki_text;
+}
+
+sub edit_page {
+    my $mech = shift;
+    my $wiki = shift;
+    my $page = shift;
+
+    my $content = get_page( $mech, $wiki, $page );
+
+    my ( $fh, $filename ) = tempfile( 'kwikigetXXXXX', UNLINK => 1 );
+    print $fh $content;
+    close($fh);
+    system( ( $ENV{'EDITOR'} || 'vi' ), $filename );
+
+    my $new_content = get_page( $mech, $wiki, $page );
+
+    unless ( $content eq $new_content ) {
+
+        my $diff = diff $filename, \$new_content;
+        open( my $newfh, ">>$filename" );
+        print $newfh
+          "\n\n-----\nWhile you were editing the following update was made:\n";
+        print $newfh $diff;
+
+        system( ( $ENV{'EDITOR'} || 'vi' ), $filename );
+    }
+
+    my $update_content;
+    open( UPDATE, $filename );
+    $update_content = join( "", <UPDATE> );
+    close(UPDATE);
+    if ( $update_content eq $new_content ) {
+        print "No changes made.\n";
+        return;
+    }
+
+    do_update( $mech, $wiki, $page, $update_content );
+
+    my $final_content = get_page( $mech, $wiki, $page );
+}
+
+sub do_update {
+    my $mech      = shift;
+    my $wiki      = shift;
+    my $page      = shift;
+    my $new_value = shift;
+
+    $mech->get( $wiki . "?action=edit&page_id=$page" );
+
+    unless ( $mech->success ) {
+        die "Error talking to $wiki";
+    }
+
+    foreach my $form ( @{ $mech->forms } ) {
+        $form->find_input('wiki_text')
+          ? $form->value( 'wiki_text', $new_value )
+          : next;
+
+        $mech->request( $form->click('button-save') ) || die $@;
+        unless ( $mech->success ) {
+            die "Error talking to $wiki";
+        }
+        print "Changes saved.\n";
+    }
+
+}
+
+sub read_config {
+    my $file = shift;
+    open( CFG, $file ) || die "$file not found";
+    my $config;
+    my @config = <CFG>;
+    @config = grep { $_ !~ /^(\s*)#/ } @config;
+
+    for (@config) {
+        my @vals = split( /\s+/, $_ );
+
+        $config->{ $vals[0] }->{URL}  = $vals[1];
+        $config->{ $vals[0] }->{Name} = $vals[2] if ( $vals[2] );
+
+
+
+    }
+    use Data::Dumper;
+    return ($config);
+
+}
+



More information about the Rt-commit mailing list