Difference between revisions of "Speech:AddExp.pl"

From Openitware
Jump to: navigation, search
(changed changes)
(Code)
Line 25: Line 25:
 
(addExp.pl v1)
 
(addExp.pl v1)
 
Original Author: Morgan Gaythworpe
 
Original Author: Morgan Gaythworpe
Updated by: Kevin Soucey, Matthew Heyner
+
Updated by: Kevin Soucey & Matthew Heyner
 
Semester: Spring 2016
 
Semester: Spring 2016
 
Start Date: 2/24/16
 
Start Date: 2/24/16
 
Last Modified: 4/23/16
 
Last Modified: 4/23/16
 
Recent changes:
 
Recent changes:
- Removed troubleshooting print statements
+
- 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
 
- 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()
 
- 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  
 
This code ustilizes two scripts written by Morgan Gaythworpe  
Line 44: Line 48:
 
$help = <<'=cut';
 
$help = <<'=cut';
  
TODO:Insert help text here.
+
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
 
=cut
Line 87: Line 99:
 
#$domain is the current directory that is used.  If you need to change the directory then change $domain
 
#$domain is the current directory that is used.  If you need to change the directory then change $domain
 
$domain = 'AD';
 
$domain = 'AD';
print "please enter your username -> " . $domain . "/";
+
print "please enter your username -> " . $domain . "/" ;
 
$cred = <>;
 
$cred = <>;
 
chomp $cred;
 
chomp $cred;
Line 96: Line 108:
 
print "please enter your password->";
 
print "please enter your password->";
 
system('/bin/stty', '-echo');  # Disable echoing
 
system('/bin/stty', '-echo');  # Disable echoing
$pass = <>;
+
my $pass = <>;
 
system('/bin/stty', 'echo');  # Turn it back on
 
system('/bin/stty', 'echo');  # Turn it back on
 
chomp $pass;
 
chomp $pass;
 +
print "\n";
  
 
#initiate login
 
#initiate login
 
my $response = $ua->post('https://foss.unh.edu/projects/api.php?action=login&lgdomain='. $domain .'&lgname='. $user .'&lgpassword='. $pass .'&format=json');
 
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) {
+
if($response->is_success)  
 
+
{
#Get the token used to authenticate Login
+
   
$info =  $response->content;
+
    #Get the token used to authenticate Login
$token_start = (index $info, "token\":") + 8;
+
    $info =  $response->content;
$substring = substr $info, $token_start, (length $info);
+
    $token_start = (index $info, "token\":") + 8;
$token_end = (index $info, "\",\"");
+
    $substring = substr $info, $token_start, (length $info);
$token = substr $info, $token_start, $token_end;
+
    $token_end = (index $info, "\",\"");
#END
+
    $token = substr $info, $token_start, $token_end;
+
    #END
#GET session id used to create a cookie
+
   
$session_start = (index $info, "sessionid\":\"") + 12;
+
    #GET session id used to create a cookie
$substring = substr $info, $session_start, (length $info);
+
    $session_start = (index $info, "sessionid\":\"") + 12;
$session_end = (length $substring) - 3; #the session info ends three characters from the end
+
    $substring = substr $info, $session_start, (length $info);
$session = substr $info, $session_start, $session_end;
+
    $session_end = (length $substring) - 3; #the session info ends three characters from the end
#END  
+
    $session = substr $info, $session_start, $session_end;
+
    #END  
#Create the login Cookie
+
   
$cj->clear;
+
    #Create the login Cookie
$cj->set_cookie(0, "oitw_wiki_session", $session, "/", "foss.unh.edu", 443, "path_spec", 0, 180, "discard");
+
    $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';
+
    #create the https request and add the cookie to the header to authenticate
$request = HTTP::Request->new(POST => $url);
+
    $url = 'https://foss.unh.edu/projects/api.php?action=login&lgdomain='. $domain .'&lgname='. $user .'&lgpassword='. $pass .'&lgtoken='. $token .'&format=json';
$cj->add_cookie_header($request);
+
    $request = HTTP::Request->new(POST => $url);
$response = $ua->request($request);
+
    $cj->add_cookie_header($request);
#end
+
    $response = $ua->request($request);
#user is now logged in
+
    #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');
+
    #get the edit token (the edit token is needed to make changes)
$info = $response->content;
+
    $response = $ua->post('https://foss.unh.edu/projects/api.php?action=tokens&format=json');
$edit_start = (index $info, "\":\"") + 3;
+
    $info = $response->content;
$substring = substr $info, $edit_start, (length $info);
+
    $edit_start = (index $info, "\":\"") + 3;
$edit_end = (index $info, "\+");
+
    $substring = substr $info, $edit_start, (length $info);
$edit_token = substr $info, $edit_start, ( (length $substring) - 6);
+
    $edit_end = (index $info, "\+");
$edit_token = $edit_token."%2B%5C";
+
    $edit_token = substr $info, $edit_start, ( (length $substring) - 6);
+
    $edit_token = $edit_token."%2B%5C";
} else {
+
   
die $response->status_line;
+
}  
 +
else
 +
{
 +
    die $response->status_line;
 
}
 
}
  
 +
