MINOR: Initial - still in prgress

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/phpinstaller/trunk@97321 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
fwink 2010-01-21 02:10:09 +00:00
parent 28277e9eca
commit 882036e886
8 changed files with 605 additions and 0 deletions

37
BeMoreHuman/_config.php Normal file
View File

@ -0,0 +1,37 @@
<?php
if($_SERVER['HTTP_HOST'] == 'test') {
header("Location: http://test.silverstripe.com$_SERVER[REQUEST_URI]");
die();
}
if($_SERVER['HTTP_HOST'] == 'dev') {
header("Location: http://dev.silverstripe.com$_SERVER[REQUEST_URI]");
die();
}
global $project;
$project = 'BeMoreHuman';
global $database;
$database = 'SS_bemorehuman';
//MySQLDatabase::set_connection_charset('utf8');
// Use _ss_environment.php file for configuration
require_once("conf/ConfigureFromEnv.php");
Director::addRules(100, array(
));
Email::setAdminEmail('support@silverstripe.com');
// This line set's the current theme. More themes can be
// downloaded from http://www.silverstripe.com/cms-themes-and-skin
SSViewer::set_theme('blackcandy');
// enable nested URLs for this site (e.g. page/sub-page/)
SiteTree::enable_nested_urls();
date_default_timezone_set('Pacific/Auckland');
?>

View File

@ -0,0 +1,114 @@
<?php
class BeMoreHumanPage extends Page {
/**
* Static variable to store data alongside with the page instance.
* @var array
*/
public static $db = array(
'PathToUploadedPictures' => 'Varchar', //Where to put the uploaded images
'PathToWallPictures' => 'Varchar', //Where to put the Wall images
'HeadText' => "HTMLText", // Text above the the Form
'WhatIsTheWall' => "HTMLText", // Text explaining what is the wall
'FooterNotHidden' => "HTMLText", // Text in The footer which is not hidden
'FooterHidden' => "HTMLText", // Text in The footer which is hidden at first
);
static $defaults = array(
'PathToUploadedPictures' => 'assets/HowHumansWin/',
'PathToWallPictures' => 'assets/TheWall/'
);
static $has_many = array(
'HowHumansWinImages' => 'HowHumansWinImage',
'WallImages' =>'WallImage'
);
/**
* Overwrites SiteTree.getCMSFields to change the CMS form behaviour,
* i.e. by adding form fields for the additional attributes defined in
* {@link CataloguePage::$db}.
*/
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldsToTab('Root.Content.BeMoreHuman',
array(
new TextField('PathToUploadedPictures','Path where to store the uploaded images from the form (e.g. assets/HowHumansWin/'),
new TextField('PathToWallPictures','Path where to store the images from the wall (e.g. assets/TheWall/)'),
new TextareaField('HeadText','Text before the form:',5,30),
new TextareaField('WhatIsTheWall','What is the wall:',10,30),
new TextareaField('FooterNotHidden','Footer - text always visible:',5,30),
new TextareaField('FooterHidden','Footer - text extends the upper one:',10,30)
));
// return the modified fieldset.
return $fields;
}
}
/**
* Controller Class for Main BeMoreHumanPage
*/
class BeMoreHumanPage_Controller extends Page_Controller {
/**
* The Form
*
*/
function Form(){
// Create fields
$fields = new FieldSet(
new TextField('Name'),
new SimpleImageField(
"PictureName",
"Upload image below"
),
new TextareaField('Text')
);
// Create actions
$actions = new FieldSet(
new FormAction('TellHowHumansWinForm', 'Send')
);
return new Form($this, 'HowHumansWinForm', $fields, $actions);
}// end Form
function TellHowHumansWinForm(){
}
function getWallImageLink(){
$image=DataObject::get_one('WallImage',NULL,false,'id DESC');
if($image){
$link=$image.Link();
}
else{
$link=Director::baseURL().$this->PathToWallPictures . "/default.jpg";
}
return $link;
}
/**
* ThereAreTweets()
* Checks if there are tweets
* @returns bool true if there are otherwise false
*
*/
function ThereAreTweets(){
return false;
}
function LatestTweets($howmany=4){
return new DatObjectSet;
}
} // end Controller
/**
* BeMoreHumanPage Exception Class
*/
class BeMoreHumanPage_Exception extends Exception { }

