User:Plrk/Watchlist RSS feed in PHP

As User:Sylvain Schmitz/Watchlist RSS feed in PHP is quite outdated, I hacked together a PHP script which uses the API to login, and then fetch the watchlist feed.

There is also a slightly more complicated version that includes diff links.

Installation edit

Copy this code into a .php file on your PHP-running web server, after editing your username/password/preferred wiki accordingly. Point your feed aggregator to the PHP script. That's it!

The code edit

<?
////////////////////////////////////////////////////////////
// CONFIGURATION ///////////////////////////////////////////
////////////////////////////////////////////////////////////

// Variables you should edit
$mediawiki_url = "http://en.wikipedia.org/"; // the wiki you want to fetch from. do not forget the trailing slash.
$credentials = array();
$credentials['lgname'] = ""; // your username
$credentials['lgpassword'] = ""; // your password
$allrev = TRUE; // whether or not you want to display all revisions (set to TRUE if you do, FALSE if you don't)

// Variables you should not edit
$login_url = "w/api.php?format=php&action=login";

$watchlistfeed_url = "w/api.php?action=feedwatchlist";
if($allrev){
	$watchlistfeed_url .= "&allrev=yes";
}

////////////////////////////////////////////////////////////
// LOGIN ///////////////////////////////////////////////////
////////////////////////////////////////////////////////////
$ch = curl_init(); // initializing curl, creating a curl resource
curl_setopt($ch, CURLOPT_URL, $mediawiki_url . $login_url); // set URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // we want the transfer to return a string
curl_setopt($ch, CURLOPT_POST, TRUE); // we are going to post something
curl_setopt($ch, CURLOPT_POSTFIELDS, $credentials); // we are going to post the array "credentials"
$output = curl_exec($ch); // $output now contains the output string
curl_close($ch); // we close curl resource to free up system resources

// if everything has been done right, $output now contains a serialized array with the
// information we need when we post the request for the watchlist.

$cookie = unserialize($output); // unserialize the output

////////////////////////////////////////////////////////////
// WATCHLIST ///////////////////////////////////////////////
////////////////////////////////////////////////////////////
$wl = curl_init(); // initializing curl, creating a curl resource
curl_setopt($wl, CURLOPT_URL, $mediawiki_url . $watchlistfeed_url); // set URL
curl_setopt($wl, CURLOPT_COOKIE,	$cookie['login']['cookieprefix'] . "UserName=" . $cookie['login']['lgusername'] . "; " .
	$cookie['login']['cookieprefix'] . "UserID=" . $cookie['login']['lguserid'] . "; " .
	$cookie['login']['cookieprefix'] . "Token=" . $cookie['login']['lgtoken'] . "; " .
	$cookie['login']['cookieprefix'] . "_session=" . $cookie['login']['sessionid']); // set the cookies to be used in the request
	
curl_setopt($wl, CURLOPT_RETURNTRANSFER, 1); // we want the transfer to return a string
$output = curl_exec($wl); // $output now contains the output string
curl_close($wl); // we close curl resource to free up system resources

echo $output;
?>