Speech:AddExp.pl

From Openitware
Revision as of 13:26, 22 March 2017 by Cmr1048 (Talk | contribs)

Jump to: navigation, search

Summary

Title: addExp.pl

Author: Kevin Soucey, Matthew Heyner

Location:
/mnt/main/scripts/user/
/mnt/main/scripts/user/History/addExp/4/
/mnt/main/scripts/user/History/addExp/cur/

Usage:
addExp.pl -r To create a root experiment
addExp.pl -s To create a sub experiment

Description

addExp.pl combines the scripts createWikiExperiment.pl and createWiki_Sub_Experiment.pl into one script allowing for a more dynamic way to generate the documentation on the Wiki.

Code


#!/usr/bin/perl
=begin comment
Add Experiment
(addExp.pl v1)
Original Author: Morgan Gaythworpe
Updated by: Kevin Soucey & Matthew Heyner
Semester: Spring 2016
Start Date: 2/24/16
Last Modified: 4/23/16
Recent changes:
- Added troubleshooting print calls
- Altered port number to 443 from 80 to address the change of Foss to HTTPS 
- Setting up the arguments for the script
- Altered the way sub experiments are passed into createWikiSubExp()
- Made $pass variable a local variable for more security.
- Created Help documentation
- #TODO:Make sure user does not overwrite already existing sub-exp Line 238
- #TODO:Make sure user does not overwrite already existing sub-exp Line 248

This code ustilizes two scripts written by Morgan Gaythworpe 
createWiki_Experiment.pl and createWiki_Sub_Experiment.pl to 
allow for a simultanious creation of a main experiment directory 
as well as allowing for the option to create sub-experiment-directories.

=cut

$help = <<'=cut';

Usage: addExp.pl -r 
Example: addExp.pl -r

Information: addExp.pl combines the scripts createWikiExperiment.pl and 
createWiki_Sub_Experiment.pl into one script allowing for a more dynamic 
way to generate the documentation on the Wiki.

Flag -r: Used if you want to create a root experiment.
Flag -s: Used if you want to create a sub experiment under a root experiment.

=cut

require LWP::UserAgent;
require HTTP::Request;
require HTTP::Cookies;

#my ($isOneMade, $arg2Placeholder) = @ARGV;

$flag = @ARGV[0];

#create cookie object
my $cj = HTTP::Cookies->new;
$cj->clear;

#create request variable
my $request;

#create user agent object
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;

#give the user agent its cookie object
$ua->cookie_jar($cj);

#variable setup
my $info = '';
my $lines = '';
my $token_start ='';
my $token_end = '';
my $token = '';
my $substring =''; 
my $session_start = '';
my $session_end = '';
my $session ='';

$temp = <>;

#get the user's username and password
#$domain is the current directory that is used.  If you need to change the directory then change $domain
$domain = 'AD';
print "please enter your username -> " . $domain . "/" ;
$cred = <>;
chomp $cred;
$user = $cred;

print "Credentials: " . $domain . "/" . $user . "\n";

print "please enter your password->";
system('/bin/stty', '-echo');  # Disable echoing
my $pass = <>;
system('/bin/stty', 'echo');   # Turn it back on
chomp $pass;
print "\n";

#initiate login
my $response = $ua->post('https://foss.unh.edu/projects/api.php?action=login&lgdomain='. $domain .'&lgname='. $user .'&lgpassword='. $pass .'&format=json');

