Category: Web Development

Securely Connecting to the WikiMedia Subversion Server from Windows

By , January 11, 2012 9:11 am

The WikiMedia Foundation (WMF) provides public access to the MediaWiki code base via a Subversion code revisioning system. For the majority of users who just want to be on the bleeding edge of MediaWiki simply pulling from this public server will suffice, however this only allows users read access to the repository. If you want to be a hardcore MediaWiki hacker you need to request commit access which will allow you to connect over the secure svn+ssh protocol and push your changes back into the core. There are a few prerequisites for gaining commits access, one of which is setting up an RSA key, which will be covered momentarily. Before we begin however, it is highly recommended that you read the entire Commit Access Guide available on the MediaWiki.org site.

NOTE: Though targeting The WMF Subversion server these instructions will be similar for all Subversion connections.

Get the apps

Before any work can be done two applications need to be downloaded, Putty and Tortoise SVN. These two applications will work together to connect you to the Subversion server.

Putty can be downloaded at: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

Tortoise can be downloaded at: http://tortoisesvn.net/downloads.html

Run through their installation process and you will be ready to go!

Generating the Key

Access to the MediaWiki Subversion server requires you to first supply an SSH key to the WMF team to setup your account. Generating the key with the Putty Key Generator is quite simple with the following steps:

  • Run puttygen
  • Click on the Generate button and wiggle your mouse over the blank area.
  • Click Save public key and save it somewhere safe. For general convenience you should append “.pub” (Without quotes) to the end of the file name.
  • Click Save private key and save it somewhere safe. This private key you will want to keep as secure as possible, as with this key a malicious user could get into your Subversion account and wreak havoc.

Setup a session in Putty

Tortoise SVN uses the Putty sessions to secure the Subversion communication and manage your keys.

  • Run putty
  • Set the hostname of the server, input a one word session name, and click save

  • From the left menu select Connections > SSH > Auth. On this screen is a Browse button. Load the private key you create earlier here. The key should have a “.ppk” extension.
  • To make sure your key will always be available go back to the Session page with the left menu and click Save again.

Your Subversion key should now be setup and ready for use in Tortoise SVN

Checkout the MediaWiki files with Tortoise SVN

NOTE: In this section you will pull only one copy of the MediaWiki source to work with. This is just for demonstration purposes. See “Getting started and set up” for proper etiquette.

Tortoise SVN makes interacting with Subversion servers from Windows trivial.

  • Create a folder to store the MediaWiki files in
  • Right click the folder and select SVN Checkout
  • In the dialog box you will checkout with a URL starting with “svn+ssh://username@svn.wikimedia.org”. It is important to note here that “svn.wikimedia.org” DOES NOT refer to the URL of the Subversion server, but rather the session you saved in Putty previously. So if your session name was “awesomeness” you would use “svn+ssh://username@awesomeness”.  The rest of the URL will point to the code you desire to checkout, probably the URL svn+ssh://username@svn.wikimedia.org/svnroot/mediawiki/trunk/phase3 is what you want.
  • A new dialog should pop up showing you a list of files that are being downloaded. Go make yourself some tea, this will take a while.

After the checkout completes you will have the MediaWiki code. Get hacking!

Committing back to MediaWiki

Tortoise SVN makes committing code changes back to the MediaWiki repository easy. The following steps will get your code flying back along the wire.

  • Right click on the folder containing the MediaWiki checkout
  • If you have created new files they need to be added to the commit first
    • Hover over the TortoiseSVN menu item and a submenu will pop up
    • From this submenu click Add
    • And new files will be show in this dialog. Choose the files you wish to include in the commit and click Ok
    • Now the really hard part, right click on the MediaWiki checkout again and click SVN Commit
    • Fill in a message about your commit and look over the list of changes. When satisfied click OK.
    • A new dialog will pop up showing the files being committed and a status message after the commit finishes.

Congratulations! You have successfully committed new code to the MediaWiki Subversion repository and enriched the community. Your code will now undergo a review process and if solid will be released with the next version of MediaWiki.

My WordCamp Victoria 2012 Presentation: The Proof is in the Pudding – Yep it works with Microsoft

By , January 8, 2012 7:42 pm

