Saturday, December 8, 2012

MySQL timestamp versus datetime

Storing a date according to UTC and automatically converting to the localized time upon retrieval, the way the MySQL database field type does, is something that appeals to me.

But, it looks as though the MySQL datetime type will store a date in excess of the UNIX 2038 date limit.

Friday, November 16, 2012

PHP Objects to XML

What worked for me was to populate object properties with other objects that have one or more of their own properties. (Credit goes to the venerable James Thompson for pointing this out to me.)

<?php

//So first we need an object of objects.

class rootObject {
    public $root;

    public function populateRoot() {
        $root=new singleFirstTierObject();
        $root->populateSingleFirstTier();
        $this->root=$root;
    }
    public function toXML() {
        $serializer = new XMLSerializer();
        return  $serializer->objToXML($this);
    }
}

class singleFirstTierObject {
    public $singleFirstTier;
    public $singleOtherFirstTier;

    public function populateSingleFirstTier() {
        $singleFirstTier=new singleSecondTierObject();
        $singleFirstTier->populateSingleSecondTier();
        $this->singleFirstTier=$singleFirstTier;
       
        $singleOtherFirstTier=new multipleSecondTierObject();
        $singleOtherFirstTier->populateSingleSecondTier();
        $this->singleOtherFirstTier=$singleOtherFirstTier;
    }
}

class singleSecondTierObject {
    public $singleSecondTier;

    public function populateSingleSecondTier() {
        $this->singleSecondTier='singleSecondTier';
    }
}

class multipleSecondTierObject {
    public $singleSecondTier;
    public $singleOtherSecondTier=array();

    public function populateSingleSecondTier() {
        $this->singleSecondTier='singleSecondTier';
        $argumentArray=array();
        for($i=0;$i<5;$i++) {
            $arrayComponent=new multipleThirdTier();
            $arrayComponent->populateMultipleThirdTier();
            $argumentArray[]=$arrayComponent;
        }
        $this->singleOtherSecondTier=$argumentArray;
    }
}

class multipleThirdTier {
    public $multipleThirdTier;
       
    public function populateMultipleThirdTier() {
        $this->multipleThirdTier='multipleThirdTier';
    }
}

$object = new rootObject();
$object->populateRoot();

echo "<hr />\n<pre>\$object: <br />\n";
echo print_r($object);
echo "</pre><hr />\n";

//Now we have an object that will translate into XML via the XMLSerializer class below using the toXML function in the rootObject class.

//http://www.akchauhan.com/php-class-for-converting-xml-to-object-and-object-to-xml/
class XMLSerializer {
    private static $xml;

    // Constructor
    public function __construct() {
        $this->xml = new XmlWriter();
        $this->xml->openMemory();
        $this->xml->startDocument('1.0');
        $this->xml->setIndent(true);
    }

    // Method to convert Object into XML string
    public function objToXML($obj) {


        $this->getObject2XML($this->xml, $obj);

        $this->xml->endElement();

        return $this->xml->outputMemory(true);
    }

    // Method to convert XML string into Object
    public function xmlToObj($xmlString) {
        return simplexml_load_string($xmlString);
    }

    private function getObject2XML(XMLWriter $xml, $data) {
        foreach($data as $key => $value) {
            if(is_object($value)) {
                $xml->startElement($key);
                $this->getObject2XML($xml, $value);
                $xml->endElement();
                continue;
            }
            else if(is_array($value)) {
                $this->getArray2XML($xml, $key, $value);
            }

            if (is_string($value)) {
                $xml->writeElement($key, $value);
            }elseif(is_numeric($value)) {
                $xml->writeElement($key, $value);
            }
        }
    }

    private function getArray2XML(XMLWriter $xml, $keyParent, $data) {
        foreach($data as $key => $value) {
            if (is_string($value)) {
                $xml->writeElement($keyParent, $value);
                continue;
            }

            if (is_numeric($key)) {
                $xml->startElement($keyParent);
            }

            if(is_object($value)) {
                $this->getObject2XML($xml, $value);
            }
            else if(is_array($value)) {
                $this->getArray2XML($xml, $key, $value);
                continue;
            }

            if (is_numeric($key)) {
                $xml->endElement();
            }
        }
    }
}

$xmlOutput1=$object->toXML();

