Date:   Sunday December 29, 2002 @ 17:46
Author: matt

Update of /home/cvs/AxKit-XSP-Wiki/lib/AxKit/XSP
In directory ted.sergeant.org:/home/matt/Perl/AxKit-XSP-Wiki/lib/AxKit/XSP

Modified Files:
        Wiki.pm 
Log Message:
Merged current live
Log:
PR:

Index: Wiki.pm
===================================================================
RCS file: /home/cvs/AxKit-XSP-Wiki/lib/AxKit/XSP/Wiki.pm,v
retrieving revision 1.5
retrieving revision 1.6
diff -b -u -r1.5 -r1.6
--- Wiki.pm     2002/06/21 15:44:08     1.5
+++ Wiki.pm     2002/12/29 17:46:30     1.6
@@ -5,7 +5,7 @@
 use Apache::AxKit::Language::XSP::TaglibHelper;
 use vars qw($VERSION $NS @ISA @EXPORT_TAGLIB);
 
-$VERSION = '0.04';
+$VERSION = '0.03';
 
 # The namespace associated with this taglib.
 $NS = 'http://axkit.org/NS/xsp/wiki/1';
@@ -13,7 +13,7 @@
 @ISA = qw(Apache::AxKit::Language::XSP::TaglibHelper);
 
 @EXPORT_TAGLIB = (
-    'display_page($dbpath,$db,$page,$action):as_xml=1',
+    'display_page($dbpath,$db,$page,$action;$id):as_xml=1',
 );
 
 use DBI;
@@ -22,13 +22,18 @@
 use XML::LibXML::SAX::Parser;
 use Text::WikiFormat::SAX;
 
-sub display_page ($$$$) {
-    my ($dbpath, $dbname, $page, $action) = @_;
-    
-    my $db;
-    $db = DBI->connect('DBI:SQLite:dbname='. $dbpath . '/wiki-' . $dbname . '.db',
+sub _mkdb {
+    my ($dbpath, $dbname) = @_;
+    return DBI->connect(
+        'DBI:SQLite:dbname='. $dbpath . '/wiki-' . $dbname . '.db',
                       '', '', { AutoCommit => 0, RaiseError => 1 }
                       );
+}
+
+sub display_page ($$$$$) {
+    my ($dbpath, $dbname, $page, $action, $id) = @_;
+    
+    my $db = _mkdb($dbpath, $dbname);
     
     eval {
        $db->do('select * from Page, Formatter where 1 = 2');
@@ -43,6 +48,15 @@
     elsif ($action eq 'edit') {
        return edit_page($db, $page);
     }
+    elsif ($action eq 'history') {
+       return show_history($db, $page);
+    }
+    elsif ($action eq 'historypage') {
+       return show_history_page($db, $page, $id);
+    }
+    # elsif ($action eq 'restore') {
+    #     return restore_page($db, $page, $id);
+    # }
     else {
        die "Unknown action: $action";
     }
@@ -67,13 +81,12 @@
            $parser->parse_string($row->[0]);
        };
        if ($@) {
-           $output = <<EOT;
-<pod>
+           $output = '<pod>
   <para>
-    Error parsing the page: $@
+    Error parsing the page: ' . xml_escape($@) . '
   </para>
 </pod>
-EOT
+  ';
        }
        last;
     }
@@ -87,6 +100,7 @@
 EOT
     }
     $output =~ s/^<\?xml\s.*?\?>//s;
+AxKit::Debug(1, "Wiki Got: $output");
     return $output;
 }
 
@@ -135,22 +149,109 @@
 }
 
 sub save_page {
-    my ($dbpath, $dbname, $page, $contents, $texttype) = @_;
+    my ($dbpath, $dbname, $page, $contents, $texttype, $ip) = @_;
     
-    my $db = DBI->connect('DBI:SQLite:dbname='. $dbpath . '/wiki-' . $dbname . '.db',
-                      '', '', { AutoCommit => 0, RaiseError => 1 }
-                      );
+    my $db = _mkdb($dbpath, $dbname);
+    _save_page($db, $page, $contents, $texttype, $ip);
+}
 
