the crazed path of file conversions, pt III

First, declare globals. I’m not always certain which are needed and which could be dropped, but it’s just easier to declare them and be done with it, since they’re going to be used anyway, at some point. (Or perhaps this is a holdover from my days of ASP, in which everything had to be dimmed or Bad Things Happened.) Then pull the information from the two places that feed into this page: at the very start (the index page) and after inserting into the database. After the proc-page(s), the app refreshes back here, so that had to be allowed for, as well.

global $offset, $auth, $user;
if (($_GET['start'] == 'A') || ($_GET['continue'] == 'B')) {
$offset =  $_GET['offset'];
$auth =  $_GET['autho'];
$user =  $_GET['user'];

//used for debugging
if (!(isset($offset))) { $offset = 1; }
$auth2 = ".../wp-content/themes/theme/folder/" . $auth;

// native WP functions to get pretty version of author name
$author = get_userdata($user);

//for display at the top of the page & used to compare/remove duplicate values
$name = $author->user_nicename;

the joy of exploding things

The author-index text file is pulled out of its text-file state and into something the app can work with; what it’s basically doing is turning the flat text file into one really long string — and then turning it into an array, which is a collection of strings (objects). The explode function has got to be one of my favorites (and not just for the name). It’s a very handy thing; basically it takes any string and treats it like it’s an array made of a bunch of strings all delineated by some defined variable. In this case, it’s the paragraph tags.

   $str = file_get_contents($auth2);
$results = (explode("<p>",$str)); $p_count = 1;
if ($offset > $p_count) continue;
foreach ($results as $result) {
$p_count++;

//if $offset is bigger, redirect to start page.
if ($offset < $p_count) {
header( 'start');
}

// append p_count to $name to make unique story-name, aka $fileno
$name .= $p_count;

Now that the file has been turned into a string which was turned into an array and then broken into strings, it’s time to take each of those strings and treat it as an array consisting of… more strings. (For those of you not sure, a string is basically a single object, or variable; an array is a set of objects.) Again with the lovely explode() function, this time breaking up a single story-entry into its separate parts, each one demarcated by the ## symbols.

Then I apply preg_replace (no, I don’t know why it’s called that, and yes, I do keep thinking pregnancy-replace, but at least that makes it easier to recall the function, given how much I ended up using it). In a nutshell, preg_replace is being used to remove all empty spaces. The original text file had great reams of white space, mostly because that’s how I’d set up the template to make it easier for anyone else to duplicate without getting too confused. But to PHP, and SQL, there’s a definite difference between ‘ here we are’ and ‘here we are’ and ‘ here we are ‘ — so we have to remove those empty spaces before we can even start to see if they’re the same values.

After that, then it’s possible to sort with some kind of reliability; in this case, I chose to sort numerically, which forces values that start with characters (non-numeric) to sit at the top, and to put all numeric-starting values together. Then, finally, we can use the array_unique() function to ditch the duplicate values.

$onemore = (explode("##",$result));
$onemore = preg_replace('/\s\s+/', ' ', $onemore);
sort($onemore, SORT_NUMERIC);
$onemore = array_unique($onemore);

And now that we’re down to only those values that are distinct, it’s time to count the number of objects in the $onemore array. If $check is equal to 1, OR it’s less than 1, then the entire sub-string gets skipped and the app automatically reroutes to the next page, and ups the value of $offset at the same time. It should suffice to have $check equal to 1, but there are instances where an author’s index page has <p> followed by immediately by <p>, which means that $check is actually equal to 0, and not 1 — hence the need for an OR (the double pipes) statement.

(Yes, I’m also aware that I could just do “$check =< 1″ … when you see lengthier code like this, it means it got added while writing and I just can’t be bothered to go back and clean up after myself. Not for this application, at least. So, yeah. Sleeker ways to do things. Yep. There sure are.)

$check = count($onemore);
if(($check == 1) || ($check < 1)) {
$offset++; header( 'Location: main?continue=B&amp;autho='.$auth.'&amp;user='.$user.'&amp;offset='.$offset ) ;
}

echo '<strong>'.$author->display_name.'</strong>; file = '.$auth .', user = '.$user.', offset = ' .$offset.'<hr>';

Now we’re past the header, and can begin outputting. There’s no output prior to this, because the redirect (the header line, above) won’t work once there’s been anything printed on the screen. Regardless, the first line is basically output for the sake of tracking and reminding this particular sometimes absent-minded and shiny-chasing user what chord this song is in. Right after that is a slightly-mangled WP function to retrieve all stories by the author in question. WP has a function already that will do this, but it’s very limited; it defaults to the 5 most recent entries by the author, and goes blank if you try to get it to show you all — if by ‘all’ you mean ‘lots of posts, like more than twenty’. So that means going the long way around to get the results needed here.

The $gotten variable gets the $post_title appended, along with a comma+space. This becomes a basis for comparison, a few lines down. The foreach function coming up is not the main one for the page, though. This just clears things out to allow the option of skipping forward without tripping over the actual editing fields themselves. (Oh, I’m sure it’s possible to tie these all together, but breaking them up was a faster code, if considerably less elegant than I might admit if this were something for, y’know, true posterity.)

The trim() function gets rid of whitespace at the start and end of a variable. This is different from the previous clean-up I ran, which removed superfluous whitespace from the entire file, but while leaving single spaces. For instance, ‘## here   we     are     ##’ would have become ‘## here we are ##’, but that leaves a blank space at the start and end. Using trim() turns that second version into the cleaner ‘##here we are##’. If the value was originally ‘## ##’, this removes that empty single space, and now $more — which is the value of whatever is between the ## — is now empty, and there’s no reason to have it show up on-screen, so we skip it by detailing that $more can’t be equal to “”, and can’t be empty.

   // set up array of all converted stories by author
$getstory = $wpdb->get_results("SELECT post_title FROM $wpdb->posts WHERE post_author='$user'");
$gotten = "";
foreach ($getstory as $got) { $gotten .= (trim($got->post_title)).', ';}
foreach ($onemore as $more) {
$more = trim($more);
if (($more !== "") || (!empty($more))) {

Now that we have only values with something in them, it’s time to start comparing to known variables, and see if the app can’t make some reasonable predictions about what goes where. The first, of course, being whether this story-entry has been converted already — seeing if the value of $more matches up with any of the elements in $gotten — and then offering the possibility of skipping this entry altogether.

First, $offset is incremented so that when the page reloads, $fileno will have gone up by a step, $offset can be compared to $p_count, and the foreach loop will repeat. But in this particular loop, the “goto next” option becomes available at the very first match, upon which the loop will set $done, and break the loop, ending it. If there’s no match, the loop cycles back to the original foreach line — where $onemore is broken into its separate parts of each $more value — and it will try again to find a match. If it doesn’t find a match, it skips the entire section that shows the link, adds to $offset, and breaks the loop. In effect, no matches means the loop ends nicely and none of it shows onscreen.

if (@preg_match("/$more/", "$gotten")) {
$offset = $offset+1;
echo '</p><p><b>&rarr; '.$more . '</b> converted  &nbsp; <a href="main?continue=B&autho='.$auth.'&user='.$user.'&offset='.$offset.'">GOTO NEXT</a></p><p>';
$done=1;
break;
}
}
}

Last bit of the basic header is reference material for me, because I can’t always remember which integers are ‘real’ categories and which aren’t, and what to add if I know a category is missing. (Nearly every story will have at least one category between 3 and 7, and a second one that’s either 17, 18, or 22. If it doesn’t have both of those, I have to go looking at the original site and find the story to insert it. More about exceptions like that, later.)

// list all converted stories
echo $gotten .'<br><br>';?>

<form action="http://www.scimitarsmile.com/wp-content/themes/scimitar/03_proc.php" method="post">
<input type="hidden" name="convert" value="Y">
3=green; 4=purple; 5=orange; 6=blue; 7=red  &nbsp; || &nbsp; <B>17=G; 18=K; 22=M;</B> 19=K-L; 20=K-S; 21=K-V;  &nbsp; || &nbsp; 23=M-L; 24=M-S; 25=M-V</p><p>

<b>FILENO:</b>  &nbsp; <input type=text size=20 name="fileno2" value="<?php echo $name; ?>"></input></p<p>

the moving, let me show you it

Now I’m just going to dump the entire code for the real work of this page. It’s commented, so that should highlight enough of it. I’ll get into the details with the next section — it seems as though WP2.8 is still struggling, because it keeps clearing out the text box if I save. (One more reason on the list not to upgrade the archives until things have settled down. Not too sure about some of the conflicts I’ve seen since taking this site up to 2.8, or maybe it’s just that I have so many so-called spelling errors — or at least red-underlined bits — that WP has maxed. Hunh. Not sure.)

<?php

$a=1;
$c=1;
foreach ($onemore as $more) {
$more = preg_replace('/\s\s+/', ' ', $more);
$more = trim($more);

if (($more !== "") || (!empty($more))) {
if (@preg_match("/$more/i", "asidian, baka neko, chauni, cryogenia, devil's devotion")) continue;

$thisauth = substr($author->user_nicename, 0, 4);
$chkauth = substr((strtolower($more)), 0, 4);
if ($chkauth == $thisauth) continue;

// set up array to get all possible prequels
$beendone = $wpdb->get_results("SELECT * FROM $tempspot WHERE filenokey='title' AND filenovalue='$more'");
// create array to identify if category
$cats = (substr($more, 0,2));
if (@preg_match("/$cats/", "3,4,5,6,7,17, 18, 19, 20, 21, 22, 23, 24, 25")) { $use = 'term'.$a; $a++;}
// get prequel info
elseif (!empty($beendone)) { $use = 'prev'; }
// identify if date
elseif ((substr($more, -3)) == "/04") { $use = 'whenarc'; }
// identify if spoiler
elseif (!strncasecmp($more, 'ep ', 3)) { $use = 'SP'; $world=3; $w=$a;$a++;}
elseif (!strncasecmp($more, 'ch ', 3)) { $use = 'SP'; $world=2; $w=$a;$a++;}
// identify timeline custom value
elseif (($more=="pre-series") || ($more == "post-series")) { $use = 'timeline';}
// identify continuity
elseif ($more=="movie") { $use = 'SP'; $world=1; $w=$a; $a++;}
// determine length, if multipart
elseif (!strncasecmp($more, '01_', 3)) {
if (is_numeric(substr($more, -2))) {
$num = (substr($more, -2)); $use = 'file'.$num; if ($num < 11) $long1=26; if (($num > 10 ) && ($num < 25)) $long2=27; if ($num > 24) $long3=28;  $w=$a; $a++;
} else {
$use = 'file'; $long=16;  $w=$a; $a++;
}
}
// set up count for continuity; measure length of string
$w++;
$check2 = strlen($more);
if ($check2 > 50) {
?>
<input type=text size=15 name="field[]" value="excerpt" AUTOFILL=ON></input> &nbsp; &nbsp;
<textarea cols="100" rows="2" name="stuff[]"><?php echo $more; ?></textarea></p><p>
<?php
// if not excerpt-length, see if there are commas or semi-colons
} elseif (((@preg_match("/, /", $more)) || (@preg_match("/; /", $more))) && ($check2 < 50)) {
$pieces = explode(",", $more);
foreach ($pieces as $piece) {
$piece = trim($piece);
$use = 'term'.$a; $a++;
?>
<input type=text size=15 name="field[]" value="<?php echo $use; ?>" AUTOFILL=ON></input> &nbsp; &nbsp;
<input type=text size=100 name="stuff[]" value="<?php echo $piece; ?>"></input></p><p>
<?php
}
$piecet = explode(";", $more);
foreach ($piecet as $pieca) {
$pieca = trim($pieca);
$use = 'term'.$a; $a++;
?>
<input type=text size=15 name="field[]" value="<?php echo $use; ?>" AUTOFILL=ON></input> &nbsp; &nbsp;
<input type=text size=100 name="stuff[]" value="<?php echo $pieca; ?>"></input></p><p>
<?php
}

} elseif ($use=="")
// create drop-down list for any values not auto-id'd, above
{
?>
<select name="field[]">
<option value=""></option>
<option value="excerpt">excerpt   &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;    &nbsp; </option>
<option value="title">title</option>
<option value="timeline">timeline</option>
<option value="term<?php echo $a; $a++;?>">term</option>
<option value="prev">prev</option>
<option value="next">next</option>
<option value="DF">DF</option>
<option value="SP">SP</option>
<option value="other">other</option>
</select> &nbsp; &nbsp;	<input type=text size=100 name="stuff[]" value="<?php echo $more; ?>"></input></p><p>
<?php

} else
// otherwise, use the auto-id'd $use as the key/field name
{
?>
<input type=text size=15 name="field[]" value="<?php echo $use; ?>" AUTOFILL=ON></input> &nbsp; &nbsp;
<input type=text size=100 name="stuff[]" value="<?php echo $more; ?>"></input></p><p>
<?php
}
}
}
// stick in the created key/values for continuity
if ($world == 1) { $use = 'term'.$w; $val = "movie";}
elseif ($world == 2) { $use = 'term'.$w; $val = "manga";}
else { $use = 'term'.$w; $val = "tv-show"; }
?>
<input type=text size=15 name="field[]" value="<?php echo $use; ?>" AUTOFILL=ON></input>
&nbsp; &nbsp;
<input type=text size=100 name="stuff[]" value="<?php echo $val; ?>"></input></p><p>

<?php
// increment the off-loop count again, just in case
$w++;
// determine which variables got set during the loop, and determine story length
if (($long3) || ($long2) || ($long1)) {
$use = 'term'.$w;
if ($long3) { $val = "multipartlong";}
elseif ($long2) { $val = "multipartmed"; }
elseif ($long1) { $val = "multipartshort"; }
?>

<input type=text size=15 name="field[]" value="<?php echo $use; ?>" AUTOFILL=ON></input>
&nbsp; &nbsp;
<input type=text size=100 name="stuff[]" value="<?php echo $val; ?>"></input></p><p>
<?php
}

// if a oneshot, insert the category# as the field's value
if ($long) {
$use = 'term'.$w;
?>
<input type=text size=15 name="field[]" value="<?php echo $use; ?>" AUTOFILL=ON></input>
&nbsp; &nbsp;
<input type=text size=100 name="stuff[]" value="<?php echo $long; ?>"></input></p><p>

<?php		}
// add a few blank fields just in case anything got left off the meta
?>
<select name="field[]">
<option value=""></option>
<option value="excerpt">excerpt   &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;   &nbsp;    &nbsp; </option>
<option value="title">title</option>
<option value="timeline">timeline</option>
<option value="term<?php echo $a; $a++;?>">term</option>
<option value="prev">prev</option>
<option value="next">next</option>
<option value="DF">DF</option>
<option value="SP">SP</option>
<option value="series">series</option>
<option value="other">other</option>
</select>
&nbsp; &nbsp;
<input type=text size=100 name="stuff[]" value=""></input></p><p>

<input type=text size=15 name="field[]" value="" AUTOFILL=ON></input>
&nbsp; &nbsp;
<input type=text size=100 name="stuff[]" value=""></input></p><p>
<input type=text size=15 name="field[]" value="" AUTOFILL=ON></input>
&nbsp; &nbsp;
<input type=text size=100 name="stuff[]" value=""></input></p><p>
<input type=text size=15 name="field[]" value="" AUTOFILL=ON></input>
&nbsp; &nbsp;
<input type=text size=100 name="stuff[]" value=""></input></p><p>
<input type=text size=15 name="field[]" value="" AUTOFILL=ON></input>
&nbsp; &nbsp;
<input type=text size=100 name="stuff[]" value=""></input></p><p>

<?php

$fileno=$name;

// make allowances for the fileno not being the same as the $name
if ($fileno2) {
$fileno = $name.$fileno2;
} ?>

<input type="hidden" name="user" value="<?php echo $user; ?>">
<input type="hidden" name="offset" value="<?php echo $offset; ?>">
<input type="hidden" name="fileno" value="<?php echo $fileno; ?>">
<input type="hidden" name="auth" value="<?php echo $auth; ?>">
</p><p><br>
<input type="submit" name="submit" value="save <?php echo $name;?>" /> &nbsp; &nbsp;
<input type="reset" value="clear">
</form>
</p><p>
<?php
// if the loop just got shown, break it off here
if ($offset = $p_count) { break; }
}
// close up everything and call it a day
}
?>

Next up, what happens when you submit to the gods of PHP and WP.

Post a Comment

Your email is never shared. Required fields are marked *

*
*