echo "<hr />\n<pre>\$xmlOutput1: <br />\n";
echo $xmlOutput1;
echo "</pre><hr />\n";

//What if we want to add attributes or edit the XML object though?

$xmlObject = new SimpleXMLElement($xmlOutput1);

echo "<hr />\n<pre>\$xmlObject: <br />\n";
echo "Round 1: <br />\n";
echo print_r($xmlObject);
echo "</pre><hr />\n";

//$xmlObject->singleOtherFirstTier->singleOtherSecondTier[$child_count]->multipleThirdTier

$child_count=0;
foreach($xmlObject->singleOtherFirstTier->singleOtherSecondTier as $element){
    $xmlObject->singleOtherFirstTier->singleOtherSecondTier[$child_count]->multipleThirdTier->addAttribute('child-element',$child_count);
    $child_count++;
}

echo "<hr />\n<pre>\$xmlObject: <br />\n";
echo "Round 2: with new attributes <br />\n";
echo print_r($xmlObject);
echo "</pre><hr />\n";

//Sorry, I know you looked. But, you won't be able to see they are there. And what is worse if you want to access them similarly now that they exist, the attribute name will need encapsulated in {curly braces}.

//And we convert our SimpleXML object back into an XML string.

$xmlOutput2=$xmlObject->asXML();

echo "<hr />\n<pre>\$xmlOutput2: <br />\n";
echo $xmlOutput2;
echo "</pre><hr />\n";

//Also, whitespace is a waste of bandwidth if you are talking to a machine.
//The following code will enable us to fine tune how we want whitespace handled by converting our data in an XML string into a DOM document object.

$domObject = new DOMDocument('1.0');
$domObject->preserveWhiteSpace = false; //self explanatory
   
$domObject->formatOutput = true; //false = no whitespace, true = human friendly tabbed indenting

//Now that we have a DOM document object with the settings we like let's load the XML string into it.
$domObject->loadXML($xmlOutput2);

//Last thing to do is output our finished DOM document object as XML document using the saveXML method.

echo "<hr />\n<pre>\$domObject->saveXML(): <br />\n";
echo $domObject->saveXML();
echo "</pre><hr />\n";

?>
<hr />
<pre>$object: <br />
rootObject Object
(
    [root] => singleFirstTierObject Object
        (
            [singleFirstTier] => singleSecondTierObject Object
                (
                    [singleSecondTier] => singleSecondTier
                )

            [singleOtherFirstTier] => multipleSecondTierObject Object
                (
                    [singleSecondTier] => singleSecondTier
                    [singleOtherSecondTier] => Array
                        (
                            [0] => multipleThirdTier Object
                                (
                                    [multipleThirdTier] => multipleThirdTier
                                )

                            [1] => multipleThirdTier Object
                                (
                                    [multipleThirdTier] => multipleThirdTier
                                )

                            [2] => multipleThirdTier Object
                                (
                                    [multipleThirdTier] => multipleThirdTier
                                )

                            [3] => multipleThirdTier Object
                                (
                                    [multipleThirdTier] => multipleThirdTier
                                )

                            [4] => multipleThirdTier Object
                                (
                                    [multipleThirdTier] => multipleThirdTier
                                )

                        )

                )

        )

)
1</pre><hr />
<hr />
<pre>$xmlOutput1: <br />
<?xml version="1.0"?>
<root>
 <singleFirstTier>
  <singleSecondTier>singleSecondTier</singleSecondTier>
 </singleFirstTier>
 <singleOtherFirstTier>
  <singleSecondTier>singleSecondTier</singleSecondTier>
  <singleOtherSecondTier>
   <multipleThirdTier>multipleThirdTier</multipleThirdTier>
  </singleOtherSecondTier>
  <singleOtherSecondTier>
   <multipleThirdTier>multipleThirdTier</multipleThirdTier>
  </singleOtherSecondTier>
  <singleOtherSecondTier>
   <multipleThirdTier>multipleThirdTier</multipleThirdTier>
  </singleOtherSecondTier>
  <singleOtherSecondTier>
   <multipleThirdTier>multipleThirdTier</multipleThirdTier>
  </singleOtherSecondTier>
  <singleOtherSecondTier>
   <multipleThirdTier>multipleThirdTier</multipleThirdTier>
  </singleOtherSecondTier>
 </singleOtherFirstTier>
