Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
a polltracker api in a movable type template
<?php
//pollxmlgen - a brute force poll api generator...
//@author al shaw
//sept 2009
//---global stuff--
error_reporting(0);
//---deal with query strings
$challenger1 = $_GET['challenger1'];
$challenger2 = $_GET['challenger2'];
$challenger3 = $_GET['challenger3'];
//---biz logic--
class Poll {
protected $poll;
function __construct($individualpoll) {
$this->poll = $individualpoll;
}
function parse() {
//regex it yadayadaydad
preg_match_all('/\w+/', $this->poll,$pollbits);
foreach ($pollbits as $bit) {
$keynum = ( max(array_keys($bit)) + 1 );
}
//echo $this->poll . ' ';
if ($keynum == 6) { //name, party, score x2
return '2_challenger_w_party';
}
else
if ($keynum == 9) { //name,party,score x3
return '3_challenger_w_party';
}
else
if ($keynum == 4) { //name,score x2
return '2_challenger_simple'; //or approve/disapprove
}
}
}
class PollNodes extends Poll {
function __construct($polltype,$poll,$permalink,$contest,$date,$sample_size,$pollster,$pollsterurl) {
$this->poll = $poll;
$this->polltype = $polltype;
$this->permalink = $permalink;
$this->contest = $contest;
$this->date = $date;
$this->sample_size = $sample_size;
$this->pollster = $pollster;
$this->pollsterurl = $pollsterurl;
}
function paramFilter($challenger_a_name,$challenger_b_name,$challenger_c_name) {
global $challenger1;
global $challenger2;
global $challenger3;
if ( (!empty($challenger1)) && (!empty($challenger2)) && (empty($challenger3)) ) { //2 challenger w/ or w/o party
if ( (($challenger_a_name == "$challenger1") || ($challenger_b_name == "$challenger1")) && (($challenger_a_name == "$challenger2") || ($challenger_b_name == "$challenger2")) ) {
return 'true';
}
}
else if (!empty($challenger3)) { //3 challenger
if ( (($challenger_a_name == "$challenger1") || ($challenger_b_name == "$challenger1") || ($challenger_c_name == "$challenger1") ) && ( ($challenger_a_name == "$challenger2") || ($challenger_b_name == "$challenger2") || ($challenger_c_name == "$challenger2")) && ( ($challenger_a_name == "$challenger3") || ($challenger_b_name == "$challenger3") || ($challenger_c_name == "$challenger3")) ) {
return 'threechallenger';
}
}
else {
return 'all';
}
}
function pollTopWrapper() {
echo '<poll>';
echo '<contest>' . $this->contest . '</contest>';
echo '<polltype>' . $this->polltype . '</polltype>'; //sanity check
$datestr = strtotime($this->date);
$m = date('m', $datestr);
$d = date('d', $datestr);
$y = date('y', $datestr);
echo '<date m="' . $m . '" d="' . $d . '" y="' . $y .'" />';
echo '<url>' . $this->permalink .'</url>';
echo '<sample_size>' . $this->sample_size . '</sample_size>';
echo '<challengers>';
}
function pollBottomWrapper() {
echo '</poll>';
}
function pollsterData() {
echo '</challengers>'; //end challengers nodeset
echo '<pollster>';
echo '<pollstername>' . $this->pollster . '</pollstername>';
echo '<pollsterlink>' . $this->pollsterurl . '</pollsterlink>';
echo '</pollster>';
}
function echoPoll() {
//need to break down poll into parts
preg_match_all('/\w+/',$this->poll,$pollbits);
foreach ($pollbits as $bit) {
if ($this->polltype == '2_challenger_w_party') {
$challenger_a_name = $bit[0];
$challenger_a_party = $bit[1];
$challenger_a_score = $bit[2];
$challenger_b_name = $bit[3];
$challenger_b_party = $bit[4];
$challenger_b_score = $bit[5];
$challengermatch = $this->paramFilter($challenger_a_name,$challenger_b_name,'');
if ($challengermatch == 'true' || $challengermatch == 'all') {
$this->pollTopWrapper();
echo '<challenger>';
echo '<name>' . $challenger_a_name . '</name>';
echo '<party>' . $challenger_a_party . '</party>';
echo '<score>' . $challenger_a_score . '</score>';
echo '</challenger>';
echo '<challenger>';
echo '<name>' . $challenger_b_name . '</name>';
echo '<party>' . $challenger_b_party . '</party>';
echo '<score>' . $challenger_b_score . '</score>';
echo '</challenger>';
$this->pollsterData();
$this->pollBottomWrapper();
}
}
else
if ($this->polltype == '3_challenger_w_party') {
$challenger_a_name = $bit[0];
$challenger_a_party = $bit[1];
$challenger_a_score = $bit[2];
$challenger_b_name = $bit[3];
$challenger_b_party = $bit[4];
$challenger_b_score = $bit[5];
$challenger_c_name = $bit[6];
$challenger_c_party = $bit[7];
$challenger_c_score = $bit[8];
$challengermatch = $this->paramFilter($challenger_a_name,$challenger_b_name,$challenger_c_name);
if ($challengermatch == 'threechallenger' || $challengermatch == 'all') {
$this->pollTopWrapper();
echo '<challenger>';
echo '<name>' . $challenger_a_name . '</name>';
echo '<party>' . $challenger_a_party . '</party>';
echo '<score>' . $challenger_a_score . '</score>';
echo '</challenger>';
echo '<challenger>';
echo '<name>' . $challenger_b_name . '</name>';
echo '<party>' . $challenger_b_party . '</party>';
echo '<score>' . $challenger_b_score . '</score>';
echo '</challenger>';
echo '<challenger>';
echo '<name>' . $challenger_c_name . '</name>';
echo '<party>' . $challenger_c_party . '</party>';
echo '<score>' . $challenger_c_score . '</score>';
echo '</challenger>';
$this->pollsterData();
$this->pollBottomWrapper();
}
}
else
if ($this->polltype == '2_challenger_simple') {
$challenger_a_name = $bit[0];
$challenger_a_score = $bit[1];
$challenger_b_name = $bit[2];
$challenger_b_score = $bit[3];
$challengermatch = $this->paramFilter($challenger_a_name,$challenger_b_name,'');
if ($challengermatch == 'true' || $challengermatch == 'all') {
$this->pollTopWrapper();
echo '<challenger>';
echo '<name>' . $challenger_a_name . '</name>';
echo '<score>' . $challenger_a_score . '</score>';
echo '</challenger>';
echo '<challenger>';
echo '<name>' . $challenger_b_name . '</name>';
echo '<score>' . $challenger_b_score . '</score>';
echo '</challenger>';
$this->pollsterData();
$this->pollBottomWrapper();
}
}
}
}
}
//---data
header('Content-Type: text/xml; charset=utf-8');
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
echo "<data>";
//poll data from mt
<MTEntries lastn="500">
$poll = "<$MTEntryTitle encode_xml="1"$>"; //data as string
$permalink = "<$MTEntryLink type="Category" encode_xml="1"$>";
$contest = "<$MTEntryCategory encode_xml="1"$>"; //category i.e. MN-SEN
$date = "<$MTEntryDate format="%m/%d/%Y"$>"; //from MT, not necessarily poll taken date
$sample_size = "<$MTentrydatapoll_sample_size$>"; //poll sample size for trend lines
$pollster = "<$MTEntryMore convert_breaks="0" encode_xml="1"$>"; //pollster string
$pollsterurl = "<$MTEntryKeywords encode_xml="1"$>"; //pollster link
$newpoll = new Poll("$poll"); //instantiate poll
$polltype = $newpoll->parse(); //parse poll out for type
//echo $polltype;
//echo '<br>';
$pollXML = new PollNodes("$polltype","$poll","$permalink","$contest","$date","$sample_size","$pollster","$pollsterurl"); //pass poll type to returnPollNodes
$polldata = $pollXML->echoPoll();
</MTEntries>
echo "</data>"
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment