snippet

Command-line batch cron processing for Drupal

In which our hero carries on the time-honored tradition of posting some random snippet to his blog because he might wish to reference it later.

I occasionally need Drupal to execute its cron handler more than once. Today, I was trying to regenerate api documentation for about 18,000 files, which, understandably, is broken up into several cron runs. If I wait for the natural course of things, the documentation will take about three days to regenerate. If I run cron as fast as I can, it usually finishes in less than an hour. So here's the snippet. Paste this bad boy in your favorite shell.


while true; do /usr/bin/wget -O - -q http://example.com/cron.php; done

When you're done, hit ctrl+C to end the loop.

Note that each wget call blocks until it's finished, so you don't get a bunch of wget processes spawned. Basically it ensures that the next cron job is started as soon as possible after the previous one finishes.

more jQuery typography

You should check out "Use the Best Available Ampersand" from SimpleBits. I love me some ampersands, and that post is a great resource for prettifying your site's ampersands with a little CSS typography.

Along the lines of last week's jQuery "widon't" post, you can offload some of the typography processing to the browser. It's really slick and simple if you have jQuery enabled.

To use some of his ampersandy style goodness, add this JavaScript snippet to your site somewhere:

jQuery('h1,h2,h3,h4,h5,h6').each(
    function(){
        this.innerHTML = this.innerHTML.replace(/&amp;/,'<span class="amp">&amp;<\/span>');
    }
);

Then simply add a few CSS font style rules to your site's stylesheets for span.amp, and you're set.

If you want to be really rad, you could combine this with widon't, like so:

jQuery('h1,h2,h3,h4,h5,h6').each(
    function(){
        this.innerHTML = this.innerHTML.replace(/&amp;/,'<span class="amp">&amp;<\/span>').replace(/\s([^\s>]{0,10})\s*$/,'&nbsp;$1');
    }
);

Now your website is 2x hotter, guaranteed :)

a jQuery "widon't" snippet

in the interest of more attractive internets, here's a quick little javascript "widon't" snippet—written in jQuery—that i've been using. now go do something cool with it.

jQuery('h1,h2,h3,li,p').each(
    function(){
        this.innerHTML = this.innerHTML.replace(/\s([^\s>]+)\s*$/,'&nbsp;$1');
    }
);

feel free to replace or augment the selector ('h1,h2...p') with any additional elements you'd like to see widon'ted.

i actually use this variant, which lets longer words be widows if they wanna:

jQuery('h1,h2,h3,li,p').each(
    function(){
        this.innerHTML = this.innerHTML.replace(/\s([^\s>]{0,10})\s*$/,'&nbsp;$1');
    }
);

note: dave cardwell posted a jQuery widon't a couple of years ago, but holy balls it's big... if you want something a bit more robust, be sure to check his out.

another note: i added newlines and tabs to make the code more readable. if i were you, i'd remove those bad boys before deploying.

wrand(); // a php weighted randomization function

dan from todaywasawesome just asked me to help him with a weighted randomization function in php. i thought my solution was cool, if a bit simplistic. like everything else i want to keep around, i'm posting it in my blag. enjoy!

/* this function expects either an array
 * ordered by weight, or with exact weights
 * as it's array indices.
 * it will return a randomly selected value.
 */
function wrand($data) {
	$totalw = $curw = 0;
	
	foreach ($data as $i => $val) {
		$totalw += $i;
	}
	
	$rand = rand(0,$totalw);
	
	foreach ($data as $i => $val) {
		if ($curw >= $rand) return $val;
		$curw += $i;
	}
	
	return array_pop($data);
}

uberquick comments in html/xhtml

Ajaxian just featured a rad comment hack, originally posted by Dirk Ginader, for those times when you need to comment and uncomment a section of code a bunch. By adding or removing just one character you can comment/uncomment an entire block of code. This saves a bunch of time, and works great for just about any language that uses C style commenting.

Here's how to do it with (X)HTML:

<!--
<p>lorem ipsum dolor</p>
<!-->

just add a >, to uncomment the whole section.

<!-->
<p>lorem ipsum dolor</p>
<!-->

This seems to work in any browser, and validates as both HTML 4 and XHTML strict.

snippet of the day: trim a string (the smart way) with php

i reworked the feed reading signature images i made a while back and added something to trim the strings to a decent length without breaking words. so here you go. a one-liner that'll trim a string to a decent length, and break it on whitespace:

if (strlen($blog_title) > 35) $blog_title =
array_shift(explode("|||", wordwrap($blog_title,
35, "|||"))) . "...";