View File

@ -0,0 +1,6 @@
<?php
class HowHumansWinImage extends Image {
}
?>

75
BeMoreHuman/code/Page.php Executable file
View File

@ -0,0 +1,75 @@
<?php
class Page extends SiteTree {
public static $db = array(
);
public static $has_one = array(
);
}
class Page_Controller extends ContentController {
/**
* An array of actions that can be accessed via a request. Each array element should be an action name, and the
* permissions or conditions required to allow the user to access it.
*
* <code>
* array (
* 'action', // anyone can access this action
* 'action' => true, // same as above
* 'action' => 'ADMIN', // you must have ADMIN permissions to access this action
* 'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true
* );
* </code>
*
* @var array
*/
public static $allowed_actions = array (
'SearchForm'
);
public function init() {
parent::init();
// Note: you should use SS template require tags inside your templates
// instead of putting Requirements calls here. However these are
// included so that our older themes still work
Requirements::themedCSS("layout");
Requirements::themedCSS("typography");
Requirements::themedCSS("form");
}
/**
* Site search form
*/
function SearchForm() {
$searchText = isset($_REQUEST['Search']) ? $_REQUEST['Search'] : 'Search';
$fields = new FieldSet(
new TextField("Search", "", $searchText)
);
$actions = new FieldSet(
new FormAction('results', 'Search')
);
return new SearchForm($this, "SearchForm", $fields, $actions);
}
/**
* Process and render search results
*/
function results($data, $form){
$data = array(
'Results' => $form->getResults(),
'Query' => $form->getSearchQuery(),
'Title' => 'Search Results'
);
return $this->customise($data)->renderWith(array('Page_results', 'Page'));
}
}
?>

View File

@ -0,0 +1,6 @@
<?php
class WallImage extends Image {
}
?>

23
twittermirror/_config.php Normal file
View File

@ -0,0 +1,23 @@
<?php
// Your Twitter asccount name
TwitterMirror_Controller::setTwitteruser('bemorehuman');
// The coresponding password
TwitterMirror_Controller::setTwitterpass('OkRC2YRKSXFo');
// mirror tweets containing this seachterm
//TwitterMirror_Controller::setSearchterm('#bemorehuman');
TwitterMirror_Controller::setSearchterm('silverstripe');
/* Set the level for logging
* Valid values are 0 .. 6
* Where 6 turns the logging off
* 0 => 'DEBUG',
* 1 => 'INFO',
* 2 => 'NOTICE',
* 3 => 'WARNING',
* 4 => 'ERROR',
* 5 => 'CRITICAL',
* 6 => 'Turn logging off'
*/
TwitterMirror_Controller::setLogThis(1);
?>

View File

