Connect PHP to PostgreSQL database

Connecting to PostgreSQL is quite simple, but bit different from what we have with MYSQL.

Here we go:

$pgconnection = pg_connect( "host=http://yourhost.com port=5432 dbname=yourdbname user=yourusername password=yourpassword" );
if ( !$pgconnection ) {
	echo "Cannot connect to datbase";
}

to insert into an existing table named FOO( bar1 varchar, bar2 numeric )

$bar1 = "MyFoo";
$bar2 = "69";

$query=<<<EOF
INSERT INTO FOO( bar1, bar2 ) VALUES ( '$bar1', '$bar2' );
EOF;

$ret = pg_query($pgconnection, $query);

if ( !$ret ) {
	echo "Cannot write to database";
}

References:
1) http://www.tutorialspoint.com/postgresql/postgresql_php.htm
2) http://php.net/manual/en/ref.pdo-pgsql.connection.php

Advertisements

[MW]$wgExtensionFunctions : Run a function when an extension is evoked

Working further with making the BounceHandler extension more production friendly – we came up with a scenario in which I wanted some of the extension global variables to take values from a MediaWiki global variable. It would’ve looked like $myExtensionGlobal = $mwGlobal – which looks weird.
Thanks to Hoo( WMDE ), I got introduced to $wgExtensionFunctions[] [1]

Scenario:
You need to run a function once your extension gets loaded. In my case, I want to check if $wgFoo ( which defaults to null ) was set in the original myExtension.php and if not – set it to the MW global – $wgBar
Solution:
in your myExtension.php

$wgFoo = null;
$wgExtensionFunctions[] = function() {
	global $wgBar;
	$wgFoo = $wgFoo ? : $wgBar;
}

This would introduce a nameless function which would do our required manipulations.
You can even make it a named function as in :

$wgExtensionFunctions[] = &amp;quot;myFunction&amp;quot;;
function myFunction() {
         // Code your task
}

Hope it help someone ! Happy Hacking 🙂
[1] https://www.mediawiki.org/wiki/Manual:$wgExtensionFunctions

MediaWiki PHP unit tests : Reading contents from a file ( @dataProvider )

While writing php unit tests for the BounceHandler extension, I came across a scenario in which I had to read contents from a text file and feed that to the test function as a variable. With the dataProvider functions it was solved simple.

Problem:
* function testProcessTextfile( $foo ) function requires $foo to be read from a file from bar/foo.txt

Solution:
In the class extending ApiTestCase give :

public static function provideFoo() {
	$foo = file_get_contents( __DIR__ .'/bar/foo.txt' );
	return array (
		array ( $foo )
	);
}

/**
* @dataProvider provideFoo
* @param $foo
*/
function testProcessTextfile( $foo ) {
	//code to process $foo
}

That would do ! Happy hacking!
PS: Please note that the following wont work without the correct function comments and headers 🙂

Manually authenticating a MediaWiki user e-mail id

While testing with emails and user accounts, you will probably hit with a scenario when you have to create a fake account and make the Wiki send e-mails to it – so that you can analyse the results. Something similar turned up to me today, and thanks to Legoktm & Hoo, here we go:
* Scenario:
You have to manually confirm e-mail id of user ‘FooBar’
* Solution:

$ cd mediawiki/maintenance/
$ php eval.php 
> $user = User::newFromName('FooBar');
> $user->confirmEmail();
> $user->saveSettings();

You’re done! Navigate to Specail:Preferences to make sure the email id is authenticated !
What you did ? You created a User object corresponding to the User Name = ‘FooBar’. You can even user User::newFromId(123); if you know the user id ( You can get it from the ‘user’ table of your wiki )
Happy Hacking!