#! /usr/bin/perl -w # #=================== Initialize TWiki Environment =============== # BEGIN { # Set default current working directory (needed for mod_perl) if( $ENV{"SCRIPT_FILENAME"} && $ENV{"SCRIPT_FILENAME"} =~ /^(.+)\/[^\/]+$/ ) { chdir $1; } # Set library paths in @INC, at compile time unshift @INC, '.'; require 'setlib.cfg'; } # #=================== Initialize TWiki Environment =============== # use TWiki; use TWiki::Store; use Sajax; use CGI; my $q = new CGI; my( $topic, $webName, $scriptUrlPath, $userName ) = TWiki::initialize( $q->path_info(), $q->remote_user(),"", $q->url, $q); # ========== Perl based Sajax ========================= # Perl version cloned from the original PHP by http://www.modernmethod.com/sajax/ # I've left commented-out examples in the code for running with static methods. # For moving to ModPerl it's important to no be redefining subs all the time, # so this code adds the rs_register(funcname,coderef) call, which takes a coderef # rather than the name of a sub to be called. my $rv = ""; $rv .= "content-type: text/html\n\n"; =pod #for static method calls sub Sajax::getTopics { ... gets matching topics as string. Single partial string as input. } =cut #equivalent modperl methods # my @topics=("ThisOne", "Vinod", "Anderson", "StandardTopics", "Bala", "Cat"); my @topics = &TWiki::Store::getTopicNames("TWiki"); my $getTopics = sub { my($partial)=@_; my $matchedStr = ""; for $topic (@topics) { $matchedStr .= $topic . "$n " if($topic =~ m/^$partial/ ); } return $matchedStr; }; Sajax::rs_init(); #register static methods (called by name) #Sajax::rs_export("getTopics"); #modperl methods (called as anonymous coderefs) Sajax::rs_register("getTopics", $getTopics); my $handled_value = Sajax::rs_handle_client_request($q); if(defined $handled_value) { $rv .= $handled_value; } else { $rv .= "\n
\n