@ -0,0 +1,284 @@
<?php
class TwitterMirror extends Object{
}
/**
* @name TwitterMirror_Controller
* @version 0.0.1
*
* This Ccontroller should be called from the commandline or better via the crond.
* sake TwitterMirror_Controller
*
* Whern run it takes the max(id) from the Tweets and issues a
* http://search.twitter.com/search.json?q={$Searchterm}&show_user=true&rpp=100&since_id={$maxid}
* This gives the results of all Tweets tagged with #bemorehuman since the
* latest stored tweet. The results are paged with 100 tweets per page.
* If there are more than 100 results the response contain a "next_page" attribute.
*
* Before every request it is checked if there are at least 2 remaining api-hits.
* http://twitter.com/account/rate_limit_status.json
* The limits apply by IP basis or user basis if suthenticated,
* so we have to check them before each run to
* prevent our IP/account from being blocked
*
* You can set the credentials for the twitter account (user/pass) in _config.php
* You can set the serchterm in _config.php
*
* @todo using the timeline, whern no searchterm is given.
* http://twitter.com/statuses/user_timeline/{user}.json.
*
*
*/
class TwitterMirror_Controller extends Controller{
protected static $twitteruser = 'twitteruser'; //'bemorehuman';
protected static $twitterpass = 'twitterpass'; //'OkRC2YRKSXFo';
protected static $Searchterm = 'TermToSearchFor'; //'%23bemorehuman';
protected static $LogThis = 4; //ERRORs
private static $LogLevels = array(
0 => 'DEBUG',
1 => 'INFO',
2 => 'NOTICE',
3 => 'WARNING',
4 => 'ERROR',
5 => 'CRITICAL',
6 => 'Turn logging off',
'DEBUG' => 0,
'INFO' => 1,
'NOTICE' => 2,
'WARNING' => 3,
'ERROR' => 4,
'CRITICAL' => 5,
'Turn logging off' => 6,
);
// Getter / Setter
public function setLogThis($val){
if(is_numeric($val) and ($val > 0) and ($val <= 5 )) self::$LogThis = $val;
return;
}
public function getLogThis(){
return self::$LogThis;
}
public function setSearchterm($val){
$val=trim($val);
if(!$val) return;
self::$Searchterm = $val;
}
public function getSearchterm(){
return self::$Searchterm;
}
public function setTwitteruser($val){
$val=trim($val);
if(!$val) return;
self::$twitteruser =$val;
}
public function getTwitteruser(){
return self::$twitteruser ;
}
public function setTwitterpass($val){
$val=trim($val);
if(!$val) return;
self::$twitterpass =$val;
}
public function getTwitterpass(){
return self::$twitterpass;
}
/**
* twitter_hits_left()
*
* @return the numer of api-hits left or -1 on error
*
*/
private function twitter_hits_left(){
$rate= new RestfulService("http://twitter.com/account/rate_limit_status.json", 0);
$rate->basicAuth(self::$twitteruser,self::$twitterpass );
$response = $rate->request(); //
if($response->isError()){
//@TODO Errorhandling
$errormsg= $response->getStatusCode() .' '. $response->getStatusDescription();
$this->logit("$errormsg",4);
return -1;
}
$arryvalues = Convert::json2array($response->getbody());
$this->logit("API requests left: " . $arryvalues['remaining_hits'], 1);
return $arryvalues['remaining_hits'];
}
/**
* has_to_stop($myStartuptime)
* checks if there are at least 2 twitter api requests left
* and it must been less than 45 seconds the program has started
* (it is assumed that the program is run every minute,
* therfore 45 sec maximum runtime)
* @param $myStartuptime time in ticks when the program started
*
* @return bool true if one of the abort criterias are matched
* false if not
*
*/
private function has_to_stop($myStartuptime){
// check runtime to avoid race conditions
if((time() - $myStartuptime ) > 45){
$this->logit("We have to stop because " . time() ." > $myStartuptime + 45", 2);
return true;
}
//if we can get another page
$hl=0;
if(($hl=$this->twitter_hits_left()) <= 2){
$this->logit("We have to stop because there are only $hl API-requests left",2);
return true;
}
$this->logit("$hl API-requests left",1);
return false;
}
/**
* logit($text='--MARK--',$loglevel=1)
* echos $text to STDOUT if $loglevel is greater or equal $LogThis, which
* can be set in _config.php via
* TwitterMirror_Controller::setLogThis(1);
* Valid values are 0 .. 6
* Where 6 turns the logging off
* 0 => 'DEBUG',
* 1 => 'INFO',
* 2 => 'NOTICE',
* 3 => 'WARNING',
* 4 => 'ERROR',
* 5 => 'CRITICAL',
* 6 => 'Turn logging off'
*
* @param $text Text to log
* @param $loglevel Level of importance of the log-entry
*
*
*/
private function logit($text='--MARK--',$loglevel=1){ //all without logleve are handeled as Info
if($loglevel < $this->LogThis) return;
echo time() . " [". $this->getSearchterm() ."] " .$this->LogLevels[$loglevel]. ": " . $text . "\n";
}
/**
* index()
* get's the newest feeds from twitter marked with {Searchterm}
* and stores them in the database;
*
*/
public function index() {
$myStartuptime=time(); // keep the start time, because in 59s we run again ;0)
$this->logit("Starting at: $myStartuptime",0);
$searchterm=$this->getSearchterm();
if($this->has_to_stop($myStartuptime)) return ; // we need at least 2 api hits left and a bit of time
$max_id=0;
$filter="searchterm = '". $searchterm ."'";
$this->logit("searchterm=[$searchterm]\n filter is ==>$filter<==",0);
$tweet=DataObject::get_one('Tweet',$filter, false,'twitterid DESC');
if($tweet){
$max_id = $tweet->twitterid;
}
$this->logit("max_id is $max_id",0);
$params = array(
'q' => $searchterm,
'show_user' => 'true',
'rpp' => 100,
);
if($max_id) $params['since_id']=$max_id; // search only for newer tweets
//Check for new tweets with #bemorehuman (searchterm)
//http://search.twitter.com/search.json?q=%23bemorehuman&show_user=true&rpp=100
$rest= new RestfulService("http://search.twitter.com/search.json", 0);
$rest->basicAuth(self::$twitteruser,self::$twitterpass );
$DoneWithIt=false;
while(!$DoneWithIt){
$rest->setQueryString($params);
$response = $rest->request(); //
if($response->isError()){
//@TODO Errorhandling
$errormsg= $response->getStatusCode() .' '. $response->getStatusDescription();
$this->logit($errormsg,4);
return ;
}
$obj=Convert::json2obj($response->getbody());
// checking the results
$results=$obj->results;
if(!is_array($results)){
// This should always be an array even if empty, so we assume the complete
// object to be corrupt.
//@TODO Throw Fatal Error or something simular
$this->logit("results should be an array",5);
die;
}
if(($i = count($results)) <= 0){
$DoneWithIt=true;
}
else{
$this->logit("processing $i new entires",0);
//Process the resuls
echo "\n\n";
foreach( $results as $result){
$tweet = new Tweet;
$d=DateTime::createFromFormat("D, j M Y H:i:s O", $result->created_at); //"Thu, 14 Jan 2010 02:46:45 +0000"
$tweet->created_at = $d->format("Y-m-d H:i:s");
$tweet->twitterid = $result->id;
$tweet->text = $result->text;
if(isset($result->iso_language_code)) $tweet->iso_language_code = $result->iso_language_code;
else $tweet->iso_language_code = "xx";
if(isset($result->profile_image_url)) $tweet->profile_image_url = $result->profile_image_url;
$tweet->from_user_id = $result->from_user_id;
$tweet->from_user = $result->from_user_id;
if(isset($result->to_user_id)) $tweet->to_user_id = $result->to_user_id;
if(isset($result->to_user)) $tweet->to_user = $result->to_user;
$tweet->searchterm = $searchterm;
$tweet->write();
$this->logit("Adding tweet " . $tweet->twitterid. " from " .$tweet->from_user . " (". $tweet->text .")",0);
//return;
}
//all results from this page should be in the DB now
// let's see if there is more to come
if(isset($obj->next_page)){
$t1=strpos($obj->next_page,'page=');
if($t1){
$t=substr($obj->next_page,$t1 + 5); //page=
$t2=strpos($t,'&');
if($t2){
$t=substr($t,0,$t2);
}
$params['page']=$t + 0; //Set the page parameter
}
else{
// "no page= found\n";
$this->logit("No next page info in next_page",2);
$DoneWithIt=true;
}
}
else{
$this->logit("No next page",0);
$DoneWithIt=true;
}
if($this->has_to_stop($myStartuptime)) return;
}
}
return ;
}
}
?>