if($response->is_success) 
{
    
    #Get the token used to authenticate Login
    $info =  $response->content;
    $token_start = (index $info, "token\":") + 8;
    $substring = substr $info, $token_start, (length $info);
    $token_end = (index $info, "\",\"");
    $token = substr $info, $token_start, $token_end;
    #END
    
    #GET session id used to create a cookie
    $session_start = (index $info, "sessionid\":\"") + 12;
    $substring = substr $info, $session_start, (length $info);
    $session_end = (length $substring) - 3; #the session info ends three characters from the end
    $session = substr $info, $session_start, $session_end;
    #END 
    
    #Create the login Cookie
    $cj->clear;
    $cj->set_cookie(0, "oitw_wiki_session", $session, "/", "foss.unh.edu", 443, "path_spec", 0, 180, "discard");
    
    #create the https request and add the cookie to the header to authenticate
    $url = 'https://foss.unh.edu/projects/api.php?action=login&lgdomain='. $domain .'&lgname='. $user .'&lgpassword='. $pass .'&lgtoken='. $token .'&format=json';
    $request = HTTP::Request->new(POST => $url);
    $cj->add_cookie_header($request);
    $response = $ua->request($request);
    #end
    #user is now logged in
    
    #get the edit token (the edit token is needed to make changes)
    $response = $ua->post('https://foss.unh.edu/projects/api.php?action=tokens&format=json');
    $info = $response->content;
    $edit_start = (index $info, "\":\"") + 3;
    $substring = substr $info, $edit_start, (length $info);
    $edit_end = (index $info, "\+");
    $edit_token = substr $info, $edit_start, ( (length $substring) - 6);
    $edit_token = $edit_token."%2B%5C";
    
} 
else
{
    die $response->status_line;	
}

# Choose which experiment type to create
if($flag eq '-r') { createWikiRootExp(); }
elsif($flag eq '-s') { createWikiSubExp(); }

=begin comment
#Commented out to test createWiki_Sub_Exp()
if ($isOneMade = 'y' or $isOneMade= 'Y'){
	createWiki_Sub_Exp();
} else {
	createWiki_Exp();
	ifNoCreateWiki_Sub_Exp();
}
=cut

# Create the root experiment 
sub createWikiRootExp
{
	
    $response = $ua->post('https://foss.unh.edu/projects/api.php?action=query&titles=Speech:Exps&prop=links&pllimit=400&format=xml');
    
    if($response->is_success)
    {
	$info = $response->content;
	$info = "" . $info;
	$number = 0;
	
	#gets all the experiment numbers
	while($info =~ /(0\d\d\d)/g)
	{
	    #if the current experiment number is higher than the previous
	    #make the current experiment number the highest
	    if($1 > $number)
	    {
		$number = $1;
	    }
	}
	
	#add 1 to the greatest experiment number to get the next experiment number
	$number++;
	
	#get experiment name
	print "What is your experiment's name?->";
	$experiment = <>;
	chomp $experiment;
	$experiment =~ s/\s/%20/g;
	
	#make the edit to the experiment page
	$text = "\n*".$number." [[Speech:Exps_".$number." | ".$experiment."]]\n";
	$response = $ua->post('https://foss.unh.edu/projects/api.php?action=edit&format=json&title=Speech:Exps&appendtext='.$text.'&token='.$edit_token);
	
	#get experiment info
	print "Please enter the author's name->";
	$author = <>;
	chomp $author;
	$author =~ s/\s/%20/g; #replaces any whitespaces with html friendly %20 that preserve the whitespace since html will not
	print "Please enter a brief description of your experiment->";
	$description = <>;
	$description =~ s/\s/%20/g; #replace whitespaces
	($sec,$min,$hour,$mday, $mon, $year,$wday,$yday,$isdat) = localtime(); #get the date and time
	$year = $year + 1900; #convert from years since 1900 to current year
	$mon++; #months listed 0-11 add 1 to make listed 1-12
	$date = $mon.'-'.$mday.'-'.$year; #formate the date
	
	#create the corresponding experiment page
	$text = "===%20Description%20===\n'''Author:'''\n".$author."\n\n'''Date:'''\n".$date."\n\n'''Purpose:'''\n".$description."\n\n'''Details:'''\n\n'''Results:'''";
	$response = $ua->post('https://foss.unh.edu/projects/api.php?action=edit&format=json&title=Speech:Exps_'.$number.'&appendtext='.$text.'&token='.$edit_token);
	
	if (index($response->content, "error") != -1)
	{
	    print "ERROR: (" . $response->content . ")\n";
	    exit -1;
	}

	print "Your experiment number is: " . $number . "\n";
	createWikiSubExp($number);
    }
    else 
    {
	die $response->status_line;	
    }
}