+sub _save_page {
+    my ($db, $page, $contents, $texttype, $ip) = @_;
     # NB fix hard coded formatterid
     my $last_modified = time;
+    my (@row) = $db->selectrow_array("SELECT * FROM Page WHERE name = ?", {}, $page);
+    if (@row) {
+        # store history
+        shift @row; # Remove id
+        $db->do('INSERT INTO History (name, formatterid, content, modified, 
+ip_address)
+                 VALUES (?, ?, ?, ?, ?)', {}, @row);
+    }
     my $sth = $db->prepare(<<'EOT');
-  INSERT OR REPLACE INTO Page ( name, formatterid, content, last_modified )
-  VALUES ( ?, ?, ?, ? )
+  INSERT OR REPLACE INTO Page ( name, formatterid, content, last_modified, ip_address 
+)
+  VALUES ( ?, ?, ?, ?, ? )
 EOT
-    $sth->execute($page, $texttype, $contents, $last_modified);
+    $sth->execute($page, $texttype, $contents, $last_modified, $ip);
     $db->commit;
 }
 
+sub show_history {
+    my ($db, $page) = @_;
+    my $sth = $db->prepare('SELECT * FROM History WHERE name = ? ORDER BY modified 
+DESC');
+    $sth->execute($page);
+    my $hist = '<history>';
+    while (my $row = $sth->fetch) {
+        $hist .= '<entry>';
+        $hist .= '<id>' . xml_escape($row->[0]) . '</id>';
+        $hist .= '<modified>' . xml_escape(scalar gmtime($row->[4])) . '</modified>';
+        $hist .= '<ip-address>' . xml_escape($row->[5]) . '</ip-address>';
+        $hist .= '<bytes>' . xml_escape(length($row->[3])) . '</bytes>';
+        $hist .= '</entry>';
+    }
+    $hist .= '</history>';
+    return $hist;
+}
+
+sub show_history_page {
+    my ($db, $page, $id) = @_;
+    my $sth = $db->prepare(<<'EOT');
+  SELECT History.content, Formatter.module,
+         History.ip_address, History.modified
+  FROM History, Formatter
+  WHERE History.formatterid = Formatter.id
+  AND   History.name = ?
+  AND   History.id = ?
+EOT
+    $sth->execute($page, $id);
+    
+    my $output = '';
+    my $handler = XML::SAX::Writer->new(Output => \$output);
+    my ($ip, $modified);
+    while ( my $row = $sth->fetch ) {
+        ($ip, $modified) = ($row->[2], scalar(gmtime($row->[3])));
+       # create the parser
+       my $parser = $row->[1]->new(Handler => $handler);
+       eval {
+           $parser->parse_string($row->[0]);
+       };
+       if ($@) {
+           $output = '<pod>
+  <para>
+    Error parsing the page: ' . xml_escape($@) . '
+  </para>
+</pod>
+  ';
+       }
+       last;
+    }
+    if (!$output) {
+       $output = <<'EOT';
+<pod>
+  <para>
+Unable to find that history page, or unable to find formatter module
+  </para>
+</pod>
+EOT
+    }
+    $output =~ s/^<\?xml\s.*?\?>\s*//s;
+    $output = "<?ip-address " . xml_escape($ip) . "?>\n" .
+              "<?modified " . xml_escape($modified) . "?>\n" .
+              $output;
+    return $output;
+}
+
+sub restore_page {
+    my ($dbpath, $dbname, $page, $ip, $id) = @_;
+    
+    my $db = _mkdb($dbpath, $dbname);
+    my $sth = $db->prepare('SELECT * FROM History WHERE name = ? and id = ?');
+    $sth->execute($page, $id);
+    my $row = $sth->fetch;
+    die "No such row" unless $row;
+    $sth->finish;
+    my ($texttype, $contents) = ($row->[2], $row->[3]);
+    _save_page($db, $page, $contents, $texttype, $ip);
+}
+
 sub create_db {
     my ($db) = @_;
     
@@ -160,7 +261,18 @@
                           name NOT NULL,
                           formatterid NOT NULL,
                           content,
-                          last_modified
+                          last_modified,
+                           ip_address
+                          )
+    });
+    $db->do(q{
+        create table History (
+                              id INTEGER PRIMARY KEY, 
+                              name NOT NULL, 
+                              formatterid NOT NULL,
+                              content,
+                              modified,
+                              ip_address
                           )
     });
     $db->do(q{

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to