php

Tar/Gzip Directory without Preserving Directory Heirarchy/Structure

I just wanted to post this code snippet up here in case anyone else is stuck Googling for many hours without success how to do this...

I have a PHP script (actually, a Drupal module I'm working on) set up to grab a folder, archive it using tar with the -z (gzip/compress) option, and post a link to the archive. However, since I was using PHP, I couldn't do what I normally do when tar-ing a folder, that is, I couldn't change directory (cd) first, then create the archive.

The problem this creates (for me) is that, upon expanding the PHP-created tar.gz archive, the system folder/directory heirarchy is expanded as well... so my files are buried in a folder 5-10 levels deep. Not ideal for zipping up a bunch of photos or something.

So, to overcome this problem, you can use tar's -C option, which allows you to change directory before tar-ing the files:

tar -C /directory/of/files-to-archive -zcvf /path/to/final-archive.tar.gz .

This simply creates an archive that expands to a folder with the archive's name, and has all the files contained directly inside!

My final command in the PHP script used the exec() function, which allows command-line commands from a PHP script.

VPS caching

I'm looking at installing APC (Alternative PHP Cache) on one of the VPS'es I support.

After doing some research, it sounds like it doesn't play nice with mod_suPHP, which I have installed as well.

Anyone have any experience with those 2?

The VPS - currently - only has 2 websites on it, one is a WordPress site, the other is Joomla.

Also, anyone using a CDN to offload content? Is it pricey

Thanks for any advice! 

Drupal: Restoring core comment title permalinks in a Zen Subtheme

I was scratching my head after trying to troubleshoot a template problem for a few hours today; I was building a Zen subtheme for a site that had, until now, been using Garland for it's styling. In this subtheme, I wanted to have comments display with a similar look to Garland, and I especially wanted automatic permalinks to comments, referenced by a comment number:

Comment template - zen removes linked comment number titles.

I looked through comment.tpl.php, and my template.php file, as well as zen's template.php file, but couldn't find anything related to the $id variable, which is used to build the numbered permalink. After opening up Devel themer, I found that there was a call to _zen_preprocess_comment(), and that was the likely culprit...

To restore these comment numbers/permalinks, you will need to grab core's TEMPLATE_preprocess_comment function and place it in your subtheme's template.php file. (This is an easy fix... if you'd like to get more advanced, you can hack the code to do other more Zen-like things, like print 'first' and 'last' classes for comments).

Be sure to change TEMPLATE to the name of your theme!

After you do that, your theme should have comment styling very similar to that of other core themes, with the permalink comment number intact (see below for the final product).

Pretty Zen comment titles - numbered permalinks!
(numbered permalinks ftw!)

Drupal: Switching Content Types the Easy Way

One thing that I've had to do every now and then is switch a node between one content type (for instance, 'story'), and another (a new content type 'blog post').

I used to go into the database via PHPMyAdmin, and change the nodes in the node table, but that was getting quite tedious, and would take forever for a list of a hundred or thousands of nodes!

Instead, I wrote up a nice little script that you can paste directly into the 'Execute PHP' block provided by the excellent Devel module:

<?php
return;
// Paste the following into the 'Execute PHP' Devel page to update nodes to a certain content type.

// Put a comma-separated list of all the nids you'd like to change in this array

$nodes_to_switch = array(7402, 7409, 7410, 7411, 7412, 7413, 7414, 7415, 7416, 7418, 7419, 7420, 7421, 7422, 7423, 7424, 7428, 7429, 7429, 7430, 7431, 7432, 7433, 7434, 7435, 7435, 7436, 7437, 7438, 7438, 7441, 7442, 37143, 37144);

// This function will update all the nodes in the node table to the proper type
// You will need to change the type = '<typename>' to whatever you need:
foreach ($nodes_to_switch as $nids) {
 
db_query("UPDATE {node} SET type = 'post' WHERE {node}.nid = %d", $nids);
  print
"$nids - You have done one more nid!\n";
}
?>

Just add a comma-separated list of node IDs you'd like to switch, and run the script. Another nice thing about this approach is that you can use Views to generate the list; just set your Views filters, then use 'Fields' as the display type, and then choose "Node: nid" for the only field to display. Copy out the list of nids, and paste them into the array.

Caching a Page; Saving a Server

A couple months ago, the Archdiocese of Saint Louis announced that a new Archbishop had been chosen (then-Archbishop-elect Robert J. Carlson). For the announcement, the Archdiocese streamed the press conference online, then posted pictures on the St. Louis Review website of the day's events (updated every hour or two).

Pageviews on April 21, 2009 - Archstl.org
Pageviews for April 21, 2009 on archstl.org – note that from 8-10 a.m., the server was practically down from the thousands of hits/requests it was getting. Just before 10 a.m., I enabled the caching described below. We announced everything via Twitter, SMS, Press Releases, and the web, just after 5 a.m.

During this period of time, the Archdiocesan website had over 2,000 visitors per hour, and almost all the visitors were hitting the home page. The website (run on Joomla 1.0.x) didn't have many caching mechanisms in place, and for almost a complete hour, the website was returning server errors as the processor was pegged at 100% utilization. Something had to be done! Continue Reading »

Syndicate content