Microsoft has been making a great effort over the years to embrace Open Source technologies and bring further support for Open Source projects to its platform. I was recruited last year by Microsoft to help further the interoperability cause and work heavily with PHP on Microsoft platforms. The platforms and tools of today have reached a point where running PHP and other Open Source projects in a Microsoft environment is a very viable option. This is especially great if you work at a location which primarily runs Microsoft products and does not have strong knowledge of setting up and maintaining Linux servers. During my presentation I will provide you with a "Microsoft Cookbook" of resources that will get you up and running with WordPress in a way that allows you to take advantage of everything from small shared hosting up to leveraging the amazing power of the cloud.

During this presentation you will learn about:

  • Openness at Microsoft
  • End-to-end WordPress development with WebMatrix
  • How to run WordPress in the Windows Azure cloud
  • Where to find free tools from Microsoft, such as Windows Server, SQL Server, etc
  • How to manage WordPress with Windows on the go

Click the title slide below to download the presentation in PDF format

I will be posting pictures from this WordCamp on my 2012 Victoria WordCamp gallery album

Introducing the Meetup API client for PHP

By , January 2, 2012 6:11 pm

I am working on a pretty major project right now and one of the requirements is the ability to interact with the Meetup API. I looked around and found one existing Meetup client in PHP, however it is horribly architected and as I started using it thinking I could improve it I decided it would be too much work and scrapped it to build my own. This new client is much more compact and efficient. The excerpt from the Meetup API client for PHP website says:

Meetup (http://meetup.com) is a social networking site based around community groups. Meetup provides an API to access their platform services from remote applications to manage authentication, events, rsvps and more.

Until now a good PHP client for the Meetup API has not existed. This project aims to bridge that gap by providing a high quality stand alone Meetup API PHP client. This client is simple to use through provided classes and also allows powerful advanced usage through direct queries to the API

The client is quite simple to setup and get running in your application quickly. Most of the GET endpoints are currently supported with plans on implmenting the rest and support for POST on the way. Check the following resources to get up and running.

You can get the client from the Meetup API client for PHP Github project page

Please try it out and leave feedback on issues and desired additions on the project issues page

P.S. oAuth support is currently in the works!

Useful sites for WordPress developers

By , November 2, 2011 8:22 am

I was asked by another WordPress developer if there are any sites that I recommend other WordPress developers follow. I do have such a list and decided that I will publish it in the hopes that others will also find these resources useful.

Someday if I get more motivated I will add fancy images and descriptions of each site :)

WordPress: Retrieve posts for last N minutes or timespan

By , November 1, 2011 9:26 pm

I am working on a project that requires me to pull the last 15 minutes worth of posts from a custom post type out of the wp_posts table for analysis. I thought this would be easy and quick, however I spend around 20 minutes digging until I finally found a nugget way at the bottom of the WP_Query codex page. Web searches and looking through the post functions unfortunately got me nowhere. I hope that if you are looking to do the same thing this post will be a helpful time saver to you.

The following will return posts just from the last 15 minutes

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
	// posts in the last 15 minutes
	$where .= " AND post_date > '" . date('Y-m-d', strtotime('-15 minutes')) . "'";
	return $where;
}
 
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );

The following will return posts from a timespan of 30 to 60 days old

 
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
	// posts  30 to 60 days old
	$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
	return $where;
}
 
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
 

Remember, it is good etiquette to prefix the name of your functions with the name of your theme or plugin. So if your plugin is named "Cat Namez" instead of using the filter_where function name you could use catNamez_filter_where. It is a bit longer, however it prevents namespace collisions with other plugins and possible fatal errors or stack overflows.

Be sure to read the WP_Query page on the WordPress Codex too! It is where I stole this code from :)

Setup WordPress on Windows Azure

By , October 25, 2011 7:22 am

If you are looking for information on hosting WordPress on Windows Azure there have been a lot of WordPress tutorials popping up in blogs lately. Unfortunately many(most) of these posts contain misinformation, error, or are out of date. The good news is that the Microsoft Interoperability team hosts an official tutorial on deploying WordPress on Windows Azure. This tutorial is the best resource available because it is written by the people who work with Windows Azure on a daily basis and who maintain the WordPress related projects for Windows Azure.Unfortunately their website does not rank well in search engines so I am posting a link to it myself to help visibility. Please please please read this tutorial. If you have questions or find bugs you can report them on the Github issue tracker or directly on the tutorial page using Disqus