</root>
</pre><hr />
<hr />
<pre>$xmlObject: <br />
Round 1: <br />
SimpleXMLElement Object
(
    [singleFirstTier] => SimpleXMLElement Object
        (
            [singleSecondTier] => singleSecondTier
        )

    [singleOtherFirstTier] => SimpleXMLElement Object
        (
            [singleSecondTier] => singleSecondTier
            [singleOtherSecondTier] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [multipleThirdTier] => multipleThirdTier
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [multipleThirdTier] => multipleThirdTier
                        )

                    [2] => SimpleXMLElement Object
                        (
                            [multipleThirdTier] => multipleThirdTier
                        )

                    [3] => SimpleXMLElement Object
                        (
                            [multipleThirdTier] => multipleThirdTier
                        )

                    [4] => SimpleXMLElement Object
                        (
                            [multipleThirdTier] => multipleThirdTier
                        )

                )

        )

)
1</pre><hr />
<hr />
<pre>$xmlObject: <br />
Round 2: with new attributes <br />
SimpleXMLElement Object
(
    [singleFirstTier] => SimpleXMLElement Object
        (
            [singleSecondTier] => singleSecondTier
        )

    [singleOtherFirstTier] => SimpleXMLElement Object
        (
            [singleSecondTier] => singleSecondTier
            [singleOtherSecondTier] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [multipleThirdTier] => multipleThirdTier
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [multipleThirdTier] => multipleThirdTier
                        )

                    [2] => SimpleXMLElement Object
                        (
                            [multipleThirdTier] => multipleThirdTier
                        )

                    [3] => SimpleXMLElement Object
                        (
                            [multipleThirdTier] => multipleThirdTier
                        )

                    [4] => SimpleXMLElement Object
                        (
                            [multipleThirdTier] => multipleThirdTier
                        )

                )

        )

)
1</pre><hr />
<hr />
<pre>$xmlOutput2: <br />
<?xml version="1.0"?>
<root>
 <singleFirstTier>
  <singleSecondTier>singleSecondTier</singleSecondTier>
 </singleFirstTier>
 <singleOtherFirstTier>
  <singleSecondTier>singleSecondTier</singleSecondTier>
  <singleOtherSecondTier>
   <multipleThirdTier child-element="0">multipleThirdTier</multipleThirdTier>
  </singleOtherSecondTier>
  <singleOtherSecondTier>
   <multipleThirdTier child-element="1">multipleThirdTier</multipleThirdTier>
  </singleOtherSecondTier>
  <singleOtherSecondTier>
   <multipleThirdTier child-element="2">multipleThirdTier</multipleThirdTier>
  </singleOtherSecondTier>
  <singleOtherSecondTier>
   <multipleThirdTier child-element="3">multipleThirdTier</multipleThirdTier>
  </singleOtherSecondTier>
  <singleOtherSecondTier>
   <multipleThirdTier child-element="4">multipleThirdTier</multipleThirdTier>
  </singleOtherSecondTier>
 </singleOtherFirstTier>
</root>
</pre><hr />
<hr />
<pre>$domObject->saveXML(): <br />
<?xml version="1.0"?>
<root>
  <singleFirstTier>
    <singleSecondTier>singleSecondTier</singleSecondTier>
  </singleFirstTier>
  <singleOtherFirstTier>
    <singleSecondTier>singleSecondTier</singleSecondTier>
    <singleOtherSecondTier>
      <multipleThirdTier child-element="0">multipleThirdTier</multipleThirdTier>
    </singleOtherSecondTier>
    <singleOtherSecondTier>
      <multipleThirdTier child-element="1">multipleThirdTier</multipleThirdTier>
    </singleOtherSecondTier>
    <singleOtherSecondTier>
      <multipleThirdTier child-element="2">multipleThirdTier</multipleThirdTier>
    </singleOtherSecondTier>
    <singleOtherSecondTier>
      <multipleThirdTier child-element="3">multipleThirdTier</multipleThirdTier>
    </singleOtherSecondTier>
    <singleOtherSecondTier>
      <multipleThirdTier child-element="4">multipleThirdTier</multipleThirdTier>
    </singleOtherSecondTier>
  </singleOtherFirstTier>
</root>
</pre><hr />

 

Are there more compact and elegant ways to do this? Sure. But, this gets the job done.

Thursday, October 11, 2012

