inicio mail me! sindicaci;ón

Project Management and Time Tracking

The world of Time Tracking and Project Management is a vast one. For small businesses like ours, there are seemingly endless time tracking solutions and a good number of project management answers.  I spent nearly two weeks evaluating options in my spare time, so hopefully this data will help save you some time.

Read the rest of this entry »

Get elements by CSS class name in javascript

Dustin Diaz wrote a nice, efficient version of getElementByClassName(), which searches HTML elements and retrieves all the items with a given CSS class specified.

Of course, if you use a lib like extjs, jQuery, et al, then you have no need of this. But if you’re trying to hack out a greasemonkey script or insert some minimalist code, here it is.

And here it is, for my own archives:

?View Code JAVASCRIPT
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

Here’s another alternative I found on Glazblog, using xpath to search the document:

?View Code JAVASCRIPT
document.getElementByClassName = function(needle) {
  var xpathResult = document.evaluate('//*[@class = needle]', document, null, 0, null);
  var outArray = new Array();
  while ((outArray[outArray.length] = xpathResult.iterateNext())) {
  }
  return outArray;
}

I don’t really know which is more efficient, though I suspect the xpath search could be taxing in very large documents. I’m positive, based on IE’s fake implementation of key/value pairs in the DOM, that they both suck in IE, even if you specify a specific tag type to search. Obviously, providing a node makes the regular expression search faster, by virtue of having less content to parse.

Sending Mail From Bash Scripts with an Attachment

Here is a great little tut on getting started in Bash. I’d recommend it to anyone trying to hack their way into a shell script.

Here is a quick script for sending an email:

#!/bin/bash
 
# Subject of email
SUBJECT="Test email with attachment from a bash script"
 
# Where to send it
TO_ADDRESS="your@email.com"
 
# Where the attachment is
ATTACHMENT_FILE="/tmp/attachment.txt"
 
# For fun, let's put something into the attachment
echo "This goes into the file."  >  $ATTACHMENT_FILE
echo "This appends to the file." >> $ATTACHMENT_FILE
 
# send the message
/bin/mail -s "$SUBJECT" "$TO_ADDRESS" < $ATTACHMENT_FILE

Manually remove a service from windows

Today I needed to manually remove a service from windows. I found this command line approach, which worked great for me.  Be sure to use the service name and not the display name:

sc delete ServiceName

You can find the service name by going to Control Panel -> Administrative Tools -> Services, right click the service and choose properties, the service name is shown there.

Find previous occurrence of string using PHP’s strrev() and preg_match()

Today I wrote a class to iterate words in a string. One challenge was finding my way backwards in a string. Specifically, given a starting position inside the string, I wanted to find the previous “word” and return it. However, since this needs to work localized (not just a-z), and the definition of a “word” is configurable, it was no simple matter of looking back for the previous space character.

So here is what I came up with; a method that finds the next or previous word given a starting position in the string:

   /**
    * Abstracted method for finding the next/prev word. This method assumes that 
    * $pos is greater than zero and less than the length of $text (check before calling)
    *
    * @param string $text the string of text to find next/prev word in
    * @param int $pos the position of first character in current word
    * @param string $wordPattern the regex definition of a word without any matching parens
    * @param string $reverse looks backward instead of forward (finds last word in string)
    * @return mixed false if no more words or array( "the word matched with junk", "the word only")
    */
   private static function nextWordMatch($text, $pos, $wordPattern, $reverse = false) {
      // we get the substring of text, starting at the current position
      if( $reverse ) {
         // in this case, we look at everything before $pos; we reverse it so that
         // we can run a simple regex on it rather than trying to deal with craziness
         // of looking backwards in string
         $text = substr($text, 0, $pos-1);
      }
      else {
         // in this case, we look at everything after $pos
         $text = substr($text, $pos);
      }
 
      // we escape the preg character just in case
      // we add in two sets of match parens, one for the word and one for the whole match
      // when looking backwards, we need to look from the end rather than the start
      $wordPattern = str_replace('@', '\\@', $wordPattern);
      $pattern = "(({$wordPattern})".self::NON_WORD_CHARS.")";
      if( $reverse ) { $pattern = "@{$pattern}\$@"; }
      else { $pattern = "@^{$pattern}@"; }
 
      // perform the match now and figure out what to do with it
      preg_match($pattern, $text, $matches);
      if( count($matches) < 3 ) { // remember that the first match is the raw text, so we add one
         // we didn't find any words, so return false
         return false;
      }
 
      // strip off the raw text, leaving our two matches
      return array_slice($matches, 1);
   }

Here is the default value for $wordPattern and the constant NON_WORD_CHARS used in the example:

   private $wordPattern = '\b[\w]+(?:[-\']\w+)*\b';
   const NON_WORD_CHARS = '\W*';

Very Cheap, Really Nice Graphics

It takes a bit of surfing, but you can find some really nice graphics on this site at a fraction of the usual cost: http://graphicriver.net/category/graphics/backgrounds/nature

Great Compilation of CSS Techniques

This compilation is awesome, covering things like fluid horizontal and vertical layouts, sprites, and many other excellent techniques for CSS: http://www.smashingmagazine.com/2009/07/20/50-new-css-techniques-for-your-next-web-design/

Mind Mapping Software: Mindomo

Mindomo is a great, free product for mind mapping. You can utilize it online for free to create some great looking and functional maps.

There is also a pro version at the great price of $6/month, which provides a desktop version, allowing you to store the files locally and organize with folders and sharing capabilities.

With the upcoming addition of collaboraiton, and there continual efforts to improve the product, it’s quickly becoming a great brainstorming and outlining tool.

Valid flash in XHTML (killing <embed>)

I wanted to validate my xhtml code for the new site design, and ran into the following fun errors from w3c:
Line 25, Column 15: there is no attribute "src".
Line 26, Column 18: there is no attribute "wmode".
Line 27, Column 17: there is no attribute "type".
Line 28, Column 18: there is no attribute "width".
Line 28, Column 31: there is no attribute "height".
Line 28, Column 38: element "embed" undefined.

Read the rest of this entry »

Dropping multiple tables in mysql (drop tables with wildcard)

I wanted to drop all mysql tables from a db that had a certain prefix, such as xx_.

Well this turned out to be an adventure…

The First Attempt

The first solution I found was to use sed/grep sort of logic:

#mysqlshow -u username -p dbname xx\\_% |sed 's/[|+-]//g'|sed 's/[ ]*$/,/'>droptables.sql

(xx\\_% is how you tell it to show tables starting with xx_)

This produced a semi-useful list. But I had to manually edit out the extra commas and line feeds to get a pure list, then add “DROP TABLE ” to the beginning of the list. Then feed this into mysql as follows:

#mysql -u username -p dbname < droptables.sql

I wasn’t real happy with that answer, naturally, so I researched more…

A Better Solution

I came across an alternative in this command:

mysqldump -u username -p --add-drop-table --no-data dbname | grep "^DROP.*\`xx_" | mysql -u username -p dbname

(replace xx_ with the prefix you want to remove, to do all tables, try just “grep ^DROP”

You can test it first by cutting off the last “| mysql…” bit and see the output, which is useful for debugging, before you go blowing away your tables.

Next entries »