How to deploy WordPress on Windows Azure

How to deploy WordPress Multisite on Windows Azure

My WordPress plugins

By , September 14, 2011 8:44 pm

I attended a great Seattle WordPress Developer Meetup this evening and a hot topic was what plugins to use for various things. I decided that until we have an official WordPress Seattle Developer's website I will post the plugins I am fond of here. Feel free to comment with better plugins or ideas for new plugins!

  • AlexaRank - Shows your website ranking compared to millions of other websites. This is just a fun plugin
  • AntiSpam Bee - Amazing plugins that stops 99.9% of all spam in it's tracks. Love it, install it on all client installs
  • AntiVirus - Helps protect your blog against known exploits and injections. Seems to work very well. I generally install it for all clients
  • Blog Metrics - Interesting plugin that shows metrics about the posts on your blog. I rarely actually check this but it is interesting sometimes.
  • CLI Switch - Super fun plugin that switches your WordPress website into a terminal. Try it here http://ben.lobaugh.net/blog/cli. A fairly pointless plugin overall
  • CodeHighlighter - Awesome plugin for posting code snippets. Changes highlighting mode per language. Unfortunately it is not available anymore, though you can email me if you want a copy.
  • Events Manager - A very powerful and capable events plugin. I highly recommend trying this one out
  • FancyBox for WordPress - Makes all the images in a post pop up in a pretty fancybox dialog
  • FoxyShop - Powerful yet easy to use looking shopping cart. Disclaimer: I have not used the plugin actively yet
  • Google Analytics for WordPress - Does like it says, adds the javascript for Google Analytics to the posts. Works well
  • Tweet, Like, Google +1 and Share - Adds simple, discreet sharing buttons to your posts. Fastest plugin for social sharing I have used
  • WP SlimStat - WordPressified version of SlimStat. Simply an awesome plugin that collects stats you can view right in WordPress. I use these stats much more than Google Analytics. It is also generally easier for clients to learn.

There you go. Try not to look for vulnerabilities in those and hack me now ;)

Running comparisons on Windows Azure Storage for PHP developers

By , March 16, 2011 3:02 pm

Let me setup a scenario. You have a Windows Azure Storage Table with a column called PartitionKey that is a series of numbers that are timestamps. You are testing your code to retrieve all timestamps greater than X in order to perform time based data calculations. Being smart you try “PartitionKey gt 1”, because 065983400000 is greater than 1 right? WRONG! Haha sucka! 065983400000 is not a number, it’s a string! Now being that I am a PHP programmer I do not even think about data types because PHP takes care of it for me in 99% of cases, so I was befuddled. Take a look again and you will see that a string comparison is happening, which means 065983400000 is in fact smaller than 1 because the string starts with a 0 which is less in value than a 1. If you had done 01 it would have worked because string comparisons start left and move right so you get: 0=0 next, 1 < 6, etc.

Be sure you keep this in mind when developing for Windows Azure Storage in PHP. Data types mean something in those other languages…

Converting DateTime.Ticks to a Unix timestamp and back in PHP

By , March 16, 2011 7:45 am

I recently found out that the DateTime library in C# provides the ability for much higher resolution of time compared to the PHP date time function. While PHP can go down to an impressive microsecond, C# can go all the way down to nanoseconds. Though I am still skeptical that it can actually calculate that small of a division of a second, it does return an impressively large representative number. Since PHP does not go down to nanoseconds it requires a bit of extra effort to convert between nanoseconds and seconds, but it is definitely doable and even fairly trivial once you know the requirements.

Some background on the PHP side of things for those who may not know, when dealing with seconds in PHP it comes from the Unix epoch and is called a Unix timestamp. The Unix epoch date is 01-01-1970 (some have a slightly different figure, but you can find that debate yourself). So 00000000 is 01-01-1970 when converted from the Unix timestamp to something human readable. 05-15-1986 is 51552440.

Taking that knowledge we need to determine when the Unix epoch occurs in DateTime.Ticks. Though I was working with Ticks generated from C# I did not have access to C# while writing this, but luckily Kramer Campbell provided the answer for us in an article on his website. You can read his article for more information on finding this number.

Unix epoch date in C# DateTime.Ticks
621355968000000000

Converting DateTime.Ticks to a Unix timestamp in PHP

