klenwell information services : SpecialCocktailNapkin

Revision [1303]

This is an old revision of SpecialCocktailNapkin made by KlenwellAdmin on 2010-04-16 11:47:59.
 

online cocktail napkin

links and notes for later organization
CocktailNapkin2009 2009 Napkin
CocktailNapkin2008 2008 Napkin
CocktailNapkin2007 2007 Napkin
WordsIReallyLike
ThumbDrive
CocktailQuestions Questions for Strangers

php safe query builder
	function safe_sql($sqlf, $ParamList) {
		$SafeParamList = array();
		foreach ( $ParamList as $value ) {
			$SafeParamList[] = mysql_real_escape_string($value);
		}
		$SprintfArgList = array_merge(array($sqlf), $SafeParamList);
		return call_user_func_array('sprintf', $SprintfArgList);
	}


PEAR HTTP_Request2 docs api
<?php

/*
	HTTP_Request2 Demo
	
	Lib File Tree
	kw_pear\
		HTTP\
			Request2\
			Request2.php
		Net\
			URL2.php
		PEAR\
			Exception.php
		PEAR.php
*/

$wwwd = dirname(dirname(__FILE__));
$peard = sprintf('%s/%s', $wwwd, 'kw_pear');
$path_ext = implode(PATH_SEPARATOR, array(get_include_path(),
										  $peard));
# imports
ini_set('include_path', $path_ext); 
require_once 'HTTP/Request2.php';

$UrlList = array(
	'ok' => 'http://pear.php.net/package/HTTP_Request2/docs',
	'404' => 'http://pear.php.net/package/HTTP_Request2/docs/null',
	'fail' => 'bad url'
);

$TestList = array_keys($UrlList);
$test = $TestList[array_rand($TestList)];
$url = $UrlList[$test];
$Client = new HTTP_Request2();
$Client->setMethod(HTTP_Request2::METHOD_GET);
$Client->setUrl($url);

try {
	$Response = $Client->send();
	$code = $Response->getStatus();
	$code_class = floor($Response->getStatus() / 100);
}
catch (HTTP_Request2_Exception $e) {
	trigger_error(sprintf('http exception: %s',
						  $e->getMessage()), E_USER_ERROR);
}

if ( $code_class == 2 ) {
	$output = htmlspecialchars($Response->getBody());
}
else {
	$output = sprintf('unexpected response [%s]: %s',
					  $Response->getStatus(),
					  $Response->getReasonPhrase());
}

printf('<h4>test: %s</h4><pre>%s</pre>', $test, $output);

