Speech:AddExp.pl

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
The purpose of addExp.pl is to allow for a more dynamic way to generate experiment documentation on the Wiki by combining the createWikiExperiment.pl and createWiki_Sub_Experiment.pl scripts into one.

This script can only be run on Caesar!

Code
=begin comment Add Experiment (addExp.pl v1) Original Author: Morgan Gaythworpe Updated by: Jaden Henry, Arias Talari, Daniel Beitel Semester: Spring 2018 Start Date: 1/24/18 Last Modified: 3/03/18 Recent changes: - Fixed error thrown when initially running script that highlighted line 81 - Added username to Author line - Re-added auto-increment for sub-experiments
 * 1) !/usr/bin/perl

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.

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


 * 1) my ($isOneMade, $arg2Placeholder) = @ARGV;

if ($#ARGV != 0) {   print "Usage: addExp.pl -[r|s]\n"; exit -1; }

$flag = $ARGV[0];

my $cj = HTTP::Cookies->new; $cj->clear;
 * 1) create cookie object

my $request;
 * 1) create request variable

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

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

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

$domain = 'AD'; print "\nThis script is used to create experiment entries on the wiki. \n\nAVOID trailing spaces as well as the '&' symbol in your experiment\n name or the hyperlink WILL BREAK. \n\nPlease enter your username -> ". $domain. "/" ; $cred = ; chomp $cred; $user = $cred;
 * 1) get the user's username and password
 * 2) $domain is the current directory that is used. If you need to change the directory then change $domain

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

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

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; }
 * 1) Choose which experiment type to create

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

sub createWikiRootExp {
 * 1) Create the root experiment

$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;
 * 1)   $experiment = trim($experiment);


 * 1)   $experiment =~ s/\./_/g; #replace '&' with '+' because '&' was breaking links on wiki

#make the edit to the experiment page $text = "\n*".$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===\nAuthor:\n".$author."\n\nDate:\n".$date."\n\nPurpose:\n".$description."\n\nDetails:\n\nResults:"; $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; } }


 * 1) End of what was originally createWikiExperiment.pl
 * 2) Beggening of existing script from originally createWiki_Sub_Experiment.pl
 * 1) 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

#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; if ( !($info =~ /page pageid=/) ) {       print "Experiment ". $experimentToEdit. " does not exist\n"; exit -1; }

$number = 0; while( ($info =~ /(Exps $experimentToEdit \d\d\d)/g) && ($done ne true) )#gets all the experiment numbers until we have found the next valid sub-experiment number {       #print "\n\n\$1: ".$1."\n\n"; $number = substr($1, -3, 3); }

#if there are no sub-experiments, set the next experiment to 001 if($number < 1) {       $number = 1; }     #else increase the number of the current sub-experiment by one to get the next sub-experiment number else {       $number = $number + 1; }

while($info =~ /(\d{4}\s\d{3})/g) {		if($1 eq $root_sub) {		   die("Sub-experiment already exists."); }	   }	}

$subExperiment = substr("000" . $number, -3, 3); # prefix with 0's

#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." ".$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; #format the date

#create the corresponding experiment page $text = "===%20Description%20===\nAuthor:\n".$author. " " . "(UserID: ". $user .")"."\n\nDate:\n".$date."\n\nPurpose:\n".$description."\n\nDetails:\n\nResults:"; $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 the Exps directory on Caesar and make a folder for this sub-experiment\n"; }   else {	die $response->status_line; } }