Now that we know the Unix epoch in Ticks we can use a simple equation to do the conversion for us. First we need to subtract the current Tick value from the Unix epoch Tick value (Remember, Unix timestamps start at the Unix epoch), then we need to convert the result from nanoseconds into seconds.

Equation
(TICKS – 621355968000000000) / 10000000

The PHP function to do this is as follows

function ticks_to_time($ticks) {
   return ($ticks621355968000000000) / 10000000;
}

Simple enough right? What if we are using a service where not only do we need to be able to read the DateTime.Ticks, but also we need to convert into Ticks as well.

Converting a Unix timestamp to DateTime.Ticks

We already have everything we need. Simple reverse the equation from above and you will get the nanoseconds from seconds.

Equation
(UNIX_TIMESTAMP * 10000000) + 621355968000000000

Not to hard right? The problem you will face lies with PHPs automatic conversion of this large number into scientific notation. An annoyance, but not really a big deal if you know how to deal with it. We run another mathematical equation on it, or even loop through and change each number in sequence manually but there is no need for that. PHP provides us with a very usable solution in the form of number_format().
Here is what you need to know for number_format() to work.

umber_format(UNIX_TIMESTAMP, 0, ‘.’, ‘’);

Putting that all together in a simple function results in

function time_to_ticks($time) {
    return number_format(($time * 10000000) + 621355968000000000 , 0, '.', '');
}

Converting strings into DateTime.Ticks

Now that I had the ability to seamlessly convert between these two systems I also needed to find the Ticks N X ago, E.G. 10 Minutes ago. PHP provides the strtotime() function which let’s us find the Unix timestamp in plain English. For example, to find the Unix timestamp from 10 minutes ago you would use

strtotime(-10 minutes”);

Pretty simple; instead of running the necessary functions manually in PHP I created another function to handle it for me. This function reuses the other functions we defined above and will take in and valid string PHP can convert into a Unix timestamp and convert it into DateTime.Ticks.

Here is the code:

function str_to_ticks($str) {
   return time_to_ticks(strtotime($str));
}

Wrap it up!

There you go, now you have the knowledge to convert between C# DateTime.Ticks and Unix timestamps in PHP. Take the provided code and toss it into a file somewhere which you can easily reference, or expand and build it into your own library. Happy timing!

Download PHP code to convert DateTime.Ticks to a Unix timestamp and back from github

 

Hacking the Windows Azure PHP Command-line Tools to use your ServiceConfiguration.cscfg settings

By , March 15, 2011 10:20 am

Newer Note: The Interoperability Team at Microsoft has annouced a new version of the Windows Azure SDK for PHP which contains a set of shiny new command line tools and has deprecated the Windows Azure PHP Command-line Tools project. This post is not valid for the SDK, however a config file of the same name will be edited with the new tools. See http://azurephp.interoperabilitybridges.com for documentation on the new SDK tools

 

Note: This is currently valid for the Thu Mar 10, 2011 release. Likely in the future custom ServiceConfiguration.cscfg files will be supported in the project directory.

It takes some time to upload a Windows Azure package and run through the update process, and if you are using the Windows Azure PHP Command-line Tools you probably have run into the same situation I have. You are going along merrily developing on  your local machine and then you are ready to deploy. You run the package.php script to build yourself a Windows Azure package, edit the ServiceConfiguration.cscfg file with your storage connection settings and upload to the cloud. Yay, your app is finally up, now it’s time to relax. But wait! What’s that? A bug you missed? Darn. Back to the coding cycle, but this time after running package.php and uploading your whole application is broken. Yikes! What happened? Oh yeah….that pesky little ServiceConfiguration.cscfg file was recreated by package.php and you forgot to edit it before uploading. Nothing to do now but edit the file and upload the whole kit-n-caboodle again.

I fairly regularly forget to edit the ServiceConfiguration.cscfg file and it drives me nuts. Turns out there is a simple way of ensuring you always get your connection settings and there is no need to worry about remembering to edit the file.

Cruise on over to the directory containing your command-line tools and take a look inside the “res\tool” folder and what sight should await your wondering eyes but the ServiceConfiguration.cscfg file! Edit it to your heart’s content and those changes will always be in your new builds.

Learn more about running PHP on Windows Azure at http://azurephp.interopbridges.com

Theme by Blam Designs
Based on Themocracy