Permalink
Browse files

Update uses of deprecated globals with TestSwarmContext

- All instances of $swarmBrowser replaced, global removed.
- All instances of $swarmConfig replaced, global removed.
- All instances of $swarmDB replaced, global removed.
- All instances of $swarmRequest replaced, global removed.
  • Loading branch information...
1 parent a9e49fa commit 1aaa420351294389a71bc25df4423ea74fc4d54a @Krinkle committed Mar 22, 2012
Showing with 109 additions and 84 deletions.
  1. +12 −7 content/home.php
  2. +5 −2 content/run.php
  3. +1 −1 content/tinder.php
  4. +1 −0 inc/Database.php
  5. +3 −3 inc/TestSwarm.php
  6. +22 −17 inc/init-usersession.php
  7. +12 −17 inc/init.php
  8. +7 −4 inc/utilities.php
  9. +5 −5 index.php
  10. +5 −3 logic/addjob.php
  11. +6 −4 logic/getrun.php
  12. +5 −3 logic/login.php
  13. +4 −2 logic/run.php
  14. +13 −10 logic/saverun.php
  15. +7 −5 logic/signup.php
  16. +1 −1 logic/tinder.php
View
@@ -61,11 +61,13 @@
/** @return bool: Whether the current user was found in the swarm */
function loadBrowsers($headingTitle, $mobile) {
- global $swarmBrowser, $swarmDB;
+ global $swarmContext;
+ $bi = $swarmContext->getBrowserInfo();
+ $db = $swarmContext->getDB();
$foundSelf = false;
- $rows = $swarmDB->getRows(str_queryf(
+ $rows = $db->getRows(str_queryf(
"SELECT
useragents.engine as engine,
useragents.name as name,
@@ -82,8 +84,8 @@ function loadBrowsers($headingTitle, $mobile) {
AND mobile = %s
ORDER BY engine, name;",
swarmdb_dateformat( strtotime( '1 minute ago' ) ),
- $swarmBrowser->getBrowserCodename(),
- $swarmBrowser->getBrowserVersion(),
+ $bi->getBrowserCodename(),
+ $bi->getBrowserVersion(),
$mobile
));
@@ -116,23 +118,26 @@ function loadBrowsers($headingTitle, $mobile) {
return $foundSelf;
}
+$request = $swarmContext->getRequest();
+$bi = $swarmContext->getBrowserInfo();
+
if ( $found ) { ?>
<div class="join">
<p><strong>TestSwarm Needs Your Help!</strong> You have a browser that we need to test against, you should join the swarm to help us out.</p>
- <?php if ( !$swarmRequest->getSessionData( "username" ) ) { ?>
+ <?php if ( !$request->getSessionData( "username" ) ) { ?>
<form action="" method="get">
<input type="hidden" name="state" value="run"/>
<br/><strong>Username:</strong><br/>
<input type="text" name="user" value=""/>
<input type="submit" value="Join the Swarm"/>
</form>
<?php } else { ?>
- <br/><p><strong>&raquo; <?php echo $swarmRequest->getSessionData( "username" ); ?></strong> <a href="<?php echo swarmpath("run/{$swarmRequest->getSessionData( "username" )}/" ); ?>">Start Running Tests</a></p>
+ <br/><p><strong>&raquo; <?php echo $request->getSessionData( "username" ); ?></strong> <a href="<?php echo swarmpath("run/{$request->getSessionData( "username" )}/" ); ?>">Start Running Tests</a></p>
<?php } ?>
</div>
<?php } else { ?>
<div class="join">
<p>TestSwarm doesn't need your help at this time. If you wish to help run tests you should load up one of the below browsers.</p>
- <p>If you feel that this may be a mistake, copy the following information (<?php echo $swarmBrowser->getBrowserCodename(); ?> <?php echo $swarmBrowser->getBrowserVersion(); ?> <?php echo $swarmBrowser->getOsCodename(); ?>) and your <a href="http://useragentstring.com/">useragent string</a>, and post it to the <a href="//groups.google.com/group/testswarm">discussion group</a>.</a>
+ <p>If you feel that this may be a mistake, copy the following information (<?php echo $bi->getBrowserCodename(); ?> <?php echo $bi->getBrowserVersion(); ?> <?php echo $bi->getOsCodename(); ?>) and your <a href="http://useragentstring.com/">useragent string</a>, and post it to the <a href="//groups.google.com/group/testswarm">discussion group</a>.</a>
</div>
<?php }
View
@@ -1,7 +1,10 @@
+<?php
+$bi = $swarmContext->getBrowserInfo();
+?>
<div class="userinfo">
<div class="browser you">
- <img src="<?php echo swarmpath( "images/{$swarmBrowser->getBrowserCodename()}.sm.png" ); ?>" class="browser-icon <?php echo $swarmBrowser->getBrowserCodename(); ?>" alt="<?php echo $swarmBrowser->getSwarmUserAgentName(); ?>" title="<?php echo $swarmBrowser->getSwarmUserAgentName(); ?>"/>
- <span class="browser-name"><?php echo preg_replace('/\w+ /', "", $swarmBrowser->getSwarmUserAgentName()); ?></span>
+ <img src="<?php echo swarmpath( "images/{$bi->getBrowserCodename()}.sm.png" ); ?>" class="browser-icon <?php echo $bi->getBrowserCodename(); ?>" alt="<?php echo $bi->getSwarmUserAgentName(); ?>" title="<?php echo $bi->getSwarmUserAgentName(); ?>"/>
+ <span class="browser-name"><?php echo preg_replace('/\w+ /', "", $bi->getSwarmUserAgentName()); ?></span>
</div>
<h3><?php echo $username; ?></h3>
View
@@ -98,7 +98,7 @@ function get_status2($num, $fail, $error, $total){
}
- $job_search = preg_replace( "/[^a-zA-Z ]/", "", $swarmRequest->getVal( "job", "" ) );
+ $job_search = preg_replace( "/[^a-zA-Z ]/", "", $swarmContext->getRequest()->getVal( "job", "" ) );
$job_search .= "%";
$search_result = mysql_queryf(
View
@@ -110,6 +110,7 @@ public function getNumRows( $res ) {
/**
* Queries other than SELECT, such as DELETE, UPDATE and INSERT.
+ * @return resource|false
*/
public function query( $sql ) {
return $this->doQuery( $sql );
View
@@ -16,11 +16,11 @@ class TestSwarmContext {
* needs to be passed is an array with all setting keys from testswarm.ini
* (including ones commented out in the sample file, it has to contain them all)
* Population of default values of optional settings happens in init.php
- * @param $swarmConfig array
+ * @param $config array
*/
- public function __construct( Array $swarmConfig ) {
+ public function __construct( Array $config ) {
$conf = new stdClass;
- foreach ( $swarmConfig as $key => $val ) {
+ foreach ( $config as $key => $val ) {
$conf->$key = is_array( $val ) ? (object)$val : $val;
}
$this->conf = $conf;
@@ -2,17 +2,22 @@
/**
* Initialize global variables related to a user,
* for requests that require a username.
- * Sets the following globals:
+ *
+ * @todo Deprecate these globals:
* - $username
* - $user_id
* - $client_id
*
* @since 0.1.0
* @package TestSwarm
*/
- global $swarmBrowser, $swarmDB, $swarmRequest;
+ global $swarmContext;
+
+ $bi = $swarmContext->getBrowserInfo();
+ $db = $swarmContext->getDB();
+ $request = $swarmContext->getRequest();
- $username = $swarmRequest->getSessionData( "username", $swarmRequest->getVal( "user" ) );
+ $username = $request->getSessionData( "username", $request->getVal( "user" ) );
if ( !$username ) {
$username = $_REQUEST["user"];
}
@@ -27,13 +32,13 @@
exit( "Username required. ?user=USERNAME." );
}
- $client_id = preg_replace( "/[^0-9]/", "", $swarmRequest->getVal( "client_id" ) );
+ $client_id = preg_replace( "/[^0-9]/", "", $request->getVal( "client_id" ) );
// Client passed
if ( $client_id ) {
// Verify that the client exists,
// And get the user ID.
- $user_id = $swarmDB->getOne(str_queryf(
+ $user_id = $db->getOne(str_queryf(
"SELECT
user_id
FROM
@@ -46,7 +51,7 @@
if ( $user_id ) {
// If the client ID is already provided, update its record so
// that we know that it's still alive
- $swarmDB->query(str_queryf(
+ $db->query(str_queryf(
"UPDATE clients SET updated=%s WHERE id=%u LIMIT 1;",
swarmdb_dateformat( SWARM_NOW ),
$client_id
@@ -62,37 +67,37 @@
} else {
// If the useragent isn't known, abort with an error message
- if ( !$swarmBrowser->isKnownInTestSwarm() ) {
+ if ( !$bi->isKnownInTestSwarm() ) {
echo "Your browser is not supported for testing right now.\n"
- . "Browser: {$swarmBrowser->getBrowserCodename()} Version: {$swarmBrowser->getBrowserVersion()}";
+ . "Browser: {$bi->getBrowserCodename()} Version: {$bi->getBrowserVersion()}";
exit;
}
// Figure out what the user's ID number is
- $user_id = $swarmDB->getOne(str_queryf( "SELECT id FROM users WHERE name=%s;", $username ));
+ $user_id = $db->getOne(str_queryf( "SELECT id FROM users WHERE name=%s;", $username ));
// If the user doesn't have one, create a new user account
if ( !$user_id ) {
- $swarmDB->query(str_queryf(
+ $db->query(str_queryf(
"INSERT INTO users (name, created, updated, seed) VALUES(%s, %s, %s, RAND());",
$username,
swarmdb_dateformat( SWARM_NOW ),
swarmdb_dateformat( SWARM_NOW )
));
- $user_id = $swarmDB->getInsertId();
+ $user_id = $db->getInsertId();
}
// Insert in a new record for the client and get its ID
- $swarmDB->query(str_queryf(
+ $db->query(str_queryf(
"INSERT INTO clients (user_id, useragent_id, useragent, os, ip, created)
VALUES(%u, %u, %s, %s, %s, %s);",
$user_id,
- $swarmBrowser->getSwarmUserAgentID(),
- $swarmBrowser->getRawUA(),
- $swarmBrowser->getOsCodename(),
- $swarmRequest->getIP(),
+ $bi->getSwarmUserAgentID(),
+ $bi->getRawUA(),
+ $bi->getOsCodename(),
+ $request->getIP(),
swarmdb_dateformat( SWARM_NOW )
));
- $client_id = $swarmDB->getInsertId();
+ $client_id = $db->getInsertId();
}
View
@@ -89,15 +89,24 @@
/**
+ * Context
+ * @{
+ */
+$swarmContext = new TestSwarmContext( $swarmConfig );
+
+/**@}*/
+
+
+/**
* Debugging
* @{
*/
function swarmExceptionHandler( Exception $e ) {
- global $swarmConfig;
+ global $swarmContext;
$msg = "<h2>TestSwarm internal error</h2>\n\n";
- if ( $swarmConfig["debug"]["show_exception_details"] ) {
+ if ( $swarmContext->getConf()->debug->show_exception_details ) {
$msg .=
'<p>' . nl2br( htmlspecialchars( $e->getMessage() ) ) .
'</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $e->getTraceAsString() ) ) .
@@ -118,7 +127,7 @@ function swarmExceptionHandler( Exception $e ) {
set_exception_handler( "swarmExceptionHandler" );
-if ( $swarmConfig["debug"]["php_error_reporting"] ) {
+if ( $swarmContext->getConf()->debug->php_error_reporting ) {
error_reporting( E_ALL );
ini_set( "display_errors", 1 );
}
@@ -127,20 +136,6 @@ function swarmExceptionHandler( Exception $e ) {
/**
- * Context
- * @{
- */
-$swarmContext = new TestSwarmContext( $swarmConfig );
-
-// Set globals for backwards compatibility
-$swarmDB = $swarmContext->getDB();
-$swarmRequest = $swarmContext->getRequest();
-$swarmBrowser = $swarmContext->getBrowserInfo();
-
-/**@}*/
-
-
-/**
* Session
* @{
*/
View
@@ -40,10 +40,12 @@ function array_extend( $arr1, $arr2 ) {
* @source php.net/mysql-query#86447
*/
function mysql_queryf(/* $string, $arg, .. */) {
+ global $swarmContext;
+
$args = func_get_args();
$sql_query = call_user_func_array( 'str_queryf', $args );
- $result = mysql_query( $sql_query );
+ $result = $swarmContext->getDB()->query( $sql_query );
if (!$result) {
echo "Invalid query: " . mysql_error();
exit;
@@ -137,14 +139,14 @@ function swarmdb_dateformat( $timestamp = 0 ) {
* @return string Relative path from the domain root to the specified file or directory
*/
function swarmpath( $rel ) {
- global $swarmConfig;
+ global $swarmContext;
// Only normalize the contextpath once
static $contextpath;
if ( is_null( $contextpath ) ) {
// Add trailing slash if it's missing
- $path = $swarmConfig["web"]["contextpath"];
+ $path = $swarmContext->getConf()->web->contextpath;
if ( substr( $path, -1 ) !== '/' ) {
$path = "$path/";
}
@@ -156,7 +158,8 @@ function swarmpath( $rel ) {
$path = "/$path";
}
- $swarmConfig["web"]["contextpath"] = $path;
+ // Update it (just in case it's used elsewhere)
+ $swarmContext->getConf()->web->contextpath = $path;
}
// Just in case, strip the leading slash
View
@@ -12,7 +12,7 @@
require_once "inc/init.php";
-$state = preg_replace("/[^a-z]/", "", $swarmRequest->getVal( "state", "" ) );
+$state = preg_replace("/[^a-z]/", "", $swarmContext->getRequest()->getVal( "state", "" ) );
if ( !$state ) {
$state = "home";
@@ -38,15 +38,15 @@
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
- <title><?php echo htmlentities( $swarmConfig['web']['title'] . ': ' . $title ); ?></title>
+ <title><?php echo htmlentities( $swarmContext->getConf()->web->title . ': ' . $title ); ?></title>
<link rel="stylesheet" href="<?php echo swarmpath( "css/site.css" ); ?>"/>
<script>window.SWARM = <?php echo json_encode( array(
- // Derived version of $swarmConfig for the browser
- // (not the entire array since it also contains DB passwords!)
+ // Export a simplified version of the TestSwarm configuration object to the browser
+ // (not the entire object since it also contains DB password and such..).
"web" => array(
"contextpath" => swarmpath( "" ),
),
- "client" => $swarmConfig["client"],
+ "client" => $swarmContext->getConf()->client,
) ); ?>;</script>
<?php
echo isset( $scripts ) ? "\t" . $scripts . "\n" : "";
View
@@ -2,12 +2,14 @@
$title = "Add New Job";
- if ( $swarmRequest->getVal( "state" ) !== "addjob" || !$swarmRequest->hasKeys( "user", "auth", "job_name" ) ) {
+ $request = $swarmContext->getRequest();
+
+ if ( $request->getVal( "state" ) !== "addjob" || !$request->hasKeys( "user", "auth", "job_name" ) ) {
return;
}
- $username = $swarmRequest->getVal( "user", "" );
- $auth = $swarmRequest->getVal( "auth", "" );
+ $username = $request->getVal( "user", "" );
+ $auth = $request->getVal( "auth", "" );
$user_id = null;
$result = mysql_queryf("SELECT id FROM users WHERE name=%s AND auth=%s;", $username, $auth);
View
@@ -1,7 +1,9 @@
<?php
require "inc/init-usersession.php";
- global $swarmConfig;
+ global $swarmContext;
+
+ $bi = $swarmContext->getBrowserInfo();
$result = mysql_queryf(
"SELECT
@@ -13,7 +15,7 @@
AND NOT EXISTS (SELECT 1 FROM run_client WHERE run_useragent.run_id = run_id AND client_id = %u)
ORDER BY run_id DESC
LIMIT 1;",
- $swarmBrowser->getSwarmUserAgentID(),
+ $bi->getSwarmUserAgentID(),
$client_id
);
@@ -45,7 +47,7 @@
mysql_queryf(
"UPDATE run_useragent SET runs = runs + 1, status = 1 WHERE run_id=%u AND useragent_id=%u LIMIT 1;",
$run_id,
- $swarmBrowser->getSwarmUserAgentID()
+ $bi->getSwarmUserAgentID()
);
# Initialize the client run
@@ -66,7 +68,7 @@
}
echo json_encode( array(
- "swarmUpdate" => array( "client" => $swarmConfig["client"] ),
+ "swarmUpdate" => array( "client" => $swarmContext->getConf()->client ),
"runInfo" => $runInfo,
) );
Oops, something went wrong.

0 comments on commit 1aaa420

Please sign in to comment.