LAMP How To – Open Source At Work

Only Passion Matters

Quick tips for setting up Eclipse and Monkey

 

 

  • Optional – add whatever plugins that you think is useful for your development work.

Monkey Script

  • To create a monkey script, add a new file under the “monkey” folder (need to create in eclipse). Name the file “author.js” for example (we are going to create a macro to insert developer’s comments). Copy and paste the following code in author.js
/*
 * Menu: Editors > Developer's Comment
 * Key: M3+M2+a
 * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
 */
var newDate = new Date();
var comment = "@author Bernard Peh "+newDate;

var commentLength = comment.length;

function main() {

	var editor = editors.activeEditor;

	// get range of lines in the selection (or at the cursor position)
	var range = editor.selectionRange;
	var startLine = editor.getLineAtOffset(range.startingOffset);
	var endLine = editor.getLineAtOffset(range.endingOffset);

	// determine if we're adding or removing comments
	var source = editor.source;
	var offset = editor.getOffsetAtLine(startLine);
	var addComment = (source.substring(offset, offset + commentLength) != comment);
	var adjust = 0;

	editor.beginCompoundChange();

	if (addComment) {
		for (var i = startLine; i <= endLine; i++) {
			var offset = range.startingOffset;
                        // var offset = editor.getOffsetAtLine(i);
			editor.applyEdit(offset, 0, comment);
		}
	} else {
		for (var i = startLine; i <= endLine; i++) {
			var offset = range.startingOffset;
                        // var offset = editor.getOffsetAtLine(i);

			if (source.substring(offset + adjust, offset + adjust + commentLength) == comment) {
				editor.applyEdit(offset, commentLength, "");
				adjust += commentLength;
			}
		}
	}

	editor.endCompoundChange();
}

Everything in author.js is javascript. Modify as you see fit. More examples can be seen in the plugins dir (org.eclipse.eclipsemonkey_x.x.x.xxxxxx). Now in any document, just press alt+shift+a to insert the author’s comment. Imagine what you can do with this technique!!

 // eg. to add phpdoc using the shortcut, modify the variables
 var newDate = new Date();
 var comment = "\t/**\n\t * .\n\t * @author Bernard Peh "+newDate+"\n\t * @param \n\t * @return \n\t */";

Note

  • If the monkey shortcut key doesn’t work, check for keys conflict. Try out new key combination.

Other Tips

  • CTRL + SHIFT + L : Shows you a list of your currently defined shortcut keys.
  • mouse over a function or class to see quick description.
  • CTRL + mouse over a function or class to see snippets of the class or function. While hovering the text, press F2 to see full code snippets.
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Slashdot
  • TwitThis
  • Yahoo! Buzz

Quick SVN Tutorial

Create New Repo

svnadmin create /home/data/svn/[dir-name]
chown apache:developer -R /home/data/svn/[dir-name]
chown 2770 /home/data/svn/[dir-name]

Import New Site

  • ssh into server
  • to import new site to the trunk,
svn import [your-site] file:///home/data/svn/[your-site-name] -m "[import comments]"

View Available Sites In Trunk

svn ls file:///home/svn/trunk/

Checking Out a Site

  • Create a directory to checkout and use ’svn co’
 cd ~
 mkdir [my-site]
 svn co file:///home/svn/trunk/[site-name] [my-site]

Updating a site

cd ~/[my-site]
svn update

View SVN Log

cd ~/[my-site]
svn log

Resolving Conflicts

svn status -u can predict conflicts Suppose you run svn update and you get the following result:

svn update
U	index.html
G	changed-b.html
C	rubbish-b.html
Updated to revision 46.
  • U – Updated: file contained no local changes, but was updated from changes from the repository
  • G – Merged: file had local changes, but the changes from the repository didn’t overlap with the local changes.
  • C – Conflict: changes from the server overlaps with your own and you need to manually choose between them.

For every conflicted file, Subversion places three additional files in your directory:

filename.ext.mine This is your file as it existed in your working copy before you updated your working copy. That is, without conflict markers. This file has your latest changes in it and nothing else. (If Subversion considers the file to be unmergable, then the .mine file isn’t created, since it would be identical to the working file).

filename.ext.r1 #old revision This is the file that was the BASE revision before you updated your working copy. That is, it is the file that you checked out before you made your latest edits.

filename.ext.r2 #new revision This is the file that your Subversion client just received from the server when you updated your working copy. This file corresponds to the HEAD revision of the repository.

The “<<<<<<<”, “=======” and “>>>>>>>>” signs are conflict markers. (You need to make sure you have removed these signs before you commit the file).

The following is an example of a conflicting file with Conflict Markers.

$ cat sandwich.txt
Top piece of bread
Mayonnaise
Lettuce
Tomato
Provolone
<<<<<<< .mine
Salami
Mortadella
Prosciutto
=======
Sauerkraut
Grilled Chicken
>>>>>>> .r2
Creole Mustard
Bottom piece of bread
>>>>>>>>
  • The text between “<<<<<<< .mine” and “=======” is the original text, indicated by the .mine
<<<<<<< .mine
Salami
Mortadella
Prosciutto
=======
  • The text between “=======” and “>>>>>>>>” is the new conflicting text, indicated by the .r2

