summaryrefslogtreecommitdiff
path: root/classes
diff options
context:
space:
mode:
Diffstat (limited to 'classes')
-rw-r--r--classes/File.php4
-rw-r--r--classes/Memcached_DataObject.php68
-rw-r--r--classes/Safe_DataObject.php24
-rw-r--r--classes/Subscription.php8
4 files changed, 99 insertions, 5 deletions
diff --git a/classes/File.php b/classes/File.php
index 33273bbdc..c9477f5f1 100644
--- a/classes/File.php
+++ b/classes/File.php
@@ -286,7 +286,9 @@ class File extends Memcached_DataObject
if(! isset($this->filename)){
$notEnclosureMimeTypes = array(null,'text/html','application/xhtml+xml');
- $mimetype = strtolower($this->mimetype);
+ if($mimetype != null){
+ $mimetype = strtolower($this->mimetype);
+ }
$semicolon = strpos($mimetype,';');
if($semicolon){
$mimetype = substr($mimetype,0,$semicolon);
diff --git a/classes/Memcached_DataObject.php b/classes/Memcached_DataObject.php
index 920966b16..0836c2019 100644
--- a/classes/Memcached_DataObject.php
+++ b/classes/Memcached_DataObject.php
@@ -330,6 +330,10 @@ class Memcached_DataObject extends Safe_DataObject
*/
function _query($string)
{
+ if (common_config('db', 'annotate_queries')) {
+ $string = $this->annotateQuery($string);
+ }
+
$start = microtime(true);
$result = parent::_query($string);
$delta = microtime(true) - $start;
@@ -342,6 +346,70 @@ class Memcached_DataObject extends Safe_DataObject
return $result;
}
+ /**
+ * Find the first caller in the stack trace that's not a
+ * low-level database function and add a comment to the
+ * query string. This should then be visible in process lists
+ * and slow query logs, to help identify problem areas.
+ *
+ * Also marks whether this was a web GET/POST or which daemon
+ * was running it.
+ *
+ * @param string $string SQL query string
+ * @return string SQL query string, with a comment in it
+ */
+ function annotateQuery($string)
+ {
+ $ignore = array('annotateQuery',
+ '_query',
+ 'query',
+ 'get',
+ 'insert',
+ 'delete',
+ 'update',
+ 'find');
+ $ignoreStatic = array('staticGet',
+ 'pkeyGet',
+ 'cachedQuery');
+ $here = get_class($this); // if we get confused
+ $bt = debug_backtrace();
+
+ // Find the first caller that's not us?
+ foreach ($bt as $frame) {
+ $func = $frame['function'];
+ if (isset($frame['type']) && $frame['type'] == '::') {
+ if (in_array($func, $ignoreStatic)) {
+ continue;
+ }
+ $here = $frame['class'] . '::' . $func;
+ break;
+ } else if (isset($frame['type']) && $frame['type'] == '->') {
+ if ($frame['object'] === $this && in_array($func, $ignore)) {
+ continue;
+ }
+ if (in_array($func, $ignoreStatic)) {
+ continue; // @fixme this shouldn't be needed?
+ }
+ $here = get_class($frame['object']) . '->' . $func;
+ break;
+ }
+ $here = $func;
+ break;
+ }
+
+ if (php_sapi_name() == 'cli') {
+ $context = basename($_SERVER['PHP_SELF']);
+ } else {
+ $context = $_SERVER['REQUEST_METHOD'];
+ }
+
+ // Slip the comment in after the first command,
+ // or DB_DataObject gets confused about handling inserts and such.
+ $parts = explode(' ', $string, 2);
+ $parts[0] .= " /* $context $here */";
+ return implode(' ', $parts);
+ }
+
// Sanitize a query for logging
// @fixme don't trim spaces in string literals
function sanitizeQuery($string)
diff --git a/classes/Safe_DataObject.php b/classes/Safe_DataObject.php
index 08bc6846f..e926cb0d5 100644
--- a/classes/Safe_DataObject.php
+++ b/classes/Safe_DataObject.php
@@ -96,6 +96,30 @@ class Safe_DataObject extends DB_DataObject
$this->_link_loaded = false;
}
+ /**
+ * Magic function called when someone attempts to call a method
+ * that doesn't exist. DB_DataObject uses this to implement
+ * setters and getters for fields, but neglects to throw an error
+ * when you just misspell an actual method name. This leads to
+ * silent failures which can cause all kinds of havoc.
+ *
+ * @param string $method
+ * @param array $params
+ * @return mixed
+ * @throws Exception
+ */
+ function __call($method, $params)
+ {
+ $return = null;
+ // Yes, that's _call with one underscore, which does the
+ // actual implementation.
+ if ($this->_call($method, $params, $return)) {
+ return $return;
+ } else {
+ throw new Exception('Call to undefined method ' .
+ get_class($this) . '::' . $method);
+ }
+ }
/**
* Work around memory-leak bugs...
diff --git a/classes/Subscription.php b/classes/Subscription.php
index 60c12cccc..0679c0925 100644
--- a/classes/Subscription.php
+++ b/classes/Subscription.php
@@ -88,8 +88,8 @@ class Subscription extends Memcached_DataObject
self::blow('user:notices_with_friends:%d', $subscriber->id);
- $subscriber->blowSubscriptionsCount();
- $other->blowSubscribersCount();
+ $subscriber->blowSubscriptionCount();
+ $other->blowSubscriberCount();
$otherUser = User::staticGet('id', $other->id);
@@ -213,8 +213,8 @@ class Subscription extends Memcached_DataObject
self::blow('user:notices_with_friends:%d', $subscriber->id);
- $subscriber->blowSubscriptionsCount();
- $other->blowSubscribersCount();
+ $subscriber->blowSubscriptionCount();
+ $other->blowSubscriberCount();
Event::handle('EndUnsubscribe', array($subscriber, $other));
}