Cambridge, MIT, and WordCamp Boston 2013

I had the pleasure of attending WordCamp Boston 2013 last weekend where I got my third WordCamp T-Shirt for 2013. The camp was held at the Microsoft New England Research & Development (NERD) Center. I also had the pleasure of checking out the MIT campus while I was in Cambridge which was a total treat. Here’s some pictures from the weekend.

A little PHP script to watch for 404 errors

This is a quick script which reads a standard RSS feed and then grabs the response header for each feed item and makes a log file of 404 errors. Useful in debugging sticky situations.

[code language=”php”]
<?php

$link_queue = array();
$exception_queue = array();

$timestamp = date( ‘Y-m-d H:i:s’ );
$rss = simplexml_load_file( $argv[1] );

foreach ( $rss-&gt;channel-&gt;item as $item )
array_push( $link_queue, $item-&gt;link );

foreach ( $link_queue as $url ) {
$handle = curl_init( $url );
curl_setopt( $handle, CURLOPT_RETURNTRANSFER, TRUE );
$response = curl_exec( $handle );
$response_code = curl_getinfo( $handle, CURLINFO_HTTP_CODE );

if ( $response_code !== 200 )
array_push( $exception_queue, &quot;{$url} returned status code {$response_code}&quot; );

curl_close( $handle );
}

if ( count( $exception_queue ) ) {
foreach ( $exception_queue as $e )
echo $timestamp . ‘ &gt; ‘ . $e . &quot;n&quot;;
} else {
echo $timestamp . &quot; &gt; All URLs returned status code 200n&quot;;
}

die();

// omit
[/code]

Throw it in cron like this:
*/30 * * * * php /path/to/script.php http://somesite.com/feed/ >> /path/to/log 2>&1

Or run it on the command line like this:
php /path/to/the/script.php http://somesite.com/feed/

Some Helpful Varnish Commands

Look at incoming requests for URL
varnishlog -c -m RxURL:"/somepage.html"

Look at requests made to the backend for URL
varnishlog -b -m TxURL:"/somepage.html"

Requests for one specific host
varnishlog -c -m RxHeader:"Host: somehost.com"

See cache age for a specific host
varnishlog -c -m RxHeader:"Host: somehost.com" | grep Age

Test VCL compilation
varnishd -C -f /path/to/file.vcl

Ban all URLs
varnishadm -S /path/to/secret -T :6082 "ban.url /"

View Ban List
varnishadm -S /path/to/secret -T :6082 "ban.list"

Real time URLs being sent to backend
varnishtop -b -i TxURL

List backend traffic
varnishlog -b -o

List URLs going to backend
varnishlog -b -o -i TxURL | grep TxURL | awk '{print $4}'

Assign Random Featured Images To All Posts

Could be used for setting up dummy content for a staging environment, or something similar. Be my guest and let me know how you decide to use it in the comments! Also, check my github for this function built into a WP-CLI command.

[code language=”php”]
$posts = new WP_Query( array(
‘posts_per_page’ => -1,
‘post_type’ => ‘post’,
));

$post_ids = array();

while ( $posts->have_posts() ) {
$posts->the_post();
array_push( $post_ids, get_the_ID() );
}

wp_reset_query();

$attachments = new WP_Query( array(
‘posts_per_page’ => -1,
‘post_type’ => ‘attachment’,
‘post_status’ => ‘inherit’,
));

$attachment_ids = array();

while ( $attachments->have_posts() ) {
$attachments->the_post();
array_push( $attachment_ids, get_the_ID() );
}

wp_reset_query();

foreach ( $post_ids as $post_id ) {
$attachment_id = rand( 0, count( $attachment_ids ) – 1 );

echo ‘post:’ . $post_id . ‘ attachment:’ . $attachment_ids[$attachment_id];

if ( update_post_meta( $post_id, ‘_thumbnail_id’, $attachment_ids[$attachment_id] ) )
echo ‘ SUCCESS’;
else
echo ‘ FAILED’;
}

die( ‘script complete’ );[/code]