copy with exclude: rsync -rv --exclude=.svn source/* dest
wikka update page function:
function update_page($page_name, $page_body, $WikkaObj) {
	$updatef = "UPDATE %s SET latest='N' WHERE tag='%s'";
	$insertf = "INSERT INTO %s SET tag='%s', body='%s', owner='%s', user='%s', note='%s', time=NOW(), latest='Y'";
	$note = 'auto updating page';
	$user = '(system)';
	$table_prefix = $WikkaObj->GetConfigValue('table_prefix');
	$table_name = sprintf('%spages', $table_prefix);
	$page_name = mysql_real_escape_string($page_name);
	$page_body = mysql_real_escape_string($page_body); 
	
	if ( $WikkaObj->existsPage($page_name, $table_prefix) ) {
		# set all other revisions to old
		$is_updated = $WikkaObj->Query(sprintf($updatef, $table_name, $page_name));
	}
	
	# add new revision
	return $WikkaObj->Query( sprintf( $insertf,
									  $table_name,
									  $page_name,
									  $page_body,
									  '(Public)',
									  $user,
									  $note )); 

To call a script, you can make it as handler and put it at (as of 1.1.6.2) the directory named handlers/page/, ie like handlers/page/cron.changemail.php source
function log() {
	datestamp=$(date "+%Y-%m-%d %H:%M:%S")
	printf "[%s] %s\n" "$datestamp" "$1"
}

CACHE_FILE_COUNT=$(find /tmp -type f | wc -l)
if [ $CACHE_FILE_COUNT -gt 0 ]; then
	log "$CACHE_FILE_COUNT files found in /tmp"
else
	log "no files found in /tmp"
fi

Office Hacks: during ethnic holidays, go up to people at random and ask if they have any plans. (e.g.: week before passover, ask Chinese co-worker: "so do you have any Passover plans this weekend?")
Against the Gun is Civilization Argument: Pastebin20100226
Pattern Request Page: jquery json ajax request with cakephp
ycombo
fly bomber (it's supafly)
classic olympic fail
gdata python question on google groups
curious
copying DB in php
    function _switch_database($new_db) {
        $Dbo = ConnectionManager::getDataSource("default");
        $old_db = $Dbo->config['database'];
        $Dbo->reconnect(array('database'=>$new_db));
        return $old_db;
    }
   
    function _copy_database($db, $new_db) {
        $Dbo = ConnectionManager::getDataSource("default");
       
        # get tables
        $sql_show_tables_ = 'SHOW TABLES IN %s';
        $extract_pattern = sprintf('{n}.TABLE_NAMES.Tables_in_%s', $db);
        $ShowTables = $Dbo->query(sprintf($sql_show_tables_, $db));
        $TableList = Set::extract($ShowTables, $extract_pattern);
       
        # create new database
        $sql = sprintf('CREATE DATABASE %s', $new_db);
        $Dbo->query($sql);
       
        # copy tables
        $sql1_t = 'CREATE TABLE %s.%s LIKE %s.%s';
        $sql2_t = 'INSERT INTO %s.%s SELECT * FROM %s.%s';
        foreach ( $TableList as $table ) {
            $sql1 = sprintf($sql1_t, $new_db, $table, $db, $table);
            $sql2 = sprintf($sql2_t, $new_db, $table, $db, $table);
            $Dbo->query($sql1);
            $Dbo->query($sql2);
        }
       
        # test table lists
        $ShowTables = $Dbo->query(sprintf($sql_show_tables_, $db));
        $TableList1 = Set::extract($ShowTables, "{n}.TABLE_NAMES.Tables_in_$db");
       
        $ShowTables = $Dbo->query(sprintf($sql_show_tables_, $new_db));
        $TableList2 = Set::extract($ShowTables, "{n}.TABLE_NAMES.Tables_in_$new_db");
       
        if ( array_diff($TableList1, $TableList2) ) {
            trigger_error('table lists in new db dows not match old', E_USER_WARNING);
            return 0;
        }
        $this->Log_[] = "all tables in $db copied to $new_db";
       
        # test query: count all records and compare
        $old_db_count = 0;
        $new_db_count = 0;
        foreach ( $TableList as $table ) {
            $sql_ = 'SELECT COUNT(*) as count_ FROM %s.%s';
            $OldDb = $Dbo->query(sprintf($sql_, $db, $table));
            $NewDb = $Dbo->query(sprintf($sql_, $new_db, $table));
            $old_db_count += $OldDb[0][0]['count_'];
            $new_db_count += $NewDb[0][0]['count_'];
        }
        $rand_table = $TableList[array_rand($TableList)];
       
       
        if ( $old_db_count != $new_db_count ) {
            $mt_ = 'record count for new db (%s) and old db (%s) does not match';
            trigger_error(sprintf($mt_, $new_db_count, $old_db_count), E_USER_WARNING);
            return 0;
        }
        else {
            $mt_ = 'record count for new db (%s) and old db (%s)';
            $this->Log_[] = sprintf($mt_, $new_db_count, $old_db_count);
            return 1;
        }
    }


php encoding tests
requires http://hsivonen.iki.fi/php-utf8/php-utf8.tar.gz
<?php
    require('utf8.inc');

    $char = 'Ü';
    $h_char = htmlentities($char);
    $utf8 = htmlspecialchars($char, ENT_QUOTES, 'UTF-8');
    $h_utf8 = htmlentities($utf8);
    $uni_char = utf8ToUnicode($char);
    $data = array(
        '$char' => $char,
        '$h_char - htmlentities($char)' => $h_char,
        '$utf8 - htmlspecialchars($char,ENT_QUOTES,\'UTF-8\')' =>
            $utf8,
        'htmlspecialchars(\'Ü\',ENT_QUOTES,\'UTF-8\')' =>
            htmlspecialchars('Ü', ENT_QUOTES, 'UTF-8'),
        '$h_utf8 - htmlentities($utf8)' =>
            $h_utf8,
        'strlen($char)' => strlen($char),
        'strlen($h_char)' => strlen($h_char),
        'strlen($utf8)' => strlen($utf8),
        'strlen($h_utf8)' => strlen($h_utf8),
        'ord($char)' => ord($char),
        'ord($utf8)' => ord($utf8),
        'utf8ToUnicode($char)' => $uni_char,
        'dechex(ord($char))' => dechex(ord($char)),
        'sprintf(\'&amp;#x%s;\', dechex(ord($char)))' =>
            sprintf('&#x%s;', dechex(ord($char))),
        'sprintf(\'&amp;#x%s;\', ord($char))' =>
            sprintf('&#x%s;', ord($char)),
        'sprintf(\'&amp;#%s;\', $uni_char[0])' =>
            sprintf('&#%s;', $uni_char[0]),
        'sprintf("&#x%s;", dechex($uni_char[0]))' =>
            sprintf("&#x%s;", dechex($uni_char[0])),
        'iconv("UTF-8","ISO-8859-1//TRANSLIT",$char)' =>
             iconv("UTF-8","ISO-8859-1//TRANSLIT",$char),
    );
   
    $xhtml = <<<XHTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>sandbox</title></head>
<body>
<pre>
%s
</pre>    
</body>
</html>
XHTML;

    $charset = 'UTF-8'; #'ISO-8859-1';
    header( 'Content-type: text/html; charset='.$charset );
    printf($xhtml, print_r($data,1));
    var_dump($char);
    var_dump($h_char);
    var_dump($utf8);
    var_dump($h_utf8);


jquery quiz
== jQuiz: jQuirks ==
A short quiz on jQuery behavior.

===1. What will these lines of code produce?===
<code><pre>
var Test1El1 = $('div#Test1El1');
var Test1El2 = $('div').attr('id', 'Test1El2');
var Test1El3 = $('<div id="Test1El3" />');
console.log(Test1El1);
console.log(Test1El2);
console.log(Test1El3);
</pre></code>

===2. What will be the output of the code below?===
<code><pre>
var Test2El1 = $('<div id="Test2El1" />');
$(document.body).append(Test2El1);
var elid = Test2El1Test2El1.attr('id');
var Test2El2 = $(elid);
console.log(Test2El1);
console.log(elid);
console.log(Test2El2);
</pre></code>

===3. True or False===
<code><pre>
var id_ = 'test3id';
$('<div id="' + id_ + '" />').appendTo(document.body);
var Test3El1 = document.getElementById(id_);
var Test3El2 = $('#'+id_);
console.log(Test3El1.id == Test3El2.id);
</pre></code>

===4. True or False===
<code><pre>
var Test4El1 = $('<div id="Test4El" />').appendTo(document.body);
var Test4El2 = $(Test4El1);
console.log(Test4El1 == Test4El2);
console.log(Test4El1.attr('id') == Test4El2.attr('id'));
</pre></code>

===Given a document with the following table code, answer the next 2 questions:===
<code><pre>
<table id="Test5Table">
  <tr id="Test5Tr1"><td>cell1</td><td>cell2</td></tr>
  <tr id="Test5Tr2"><td>cell1</td><td>cell2</td></tr>
</table>

var Test5Table = $('<table id="Test5Table"><tr id="tr1"><td>cell1</td><td>cell2</td></tr><tr id="tr2"><td>cell1</td><td>cell2</td></tr></table>');
$(document.body).append(Test5Table);
</pre></code>

====5. What will the value of TableRows[0] be below?====
<code><pre>
var Test5Table = $('table#Test5Table');
var TableRows = Test5Table.find('tr');
console.log(TableRows[0].attr('id'));
</pre></code>

====6. What will the value of RowIds[0] be below?====
<code><pre>
var Test5Table = $('table#Test5Table');
var TableRows = Test5Table.find('tr');
var RowIds = [];
TableRows.each( function(i, El) {
  RowIds.push(El.attr('id'));
});
console.log(RowIds[0]);
</pre></code>

===7. What will the value of FindEl be below?===
<code><pre>
// build and add element to our document
var elid = 'formbid-0.field';
var NewDiv = $('<div id="'+elid+'" />').text(elid);
$(document.body).append(NewDiv);

var jqid = NewDiv.attr('id');
var FindEl = $(document.body).find('#'+jqid);    // note # sign
console.log(jqid, FindEl);
</pre></code>

<div style="display:none;">Answer:
<code><pre>
jesc = jqid.replace('.', '\\.');
var FindEl = $(document.body).find('#'+jesc);
console.log(jesc, FindEl);
</pre></code>
</div>


music on-demand
this + http://www.dizzler.com/ + http://getfirebug.com/net.html

some cakephp code
	function render($action=NULL, $layout=NULL, $file=NULL) {
		if ( $action == null ) {
			$action = $this->view_action;
		}
		$output = parent::render($action, $layout, $file);
		return $output;
	}

	function index()
	{
		$m = 'null';
		if ( isset($this->params['pass'][0]) ) {
			$m = $this->params['pass'][0];
		}
		#debug($this->params);
		#debug($m);
		
		if ( method_exists($this, $m) ) {
			$this->view_action = sprintf('/journal/%s', $m);
			return $this->$m();
		}
		else {
			$this->set('error', 'page_not_found');
		}
	}




CategorySpecial