# End of what was originally createWikiExperiment.pl
#-------------------------------------------------------------------------
#Beggening of existing script from originally createWiki_Sub_Experiment.pl

sub createWikiSubExp 
{
    #Get XML file
    $response = $ua->post('https://foss.unh.edu/projects/api.php?action=query&titles=Speech:Exps&prop=links&pllimit=400&format=xml');
    
    if($response->is_success)
    {
	my ($number) = @_;
	
	$experimentToEdit;
	if($number)
	{
	    $experimentToEdit = $number;
	}
	else
	{
	    #get which experiment to make a sub-experiment in
	    print "Please enter the main experiment number (Ex: 0268)->";
	    $experimentToEdit = <>;
	    chomp $experimentToEdit;
	}
	
	#START check if root experiment exists
	$info = $response->content;
	$info = "" . $info;
	my $bool = 0;
	while($info =~ /(0\d\d\d)/g)
	{
	    if($1 eq $experimentToEdit)
	    { 
		$bool = 1;
	    }
	}
		
	if($bool == 0)
	{
	    die("The root experiment does not exist.");
	}
	#END make final check 
	
	#get which experiment to make a sub-experiment in
	print "Please enter the sub experiment number (Ex: 001)->";
	$subExperiment = <>;
	chomp $subExperiment;
	
	#TODO:Make sure user does not overwrite already existing sub-exp
	$root_sub = "$experimentToEdit". " " ."$subExperiment";
	
	#Get XML file
	$response = $ua->post('https://foss.unh.edu/projects/api.php?action=query&titles=Speech:Exps_'.$experimentToEdit.'&prop=links&pllimit=400&format=xml');
	
	if($response->is_success)
	{
	    $info = $response->content;
	    $info = "" . $info;
	    
	    while($info =~ /(\d{4}\s\d{3})/g)
	    {
		if($1 eq $root_sub) 
		{
		    die("Sub-experiment already exists.");
		}
	    }
	}
	
	#get experiment name
	print "What is your sub-experiment's name?->";
	$experiment = <>;
	chomp $experiment;
	$experiment =~ s/\s/%20/g;
	
	#make the edit to the experiment page
	$text = "\n*".$subExperiment." [[Speech:Exps_".$experimentToEdit.' '.$subExperiment." | ".$experiment."]]\n";
	$response = $ua->post('https://foss.unh.edu/projects/api.php?action=edit&format=json&title=Speech:Exps_'.$experimentToEdit.'&appendtext='.$text.'&token='.$edit_token);
	
	if (index($response->content, "error") != -1)
	{
	    print "ERROR: (" . $response->content . ")\n";
	    exit -1;
	}
	
	#get experiment info
	print "Please enter the author's name->";
	$author = <>;
	chomp $author;
	$author =~ s/\s/%20/g; #replaces any whitespaces with html friendly %20 that preserve the whitespace since html will not
	print "Please enter a brief description of your sub-experiment->";
	$description = <>;
	$description =~ s/\s/%20/g; #replace whitespaces
	($sec,$min,$hour,$mday, $mon, $year,$wday,$yday,$isdat) = localtime(); #get the date and time
	$year = $year + 1900; #convert from years since 1900 to current year
	$mon++; #months listed 0-11 add 1 to make listed 1-12
	$date = $mon.'-'.$mday.'-'.$year; #formate the date
	
	#create the corresponding experiment page
	$text = "===%20Description%20===\n'''Author:'''\n".$author."\n\n'''Date:'''\n".$date."\n\n'''Purpose:'''\n".$description."\n\n'''Details:'''\n\n'''Results:'''";
	$response = $ua->post('https://foss.unh.edu/projects/api.php?action=edit&format=json&title=Speech:Exps_'.$experimentToEdit.' '.$subExperiment.'&appendtext='.$text.'&token='.$edit_token);
	
	if (index($response->content, "error") != -1)
	{
	    print "ERROR: (" . $response->content . ")\n";
	    exit -1;
	}
	
	print "\nYour sub-experiment number is: " . $subExperiment . "\nPlease go to Exps on Caesar and make a directory for this sub-experiment\n";
    } 
    else 
    {
	die $response->status_line;	
    }
}