<?php if (!defined('PmWiki')) exit();

## TWITTERPOST -- SEND NEW-PAGE UPDATES TO TWITTER ##
# A cron job should send the msg: wget http://mysite.com/Main/Index?action=twitterpost
$HandleActions['twitterpost'] = 'twitterpost';  # if url contains action=twitterpost
$HandleAuth['twitterpost'] = 'admin';              # authorization level $auth for twitterpost
# create a PV for use in pagelist - it retrieves the creation-time of the page, in the format YYYYMMDDHHSS
$FmtPV['$PageCreationDateYMDHS'] = 'strftime("%Y%m%d%H%M", $page["ctime"])';

function twitterpost($pagename, $auth) {
	global $twitterPosts_verbose, $twitter_username, $twitter_password;

	# call MakePageList function from pagelist.php
	$elapsedTime = strtotime('-24 hours');  # last 24 hours - input for strtotime
	$startTime = strftime('%Y%m%d%H%M', $elapsedTime);


	## NEW PAGES - Use PageList to generate a list of new pages from the last 24 hours!!
	# args for pagelist
	$optNew = array(
		# essential parameters: If
		# this "if=" includes new pages from the last 24 hours
		'if' => 'date '.$startTime.'.. {=$PageCreationDateYMDHS}' # checks for creation dates
		# optional pagelist parameters -- comment in and out at will, and add others as needed in the same format
		,'list' => 'normal'
		#,'group' => '-Test'   # perhaps there are some groups you'd care to include/exclude?
		#,'name' => '-Test'   # perhaps there are some pages you'd care to include/exclude?
	);
	# super simple - pages created in the last 24 hours!!
	$pagesArray_New = MakePageList($pagename, $optNew, 0);  # this is the function from scripts/pagelist.php


	## UPDATED PAGES - Use PageList to generate a list of pages updated within the last 24 hours
	$optUpdated = array(
		# essential parameters: If
		# If you want to include pages that have simply been updated, use this "if=" instead
		'if' => 'date '.$startTime.'.. @{=$LastModifiedTime}'  # checks for updates
		# optional pagelist parameters -- comment in and out at will, and add others as needed in the same format
		,'list' => 'normal'  # just events
	);
	# generate list of recently updated pages - comment this out if you don't need it
	$pagesArray_Updated = MakePageList($pagename, $optUpdated, 0);


	# feel free to comment out either of the UPDATED PAGES or NEW PAGES sections above depending on your needs

	# Combine two Arrays -- New Pages + Updated Events
	# combine the two arrays, and prevent duplicate entries
	if ($pagesArray_Updated) {
		$pagesArray = array_unique(array_merge($pagesArray_New, $pagesArray_Updated));
	} else {
		$pagesArray = $pagesArray_New;
	}

	# send twitter updates for each new page or updated page made in the last 24 hours
	foreach ($pagesArray as $pn) {

		# common tweet parameters
		$tweetUrl = PageVar($pn, '$PageUrl');
		# perhaps you'd care to shorten that URL? see http://pmwiki.org/wiki/Cookbook/ShortURLs for a recipe
		# if ($twitterShortUrls) $tweetUrl = bitly_shortened_url($bitLy_login, $bitLy_apiKey, $tweetUrl);
		$tweetName = PageVar($pn, '$Namespaced');
		$tweetGroup = PageVar($pn, '$Groupspaced');
		
		# new group announcement (name==Index)
		if ($pn_name=="Index") {
			$tweetMsg = "$tweetGroup - new group created!! $tweetUrl";
		}
		# new page announcement (name!=Index)
		else {
			$tweetMsg = "$tweetName - new page created in $tweetGroup!! $tweetUrl";
		}

		# ... or call your own special twitter message formatting function -
		# perhaps different pages or groups deserve differently formatted tweets?
		# $tweetMsg = personalizedTweetMsg_func($pn); # call function

		# for debugging
		if ($twitterPosts_verbose) {
			echo "$pn<br />";
			echo "$tweetMsg<br />";
		}
		
		# finally, send twitter post by calling the twitterPostMsg_func()
		twitterPostMsg_func($twitter_username, $twitter_password, $tweetMsg_UDP, $twitterPosts_verbose);

		if ($twitterPosts_verbose) {
			echo ".<br />"; # this posts after all Twitter messages...
		}

	}

	# use this if you want to display a page
	//HandleBrowse($pagename);	# display the page specified in the url (e.g. MyGroup.MyPage)
}