SQLProfilePlugin.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('STATUSNET')) {
  20. exit(1);
  21. }
  22. /**
  23. * Check DB queries for filesorts and such and log em.
  24. *
  25. * @package SQLProfilePlugin
  26. * @maintainer Brion Vibber <brion@status.net>
  27. */
  28. class SQLProfilePlugin extends Plugin
  29. {
  30. const PLUGIN_VERSION = '2.0.0';
  31. private $recursionGuard = false;
  32. function onPluginVersion(array &$versions)
  33. {
  34. $versions[] = array('name' => 'SQLProfile',
  35. 'version' => self::PLUGIN_VERSION,
  36. 'author' => 'Brion Vibber',
  37. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/SQLProfile',
  38. 'rawdescription' =>
  39. // TRANS: Plugin description.
  40. _m('Debug tool to watch for poorly indexed DB queries.'));
  41. return true;
  42. }
  43. function onStartDBQuery($obj, $query, &$result)
  44. {
  45. if (!$this->recursionGuard && preg_match('/\bselect\b/i', $query)) {
  46. $this->recursionGuard = true;
  47. $xobj = clone($obj);
  48. $explain = $xobj->query('EXPLAIN ' . $query);
  49. $this->recursionGuard = false;
  50. while ($xobj->fetch()) {
  51. $extra = $xobj->Extra;
  52. $evil = (strpos($extra, 'Using filesort') !== false) ||
  53. (strpos($extra, 'Using temporary') !== false);
  54. if ($evil) {
  55. $xquery = $xobj->sanitizeQuery($query);
  56. common_log(LOG_DEBUG, "$extra | $xquery");
  57. }
  58. }
  59. }
  60. return true;
  61. }
  62. }