more global removal

This commit is contained in:
Shish 2015-08-02 20:39:41 +01:00
parent c17250b6b9
commit 18490ed488
9 changed files with 55 additions and 60 deletions

View file

@ -23,9 +23,6 @@
* Classes *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
$_flexihash = null;
$_fh_last_opts = null;
$order_sql = null; // this feels ugly
require_once "lib/flexihash.php";
@ -40,6 +37,7 @@ require_once "lib/flexihash.php";
*/
class Image {
private static $tag_n = 0; // temp hack
public static $order_sql = null; // this feels ugly
/** @var null|int */
public $id = null;
@ -148,7 +146,7 @@ class Image {
assert('is_numeric($start)');
assert('is_numeric($limit)');
assert('is_array($tags)');
global $database, $user, $config, $order_sql;
global $database, $user, $config;
$images = array();
@ -168,7 +166,7 @@ class Image {
if(!$result) {
$querylet = Image::build_search_querylet($tags);
$querylet->append(new Querylet(" ORDER BY ".($order_sql ?: "images.".$config->get_string("index_order"))));
$querylet->append(new Querylet(" ORDER BY ".(Image::$order_sql ?: "images.".$config->get_string("index_order"))));
$querylet->append(new Querylet(" LIMIT :limit OFFSET :offset", array("limit"=>$limit, "offset"=>$start)));
#var_dump($querylet->sql); var_dump($querylet->variables);
$result = $database->execute($querylet->sql, $querylet->variables);
@ -177,7 +175,7 @@ class Image {
while($row = $result->fetch()) {
$images[] = new Image($row);
}
$order_sql = null;
Image::$order_sql = null;
return $images;
}
@ -689,16 +687,17 @@ class Image {
$tmpl = $plte->link;
}
global $_flexihash, $_fh_last_opts;
static $flexihash = null;
static $fh_last_opts = null;
$matches = array();
if(preg_match("/(.*){(.*)}(.*)/", $tmpl, $matches)) {
$pre = $matches[1];
$opts = $matches[2];
$post = $matches[3];
if($opts != $_fh_last_opts) {
$_fh_last_opts = $opts;
$_flexihash = new Flexihash();
if($opts != $fh_last_opts) {
$fh_last_opts = $opts;
$flexihash = new Flexihash();
foreach(explode(",", $opts) as $opt) {
$parts = explode("=", $opt);
$parts_count = count($parts);
@ -712,11 +711,11 @@ class Image {
$opt_val = $parts[0];
$opt_weight = 1;
}
$_flexihash->addTarget($opt_val, $opt_weight);
$flexihash->addTarget($opt_val, $opt_weight);
}
}
$choice = $_flexihash->lookup($pre.$post);
$choice = $flexihash->lookup($pre.$post);
$tmpl = $pre.$choice.$post;
}

View file

@ -49,7 +49,7 @@ class User {
* @throws SCoreException
*/
public function __construct($row) {
global $_user_classes;
global $_shm_user_classes;
$this->id = int_escape($row['id']);
$this->name = $row['name'];
@ -57,8 +57,8 @@ class User {
$this->join_date = $row['joindate'];
$this->passhash = $row['pass'];
if(array_key_exists($row["class"], $_user_classes)) {
$this->class = $_user_classes[$row["class"]];
if(array_key_exists($row["class"], $_shm_user_classes)) {
$this->class = $_shm_user_classes[$row["class"]];
}
else {
throw new SCoreException("User '{$this->name}' has invalid class '{$row["class"]}'");

View file

@ -1,9 +1,9 @@
<?php
/**
* @global UserClass[] $_user_classes
* @global UserClass[] $_shm_user_classes
*/
global $_user_classes;
$_user_classes = array();
global $_shm_user_classes;
$_shm_user_classes = array();
/**
* Class UserClass
@ -31,16 +31,16 @@ class UserClass {
* @param array $abilities
*/
public function __construct($name, $parent=null, $abilities=array()) {
global $_user_classes;
global $_shm_user_classes;
$this->name = $name;
$this->abilities = $abilities;
if(!is_null($parent)) {
$this->parent = $_user_classes[$parent];
$this->parent = $_shm_user_classes[$parent];
}
$_user_classes[$name] = $this;
$_shm_user_classes[$name] = $this;
}
/**
@ -61,10 +61,10 @@ class UserClass {
return $this->parent->can($ability);
}
else {
global $_user_classes;
global $_shm_user_classes;
$min_dist = 9999;
$min_ability = null;
foreach($_user_classes['base']->abilities as $a => $cando) {
foreach($_shm_user_classes['base']->abilities as $a => $cando) {
$v = levenshtein($ability, $a);
if($v < $min_dist) {
$min_dist = $v;

View file

@ -301,8 +301,8 @@ function validate_input($inputs) {
$outputs[$key] = $value;
}
else if(in_array('user_class', $flags)) {
global $_user_classes;
if(!array_key_exists($value, $_user_classes)) {
global $_shm_user_classes;
if(!array_key_exists($value, $_shm_user_classes)) {
throw new InvalidInput("Invalid user class: ".html_escape($class));
}
$outputs[$key] = $value;
@ -700,14 +700,14 @@ function getExtension ($mime_type){
}
$_execs = 0;
$_shm_execs = 0;
/**
* $db is the connection object
*
* @private
*/
function _count_execs($db, $sql, $inputarray) {
global $_execs;
global $_shm_execs;
if ((defined('DEBUG_SQL') && DEBUG_SQL === true) || (!defined('DEBUG_SQL') && @$_GET['DEBUG_SQL'])) {
$fp = @fopen("data/sql.log", "a");
if($fp) {
@ -729,10 +729,10 @@ function _count_execs($db, $sql, $inputarray) {
#log_error("core", "failed to open sql.log for appending");
}
}
if (!is_array($inputarray)) $_execs++;
if (!is_array($inputarray)) $_shm_execs++;
# handle 2-dimensional input arrays
else if (is_array(reset($inputarray))) $_execs += sizeof($inputarray);
else $_execs++;
else if (is_array(reset($inputarray))) $_shm_execs += sizeof($inputarray);
else $_shm_execs++;
# in PHP4.4 and PHP5, we need to return a value by reference
$null = null; return $null;
}
@ -1137,24 +1137,23 @@ function log_error( /*string*/ $section, /*string*/ $message, $flash=false, $a
function log_critical(/*string*/ $section, /*string*/ $message, $flash=false, $args=array()) {log_msg($section, SCORE_LOG_CRITICAL, $message, $flash, $args);}
$_request_id = null;
/**
* Get a unique ID for this request, useful for grouping log messages.
*
* @return null|string
*/
function get_request_id() {
global $_request_id;
if(!$_request_id) {
static $request_id = null;
if(!$request_id) {
// not completely trustworthy, as a user can spoof this
if(@$_SERVER['HTTP_X_VARNISH']) {
$_request_id = $_SERVER['HTTP_X_VARNISH'];
$request_id = $_SERVER['HTTP_X_VARNISH'];
}
else {
$_request_id = "P" . uniqid();
$request_id = "P" . uniqid();
}
}
return $_request_id;
return $request_id;
}
@ -1400,9 +1399,10 @@ function add_event_listener(Extension $extension, $pos=50, $events=array()) {
}
}
/** @private */
global $_event_count;
$_event_count = 0;
global $_shm_event_count;
$_shm_event_count = 0;
/**
* Send an event to all registered Extensions.
@ -1410,7 +1410,7 @@ $_event_count = 0;
* @param Event $event
*/
function send_event(Event $event) {
global $_event_listeners, $_event_count;
global $_event_listeners, $_shm_event_count;
if(!isset($_event_listeners[get_class($event)])) return;
$method_name = "on".str_replace("Event", "", get_class($event));
@ -1425,7 +1425,7 @@ function send_event(Event $event) {
}
ctx_log_endok();
}
$_event_count++;
$_shm_event_count++;
ctx_log_endok();
}
@ -1437,7 +1437,7 @@ function send_event(Event $event) {
// SHIT by default this returns the time as a string. And it's not even a
// string representation of a number, it's two numbers separated by a space.
// What the fuck were the PHP developers smoking.
$_load_start = microtime(true);
$_shm_load_start = microtime(true);
/**
* Collects some debug information (execution time, memory usage, queries, etc)
@ -1446,7 +1446,7 @@ $_load_start = microtime(true);
* @return string debug info to add to the page.
*/
function get_debug_info() {
global $config, $_event_count, $database, $_execs, $_load_start;
global $config, $_shm_event_count, $database, $_shm_execs, $_shm_load_start;
$i_mem = sprintf("%5.2f", ((memory_get_peak_usage(true)+512)/1024)/1024);
@ -1456,15 +1456,15 @@ function get_debug_info() {
else {
$commit = " (".$config->get_string("commit_hash").")";
}
$time = sprintf("%.2f", microtime(true) - $_load_start);
$time = sprintf("%.2f", microtime(true) - $_shm_load_start);
$dbtime = sprintf("%.2f", $database->dbtime);
$i_files = count(get_included_files());
$hits = $database->cache->get_hits();
$miss = $database->cache->get_misses();
$debug = "<br>Took $time seconds (db:$dbtime) and {$i_mem}MB of RAM";
$debug .= "; Used $i_files files and $_execs queries";
$debug .= "; Sent $_event_count events";
$debug .= "; Used $i_files files and $_shm_execs queries";
$debug .= "; Sent $_shm_event_count events";
$debug .= "; $hits cache hits and $miss misses";
$debug .= "; Shimmie version ". VERSION . $commit; // .", SCore Version ". SCORE_VERSION;

View file

@ -34,7 +34,6 @@ class ET extends Extension {
*/
private function get_info() {
global $config, $database;
global $_event_listeners; // yay for using secret globals \o/
$info = array();
$info['site_title'] = $config->get_string("title");

View file

@ -377,19 +377,17 @@ class Index extends Extension {
$event->add_querylet(new Querylet("height $cmp :height{$this->stpen}",array("height{$this->stpen}"=>int_escape($matches[2]))));
}
else if(preg_match("/^order[=|:](id|width|height|filesize|filename)[_]?(desc|asc)?$/i", $event->term, $matches)){
global $order_sql;
$ord = strtolower($matches[1]);
$default_order_for_column = preg_match("/^(id|filename)$/", $matches[1]) ? "ASC" : "DESC";
$sort = isset($matches[2]) ? strtoupper($matches[2]) : $default_order_for_column;
$order_sql = "images.$ord $sort";
Image::$order_sql = "images.$ord $sort";
$event->add_querylet(new Querylet("1=1")); //small hack to avoid metatag being treated as normal tag
}
else if(preg_match("/^order[=|:]random[_]([0-9]{1,4})$/i", $event->term, $matches)){
global $order_sql;
//order[=|:]random requires a seed to avoid duplicates
//since the tag can't be changed during the parseevent, we instead generate the seed during submit using js
$seed = $matches[1];
$order_sql = "RAND($seed)";
Image::$order_sql = "RAND($seed)";
$event->add_querylet(new Querylet("1=1")); //small hack to avoid metatag being treated as normal tag
}

View file

@ -240,10 +240,9 @@ class NumericScore extends Extension {
array("ns_user_id"=>$iid)));
}
else if(preg_match("/^order[=|:](numeric_)?(score)[_]?(desc|asc)?$/i", $event->term, $matches)){
global $order_sql;
$default_order_for_column = "DESC";
$sort = isset($matches[3]) ? strtoupper($matches[3]) : $default_order_for_column;
$order_sql = "images.numeric_score $sort";
Image::$order_sql = "images.numeric_score $sort";
$event->add_querylet(new Querylet("1=1")); //small hack to avoid metatag being treated as normal tag
}
}

View file

@ -19,15 +19,15 @@ class StatsDInterface extends Extension {
public static $stats = array();
private function _stats($type) {
global $config, $_event_count, $database, $_execs, $_load_start;
$time = microtime(true) - $_load_start;
global $config, $_shm_event_count, $database, $_shm_execs, $_shm_load_start;
$time = microtime(true) - $_shm_load_start;
StatsDInterface::$stats["shimmie.$type.hits"] = "1|c";
StatsDInterface::$stats["shimmie.$type.time"] = "$time|ms";
StatsDInterface::$stats["shimmie.$type.time-db"] = "{$database->dbtime}|ms";
StatsDInterface::$stats["shimmie.$type.memory"] = memory_get_peak_usage(true)."|c";
StatsDInterface::$stats["shimmie.$type.files"] = count(get_included_files())."|c";
StatsDInterface::$stats["shimmie.$type.queries"] = $_execs."|c";
StatsDInterface::$stats["shimmie.$type.events"] = $_event_count."|c";
StatsDInterface::$stats["shimmie.$type.queries"] = $_shm_execs."|c";
StatsDInterface::$stats["shimmie.$type.events"] = $_shm_event_count."|c";
StatsDInterface::$stats["shimmie.$type.cache-hits"] = $database->cache->get_hits()."|c";
StatsDInterface::$stats["shimmie.$type.cache-misses"] = $database->cache->get_misses()."|c";
}
@ -51,8 +51,8 @@ class StatsDInterface extends Extension {
$this->_stats("rss");
}
else {
#global $_load_start;
#$time = microtime(true) - $_load_start;
#global $_shm_load_start;
#$time = microtime(true) - $_shm_load_start;
#file_put_contents("data/other.log", "{$_SERVER['REQUEST_URI']} $time\n", FILE_APPEND);
$this->_stats("other");
}

View file

@ -208,9 +208,9 @@ class UserPageTheme extends Themelet {
$i_user_id = int_escape($duser->id);
if($user->can("edit_user_class")) {
global $_user_classes;
global $_shm_user_classes;
$class_html = "";
foreach($_user_classes as $name => $values) {
foreach($_shm_user_classes as $name => $values) {
$h_name = html_escape($name);
$h_title = html_escape(ucwords($name));
$h_selected = ($name == $duser->class->name ? " selected" : "");