You must run svn resolved when finished This removes the three temporary files and tells subversion the file is no longer in conflict.

Checking out an older Version of a file / directory

svn co -r 1204 file:///home/svn/trunk/[your-site-name]

to check out revision 1204 of that directory. Just change the revision number and the directory you want to check out and you should be good.

Undeleting a file / directory

Best way to do it is to cp an old version over the deleted version. In the directory of the deleted file, ie

svn cp -r 109 foo.bar foo.bar

Note

  • type ’svn help’ on command line to see options.
  • check files permissions if things aren’t working.
  • Using tortoiseSVN is an alternative to command line.
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Slashdot
  • TwitThis
  • Yahoo! Buzz

Implementing SSL Certificates in Apache

Creating a Private Key

To create a private key without triple des encryption, use the following command:

openssl genrsa -out ssl.key 1024

Creating a Certificate Signing Request

To obtain a certificate signed by a certificate authority, you will need to create a Certificate Signing Request (CSR). The purpose is to send the certificate authority enough information to create the certificate without sending the entire private key or compromising any sensitive information. The CSR also contains the information that will be included in the certificate, such as, domain name, locality information, etc.

Locate the private key that you would like to create a CSR from. Enter the following command:

openssl req -new -key filename.key -out filename.csr

You will be prompted for Locality information, common name (domain name), organizational information, etc. Check with the CA that you are applying to for information on required fields and invalid entries. Send the CSR to the CA per their instructions.

Wait for your new certificate and/or create a self-signed certificate. A self-signed certificate can be used until you receive your certificate from the certificate authority.

It is not necessary to create a self-signed certificate if you are obtaining a CA-signed certificate. However, creating a self-signed certificate is very simple. All you need is a private key and the name of the server (fully qualified domain name) that you want to secure. You will be prompted for information such as locality information, common name (domain name), organizational information, etc. The only required field for the certificate to function correctly is the common name (domain name) field. If this is not present or incorrect, you will receive a Certificate Name Check warning from your browser.

To create a self-signed certificate

openssl req -new -key filename.key -x509 -out filename.crt

Configuring your Apache Server

An example of a secure virtual host:

   <VirtualHost 123.456.789.42:443>
   DocumentRoot /etc/httpd/htdocs
   ServerName www.somewhere.com
   ServerAdmin someone@somewhere.com
   ErrorLog /etc/httpd/logs/error_log
   TransferLog /etc/httpd/logs/access_log
   SSLEngine on
   SSLCertificateFile /etc/httpd/conf/ssl.crt/server.crt
   SSLCertificateKeyFile /etc/httpd/conf/ssl.key/server.key
   SSLCACertificateFile /etc/httpd/conf/ssl.crt/ca-bundle.crt
   <Files ~ "\.(cgi|shtml)$">
         SSLOptions +StdEnvVars
   </Files>
   <Directory "/etc/httpd/cgi-bin">
         SSLOptions +StdEnvVars
   </Directory>
   SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
   CustomLog /etc/httpd/logs/ssl_request_log \
             "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
   </VirtualHost>

The directives that are the most important for SSL are the SSLEngine on, SSLCertificateFile, SSLCertificateKeyFile, and in many cases SSLCACertificateFile directives.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Slashdot
  • TwitThis
  • Yahoo! Buzz

Quick Mysql Tips

Logging slow queries

To enable the slow query log, start mysqld with the –log-slow-queries[=file_name] option. Use the mysqldumpslow command to summarize the queries that appear in the log. For example:

mysqldumpslow /path/to/your/mysql-slow-queries.log -t 10

shows you top 10 performance killers.

Lost Password

  • shutdown mysql, then
 mysqld –skip-grant-tables –u root
 mysql -u root
 mysql> use mysql;
 mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE User = 'root';
 mysql> FLUSH PRIVILEGES;
  • if get error on ip cannot access db
GRANT ALL ON *.* TO 'someuser'@'somehost';
  • if get error on password, check if the host is correct.

Add new User

GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost' IDENTIFIED BY 'password';

Remove foreign key constraint

Mass Update Substring

update table_name set column=replace(column, 'old_substring', 'new_substring') where title like '%/abc/123%'

* Sometimes it may be very difficult to use regex in mysql. If that is the case, use php or perl to do the query, extract the relevant variables, then do a manual update query. Abit more time consuming but this method is very reliable.

Exporting to cvs

SELECT a,b,a+b INTO OUTFILE '/tmp/result.text'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;

or doing it from command line

mysql -uexampleuser -pletmein exampledb -B -e "select * from \`person\`;" | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > filename.csv

Check Mysql Config

mysql -uroot -p
show variables;

sort mysql database/tables based on table size

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Slashdot
  • TwitThis
  • Yahoo! Buzz

synchronising system clock manually

if there is a need to synchronise the system clock manually, it can be a straight forward thing

service ntpd stop
ntpdate 0.rhel.pool.ntp.org
service ntpd start

some of the files of interest:

/etc/sysconfig/clock
/usr/share/zoneinfo
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Slashdot
  • TwitThis
  • Yahoo! Buzz