Monday, April 15, 2013

VIm and plugins

KNOW - basic VIM
basic command line unederstanding.

VIM is probably one of the most misunderstood and under appreciated text editors available.  Known as the learning curve from hell, vim though is incredibly popular because it's so powerful and so customizable -- and yes HARD TO LEARN.

Today, I want to talk about plugins.  For the uninitiated, plugins for Vim are scripts that customize vim and do some pretty amazing things.  If there is something you want for Vim chances are there is a plugin for it. 

Of course, getting plugins is easy -- once you install Pathogen. Pathogen IS the plugin manager for the modern era.  SO let's start installing it.

NOTE:
~/ is a shortcut to the home directory.  If your name is Fred.

/home/fred
$HOME 
~/ 
 
all mean the same thing.
 
mkdir -p ~/.vim/autoload 
 
mkdir -p  ~/.vim/bundle 
 
curl -Sso ~/.vim/autoload/pathogen.vim https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim
 
Easy.

First, we make a new directory.

Vim uses .vim, a hidden directory (ls -al in case you don't know how to list directories that are hidden) and store them all in autoload.  That's where pathogen is going to store pathogen.
bundle is where ALL plugins are going to go from now on.

NOW...that we have pathogen installed, add pathogen to the .vimrc file.

(in case you don't know it, .vimrc is the hidden file that you can use to customize the look and feel of vim.

 "---------------------------- Load pathogen ----------------------------"
execute pathogen#infect()
syntax on
filetype plugin indent on
 
DONE!!!!!!!!!!!!!
Now let's grab some plugins.  Now I might add that the documentation I've personally found tends to say -- just use it.  Let's be nicer.
 
the git website is where all vim plugins are stored. 
 
https://github.com/vim-scripts
 
Now let's install the tagin plugin.  

https://github.com/vim-scripts/taglist.vim

(Any website that lists popular plugins will always include a link like this so don't worry.

ONCE you get to that page, feel free to be lost.  I was :)

Now look for a link that says:

git: read only
To the right of it is an address you want to grab as you'll need it.
In this example, I'm using the taglist one.
IF it's on http (e.g. 

http://github.com/vim-scripts/taglist.vim.git

this is simply a matter of changing the http to git as in:

git://github.com/vim-scripts/taglist.vim.git <- this is what you want!!!

git clone git://github.com/vim-scripts/taglist.vim.git 
 
 
Now since this is a clone you HAVE to make sure you are in a particular directory.  Specifically the bundle directory

cd ~/.vim/bundle

 

you can add a directory path if you want but keep it consistent.  I like everything under bundle myself so...
git clone git://github.com/vim-scripts/taglist.vim.git  /fred/bob

will put it there.  WHY you would WANT to is beyond me but you can.


Incidentally, if you don't have git, grab it. 
 
apt-get install git
yum install git (etc.)

Essentially what you are doing is syncing the files on the server with your local directory.  Granted you could...copy them down, make the directories, etc. but this is far easier.

DONE!
 
Have fun!
 
 

Tuesday, April 9, 2013

useradd details

KNOW: Linux command line


Adding a user to Linux, or any Linux flavor, on the surface is easy enough.

useradd <username>

So the add the user Fred, you would do:

useradd fred

Then of course you need to add a password.

passwd fred

Done...well not really.  The problem is that a home directory, or any configuration scripts are absolutely absent.  We also don't have a group to work with.  In short, you have not much of anything but a username floating in the ether.

useradd -d /home/fred -m fred

Hmmm....that's different.

-d as this defines the home directory.  Standards are the username being the name of the home directory although there is no law supporting that option for sure.

-m will actually create the home directory.  Ya...go figure.

Skeleton files
/etc/skel  is a directory you should know about.  The /etc/skel is located at...OK you know right?  Anyway the point of that directory is to provide basic configuration files so that the user has something to work with.  This means of course you can create a set of files on your own and they will be copied to the user's home directory. 

IF you want, you can use -k and specify an alternate directory for the skeleton files.  It's up to you.


Just in case, please note that passwd <username> will add the password.

On some systems you can use :

adduser >username>

this is a much more automated and up-to-date idea so try it if you have support for it.





Thursday, March 28, 2013

Disable Enter Key from Submit on Forms

Know:
Forms, Javascript, patience.

NOTHING and I mean NOTHING has frustrated me more than this annoyance.

By default, ALL forms treat <ENTER> the same as clicking a button.  This means the moment you press enter the form is submitted.  What, but what if I have a form with multiple fields and every one needs to be entered BEFORE submission.
Sure, you could disable the enter key but wow, that's fun for the user.  Pressing tab and not enter gets old REAL FAST.  So...what do I do.

Step 1:
Disable the form.

This is pretty easy and requires NO coding.

<Form id='myform' name='myform action='dosomething.php' >

Nothing special but we need to stop submit.  We will add ONE line.

onsubmit='return false'

Now...

<form id='myform' name='myform' action='dosomething.php' onsubmit='return false'>

This essentially is taking advantage of Javascript.  If you want to hook up an event to onsubmit you can do that.  Returning false will keep you in the form.  This is great for validating text, etc.  For now though, we're just going to return false.  OH NO.  Now I will NEVER have the form submit right?

WRONG!!!!

This is the key.
Create a button that will submit the form.  We're going to add a PAINLESS function in Javascript to submit the form.  Really, it's quick.

First the function.  You can either add it to the top of your html, OR, add it to your javascript library.  For this example, we'll just add it to a script.

<script>
function SubmitForm ('IdOfForm')  // that's the ID of the form.  Handy for submitting
{
    var SelectedForm = document.getElementById (IDOfForm);
     SelectedForm.submit ();

}

</script>

OK so now you have the script.  BUT WAIT???? Where do I put it?  Well an easy solution is with a button.

<!-- this is all one line but is wrapping on this blogspot --!>
<input type='button' class='your class (optional) value='Save' onsubmit='SubmitForm(this.id); return false;"></input>

The return false is VERY important.  Take it out and you'll have the return key submitting your form.  Try it.  Works great for me and have fun.

Too many quotes

KNOW:
php, Javascript

Ever came across this problem?

echo ('<input id='fred' ...etc)

The problem is that we have an embedded quote but you say -- no problem right?

echo ("<input id='fred' ...etc)

So far so good.  Here we have the id of the tag set to 'fred'.  We use single quotes for fred and double quotes for input...but!


echo ("<input id='fred' onclick='DoSomething ('Bob');'...mess!

The problem is that you only have two normal layers of quotes.  The single can be embedded in the double but the double...we don't have a triple.  What we do have is the escape.

The escape lets us embed as many quotes as we want with the use of the \ character.

\" means to tell the processor that we want a double quote as it.  So when PHP processes the code we will see a double quote.  This won't work for single quotes by the way.  Don't do \' and expect good things to happen.

Now...

echo (\"<input id=\"fred\" onclick=\"DoSomething (\"Bob\")\" >\" );

I agree, this takes a bit of getting used to but the advantage is that you don't have to deal with running out of characters for quotes.  It works this way.

The first quote is always \"
The second quote is always \"
The third quote is always \"

You can do as many as you want without pulling hairs.  Messy -- ya...but it works.

Javascript and the this statement

What to know:
PHP, basic Javascript.


Often times in php, I'm generating lists of objects -- specifically html objects -- and I will include a Javascript link to process the information.  That, in itself, is not my favorite option but php is server and Javascript is client (Browser).  This relationship can be a headache but here are some tips to aid the pain.

First things first.  Javascript does provide a painless way to extract information from any object -- this.  The 'this' object gives you all the tools you need to gather anything you want about an object and pass it along to a procedure. 

Example
<input id='fred' value='bob' onclick='DoSomething (this) >this is nice</input>

This is your gateway to freedom.  The 'this'.  Don't believe me?

In your function

function DoSomething (Value)
{


}


You can extract:
The ID of the object, the value of the object and the text past the object

Now, let's see how

function DoSomething (Value)
{

   alert (Value.id);   // get the ID of the object or in this case 'fred'
   alert (Value.value) // get the value of the object or in this case 'bob'
   alert (Value.innerHTML) // get the text or in this case 'this is nice'
}

Now if you're worried about extracting information about an object, you need worry no more.

I'm no expert on Javascript and try to avoid it when I can but I can't deny its usefulness.



 

Monday, March 11, 2013

VIM Registers and Macros

TO KNOW: Basic VIM beyond :w of course.
TO KNOW: how to copy/paste/delete text.
TO WANT: Make your life much easier.


Registers in VIM may be a lousy term.  A better term might be buffers.  Vim stores a variety of buffers and uses them for a variety of tasks. Copy and paste AND Macros are just two examples.

Now, if you've never seen a vim register, type this:

1) Press [ESC] ...as many times as needed to ensure you are in command mode.
2) Type ':' {minus the quotes} and reg ( :reg) or (:registers).

If you did everything correctly you should see a messy list on the screen.  Pay CLOSE attention to the letters and/or numbers on the LEFT side of the screen.

"0  (blah blah)
"1 (blah blah)
"2 (blah blah)
...
"A (blah blah)

Each of these "0, "1, etc. represents a register -- a letter representing a buffer.  So what right? Well hold on OK!

In case you are wondering...

0 (yank register), 
1 to 9 (shifting delete registers), 
_ (like /dev/null, this is a black hole), 
" (default register, hence the Ctrl-R, "), 
- (small delete register), 
/ (the search pattern register), 
: (last command register), 
a to z for your own use (capitalized A to Z are for appending to corresponding registers).


Well they are important. Really, I get it, but these really are USEFUL!

COPY A BUFFER
First, let's get the contents into our document.

The command to get into the buffers are the double quote (") vs the command mode colon. (:)  Ya, it's strange but really...we're talking VIM here.

The sequence is...

(double quote) (name of buffer) (p for paste)

You may recall that p is pasting.

So, for example we want to access the k register -- assuming we see it in reg.

"ky  (" for the register access, k for k register, and p for paste)

If you did everything right, you will see the contents of the register in your document.

SO WHAT!!!!!!!!!!!!!!
Here is why.  This is ALL leading to Macros and it took me some time to gather these tidbits.  They are all out there...in pieces of documentation.

MACROS!!!!!!!!!!
Anyway:

Let's say you have a set of keystrokes you type OVER and OVER and OVER again.  BORING!
Macro it!!!

The command for macros (assuming command mode of course (press [ESC]) is

q (name of register or  buffer but everyone calls them registers)

So to record a macro in the b register type:

qb (NO COLON!!!!)
and you should see recording

EVERYTHING and I mean everything you type is now stuffed into the register.   Now as you type commands it will be recorded but STOP...I have to stop recording endless commands!

No problem

type
q to end the macro.  YA!

RUNNING A MACRO
:@ (letter of register)  (now you can use that colon)

So in our case...

:@b

But...it go away when me leave VIM so BAD RIGHT?

Yes and we need better English for me to understand your question so let's try again

But...it goes away when we leave VIM?

That's much better.

SAVING A MACRO FOREVER
Here's how.  Using our example of qb for the 'b' buffer and assuming we have the macro.  We need to get it into a document.

type

"bp

That command (not British Petroleum by the way) pastes the b register into the document. (Remember we did that earlier)

Now, load up .vimrc.  That's a hidden file that stores commands that vim can access.  Essentially, it's a huge configuration file.  If you don't have one -- shame on you -- make one and store it in your home directory on the linux workstation.

NOW...

Copy all those messy commands into your .vimrc...and it will work right?

ALMOST

Do this:

let @b = '  (your text in between the quotes -- single quotes mind you)'

Remember the text between quotes is the text from the register

(for this you would type "bp" to paste it in RIGHT BETWEEN the quotes.  This way you make NO typing mistakes

(save the file .vimrc but you know that right :)

NOW when you run vim...ya...it's remembered.  Isn't that AWESOME.

OH YA

to access the macro remember to type:

@b  (where b is the name of the register and @ is the command to run the macro.



Sunday, February 24, 2013

Ubuntu Apache2 Can't find fully qualified domain name

TO KNOW:
Linux, VIM or any text editor.

This is a QUICK and DIRTY fix to this problem.  It works and for a local (off the web server) you can call it a day.  Lots more to do if you want to make this a production webserver of course.

vim /etc/apache2/httpd.conf

By default this is a blank file.

Add this line exactly:

Servername Localhost

Restart

/etc/init.d/apache2 restart

Cheers!

Install Imagick in Ubuntu or CentOS

KNOW:
How to setup a LAMP Webserver in either CentOS or Ubuntu
How to program in PHP
Actually have a need for this library for viewing graphic images.

What is Imagick???
Imagick, a horrible name by the way, is a great alternative to the built in GD library supplied by PHP for viewing images.  It's much faster and more structured.
Sadly the documentation to install the software is PAINFUL and lacking.  I checked many a forums and blogposts for a variety of BAD information.  It's amazing.  
Anyway, let's figure out how to do it. 

Please note I am using repositories rather than download from source as I hate involving those headaches.  I'd rather have others instead.  These have been tested with the OS in question and I personally recommend that approach

CentOS: (for old-outdated version)
By far the most work.  If you want to use the CentOS repositories, I have set this up as a script for you. This should work just fine but I honestly don't recommend it.  Imagick is horribly out of date with many basic functions not even included.


!#/bin/bash
yum install ImageMagick.386 -y

# need to investigate.  Do we need the devel.
yum install ImageMagick-devel.386 -y

# you need this and pear for the next two steps.
yum install gcc gcc-c++ autoconf automake -y
yum install pear -y

# update the pear channel
pecl channel-update pecl.php.net

# use pear to install it
pecl install imagick -y

# make a new file called imagick.ini and have one line in it to alert php of this new feature
# It will not work installing in php.ini
echo 'extension=imagick.so' > /ect/php.d/imagick.ini
service httpd restart

# just to test it from the command line
php -m | grep imagick

BUT WAIT!!!!!!!!!!!!!!
This version of Imagick ???  You might as well have downloaded the Model-T. 

TRICKY!!!!
The easiest way is to simply use a repository.  REMI is an excellent repository for the most up-to-date RPM's for CentOS  Keep in mind that you now will deal with two repositories so their are some basic bookkeeping issues here. 

1) ALWAYS use -- enablerepo=remi --disablerepo=* when dealing with this repo for installing and removing packages.  
2) Be sure to think about integrated packages.  In this case we need both php AND Imagick so we will be getting both.  You can live with the old versions of Mysql and Apache as they won't conflict. 

Now, here's the messy ugly steps you need to do.

As of 2013 this is the valid link
#!/bin/bash

# get the data to support REMI Link is valid for 2013

wget http://rpms.famillecollet.com/enterprise/remi.repo
 

# tell YUM to enable the repo
yum --enablerepo=remi

# remove your current outdated version of PHP.  VERY IMPORTANT as we don't want library conflicts.
yum remove php

# Install php and ImageMagik and pear
# NOTE --disablerepo=* we disable EVERY repo for PHP and just use REMI otherwise
# messy dependency issues reveal its ugly head

yum install --disablerepo=*  php ImageMagick2 php-pecl-imagick --enablerepo=remi
 

# restart apache
service restart httpd

# php -m | grep imagick


Ubuntu 12.04 (somewhat easier right :)

!#/bin/bash

apt-get install imagemagick libmagickcore-dev -y
apt-get install php-5-imagick -y
You went through all that work and I'm not showing you how to test it.  That's not nice.  Here's a quick and dirty test file you can generate.  I called my Test.php since I was in a hurry.


<?php

       $FullPathName = '(put your test image here. I'd recommend using the full path ';

       // setup a new image and import from file.
        $Image = new Imagick ($FullPathName);
       
        // generate a header for display
         header('Content-type: image/jpeg');

        // If 0 is provided as a width or height parameter,
        // // aspect ratio is maintained
    
        $Image->thumbnailImage (100, 0);
        //
        echo $Image;
        //
        die ();

?>




Helpful ImagMagick commands


What to know:
Php programming.
That you are insane enough to tackle photos with php

REFERENCES
// at least it gives you the function lists
http://php.net/manual/en/book.imagick.php 

// nice function list with ACTUAL examples
http://eclecticdjs.com/mike/tutorials/php/imagemagick/imagick_1.php

// nice article on EXIF info (turn off your scriptblocker to see the actual script on the blog site)
http://blog.jmoz.co.uk/imagick-strip-exif-data

For those working with Imagick and its libraries you know how 'well' documented the information is.  Well let's help you out with some valuable functions.

EXIF information
There are several, barely documented features for EXIF data in JPG files.  
$File = '(Name of File)';
$Image = new Imagick($File); 
 
// GET EXIF data 
print_r($Image->getImageProperties ('exif:*')); 
 
// STRIP EXIF data 
$Image->stripImage(); 
 
// WRITE file WITHOUT EXIF data 
$DestFile = 'Fred.jpg';
$Image->writeImage ($DestFile); 
 
// read image data 
$Image->readImage($DestFile); 

// output the data
print_r($Image->getImageProperties('exif:*'));

Wednesday, February 20, 2013

Css Tag Styles - updated

WHAT you need to know:
Basic CSS:
HTML:

My first draft, was...terrible...so I have corrected it and made it much easier and  much more complete.  Here we go!

In CSS (cascading style sheets),  one of the first things you need to know is how to label a tag.  For people not that familiar with CSS, basically CSS allows you to move the configuration of HTML tags to another file and call that file ((something).css) for a variety of customizations. 

Now labeling a tag for configuration has some flexibility but also some confusion.  Let's try to move away from confusion to understanding.  For the purposes of this tutorial, I'll be using one line of code for configuration.  I'll change the background color to red (I dare you to do red in a document and like it :)

 1) Basic Tag selection (Tag Selector)
If you want the least painful and quickest way to tag something than look no further than call your selector the tag itself.  That's right, you can actually configure a tag in html by it's actual name.
Example.  Suppose I want the background color of a button to be red (ug).  Anyway:

IN HTML
<button>(not complete of course)

IN CSS
button
{

   background-color:red

}

Now this has one big advantage. Anytime I call the stylesheet, the button tag in html will be red.  The disadvantage is that this has zero customization.  EVERY button will have the background color of red.  If that's what you want then go for it, but CSS gives us some more flexibility than that.

1) Class Selector (a period)

This is pretty basic.  Using a period for the label, you can assign a label to a variety of tags.  This means that anytime I use this label, buttons, body, etc will all inherit this tag.  The key is that this is designed to be used over and OVER and OVER again.  Therefore use it when you need to use it a LOT for a variety of element.s

HTML
<body class='Fred'>
<button class='Fred'>
<input class='Fred'>

CSS
.Fred
{
    background-color:red;

}



Ya, you can use this class with any element your heart desires.  However, what if you want to filter it to only certain elements.  NO PROBLEM.  CSS gives us a nice feature and in my coding, most of the time I do it this way.

HTML
<body class='Fred'>
<button class='Fred'>
<input class='Fred'>

Fred.button
{
   background-color:red;
}

Yes in this case ONLY the button will see red color.  I filtered it so that only a button can have that feature (if you consider ugly red a feature).  Now don't get discouraged.  Using a comma we can actually do more than one element.  Using the same example.

HTML
<body class='Fred'>
<button class='Fred'>
<input class='Fred'>

Fred.button, Fred.input
{
   background-color:red;

}

Now both button and input get the red color. Body will be left alone.  Cool!


ID Selectors (a pound symbol)

IDENTICAL except that you use this only ONCE.  If you only need this label once use this; otherwise use the Class selector.  As it's called, the ID selector means you use the id label of an element.  Given that ID's must be unique to an element, this is something useful if you have need for it in only one element.

#Fred
{
  background-color:red;
}

<table id = 'Fred'>

That's it.  Now isn't CSS labeling flexible?  Their are a few misc. things you can do but stick with the basics.  Most of the time this will be just fine for your issues. 

Thursday, January 3, 2013

Finding Duplicates in MySQL

NEED TO KNOW:
MySQL

NOTES:
Can be slow but heck, it's a powerful command.

Finding duplicates in MySQL isn't that hard.  For the purposes of clarity, all SQL commands are in caps.  Nuff said. 

So anyway, you have a billion record table and you want to see the duplicate values.  Use the following command from MySQL

HAVING count( Field in question) > 1

count will automatically determine any records that are duplicated.  We use > 1 to distinguish those that are unique.  Conversely you can change it to =1 to find unique records. 

In our example. we'll be using TABLE365 for the example.  The field we'll be using is InputDate for finding duplicates. 

SELECT * 
FROM TABLE365
GROUP BY InputDate
HAVING count(InputDate) >1

MySQL is annoyingly picky about count.  Don't add a space (e.g. count (InputDate)) otherwise it will WHINE back at you over your harseness and cruelty. 

NOW the problem with this query is that it won't show the duplicates, only the first appearance - not good but a start.  We can embed a query to essentially have MySQL run a query ON a query.

SELECT * 
FROM TABLE365
WHERE InputDate IN (  -> The query we just used <- ) 
ORDER BY InputDate

As you can see, the second query is buried in parantheses.  Of course you can use this to do a query on a query.  It's a way to avoid generating a table just to perform a special query on it. 


FINALLY, the entire piece of code looks like this:

SELECT *
FROM TABLE365
WHERE InputDate
IN (


SELECT InputDate
FROM TABLE365
GROUP BY InputDate
HAVING count( InputDate ) >1
)
ORDER BY InputDate

Of course you can modify this for PHP such as:
$QueryString = 'Select * from TABLE365 where InputDate in ( Select InputDate FROM...
You got the idea.