# Choose which experiment type to create
 
if($flag eq '-r') { createWikiRootExp(); }
 
if($flag eq '-r') { createWikiRootExp(); }
 
elsif($flag eq '-s') { createWikiSubExp(); }
 
elsif($flag eq '-s') { createWikiSubExp(); }
Line 158: Line 175:
 
=cut
 
=cut
  
sub createWikiRootExp {
+
# 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');
+
    $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) {
+
    if($response->is_success)
$info = $response->content;
+
    {
$info = "" . $info;
+
$info = $response->content;
$number = 0;
+
$info = "" . $info;
$ifNoHolder = $number;
+
$number = 0;
+
#gets all the experiment numbers
+
#gets all the experiment numbers
while($info =~ /(0\d\d\d)/g) {
+
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 the current experiment number is higher than the previous
if($1 > $number) {
+
    #make the current experiment number the highest
$number = $1;
+
    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;
+
 
}
 
}
 +
 +
#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;
 +
    }
 
}
 
}
  
Line 223: Line 247:
 
#Beggening of existing script from originally createWiki_Sub_Experiment.pl
 
#Beggening of existing script from originally createWiki_Sub_Experiment.pl
  
sub createWikiSubExp {
+
sub createWikiSubExp  
 
+
{
if($response->is_success) {
+
    #Get XML file
 
+
    $response = $ua->post('https://foss.unh.edu/projects/api.php?action=query&titles=Speech:Exps&prop=links&pllimit=400&format=xml');
#get which experiment to make a sub-experiment in
+
   
print "please enter the main experiment number (Ex: 0268)->";
+
    if($response->is_success)
$experimentToEdit = <>;
+
    {
chomp $experimentToEdit;
+
my ($number) = @_;
+
#get which experiment to make a sub-experiment in
+
print "please enter the sub experiment number (Ex: 001)->";
+
$subExperiment = <>;
+
chomp $subExperiment;
+
 
 
#get experiment name
+
$experimentToEdit;
print "what is your sub-experiment's name?->";
+
if($number)
$experiment = <>;
+
{
chomp $experiment;
+
    $experimentToEdit = $number;
$experiment =~ s/\s/%20/g;
+
}
 +
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;
 +
    }
 +
}
 
 
#make the edit to the experiment page
+
if($bool == 0)
$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);
+
    die("The root experiment does not exist.");
+
}
if (index($response->content, "error") != -1) {
+
#END make final check
print "ERROR: (" . $response->content . ")\n";
+
exit -1;
+
#get which experiment to make a sub-experiment in
}
+
print "Please enter the sub experiment number (Ex: 001)->";
 
+
$subExperiment = <>;
#get experiment info
+
chomp $subExperiment;
print "please enter the author's name->";
+
$author = <>;
+
#TODO:Make sure user does not overwrite already existing sub-exp
chomp $author;
+
$root_sub = "$experimentToEdit". " " ."$subExperiment";
$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->";
+
#Get XML file
$description = <>;
+
$response = $ua->post('https://foss.unh.edu/projects/api.php?action=query&titles=Speech:Exps_'.$experimentToEdit.'&prop=links&pllimit=400&format=xml');
$description =~ s/\s/%20/g;#replace whitespaces
+
($sec,$min,$hour,$mday, $mon, $year,$wday,$yday,$isdat) = localtime(); #get the date and time
+
if($response->is_success)
$year = $year + 1900;#convert from years since 1900 to current year
+
{
$mon++;#months listed 0-11 add 1 to make listed 1-12
+
    $info = $response->content;
$date = $mon.'-'.$mday.'-'.$year;#formate the date
+
    $info = "" . $info;
+
   
#create the corresponding experiment page
+
    while($info =~ /(\d{4}\s\d{3})/g)
$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($1 eq $root_sub)  
 
+
{
if (index($response->content, "error") != -1) {
+
    die("Sub-experiment already exists.");
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;
+
 
}
 
}
 +
 +
#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;
 +
    }
 
}
 
}
 
</pre>
 
</pre>

Revision as of 13:26, 22 March 2017

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