Automatic Touchpad Disable While Typing in Ubuntu (Kubuntu)

If you are running Ubuntu or an Ubuntu variant (like me using Kubuntu) then you can:

sudo apt-get install gsynaptics


to get additional options for the configuration of your Synaptics or Synaptics compatible touchpad. --I've been grazing the touchpad while typing, but there is a feature that disables the touchpad for a short while if a keyboard key is touched. Huge reduction in stupid and annoying accidental typo corrections.

Wednesday, September 26, 2012

Recursive String Replacement

find /recursive/replace/directory/ -type f -exec sed -i 's/properlyescapedsearchstring/properlyescapedreplacementstring/g' {} +

by properly escaped, I mean using the backslash character(\) before special characters ( \. \/  \\ )
I should make myself a tool for this.

Installing the Papyrus Eclipse Plugin

The software source you will need is:
http://download.eclipse.org/modeling/mdt/papyrus/updates/releases/juno/main

Then you will need to install the modeling libraries from eclipse.org. I ended up just installing them all.

For whatever reason, I did not have a 'palette' tool box on my first model. It is there now, I created a new specific model (use case checkbox) with basic primitive types (checkbox) and they were magically there-- if you don't see any way to add diagram graphics via the 'palette'.

About Cloned Wordpress Servers and Logging In to the Wrong Server

So, I cloned an entire server as a development environment.

On that server was a wordpress installation.

The server was logging in to the wrong wordpress installation.

There is probably a 'correct' way to do this. But I found the wordpress options I needed to change in the wordpress database in the prefix_options table. (We have prefixes set to ON for multiple wordpress installations. Our tables are prefixed and referenced with prefix_ prepended in this post.)

SELECT * FROM prefix_options LIMIT 0,20;

From there, you should see why your new wordpress is logging you in to your old wordpress...

Hint:
mysql> select * from prefix_options limit 0,20;
+---------+-----------+-------------------------+--------+
|option_id|option_name|option_value             |autoload|
+---------+-----------+-------------------------+--------+
|        1|siteurl    |http://notdevelopment.com|yes     |


mysql> UPDATE prefix_options SET option_value='http://development.com' WHERE option_id=1;


Monday, September 24, 2012

Changing the default user and group for files in linux directories

Issue this command to have new files in the directory get set with the owner the same as the owner of the directory

chmod u+s

or issue this command to have new files in the directory get set with the group that is the same as the group of the directory

chmod g+s

also restricting deletion by any non-owner or privileged user is chmod +t to set the sticky bit

Capitalization is used to show whether the execute permission is set or not since these settings show up in the same spaces as the execute bit.

Here are a few examples that you might see if you ls -la:

rwSrwSrwT
(that means rwxrwxrwx with uid, gid, & sticky set)

rwxrwSrwt
(that means rwxrwxrw- with only the gid & sticky set)

Thursday, August 30, 2012

Kate Regex Search and Replace

If you are using the Kate text editor and you are going to use parenthesis to capture parts of your regex that you want to reuse (a submatch), the character you have to prepend is \, the backslash. But it isn't the dollar sign or any other character on Kate for Kubuntu Linux anyway.
So if you are wanting to switch wordsomething to somethingword in Kate, your regex will look like this (word)(something) and your replacement will look like this \2\1 to end up with somethingword when it is all done

wordsomething find (word)(something)

replace with \2\1 finishes with something word

Thursday, August 2, 2012

Best value web hosting

I have become a fan of VPS.net . I have been more than happily running a Debian instance of 2 nodes. I am extremely happy with the breadth of control I have over a portion of a server.

Granted, it is a lot more work than other hosts I have used because it is so much like having a raw machine-- but the response and flexibility of the platform is well worth the ~$20 per node and effort each month.

The successes and happiness with the outcomes of incremental efforts have made it so worth it.

We are using fusemail as our email service provider. They are sufficient at $2 per account each month. I would consider other providers.

Thursday, March 29, 2012

Get Column Names Text from MySQL

So, I was using describe table. But, then I found this which helps me reduce the amount of crap I have to pull out while creating a comma delimited list.

select column_name from information_schema.columns where table_name='tablename';

Thursday, March 15, 2012

Recovering a Text File in Linux

 Did I ever tell you how thankful I am that I do most of my writing in a more or less plain text editor?