View File

@ -0,0 +1,60 @@
<?php
class Tweet extends DataObject {
static $db = array(
"created_at" => "SSDatetime",
"twitterid" => "Varchar", //is is in json
"text" => "Varchar(150)",
"iso_language_code" => "Varchar(5)",
"profile_image_url" =>"Varchar",
"from_user" =>"Varchar",
"from_user_id" => "Varchar",
"to_user" => "Varchar",
"to_user_id" => "Varchar",
"searchterm" => "Varchar"
);
}
class Tweet_Controller extends Controller{
private function TweetLink($ext='.json'){
if ($this->twitterid)
return "http://twitter.com/statuses/show/" . $this->twitterid . $ext;
return NULL;
}
public function TweetLinkXML(){
return TweetLink('.xml');
}
public function TweetLinkJSON(){
return TweetLink('.json');
}
public function Link2FromTwitterUser(){
if($this->from_user_id){
return "http://twitter.com/" . $this->from_user_id;
}
return NULL;
}
public function Link2ToTwitterUser(){
if($this->to_user_id){
return "http://twitter.com/" . $this->to_user_id;
}
return NULL;
}
public function Link2Twitter($searchterm='#bemorehuman'){
if(!$searchterm) return NULL;
if($this->from_user_id){
return "http://twitter.com/#search?q=" . urlencode($searchterm);
}
return NULL;
}
}
?>