<?php
/* vcab.php
*
* vcab v0.6
* Copyleft (c) 2022 Bailey Moore <
[email protected]>
*
gopher://sdf.org/1/users/bunz/code/vcab.zip
*
* Free for all. There's not much to steal.
*
*/
/* USER DEFINED
* $file :: Location of vocabulary file
* Format: word - [n./v./adj.] [(#)] definition
* Example: ignominy - n. (1) disgrace; dishonor; public contempt
*
* $save :: Name of file to format HTML
* Format: filename.html
*
* $done :: Name of repeated word file
* Format: filename.txt
*
* $date :: Date
* Format: DAY-OF-WEEK, DAY MONTH YEAR
* Example: Monday, 8 August 2022
*
* $website :: Website to link to
* Format:
http://www.website.com/ <--where the "$word" can be added easily at end
*/
$main = "/home/bailey/.scripts/vcab/";
$file = "/home/bailey/Documents/Lists/vocabulary.list";
$save = "/home/bailey/Documents/vocabulary.html";
//$main = "/home/bailey/b/@/vcab/";
//$file = "/home/bailey/b/documents/lists/vocabulary.list";
//$save = "/home/bailey/b/vocabulary.html";
$done = $main . "repeated.txt";
$date = date('l d F Y');
$website = "
https://thefreedictionary.com/";
//$website = "
https://www.dictionary.com/browse/";
/*
* BEGIN VCAB
*
*/
//require_once $main . "functions.vcab.php";
require_once "functions.vcab.php";
$version = "0.6";
$banner = "vcab v" . $version . " :: Vocabulary List System :: $date";
$help = "\nUsage: " . basename(__FILE__) . " [-n #] [-f filename] [-l ?] [-g | -i] [-e | -a] [-nsw] [-h]\n\nOptions:" .
"\n\t-n --number=# number of words/definitions you'd like found" .
"\n\t-f --filename=FILE use this location of vocabulary file" .
"\n\t-l --letter=? only retrieve words beginning with specified letter" .
"\n\t-r --no-repeat ignore previously used words as well as add new words to repeat list" .
"\n\t-g --get-empty get only words without definitions from vocabulary file and write to export file" .
"\n\t-G --get-definitions retrieve definitions for words from above command and add vocabulary file" .
"\n\t-i --ignore-empty ignore words without definitions" .
"\n\t-e --export export results as an HTML file (create or overwrite)" .
"\n\t-a --append append to current HTML file" .
"\n\t-A --add-word=WORD insert word (and optionally retrieved definition) into vocabulary file" .
"\n\t-s --sort sort results alphabetically" .
"\n\t-w --words-only only print words" .
"\n\t-o --override-config ignore stored configuration options and only use command-line input" .
"\n\t-c --configure configure vcab to load settings from file upon starting" .
"\n\t-h --help print this help and exit" .
"\n\nCopyleft (c) 2022 Bailey Moore [
[email protected] ]\n";
$head = "<html>\n\t<head>\n\t\t<title>Words of the Day</title>\n\t</head>\n\t<body>\n\t\t" .
"<center>\n\t\t\t<h1>Words of the Day</h1>\n\t\t</center>";
$tail = "\n\t\t<center>\n\t\t\t<footer>Generated by <a href='
gopher://gopher.club/1/users/bunz/code/'>" .
"<i>vcab v" . $version . "</i></a></footer>\n\t\t</center>\n\t</body>\n</html>";
$char_options = "n:f:l:rgGieaA:swch";
$word_options = ["number:", "filename:", "letter:", "no-repeat", "get-empty", "get-definitions", "ignore-empty", "export", "append", "add-word", "sort",
"words-only", "configure", "help"];
$count = 0;
$total = 0;
$space = "\n\n";
$break = " - ";
$letter = "";
$alphabet = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
$exported = array();
$repeated = array();
$upgraded = FALSE;
$specific = FALSE;
$norepeat = FALSE;
$getempty = FALSE;
$getentry = FALSE;
$ignore = FALSE;
$export = FALSE;
$append = FALSE;
$sort = FALSE;
$wordsonly = FALSE;
$overridec = FALSE;
if (!file_exists($file)) {
echo "\n[ ERROR ] Vocabulary file doesn't exist.\n";
return -1;
}
if (file_exists($save)) {
if (!search_file("v" . $version, $save)) {
$exp = explode(".", $save, 2);
$new = $exp[0] . "." . date('f-d-y') . ".html";
$upgraded = TRUE;
rename($save, $main . $new);
echo "\n[ ALERT ] You have upgraded to a new version of vcab. Your export file has been renamed $new. You must first create a new export file using
--export before using --append.\n";
}
}
echo $banner;
$list = file($file);
if (!($options = getopt($char_options, $word_options))) {
echo "\n# of words (1-10) ";
$count = trim(fgets(STDIN));
$space = "\n";
}
else {
if (isset($options['h']) || isset($options['help'])) {
echo $help;
return;
}
if (isset($options['n']) || isset($options['number'])) {
$count = isset($options['n']) ? trim($options['n']) : trim($options['number']);
}
if (isset($options['f']) || isset($options['filename'])) {
$file = isset($options['f']) ? trim($options['f']) : trim($options['filename']);
$list = file($file);
}
if (isset($options['l']) || isset($options['letter'])) {
$letter = isset($options['l']) ? trim(strtolower($options['l'])) : trim(strtolower($options['letter']));
if (!in_array($letter, $alphabet)) {
echo "\n[ ERROR ] You did not enter a valid letter. Only a-z/A-Z can be used.\n";
return -1;
}
$specific = TRUE;
}
if (isset($options['r']) || isset($options['no-repeat'])) {
$norepeat = TRUE;
}
if (isset($options['g']) || isset($options['get-empty'])) {
if (isset($options['G']) || isset($options['get-definitions'])) {
$getentry = TRUE;
}
foreach ($list as $line) {
if ($line == "\n" || strlen($line) == 2) {
continue;
}
if (strpos($line, ".")) {
continue;
}
$word = itemize($line, FALSE);
if ($getentry) {
$get = get_entry($word);
if (count($get) > 1) {
$x = 0;
foreach ($get as $got) {
$exported[] = $got;
echo "\t" . $got . "\n";
if ($x > 0) {
echo "\t[ ALTERNATE ] " . $got . "\n";
}
add_entry($got, FALSE);
$x++;
}
continue;
}
$word = $get[0];
add_entry($word, FALSE);
}
$exported[] = $word;
echo "\t" . $word . "\n";
}
if (count($exported) == 0) {
echo "\n[ ALERT ] You do not have any words without definitions in your vocabulary file.";
if ($count == 0) {
echo "\nNo other words requested. Exiting. . .\n";
return;
}
}
if (!file_exists($save)) {
echo "\n[ ERROR ] You must use --export once before using --get-empty.\n";
return -1;
}
$getempty = TRUE;
}
if (isset($options['G']) || isset($options['get-definitions'])) {
if ($getempty == FALSE) {
echo "\n[ ERROR ] You must use the --get-empty option with --get-definitions.\n";
return -1;
}
}
if (isset($options['i']) || isset($options['ignore-empty'])) {
if ($getempty == TRUE) {
echo "\n[ ERROR ] You cannot use this option with option -g/--get-empty.\n";
return -1;
}
$ignore = TRUE;
}
if (isset($options['e']) || isset($options['export'])) {
if (file_exists($save)) {
echo "\n[ ALERT ] You have chosen the --export option when you have already created an export file.\nWould you like to overwrite? [y/n/q]
";
$choice = trim(strtolower(fgets(STDIN)));
if ($choice == "y") {
$export = TRUE;
echo "\n[ ALERT ] Overwriting export file. . .";
}
elseif ($choice == "n") {
$append = TRUE;
echo "\n[ ALERT ] Appending to export file. . .";
}
elseif ($choice == "q") {
echo "\nExiting. . .\n";
return;
}
else {
echo "\n[ ERROR ] You did not enter a valid choice.\n";
return -1;
}
}
else {
$export = TRUE;
}
}
if (isset($options['a']) || isset($options['append'])) {
if ($export == TRUE) {
echo "\n[ ERROR ] You cannot use --export and --append at the same time.\n";
return -1;
}
if (!file_exists($save)) {
echo "\n[ ERROR ] You can only use --append after using --export once.\n";
return -1;
}
$append = TRUE;
if ($upgraded) {
$append = FALSE;
$export = TRUE;
}
}
if (isset($options['A']) || isset($options['add-word'])) {
$add = isset($options['A']) ? trim(strtolower($options['A'])) : trim(strtolower($options['add-word']));
echo "\nYou are adding the word $add to your vocabulary file. Would you like to retrieve and add the definition as well? [y/n/q] ";
$choice = trim(strtolower(fgets(STDIN)));
if ($choice == "y") {
if (add_entry($add, FALSE)) {
echo "\n[ ALERT ] Added word $add to vocabulary file.";
}
else {
echo "\n[ ERROR ] There was a problem adding word to vocabulary file.\n";
return -1;
}
}
elseif ($choice == "n") {
if (add_entry($add, TRUE)) {
echo "\n[ ALERT ] Added word $add and definition to vocabulary file.";
}
else {
echo "\n[ ERROR ] There was a problem adding word $add and definition to vocabulary file.\n";
return -1;
}
}
elseif ($choice == "q") {
echo "\nExiting. . .\n";
return;
}
else {
echo "\n[ ERROR ] You did not enter a valid choice.\n";
return -1;
}
}
if (isset($options['s']) || isset($options['sort'])) {
$sort = TRUE;
}
if (isset($options['w']) || isset($options['words-only'])) {
$wordsonly = TRUE;
}
if (isset($options['o']) || isset($options['override-config'])) {
$overridec = TRUE;
}
if (isset($options['c']) || isset($options['configure'])) {
vcab_configure();
}
}
$x = 0;
$y = 0;
$z = 0;
if ($specific) {
foreach ($list as $line) {
if (($first = substr($line, 0, 1)) == $letter) {
$total++;
}
}
if ($count > $total) {
if ($total == 0) {
echo "\n[ ERROR ] You do not have any words that start with the letter $letter in your vocabulary file.\n";
return -1;
}
echo "\n[ ALERT ] You only have have $total $letter words in your vocabulary file. Processing the remainder. . .";
}
}
if ((is_numeric($count) && $count >= 1 && $count < 11) || $getempty) {
echo $space;
if ($getempty == FALSE) {
shuffle($list);
while ($x < $count) {
$selection = $list[$y];
if ($selection == "\n" || strlen($selection) == 2) {
$y++;
continue;
}
$check = explode("-", $selection, 2);
if ($norepeat) {
if (file_exists($done)) {
if (search_file($check[0], $done, FALSE)) {
$y++;
continue;
}
}
}
if ($specific) {
if ($x == ($total - $z)) {
break;
}
if (($first = substr($selection, 0, 1)) != $letter) {
$y++;
continue;
}
}
if ($ignore) {
if ($check[1] == "\n" || strlen($check[1]) == 2) {
if ($specific) {
$z++;
}
$y++;
continue;
}
}
if ($norepeat) {
$repeated[] = trim($check[0]);
}
$x++;
$y++;
if ($wordsonly) {
$exported[] = trim($check[0]);
continue;
}
$exported[] = $selection;
}
if ($sort) {
sort($exported);
}
for ($x = 0; $x < count($exported); $x++) {
echo "\t" . $exported[$x];
if ($wordsonly) {
echo "\n";
}
}
echo "\n";
}
if ($export || $append || $getempty) {
$total = count($exported);
if ($append || $getempty) {
$file = fopen($save, "a") OR die("\n[ ERROR ] Unable to open file: $save\n");
$undo = file_get_contents($save);
$undo = str_replace($tail, "", $undo);
file_put_contents($save, $undo);
}
else {
$file = fopen($save, "w") OR die("\n[ ERROR ] Unable to create file: $save\n");
fwrite($file, $head);
}
fwrite($file, "\n\t\t<h2>" . $date . "</h2>\n\t\t<ul>");
for ($x = 0; $x < $total; $x++) {
if ($wordsonly || $getempty) {
$word = itemize($exported[$x], FALSE);
$line = "\n\t\t\t<li><strong><a href='" . $website . $word . "' target='_blank'>" . $word . "</a></strong></li>";
if ($x == ($total - 1)) {
$line .= "\n\t\t</ul>";
}
fwrite($file, $line);
}
else {
$list = itemize($exported[$x]);
list($word, $type, $definition) = $list;
switch ($type) {
case 'n':
$type = "noun";
break;
case 'adj':
$type = "adjective";
break;
case 'v':
$type = "verb";
break;
case 'trans v':
$type = "transitive verb";
break;
case 'intra v':
$type = "intransitive verb";
break;
case 'adv':
$type = "adverb";
break;
default:
$type = "noun/verb";
}
$line = "\n\t\t\t<li><strong><a href='" . $website . $word . "' target='_blank'>" . $word . "</a></strong> <small><i>" . $type .
"</i></small> <p style='font-family:Monospace'>" . $definition . "</p></li>";
if ($x == ($total - 1)) {
$line .= "\n\t\t</ul>";
}
fwrite($file, $line);
}
}
fwrite($file, $tail);
fclose($file);
echo "Contents written to $save\n";
}
if ($norepeat) {
$write = file_exists($done) ? fopen($done, "a+") : fopen($done, "w");
foreach ($repeated as $repeat) {
fwrite($write, $repeat . "\n");
}
fclose($write);
}
}
elseif (!is_numeric($count)) {
echo "\n[ ERROR ] You didn't enter a number!\n";
return -1;
}
elseif ($count > 10 || $count <= 0) {
echo "\n[ ERROR ] Enter a number between 1-10!\n";
return -1;
}
else {
echo $help;
return;
}