Speech:AddExp.pl

From Openitware
Revision as of 14:22, 8 February 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:
- Removed troubleshooting print statements
- Setting up the arguments for the script
- Refactored the logins to now run outside of the sub-routines by default
- Altered the way sub experiments are passed into createWikiSubExp()

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';

TODO:Insert help text here.

=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
$pass = <>;
system('/bin/stty', 'echo');   # Turn it back on
chomp $pass;

#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;	
}

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

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;
		$ifNoHolder = $number;
		
		#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;
			}
		}
			
		$number++;
		#add 1 to the greatest experiment number to get the next experiment 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);
		#print $response->content;
		
		#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 . "\nPlease go to Exps on Caesar and make a directory for this experiment\n";
	} else {
		die $response->status_line;	
	}
}

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

sub createWikiSubExp {

	if($response->is_success) {

		#get which experiment to make a sub-experiment in
		print "please enter the main experiment number (Ex: 0268)->";
		$experimentToEdit = <>;
		chomp $experimentToEdit;
		
		#get which experiment to make a sub-experiment in
		print "please enter the sub experiment number (Ex: 001)->";
		$subExperiment = <>;
		chomp $subExperiment;
	
		#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;	
	}
}