grep -a -A800 -B800 'obustness' /dev/sda5 | strings > recovered_file



Everyone loves grep
-a says to process binary as text
-A[number] tells grep to include 800 lines after the matching term is found
-B[number] tells grep to include 800 lines before the matching term is found
then of course you will want to supply a term for grep to match against
and finally where you want grep to look /dev/sda5 happens to be my /home partition




Then pipe all that off to strings to be redirected into a file.
The reason you would do that is because it will clean up some of the gobbledygook the -a option of grep will not.

--I figured that Robustness would be a somewhat rare string to search for. And I could make it case insensitive by just leaving off the 'R' (yes, it can be done with switches).

Interesting thing to note, people besides me use the word Robustness. Not as rare as I was hoping.

Friday, January 27, 2012

Default Select Box "Prefilling" to emulate a Choice of the Existing Data

For editing information in my web applications, I prefill what existing data I have in my forms. For text type input tags, it is easy as pie. But, what to do for a select tag. Yeah, fun stuff. Let's get to it.

//$address is just an integer counter that starts at 0 and increments before a new address is pulled from the database.

//So, having said that, let's build the HTML select element where $address makes this chunk of code able to be specifically referenced later on by creating a unique id="state0", id="state1", id="state2", and so on for each address.


<?php

echo "<div>State:<select id=\"state" . $address . "\" name=\"UTstate\">\n
<optgroup label=\"U.S. States\">\n
<option value=\"IN\">Indiana</option>\n
<option value=\"MI\">Michigan</option>\n
<option value=\"NY\">New York</option>\n
<option value=\"OH\">Ohio</option>\n
<option value=\"PA\">Pennsylvania</option>\n
<option value=\"TN\">Tennessee</option>\n
<option value=\"VA\">Virginia</option>\n
<option value=\"WV\">West Virginia</option>\n
</optgroup>\n
</select>\n";

//There we have it. But now to make things easier for editing let's make the default value the same as what had been in the database using jQuery magic.

echo "<script>$('#state" . $address . " option[value=" . $row2['state'] . "]').attr('selected', 'selected');</script>";

?>

//This short chunk of jQuery JavaScript selects the corresponding state0, state1, or state2 and so on which should be the preceeding address state select block. It searches for the $row2['state'] database information within the option group tags. When it finds a match, it modifies the selected attribute of the tag to be set to selected.



Can we just show the HTML? Please?

Sure, hold on to your biscuits.




//This is what we start off with. All of the PHP code has done it's thing, this has become raw HTML code. Note that database read resulted in OH and we pumped OH into the jQuery JavaScript as you will soon see.

State:<select id="state0" name="UTstate">
<optgroup label="U.S. States">
<option value="IN">Indiana</option>
<option value="MI">Michigan</option>
<option value="NY">New York</option>
<option value="OH">Ohio</option>
<option value="TN">Tennessee</option>
<option value="VA">Virginia</option>
<option value="WV">West Virginia</option>
</optgroup>
</select>

//Then as I said we hit the short chunk of jQuery JavaScript. The OH was read from the database as mentioned earlier telling the browser which option tag identified by it's value to modify contained within the state0 select tag.

<script>$('#state0 option[value=OH]').attr('selected', 'selected');</script>

//The DOM gets modified and now the <otion value="OH">Ohio</option> tag has been modified as far as the user can see and the browser is keeping track of. It now looks and acts as so...

State:<select id="state0" name="UTstate">
<optgroup label="U.S. States">
<option value="IN">Indiana</option>
<option value="MI">Michigan</option>
<option value="NY">New York</option>
<option value="OH" selected="selected">Ohio</option>
<option value="TN">Tennessee</option>
<option value="VA">Virginia</option>
<option value="WV">West Virginia</option>
</optgroup>
</select>

Yaay!

This is for examples where only one selection is expected, for select boxes that allow multiple selections the code will be somewhat different but a similar method can be used.

Friday, January 6, 2012

WinSCP

WinSCP has a sync function in it that allows connection to a SFTP server that mirrors a local directory and/or a remote directory.

I have been using and recommending Filezilla which currently does not have that feature despite being great and free. I have many choices for how I wish to keep my files stored redundantly and Filezilla works with most of the Operating Systems I use, so I will continue to use it.

The thing that WinSCP does for me is that it allows me to not need Samba on my file servers.

Followers