Computer Programming

Yes, a bit specific, but I need to store some links!

Generating Word Files with WordML (xml) as a Template

Background

I've been spending the past day messing around with WordML to get it to generate Word files from PHP. The goal was to create printable tables. As George Bush said as the Iraq War slid toward failure: "Mission Accomplished."

My strategy was to start with a Word file, save it as xml, aka WordML, and then using the file as an xml template for the table layout. This is analogous to designing your output forms in NVU or Dreamweaver, and then turning it into a template.

Note that I saved the document out as an .xml file, not an .htm file. The .htm file could also be used as a template, but it doesn't retain all the Word features. There's also an alternate format, .htx, that is a MIME encoded .htm with enclosures, but the entire thing is MIME encoded, and decoding (and re-encoding) is a pain. XML is clean and relatively simple, and allows you to embed graphics.

htdigest and htaccess Editing Classes

This is a first draft of some classes to manipulate .htaccess and htdigest files. I'm putting it up because there aren't many classes to do this, and seemingly none for htdigest files. (I just learned about the PEAR::Config classes. Neat.)

HTAuthFile is a superclass with a common writer. HTAccessFile is a very incomplete way to manipulate Apache .htaccess files. It is basically incorrect, because it doesn't support those <Scoping things> in Apache config files, and it doesn't take into account the order of directives, and it doesn't deal with using the same directive mulitiple times, but it will do for writing simple files to enable directory passwords. It's at times like this, I wish Apache used XML for the config format... but, this is the only time I've really had that feeling.

htdigest Password Changing Function in PHP

This is a function to change a password within an htdigest password database file. htdigest is one method of user authentication in Apache HTTP Server.

Password Quality Evaluator

This is an up-and-coming feature on some sites. It measures the quality of a password, and gives the user immediate feedback about how good it is. The JavaScript below does that.

Ugly XML Parser Code to Generate INSERT Statements

Code this ugly should be illegal. It does what it says, though.

I'd add some sample data, but this is just too far in my past. The original data was just an xml file with tags going two levels deep.


<?php

if (! ($xmlparser = xml_parser_create()) )
{
     die ("Cannot create parser");
}

xml_set_element_handler($xmlparser, "start_tag", "end_tag");
xml_set_character_data_handler($xmlparser, "tag_contents");
$filename = "Rates.xml";
$current = "";
$values = array();
$fieldsToInclude = array( 'PUT', 'TAGS', 'TO', 'PUT', 'IN', 'DB', 'HERE' );
$endingTag = 'TTS';  // this is the tag that triggers the creation of the query

if (!($fp = fopen($filename, "r"))) { die("cannot open ".$filename); }

