NanoWiki

Once a friend came out with a wiki project. He wanted to code something on his own, and done so in C++. It took him well over a thousand lines to make something usable. Then I wanted to check, how many lines do I need to make a wiki in Perl. I spent one afternoon in September 2005 on it.

Description

NanoWiki is just what it is meant to be - a minimalistic wiki engine. It uses only quite basic modules as DBI and CGI. It's 20 lines (1371 bytes with meaningful variable names, 14 semicolons) in what I perceive as 1 command/line, although if you want it pretty it's best reformatted at about 35 lines (at least what I perceive pretty ;)). It was developed with using strict;, but I took it out while reformatting to 20 lines. Also I believe it doesn't contain holes that could harm the server running it. On the other hand, it allows anyone to put any code on it, so I expect to see some XSS or malicious things appearing after some time. If I ever feel the need, I will make it secure and comfortable for the user, and maybe I could fit that in 100 lines :)

Source code

use CGI;
use DBI;
my $node = CGI::param('node') || 'main';
my $action = CGI::param('action') || 'show';
my $dbh = DBI->connect("dbi:mysql:wiki","root","");
my $main = my $content = CGI::param('field') || $dbh->selectrow_array('SELECT content FROM pages WHERE title LIKE '.$dbh->quote($node));
if($action eq 'edit'){
    $main = CGI::submit({-label=>'Apply', -name=>'apply'}).CGI::textfield('node', $node).CGI::br().CGI::textarea(-name=>'field', -default=>$content, -rows=>20, -columns=>60);}
elsif(CGI::param('apply')){
    $dbh->do("REPLACE INTO pages (title, content) VALUES (".$dbh->quote($node).",".$dbh->quote(CGI::param('field')).")");}
elsif($action eq 'remove'){
    $main = CGI::submit({-label=>"Delete node $node", -name=>'dodel'}).CGI::hidden('node', $node);}
elsif(CGI::param('dodel')){
    $dbh->do("DELETE FROM pages WHERE title LIKE ".$dbh->quote($node));}
foreach(@{$dbh->selectall_arrayref("SELECT title FROM pages")})
{push our @menu, CGI::a({href=>CGI::url()."?action=$action&node=".$_->[0]}, @{$_});}
print CGI::header({-charset=>'ISO-8859-2'}).CGI::start_html({-lang=>'pl-PL', -title=>'Proof of concept wiki', -encoding=>'iso-8859-2'});
foreach (('show', 'edit', 'remove'))
{print CGI::a({href=>CGI::url()."?action=$_&node=$node"},"[$_]");}
print CGI::start_form().CGI::table({-width=>"100%"},CGI::Tr(CGI::td([$main, CGI::ul(\our @menu)]))).CGI::end_form().CGI::end_html();

You can also download.