while ($data = fread($fp, 4096)){

Automatically Naming a Page From the File Name

This bit of code will set the page's title based on the filename of the script. So, my_homepage.php will be titled "My Homepage". Laziness prevails, sort of.

<?php

Screen Scraping a Cel Phone Photo

This is a simple screen-scraper to pull an image from a cel-phone page. I guess when people send you a file, you can use this to download it, sorta. It appears to be for a canadian internet company.

There were a few roadbumps to success. First was finding the precise location where the photo's URL was. To find it, I went to the page, did a "view source", and then back to the page and "view image". Then, i did a search for parts of the URL within the source. It turned out the URL was in a bit of JavaScript (not in an IMG tag).

1-Pixel GIF File, in Base64, for Web Bugs

This little bit of code returns a 1-pixel gif file. You can add it to the bottom of any script that you want to call from an IMG tag.


<?php
header('Content-type: image/gif');

Learning VB: (s)locate files on a disk with VBA

This is a class that will help you find files on the disk, without hitting the disk too much. It's a simplified unix "slocate" library. The first time you use it, it creates an index of all the files on the drive. (Subsequent uses update the file when a day passes.) The index is loaded into memory, and searched with regular expressions.

On an AMD Sempron 2000, the first search on 200,000+ files takes around 11 minutes. The second search takes around 2 seconds, and subsequent searches during a single run take around 1 second each. The first run builds the database, and the subsequent searches use it.

Adjacency Table to Hierarchy

Here's a way to turn an adjacency table into a hierarchy in html.


<?php
$menu = array();
$menu[] = array( 'id' => 1, 'parent_id' => 0, 'name' => 'a' );
$menu[] = array( 'id' => 2, 'parent_id' => 1, 'name' => 'a.1' );
$menu[] = array( 'id' => 3, 'parent_id' => 0, 'name' => 'b' );
$menu[] = array( 'id' => 4, 'parent_id' => 3, 'name' => 'b.1' );
$menu[] = array( 'id' => 5, 'parent_id' => 3, 'name' => 'b.2' );
$menu[] = array( 'id' => 6, 'parent_id' => 5, 'name' => 'b.2.1' );

// acts like a SELECT statement
function selectWhereParentIdIs( $id )
{
global $menu;
$out = array();

for( $i=0; $i

Hierarchical Menu

I've been coding on experts exchange, testing myself. Here's some relevant code for one of the answers.

They're classes that have been used to generate hierarchical menus.

Learning VB: no new code

I thought there'd be a whole system to deal with the bad files, by now, but my attention shifted to understanding ESRI datasets. Spot checks on the copied data indicated that there were a fair number of datasets used, and they weren't copied over. The topic seems to be involved, and there's a lot to read.

Datasets are basically databases. They're referenced indirectly, via a driver of some kind, and the driver, rather than the app, manages the data files. I suspect there's going to be a serious problem with these, because I was unable to navigate to a missing dataset via the UI. I'd click on the "Set Datasource" button, and a dialog would pop up, but clicking around the interface never seemed to bring up any data.

Chances are, data sources need to be mounted at the workstation before I can navigate to them.

Learning VB: running the code

This blog entry doesn't discuss any code, because I ended up doing very little work on the software. I was busy running it, and the app tends to take over the UI, making programing difficult. For the most part, it functioned as expected, but a couple things changed in how I actually used it.

I added a text box to restrict scanning for MXD files to a subdirectory. This new feature allowed me to set the source root and destination root directories, and then type the subdirectory to be processed. The text box could also be left blank.

I used this feature a lot.

The main feature I stopped using was the scheduler. The software was so slow that it failed to use the network 100%. The point of having the program pause at night was to let backups happen faster, to avoid competing for the network.... but since the job appeared to be CPU-bound, I had was a big disincentive to be a good network citizen. The most efficient thing to do was let the app run all the time, even if it impacted the network -- every time the app was "nice" to the network, the length of time to complete it's task would grow by that amount plus the additional CPU time it took to complete the task.

Learning VB: crash woes, threading template

It was looking pretty grim for the file copier. The main problems were twofold:

First, the app sometimes crashed on bad data. The Windows crash-reporting dialog box came up, and all work stopped until it was dismissed.

Second, after dismissing the dialog box, the app looped furiously, failing to process the remaining files, but marked them as completed (that's a little design bug there). The problem was that the COM server was returning an exception, and it was being trapped and effectively ignored. I say "effectively" because a special call to kill the app, via WMI, didn't clear the problem -- the underlying COM server didn't get stopped and unloaded.

The error could be cleared if I paused the batch job, and restarted it. That caused the thread that communicated with the COM server to abort, thus (probably) releasing the COM server. If only this could be automated.

Learning VB: rubber meets road

I'm finally running that program I've been writing. For the most part, it was "bug free" inasmuch as the different parts ran their test cases correctly, and it runs fine on a small subset of data. Of course, it's not like I ever really learned the system completely, so there are a lot of situations that I'm not handling (or even aware of). Moreover, because the users probably don't use all the features of the software, it's not likely that a complete solution is necessary. No project is debugged "enough" until it operates on real data, in a real situation. Fortunately, my first iterations were done in Perl and VBA, in a scripting environment, on real data, so my experiences debugging those determined the overall structure of the code -- that is, the code deals with messy situations.

.

Donate Bitcoin to support the writer: 1F13NCPcFKjJ7oy36zc2vBTtqoQCBPpAPH

Comments are disabled

If you wish to comment, post this article on reddit or hacker news.

Syndicate content