devel.module 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  1. <?php
  2. // This module holds functions useful for Drupal development.
  3. // Please contribute!
  4. // Suggested profiling and stacktrace library from http://www.xdebug.org/index.php
  5. define('DEVEL_QUERY_SORT_BY_SOURCE', 0);
  6. define('DEVEL_QUERY_SORT_BY_DURATION', 1);
  7. define('DEVEL_ERROR_HANDLER_NONE', 0);
  8. define('DEVEL_ERROR_HANDLER_STANDARD', 1);
  9. define('DEVEL_ERROR_HANDLER_BACKTRACE_KRUMO', 2);
  10. define('DEVEL_ERROR_HANDLER_BACKTRACE_DPM', 4);
  11. define('DEVEL_MIN_TEXTAREA', 50);
  12. /**
  13. * Implementation of hook_help().
  14. */
  15. function devel_help($section) {
  16. switch ($section) {
  17. case 'devel/reference':
  18. return '<p>'. t('This is a list of defined user functions that generated this current request lifecycle. Click on a function name to view its documention.') .'</p>';
  19. case 'devel/session':
  20. return '<p>'. t('Here are the contents of your <code>$_SESSION</code> variable.') .'</p>';
  21. case 'devel/variable':
  22. $api = variable_get('devel_api_url', 'api.drupal.org');
  23. return '<p>'. t('This is a list of the variables and their values currently stored in variables table and the <code>$conf</code> array of your settings.php file. These variables are usually accessed with <a href="@variable-get-doc">variable_get()</a> and <a href="@variable-set-doc">variable_set()</a>. Variables that are too long can slow down your pages.', array('@variable-get-doc' => "http://$api/api/HEAD/function/variable_get", '@variable-set-doc' => "http://$api/api/HEAD/function/variable_set")) .'</p>';
  24. case 'devel/reinstall':
  25. return t('Warning - will delete your module tables and variables.');
  26. }
  27. }
  28. /**
  29. * Implements hook_modules_installed().
  30. *
  31. * @see devel_install()
  32. */
  33. function devel_modules_installed($modules) {
  34. if (in_array('menu', $modules)) {
  35. $menu = array(
  36. 'menu_name' => 'devel',
  37. 'title' => t('Development'),
  38. 'description' => t('Development link'),
  39. );
  40. menu_save($menu);
  41. }
  42. }
  43. /**
  44. * Implements hook_menu().
  45. */
  46. function devel_menu() {
  47. // Note: we can't dynamically append destination to querystring. Do so at theme layer. Fix in D7?
  48. $items['devel/cache/clear'] = array(
  49. 'title' => 'Empty cache',
  50. 'page callback' => 'devel_cache_clear',
  51. 'description' => 'Clear the CSS cache and all database cache tables which store page, node, theme and variable caches.',
  52. 'access arguments' => array('access devel information'),
  53. 'file' => 'devel.pages.inc',
  54. 'menu_name' => 'devel',
  55. );
  56. $items['devel/reference'] = array(
  57. 'title' => 'Function reference',
  58. 'description' => 'View a list of currently defined user functions with documentation links.',
  59. 'page callback' => 'devel_function_reference',
  60. 'access arguments' => array('access devel information'),
  61. 'file' => 'devel.pages.inc',
  62. 'menu_name' => 'devel',
  63. );
  64. $items['devel/reinstall'] = array(
  65. 'title' => 'Reinstall modules',
  66. 'page callback' => 'drupal_get_form',
  67. 'page arguments' => array('devel_reinstall'),
  68. 'description' => 'Run hook_uninstall() and then hook_install() for a given module.',
  69. 'access arguments' => array('access devel information'),
  70. 'file' => 'devel.pages.inc',
  71. 'menu_name' => 'devel',
  72. );
  73. $items['devel/menu/reset'] = array(
  74. 'title' => 'Rebuild menus',
  75. 'description' => 'Rebuild menu based on hook_menu() and revert any custom changes. All menu items return to their default settings.',
  76. 'page callback' => 'drupal_get_form',
  77. 'page arguments' => array('devel_menu_rebuild'),
  78. 'access arguments' => array('access devel information'),
  79. 'file' => 'devel.pages.inc',
  80. 'menu_name' => 'devel',
  81. );
  82. $items['devel/menu/item'] = array(
  83. 'title' => 'Menu item',
  84. 'description' => 'Details about a given menu item.',
  85. 'page callback' => 'devel_menu_item',
  86. 'access arguments' => array('access devel information'),
  87. 'file' => 'devel.pages.inc',
  88. 'menu_name' => 'devel',
  89. );
  90. $items['devel/variable'] = array(
  91. 'title' => 'Variable editor',
  92. 'description' => 'Edit and delete site variables.',
  93. 'page callback' => 'devel_variable_page',
  94. 'access arguments' => array('access devel information'),
  95. 'file' => 'devel.pages.inc',
  96. 'menu_name' => 'devel',
  97. );
  98. // we don't want the abbreviated version provided by status report
  99. $items['devel/phpinfo'] = array(
  100. 'title' => 'PHPinfo()',
  101. 'description' => 'View your server\'s PHP configuration',
  102. 'page callback' => 'devel_phpinfo',
  103. 'access arguments' => array('access devel information'),
  104. 'file' => 'devel.pages.inc',
  105. 'menu_name' => 'devel',
  106. );
  107. $items['devel/php'] = array(
  108. 'title' => 'Execute PHP Code',
  109. 'description' => 'Execute some PHP code',
  110. 'page callback' => 'drupal_get_form',
  111. 'page arguments' => array('devel_execute_form'),
  112. 'access arguments' => array('execute php code'),
  113. 'file' => 'devel.pages.inc',
  114. 'menu_name' => 'devel',
  115. );
  116. $items['devel/theme/registry'] = array(
  117. 'title' => 'Theme registry',
  118. 'description' => 'View a list of available theme functions across the whole site.',
  119. 'page callback' => 'devel_theme_registry',
  120. 'access arguments' => array('access devel information'),
  121. 'file' => 'devel.pages.inc',
  122. 'menu_name' => 'devel',
  123. );
  124. $items['devel/entity/info'] = array(
  125. 'title' => 'Entity info',
  126. 'description' => 'View entity information across the whole site.',
  127. 'page callback' => 'devel_entity_info_page',
  128. 'access arguments' => array('access devel information'),
  129. 'file' => 'devel.pages.inc',
  130. 'menu_name' => 'devel',
  131. );
  132. $items['devel/field/info'] = array(
  133. 'title' => 'Field info',
  134. 'description' => 'View fields information across the whole site.',
  135. 'page callback' => 'devel_field_info_page',
  136. 'access arguments' => array('access devel information'),
  137. 'file' => 'devel.pages.inc',
  138. 'menu_name' => 'devel',
  139. );
  140. $items['devel/elements'] = array(
  141. 'title' => 'Hook_elements()',
  142. 'description' => 'View the active form/render elements for this site.',
  143. 'page callback' => 'devel_elements_page',
  144. 'access arguments' => array('access devel information'),
  145. 'file' => 'devel.pages.inc',
  146. 'menu_name' => 'devel',
  147. );
  148. $items['devel/variable/edit/%'] = array(
  149. 'title' => 'Variable editor',
  150. 'page callback' => 'drupal_get_form',
  151. 'page arguments' => array('devel_variable_edit', 3),
  152. 'access arguments' => array('access devel information'),
  153. 'type' => MENU_CALLBACK,
  154. 'file' => 'devel.pages.inc',
  155. 'menu_name' => 'devel',
  156. );
  157. $items['devel/session'] = array(
  158. 'title' => 'Session viewer',
  159. 'description' => 'List the contents of $_SESSION.',
  160. 'page callback' => 'devel_session',
  161. 'access arguments' => array('access devel information'),
  162. 'file' => 'devel.pages.inc',
  163. 'menu_name' => 'devel',
  164. );
  165. $items['devel/switch'] = array(
  166. 'title' => 'Switch user',
  167. 'page callback' => 'devel_switch_user',
  168. 'access callback' => '_devel_switch_user_access',
  169. 'access arguments' => array(2),
  170. 'type' => MENU_CALLBACK,
  171. 'file' => 'devel.pages.inc',
  172. 'menu_name' => 'devel',
  173. );
  174. $items['devel/explain'] = array(
  175. 'title' => 'Explain query',
  176. 'page callback' => 'devel_querylog_explain',
  177. 'description' => 'Run an EXPLAIN on a given query. Used by query log',
  178. 'access arguments' => array('access devel information'),
  179. 'file' => 'devel.pages.inc',
  180. 'type' => MENU_CALLBACK
  181. );
  182. $items['devel/arguments'] = array(
  183. 'title' => 'Arguments query',
  184. 'page callback' => 'devel_querylog_arguments',
  185. 'description' => 'Return a given query, with arguments instead of placeholders. Used by query log',
  186. 'access arguments' => array('access devel information'),
  187. 'file' => 'devel.pages.inc',
  188. 'type' => MENU_CALLBACK
  189. );
  190. $items['devel/run-cron'] = array(
  191. 'title' => 'Run cron',
  192. 'page callback' => 'system_run_cron',
  193. 'access arguments' => array('administer site configuration'),
  194. 'file' => 'system.admin.inc',
  195. 'file path' => drupal_get_path('module', 'system'),
  196. 'menu_name' => 'devel',
  197. );
  198. // Duplicate path in 2 different menus. See http://drupal.org/node/601788.
  199. $items['devel/settings'] = array(
  200. 'title' => 'Devel settings',
  201. 'description' => 'Helper functions, pages, and blocks to assist Drupal developers. The devel blocks can be managed via the <a href="' . url('admin/structure/block') . '">block administration</a> page.',
  202. 'page callback' => 'drupal_get_form',
  203. 'page arguments' => array('devel_admin_settings'),
  204. 'access arguments' => array('administer site configuration'),
  205. 'file' => 'devel.admin.inc',
  206. 'menu_name' => 'devel',
  207. );
  208. $items['admin/config/development/devel'] = array(
  209. 'title' => 'Devel settings',
  210. 'description' => 'Helper functions, pages, and blocks to assist Drupal developers. The devel blocks can be managed via the <a href="' . url('admin/structure/block') . '">block administration</a> page.',
  211. 'page callback' => 'drupal_get_form',
  212. 'page arguments' => array('devel_admin_settings'),
  213. 'file' => 'devel.admin.inc',
  214. 'access arguments' => array('administer site configuration'),
  215. );
  216. $items['node/%node/devel'] = array(
  217. 'title' => 'Devel',
  218. 'page callback' => 'devel_load_object',
  219. 'page arguments' => array('node', 1),
  220. 'access arguments' => array('access devel information'),
  221. 'type' => MENU_LOCAL_TASK,
  222. 'file' => 'devel.pages.inc',
  223. 'weight' => 100,
  224. );
  225. $items['node/%node/devel/load'] = array(
  226. 'title' => 'Load',
  227. 'type' => MENU_DEFAULT_LOCAL_TASK,
  228. );
  229. $items['node/%node/devel/render'] = array(
  230. 'title' => 'Render',
  231. 'page callback' => 'devel_render_object',
  232. 'page arguments' => array('node', 1),
  233. 'access arguments' => array('access devel information'),
  234. 'file' => 'devel.pages.inc',
  235. 'type' => MENU_LOCAL_TASK,
  236. 'weight' => 100,
  237. );
  238. $items['comment/%comment/devel'] = array(
  239. 'title' => 'Devel',
  240. 'page callback' => 'devel_load_object',
  241. 'page arguments' => array('comment', 1),
  242. 'access arguments' => array('access devel information'),
  243. 'type' => MENU_LOCAL_TASK,
  244. 'file' => 'devel.pages.inc',
  245. 'weight' => 100,
  246. );
  247. $items['comment/%comment/devel/load'] = array(
  248. 'title' => 'Load',
  249. 'type' => MENU_DEFAULT_LOCAL_TASK,
  250. );
  251. $items['comment/%comment/devel/render'] = array(
  252. 'title' => 'Render',
  253. 'page callback' => 'devel_render_object',
  254. 'page arguments' => array('comment', 1),
  255. 'access arguments' => array('access devel information'),
  256. 'type' => MENU_LOCAL_TASK,
  257. 'file' => 'devel.pages.inc',
  258. 'weight' => 100,
  259. );
  260. $items['user/%user/devel'] = array(
  261. 'title' => 'Devel',
  262. 'page callback' => 'devel_load_object',
  263. 'page arguments' => array('user', 1),
  264. 'access arguments' => array('access devel information'),
  265. 'type' => MENU_LOCAL_TASK,
  266. 'file' => 'devel.pages.inc',
  267. 'weight' => 100,
  268. );
  269. $items['user/%user/devel/load'] = array(
  270. 'title' => 'Load',
  271. 'type' => MENU_DEFAULT_LOCAL_TASK,
  272. );
  273. $items['user/%user/devel/render'] = array(
  274. 'title' => 'Render',
  275. 'page callback' => 'devel_render_object',
  276. 'page arguments' => array('user', 1),
  277. 'access arguments' => array('access devel information'),
  278. 'file' => 'devel.pages.inc',
  279. 'type' => MENU_LOCAL_TASK,
  280. 'weight' => 100,
  281. );
  282. $items['taxonomy/term/%taxonomy_term/devel'] = array(
  283. 'title' => 'Devel',
  284. 'page callback' => 'devel_load_object',
  285. 'page arguments' => array('taxonomy_term', 2, 'term'),
  286. 'access arguments' => array('access devel information'),
  287. 'file' => 'devel.pages.inc',
  288. 'type' => MENU_LOCAL_TASK,
  289. 'weight' => 100,
  290. );
  291. $items['taxonomy/term/%taxonomy_term/devel/load'] = array(
  292. 'title' => 'Load',
  293. 'type' => MENU_DEFAULT_LOCAL_TASK,
  294. );
  295. $items['taxonomy/term/%taxonomy_term/devel/render'] = array(
  296. 'title' => 'Render',
  297. 'page callback' => 'devel_render_object',
  298. 'page arguments' => array('taxonomy_term', 2, 'term'),
  299. 'access arguments' => array('access devel information'),
  300. 'type' => MENU_LOCAL_TASK,
  301. 'file' => 'devel.pages.inc',
  302. 'weight' => 100,
  303. );
  304. return $items;
  305. }
  306. /**
  307. * Menu item access callback - check permission and token for Switch User.
  308. */
  309. function _devel_switch_user_access($name) {
  310. // Suppress notices when on other pages when menu system still checks access.
  311. return user_access('switch users') && drupal_valid_token(@$_GET['token'], "devel/switch/$name|" . @$_GET['destination'], TRUE);
  312. }
  313. /**
  314. * Implements hook_admin_paths().
  315. */
  316. function devel_admin_paths() {
  317. $paths = array(
  318. 'devel/*' => TRUE,
  319. 'node/*/devel' => TRUE,
  320. 'node/*/devel/*' => TRUE,
  321. 'comment/*/devel' => TRUE,
  322. 'comment/*/devel/*' => TRUE,
  323. 'user/*/devel' => TRUE,
  324. 'user/*/devel/*' => TRUE,
  325. 'taxonomy/term/*/devel' => TRUE,
  326. 'taxonomy/term/*/devel/*' => TRUE,
  327. );
  328. return $paths;
  329. }
  330. function devel_menu_need_destination() {
  331. return array('devel/cache/clear', 'devel/reinstall', 'devel/menu/reset', 'devel/variable', 'admin/reports/status/run-cron');
  332. }
  333. /**
  334. * An implementation of hook_menu_link_alter(). Flag this link as needing alter at display time.
  335. * This is more robust than setting alter in hook_menu().
  336. * @see devel_translated_menu_link_alter().
  337. *
  338. **/
  339. function devel_menu_link_alter(&$item) {
  340. if (in_array($item['link_path'], devel_menu_need_destination()) || $item['link_path'] == 'devel/menu/item') {
  341. $item['options']['alter'] = TRUE;
  342. }
  343. }
  344. /**
  345. * An implementation of hook_translated_menu_item_alter(). Append dynamic
  346. * querystring 'destination' to several of our own menu items.
  347. *
  348. **/
  349. function devel_translated_menu_link_alter(&$item) {
  350. if (in_array($item['href'], devel_menu_need_destination())) {
  351. $item['localized_options']['query'] = drupal_get_destination();
  352. }
  353. elseif ($item['href'] == 'devel/menu/item') {
  354. $item['localized_options']['query'] = array('path' => $_GET['q']);
  355. }
  356. }
  357. /**
  358. * Implementation of hook_theme()
  359. */
  360. function devel_theme() {
  361. return array(
  362. 'devel_querylog' => array(
  363. 'variables' => array('header' => array(), 'rows' => array()),
  364. ),
  365. 'devel_querylog_row' => array(
  366. 'variables' => array('row' => array()),
  367. ),
  368. );
  369. }
  370. /**
  371. * Implementation of hook_init().
  372. */
  373. function devel_init() {
  374. if (!devel_silent()) {
  375. if (user_access('access devel information')) {
  376. devel_set_handler(devel_get_handlers());
  377. // We want to include the class early so that anyone may call krumo() as needed. See http://krumo.sourceforge.net/
  378. has_krumo();
  379. // See http://www.firephp.org/HQ/Install.htm
  380. $path = NULL;
  381. if (@include_once('fb.php')) {
  382. // FirePHPCore is in include_path. Probably a PEAR installation.
  383. $path = '';
  384. }
  385. elseif (module_exists('libraries')) {
  386. // Support Libraries API - http://drupal.org/project/libraries
  387. $firephp_path = libraries_get_path('FirePHPCore');
  388. $firephp_path = ($firephp_path ? $firephp_path . '/lib/FirePHPCore/' : '');
  389. $chromephp_path = libraries_get_path('chromephp');
  390. }
  391. else {
  392. $firephp_path = './' . drupal_get_path('module', 'devel') . '/FirePHPCore/lib/FirePHPCore/';
  393. $chromephp_path = './' . drupal_get_path('module', 'devel') . '/chromephp';
  394. }
  395. // Include FirePHP if it exists.
  396. if (!empty($firephp_path) && file_exists($firephp_path . 'fb.php')) {
  397. include_once $firephp_path . 'fb.php';
  398. include_once $firephp_path . 'FirePHP.class.php';
  399. }
  400. // Include ChromePHP if it exists.
  401. if (!empty($chromephp_path) && file_exists($chromephp_path .= '/ChromePhp.php')) {
  402. include_once $chromephp_path;
  403. }
  404. // Add CSS for query log if should be displayed.
  405. if (variable_get('devel_query_display', 0)) {
  406. drupal_add_css(drupal_get_path('module', 'devel') . '/devel.css');
  407. drupal_add_js(drupal_get_path('module', 'devel') . '/devel.js');
  408. }
  409. }
  410. }
  411. if (variable_get('devel_rebuild_theme_registry', FALSE)) {
  412. drupal_theme_rebuild();
  413. if (flood_is_allowed('devel_rebuild_registry_warning', 1)) {
  414. flood_register_event('devel_rebuild_registry_warning');
  415. if (!devel_silent() && user_access('access devel information')) {
  416. drupal_set_message(t('The theme registry is being rebuilt on every request. Remember to <a href="!url">turn off</a> this feature on production websites.', array("!url" => url('admin/config/development/devel'))));
  417. }
  418. }
  419. }
  420. }
  421. function devel_set_message($msg, $type = NULL) {
  422. $function = function_exists('drush_log') ? 'drush_log' : 'drupal_set_message';
  423. $function($msg, $type);
  424. }
  425. // Return boolean. No need for cache here.
  426. function has_krumo() {
  427. // see README.txt or just download from http://krumo.sourceforge.net/
  428. @include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'devel') . '/krumo/class.krumo.php';
  429. if (function_exists('krumo') && !drupal_is_cli()) {
  430. drupal_add_js(drupal_get_path('module', 'devel') . '/devel_krumo_path.js');
  431. return TRUE;
  432. }
  433. return FALSE;
  434. }
  435. /**
  436. * Decide whether or not to print a debug variable using krumo().
  437. *
  438. * @param $input
  439. * @return boolean
  440. */
  441. function merits_krumo($input) {
  442. return (is_object($input) || is_array($input)) && has_krumo() && variable_get('devel_krumo_skin', '') != 'disabled';
  443. }
  444. /**
  445. * Calls the http://www.firephp.org/ fb() function if it is found.
  446. *
  447. * @return void
  448. */
  449. function dfb() {
  450. if (function_exists('fb') && user_access('access devel information') && !headers_sent()) {
  451. $args = func_get_args();
  452. call_user_func_array('fb', $args);
  453. }
  454. }
  455. /**
  456. * Calls dfb() to output a backtrace.
  457. */
  458. function dfbt($label) {
  459. dfb($label, FirePHP::TRACE);
  460. }
  461. /**
  462. * Wrapper for ChromePHP Class log method
  463. */
  464. function dcp() {
  465. if (class_exists('ChromePhp', FALSE) && user_access('access devel information')) {
  466. $args = func_get_args();
  467. call_user_func_array(array('ChromePhp', 'log'), $args);
  468. }
  469. }
  470. /**
  471. * Implements hook_watchdog().
  472. */
  473. function devel_watchdog(array $log_entry) {
  474. if (class_exists('FirePHP', FALSE) && !drupal_is_cli()) {
  475. switch ($log_entry['severity']) {
  476. case WATCHDOG_EMERGENCY:
  477. case WATCHDOG_ALERT:
  478. case WATCHDOG_CRITICAL:
  479. case WATCHDOG_ERROR:
  480. $type = FirePHP::ERROR;
  481. break;
  482. case WATCHDOG_WARNING:
  483. $type = FirePHP::WARN;
  484. break;
  485. case WATCHDOG_NOTICE:
  486. case WATCHDOG_INFO:
  487. $type = FirePHP::INFO;
  488. break;
  489. case WATCHDOG_DEBUG:
  490. DEFAULT:
  491. $type = FirePHP::LOG;
  492. }
  493. }
  494. else {
  495. $type = 'watchdog';
  496. }
  497. $function = function_exists('decode_entities') ? 'decode_entities' : 'html_entity_decode';
  498. $watchdog = array(
  499. 'type' => $log_entry['type'],
  500. 'message' => $function(strtr($log_entry['message'], (array)$log_entry['variables'])),
  501. );
  502. if (isset($log_entry['link'])) {
  503. $watchdog['link'] = $log_entry['link'];
  504. }
  505. dfb($watchdog, $type);
  506. }
  507. function devel_get_handlers() {
  508. $error_handlers = variable_get('devel_error_handlers', array(DEVEL_ERROR_HANDLER_STANDARD => DEVEL_ERROR_HANDLER_STANDARD));
  509. if (!empty($error_handlers)) {
  510. unset($error_handlers[DEVEL_ERROR_HANDLER_NONE]);
  511. }
  512. return $error_handlers;
  513. }
  514. function devel_set_handler($handlers) {
  515. if (empty($handlers)) {
  516. restore_error_handler();
  517. }
  518. elseif (count($handlers) == 1 && isset($handlers[DEVEL_ERROR_HANDLER_STANDARD])) {
  519. // do nothing
  520. }
  521. else {
  522. if (has_krumo()) {
  523. set_error_handler('backtrace_error_handler');
  524. }
  525. }
  526. }
  527. function devel_silent() {
  528. // isset($_GET['q']) is needed when calling the front page. q is not set.
  529. // Don't interfere with private files/images.
  530. return
  531. function_exists('drupal_is_cli') && drupal_is_cli() ||
  532. (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'ApacheBench') !== FALSE) ||
  533. !empty($_REQUEST['XDEBUG_PROFILE']) ||
  534. isset($GLOBALS['devel_shutdown']) ||
  535. strstr($_SERVER['PHP_SELF'], 'update.php') ||
  536. (isset($_GET['q']) && (
  537. in_array($_GET['q'], array( 'admin/content/node-settings/rebuild')) ||
  538. substr($_GET['q'], 0, strlen('system/files')) == 'system/files' ||
  539. substr($_GET['q'], 0, strlen('batch')) == 'batch' ||
  540. substr($_GET['q'], 0, strlen('file/ajax')) == 'file/ajax')
  541. );
  542. }
  543. function devel_xhprof_enable() {
  544. if (devel_xhprof_is_enabled()) {
  545. if ($path = variable_get('devel_xhprof_directory', '')) {
  546. include_once $path . '/xhprof_lib/utils/xhprof_lib.php';
  547. include_once $path . '/xhprof_lib/utils/xhprof_runs.php';
  548. // @todo: consider a variable per-flag instead.
  549. xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
  550. }
  551. }
  552. }
  553. function devel_xhprof_is_enabled() {
  554. return extension_loaded('xhprof') && variable_get('devel_xhprof_enabled', FALSE);
  555. }
  556. /**
  557. * Implementation of hook_boot(). Runs even for cached pages.
  558. */
  559. function devel_boot() {
  560. // Initialize XHProf.
  561. devel_xhprof_enable();
  562. if (!devel_silent()) {
  563. if (variable_get('dev_mem', 0)) {
  564. global $memory_init;
  565. $memory_init = memory_get_usage();
  566. }
  567. if (devel_query_enabled()) {
  568. @include_once DRUPAL_ROOT . '/includes/database/log.inc';
  569. Database::startLog('devel');;
  570. }
  571. }
  572. // We need user_access() in the shutdown function. make sure it gets loaded.
  573. // Also prime the drupal_get_filename() static with user.module's location to
  574. // avoid a stray query.
  575. drupal_get_filename('module', 'user', 'modules/user/user.module');
  576. drupal_load('module', 'user');
  577. drupal_register_shutdown_function('devel_shutdown');
  578. }
  579. /**
  580. * Displays backtrace showing the route of calls to the current error.
  581. *
  582. * @param $error_level
  583. * The level of the error raised.
  584. * @param $message
  585. * The error message.
  586. * @param $filename
  587. * The filename that the error was raised in.
  588. * @param $line
  589. * The line number the error was raised at.
  590. * @param $context
  591. * An array that points to the active symbol table at the point the error
  592. * occurred.
  593. */
  594. function backtrace_error_handler($error_level, $message, $filename, $line, $context) {
  595. // Hide stack trace and parameters from unqualified users.
  596. if (!user_access('access devel information')) {
  597. // Do what core does in bootstrap.inc and errors.inc.
  598. // (We need to duplicate the core code here rather than calling it
  599. // to avoid having the backtrace_error_handler() on top of the call stack.)
  600. require_once DRUPAL_ROOT . '/includes/errors.inc';
  601. if ($error_level & error_reporting()) {
  602. $types = drupal_error_levels();
  603. list($severity_msg, $severity_level) = $types[$error_level];
  604. $caller = _drupal_get_last_caller(debug_backtrace());
  605. if (!function_exists('filter_xss_admin')) {
  606. require_once DRUPAL_ROOT . '/includes/common.inc';
  607. }
  608. // We treat recoverable errors as fatal.
  609. _drupal_log_error(array(
  610. '%type' => isset($types[$error_level]) ? $severity_msg : 'Unknown error',
  611. // The standard PHP error handler considers that the error messages
  612. // are HTML. We mimick this behavior here.
  613. '!message' => filter_xss_admin($message),
  614. '%function' => $caller['function'],
  615. '%file' => $caller['file'],
  616. '%line' => $caller['line'],
  617. 'severity_level' => $severity_level,
  618. ), $error_level == E_RECOVERABLE_ERROR);
  619. }
  620. return;
  621. }
  622. // Don't respond to the error if it was suppressed with a '@'
  623. if (error_reporting() == 0) {
  624. return;
  625. }
  626. // Don't respond to warning caused by ourselves.
  627. if (preg_match('#Cannot modify header information - headers already sent by \\([^\\)]*[/\\\\]devel[/\\\\]#', $message)) {
  628. return;
  629. }
  630. if ($error_level & error_reporting()) {
  631. // Only write each distinct NOTICE message once, as repeats do not give any
  632. // further information and can choke the page output.
  633. if ($error_level == E_NOTICE) {
  634. static $written = array();
  635. if (!empty($written[$line][$filename][$message])) {
  636. return;
  637. }
  638. $written[$line][$filename][$message] = TRUE;
  639. }
  640. require_once DRUPAL_ROOT . '/includes/errors.inc';
  641. $types = drupal_error_levels();
  642. $type = $types[$error_level];
  643. $backtrace = debug_backtrace();
  644. $variables = array('%error' => $type[0], '%message' => $message, '%function' => $backtrace[1]['function'] .'()', '%file' => $filename, '%line' => $line);
  645. $msg = t('%error: %message in %function (line %line of %file).', $variables);
  646. // Show message if error_level is ERROR_REPORTING_DISPLAY_SOME or higher.
  647. // (This is Drupal's error_level, which is different from $error_level,
  648. // and we purposely ignore the difference between _SOME and _ALL,
  649. // see #970688!)
  650. if (variable_get('error_level', 1) >= 1) {
  651. $error_handlers = devel_get_handlers();
  652. if (!empty($error_handlers[DEVEL_ERROR_HANDLER_STANDARD])) {
  653. drupal_set_message($msg, ($type[1] <= WATCHDOG_ERROR ? 'error' : 'warning'));
  654. }
  655. if (!empty($error_handlers[DEVEL_ERROR_HANDLER_BACKTRACE_KRUMO])) {
  656. print $msg ." =&gt;\n";
  657. ddebug_backtrace(FALSE, 1);
  658. }
  659. if (!empty($error_handlers[DEVEL_ERROR_HANDLER_BACKTRACE_DPM])) {
  660. dpm(ddebug_backtrace(TRUE, 1), $msg, 'warning');
  661. }
  662. }
  663. $watchdog = 'watchdog';
  664. $watchdog('php', $msg, array(), $type[1]);
  665. }
  666. }
  667. /**
  668. * Implement hook_permission().
  669. */
  670. function devel_permission() {
  671. return array(
  672. 'access devel information' => array(
  673. 'description' => t('View developer output like variable printouts, query log, etc.'),
  674. 'title' => t('Access developer information'),
  675. 'restrict access' => TRUE,
  676. ),
  677. 'execute php code' => array(
  678. 'title' => t('Execute PHP code'),
  679. 'description' => t('Run arbitrary PHP from a block.'),
  680. 'restrict access' => TRUE,
  681. ),
  682. 'switch users' => array(
  683. 'title' => t('Switch users'),
  684. 'description' => t('Become any user on the site with just a click.'),
  685. 'restrict access' => TRUE,
  686. ),
  687. );
  688. }
  689. function devel_block_info() {
  690. $blocks['execute_php'] = array(
  691. 'info' => t('Execute PHP'),
  692. 'cache' => DRUPAL_NO_CACHE,
  693. );
  694. $blocks['switch_user'] = array(
  695. 'info' => t('Switch user'),
  696. 'cache' => DRUPAL_NO_CACHE,
  697. );
  698. return $blocks;
  699. }
  700. /**
  701. * Implementation of hook_block_configure().
  702. */
  703. function devel_block_configure($delta) {
  704. if ($delta == 'switch_user') {
  705. $form['list_size'] = array(
  706. '#type' => 'textfield',
  707. '#title' => t('Number of users to display in the list'),
  708. '#default_value' => variable_get('devel_switch_user_list_size', 10),
  709. '#size' => '3',
  710. '#maxlength' => '4',
  711. );
  712. $form['include_anon'] = array(
  713. '#type' => 'checkbox',
  714. '#title' => t('Include %anonymous', array('%anonymous' => format_username(drupal_anonymous_user()))),
  715. '#default_value' => variable_get('devel_switch_user_include_anon', FALSE),
  716. );
  717. $form['show_form'] = array(
  718. '#type' => 'checkbox',
  719. '#title' => t('Allow entering any user name'),
  720. '#default_value' => variable_get('devel_switch_user_show_form', TRUE),
  721. );
  722. return $form;
  723. }
  724. }
  725. function devel_block_save($delta, $edit = array()) {
  726. if ($delta == 'switch_user') {
  727. variable_set('devel_switch_user_list_size', $edit['list_size']);
  728. variable_set('devel_switch_user_include_anon', $edit['include_anon']);
  729. variable_set('devel_switch_user_show_form', $edit['show_form']);
  730. }
  731. }
  732. function devel_block_view($delta) {
  733. $block = array();
  734. switch ($delta) {
  735. case 'switch_user':
  736. $block = devel_block_switch_user();
  737. break;
  738. case 'execute_php':
  739. if (user_access('execute php code')) {
  740. $block['content'] = drupal_get_form('devel_execute_block_form');
  741. }
  742. break;
  743. }
  744. return $block;
  745. }
  746. function devel_block_switch_user() {
  747. $links = devel_switch_user_list();
  748. if (!empty($links) || user_access('switch users')) {
  749. drupal_add_css(drupal_get_path('module', 'devel') . '/devel.css');
  750. $block['subject'] = t('Switch user');
  751. $build['devel_links'] = array('#theme' => 'links', '#links' => $links);
  752. if (variable_get('devel_switch_user_show_form', TRUE)) {
  753. $build['devel_form'] = drupal_get_form('devel_switch_user_form');
  754. }
  755. $block['content'] = $build;
  756. return $block;
  757. }
  758. }
  759. function devel_switch_user_list() {
  760. global $user;
  761. $links = array();
  762. if (user_access('switch users')) {
  763. $list_size = variable_get('devel_switch_user_list_size', 10);
  764. if ($include_anon = ($user->uid && variable_get('devel_switch_user_include_anon', FALSE))) {
  765. --$list_size;
  766. }
  767. $dest = drupal_get_destination();
  768. // Try to find at least $list_size users that can switch.
  769. // Inactive users are omitted from all of the following db selects.
  770. $roles = user_roles(TRUE, 'switch users');
  771. $query = db_select('users', 'u');
  772. $query->addField('u', 'uid');
  773. $query->addField('u', 'access');
  774. $query->distinct();
  775. $query->condition('u.uid', 0, '>');
  776. $query->condition('u.status', 0, '>');
  777. $query->orderBy('u.access', 'DESC');
  778. $query->range(0, $list_size);
  779. if (!isset($roles[DRUPAL_AUTHENTICATED_RID])) {
  780. $query->leftJoin('users_roles', 'r', 'u.uid = r.uid');
  781. $or_condition = db_or();
  782. $or_condition->condition('u.uid', 1);
  783. if (!empty($roles)) {
  784. $or_condition->condition('r.rid', array_keys($roles), 'IN');
  785. }
  786. $query->condition($or_condition);
  787. }
  788. $uids = $query->execute()->fetchCol();
  789. $accounts = user_load_multiple($uids);
  790. foreach ($accounts as $account) {
  791. $path = 'devel/switch/'. $account->name;
  792. $links[$account->uid] = array(
  793. 'title' => drupal_placeholder(format_username($account)),
  794. 'href' => $path,
  795. 'query' => array($dest, 'token' => drupal_get_token($path . '|' . $dest['destination'])),
  796. 'attributes' => array('title' => t('This user can switch back.')),
  797. 'html' => TRUE,
  798. 'last_access' => $account->access,
  799. );
  800. }
  801. $num_links = count($links);
  802. if ($num_links < $list_size) {
  803. // If we don't have enough, add distinct uids until we hit $list_size.
  804. $uids = db_query_range('SELECT uid FROM {users} WHERE uid > 0 AND uid NOT IN (:uids) AND status > 0 ORDER BY access DESC', 0, $list_size - $num_links, array(':uids' => array_keys($links)))->fetchCol();
  805. $accounts = user_load_multiple($uids);
  806. foreach ($accounts as $account) {
  807. $path = 'devel/switch/'. $account->name;
  808. $links[$account->uid] = array(
  809. 'title' => format_username($account),
  810. 'href' => $path,
  811. 'query' => array($dest, 'token' => drupal_get_token($path . '|' . $dest['destination'])),
  812. 'attributes' => array('title' => t('Caution: this user will be unable to switch back.')),
  813. 'last_access' => $account->access,
  814. );
  815. }
  816. uasort($links, '_devel_switch_user_list_cmp');
  817. }
  818. if ($include_anon) {
  819. $path = 'devel/switch';
  820. $link = array(
  821. 'title' => format_username(drupal_anonymous_user()),
  822. 'href' => $path,
  823. 'query' => array($dest, 'token' => drupal_get_token($path . '/|' . $dest['destination'])),
  824. 'attributes' => array('title' => t('Caution: the anonymous user will be unable to switch back.')),
  825. );
  826. if (user_access('switch users', drupal_anonymous_user())) {
  827. $link['title'] = drupal_placeholder($link['title']);
  828. $link['attributes'] = array('title' => t('This user can switch back.'));
  829. $link['html'] = TRUE;
  830. }
  831. $links[] = $link;
  832. }
  833. }
  834. if (array_key_exists($user->uid, $links)) {
  835. $links[$user->uid]['title'] = '<strong>' . $links[$user->uid]['title'] . '</strong>';
  836. }
  837. return $links;
  838. }
  839. /**
  840. * Comparison helper function for uasort() in devel_switch_user_list().
  841. *
  842. * Sorts the Switch User links by the user's last access timestamp.
  843. */
  844. function _devel_switch_user_list_cmp($a, $b) {
  845. return $b['last_access'] - $a['last_access'];
  846. }
  847. function devel_switch_user_form() {
  848. $form['username'] = array(
  849. '#type' => 'textfield',
  850. '#description' => t('Enter username'),
  851. '#autocomplete_path' => 'user/autocomplete',
  852. '#maxlength' => USERNAME_MAX_LENGTH,
  853. '#size' => 16,
  854. );
  855. $form['submit'] = array(
  856. '#type' => 'submit',
  857. '#value' => t('Switch'),
  858. );
  859. $form['#attributes'] = array('class' => array('clearfix'));
  860. return $form;
  861. }
  862. function devel_doc_function_form() {
  863. $version = devel_get_core_version(VERSION);
  864. $form['function'] = array(
  865. '#type' => 'textfield',
  866. '#description' => t('Enter function name for api lookup'),
  867. '#size' => 16,
  868. '#maxlength' => 255,
  869. );
  870. $form['version'] = array('#type' => 'value', '#value' => $version);
  871. $form['submit_button'] = array(
  872. '#type' => 'submit',
  873. '#value' => t('Submit'),
  874. );
  875. return $form;
  876. }
  877. function devel_doc_function_form_submit($form, &$form_state) {
  878. $version = $form_state['values']['version'];
  879. $function = $form_state['values']['function'];
  880. $api = variable_get('devel_api_url', 'api.drupal.org');
  881. $form_state['redirect'] = "http://$api/api/function/$function/$version";
  882. }
  883. function devel_switch_user_form_validate($form, &$form_state) {
  884. if (!$account = user_load_by_name($form_state['values']['username'])) {
  885. form_set_error('username', t('Username not found'));
  886. }
  887. }
  888. function devel_switch_user_form_submit($form, &$form_state) {
  889. $path = 'devel/switch/' . $form_state['values']['username'];
  890. $form_state['redirect'] = array(
  891. $path,
  892. array(
  893. 'query' => array(
  894. 'destination' => '',
  895. 'token' => drupal_get_token($path . '|'),
  896. )));
  897. }
  898. /**
  899. * Implements hook_drupal_goto_alter().
  900. */
  901. function devel_drupal_goto_alter($path, $options, $http_response_code) {
  902. global $user;
  903. if (isset($path) && !devel_silent()) {
  904. // The page we are leaving is a drupal_goto(). Present a redirection page
  905. // so that the developer can see the intermediate query log.
  906. // We don't want to load user module here, so keep function_exists() call.
  907. if (isset($user) && function_exists('user_access') && user_access('access devel information') && variable_get('devel_redirect_page', 0)) {
  908. $destination = function_exists('url') ? url($path, $options) : $path;
  909. $output = t_safe('<p>The user is being redirected to <a href="@destination">@destination</a>.</p>', array('@destination' => $destination));
  910. drupal_deliver_page($output);
  911. // Don't allow the automatic redirect to happen.
  912. exit();
  913. }
  914. else {
  915. $GLOBALS['devel_redirecting'] = TRUE;
  916. }
  917. }
  918. }
  919. /**
  920. * Implements hook_library_alter().
  921. */
  922. function devel_library_alter(&$libraries, $module) {
  923. // Use an uncompressed version of jQuery for debugging.
  924. if ($module === 'system' && variable_get('devel_use_uncompressed_jquery', FALSE) && isset($libraries['jquery'])) {
  925. // Make sure we're not changing the jQuery version used on the site.
  926. if (version_compare($libraries['jquery']['version'], '1.4.4', '=')) {
  927. $libraries['jquery']['js'] = array(
  928. drupal_get_path('module', 'devel') . '/jquery-1.4.4-uncompressed.js' => array('weight' => JS_LIBRARY - 20),
  929. );
  930. }
  931. else {
  932. if (!devel_silent() && user_access('access devel information')) {
  933. drupal_set_message(t('jQuery could not be replaced with an uncompressed version of 1.4.4, because jQuery @version is running on the site.', array('@version' => $libraries['jquery']['version'])));
  934. }
  935. }
  936. }
  937. }
  938. /**
  939. * Runs on shutdown to clean up and display developer information.
  940. *
  941. * devel_boot() registers this function as a shutdown function.
  942. * The bulk of the work is done in devel_shutdown_real().
  943. */
  944. function devel_shutdown() {
  945. // Register the real shutdown function so it runs later than other shutdown functions.
  946. drupal_register_shutdown_function('devel_shutdown_real');
  947. global $devel_run_id;
  948. $devel_run_id = devel_xhprof_is_enabled() ? devel_shutdown_xhprof(): NULL;
  949. if ($devel_run_id && function_exists('drush_log')) {
  950. drush_log('xhprof link: ' . devel_xhprof_link($devel_run_id, 'url'), 'notice');
  951. }
  952. }
  953. function devel_page_alter($page) {
  954. if (variable_get('devel_page_alter', FALSE) && user_access('access devel information')) {
  955. dpm($page, 'page');
  956. }
  957. }
  958. // AJAX render reponses sometimers are sent as text/html so we have to catch them here
  959. // and disable our footer stuff.
  960. function devel_ajax_render_alter() {
  961. $GLOBALS['devel_shutdown'] = FALSE;
  962. }
  963. /**
  964. * Runs on shutdown to display developer information in the footer.
  965. *
  966. * devel_shutdown() registers this function as a shutdown function.
  967. */
  968. function devel_shutdown_real() {
  969. global $user;
  970. $output = $txt = '';
  971. // Set $GLOBALS['devel_shutdown'] = FALSE in order to supress the
  972. // devel footer for a page. Not necessary if your page outputs any
  973. // of the Content-type http headers tested below (e.g. text/xml,
  974. // text/javascript, etc). This is is advised where applicable.
  975. if (!devel_silent() && !isset($GLOBALS['devel_shutdown']) && !isset($GLOBALS['devel_redirecting'])) {
  976. // Try not to break non html pages.
  977. if (function_exists('drupal_get_http_header')) {
  978. $header = drupal_get_http_header('content-type');
  979. if ($header) {
  980. $formats = array('xml', 'javascript', 'json', 'plain', 'image', 'application', 'csv', 'x-comma-separated-values');
  981. foreach ($formats as $format) {
  982. if (strstr($header, $format)) {
  983. return;
  984. }
  985. }
  986. }
  987. }
  988. if (isset($user) && user_access('access devel information')) {
  989. $queries = (devel_query_enabled() ? Database::getLog('devel', 'default') : NULL);
  990. $output .= devel_shutdown_summary($queries);
  991. $output .= devel_shutdown_query($queries);
  992. }
  993. if ($output) {
  994. // TODO: gzip this text if we are sending a gzip page. see drupal_page_header().
  995. // For some reason, this is not actually printing for cached pages even though it gets executed
  996. // and $output looks good.
  997. print $output;
  998. }
  999. }
  1000. }
  1001. function devel_shutdown_summary($queries) {
  1002. $sum = 0;
  1003. $output = '';
  1004. list($counts, $query_summary) = devel_query_summary($queries);
  1005. if (variable_get('devel_query_display', FALSE)) {
  1006. // Query log on.
  1007. $output .= $query_summary;
  1008. $output .= t_safe(' Queries exceeding @threshold ms are <span class="marker">highlighted</span>.', array('@threshold' => variable_get('devel_execution', 5)));
  1009. }
  1010. if (variable_get('dev_timer', 0)) {
  1011. $output .= devel_timer();
  1012. }
  1013. if (devel_xhprof_is_enabled()) {
  1014. $output .= ' ' . devel_xhprof_link($GLOBALS['devel_run_id']);
  1015. }
  1016. $output .= devel_shutdown_memory();
  1017. if ($output) {
  1018. return '<div class="dev-query">' . $output . '</div>';
  1019. }
  1020. }
  1021. function devel_shutdown_xhprof() {
  1022. $namespace = variable_get('site_name', ''); // namespace for your application
  1023. $xhprof_data = xhprof_disable();
  1024. $xhprof_runs = new XHProfRuns_Default();
  1025. return $xhprof_runs->save_run($xhprof_data, $namespace);
  1026. }
  1027. function devel_xhprof_link($run_id, $type = 'link') {
  1028. // @todo: render results from within Drupal.
  1029. $xhprof_url = variable_get('devel_xhprof_url', '');
  1030. $namespace = variable_get('site_name', ''); // namespace for your application
  1031. if ($xhprof_url) {
  1032. $url = $xhprof_url . "/index.php?run=$run_id&source=$namespace";
  1033. return $type == 'url' ? $url : t('<a href="@xhprof">XHProf output</a>. ', array('@xhprof' => $url));
  1034. }
  1035. }
  1036. function devel_shutdown_memory() {
  1037. global $memory_init;
  1038. if (variable_get('dev_mem', FALSE)) {
  1039. $memory_shutdown = memory_get_usage();
  1040. $args = array('@memory_boot' => round($memory_init / 1024 / 1024, 2), '@memory_shutdown' => round($memory_shutdown / 1024 / 1024, 2), '@memory_peak' => round(memory_get_peak_usage(TRUE) / 1024 / 1024, 2));
  1041. $msg = '<span class="dev-memory-usages"> Memory used at: devel_boot()=<strong>@memory_boot</strong> MB, devel_shutdown()=<strong>@memory_shutdown</strong> MB, PHP peak=<strong>@memory_peak</strong> MB.</span>';
  1042. // theme() may not be available. not t() either.
  1043. return t_safe($msg, $args);
  1044. }
  1045. }
  1046. function devel_shutdown_query($queries) {
  1047. if (!empty($queries)) {
  1048. if (function_exists('theme_get_registry') && theme_get_registry()) {
  1049. // Safe to call theme('table).
  1050. list($counts, $query_summary) = devel_query_summary($queries);
  1051. $output = devel_query_table($queries, $counts);
  1052. // Save all queries to a file in temp dir. Retrieved via AJAX.
  1053. devel_query_put_contents($queries);
  1054. }
  1055. else {
  1056. $output = '</div>' . dprint_r($queries, TRUE);
  1057. }
  1058. return $output;
  1059. }
  1060. }
  1061. // Write the variables information to the a file. It will be retrieved on demand via AJAX.
  1062. function devel_query_put_contents($queries) {
  1063. $request_id = mt_rand(1, 1000000);
  1064. $path = "temporary://devel_querylog";
  1065. // Create the devel_querylog within the temp folder, if needed.
  1066. file_prepare_directory($path, FILE_CREATE_DIRECTORY);
  1067. // Occassionally wipe the querylog dir so that files don't accumulate.
  1068. if (mt_rand(1, 1000) == 401) {
  1069. devel_empty_dir($path);
  1070. }
  1071. $path .= "/$request_id.txt";
  1072. $path = file_stream_wrapper_uri_normalize($path);
  1073. // Save queries as a json array. Suppress errors due to recursion ()
  1074. file_put_contents($path, @json_encode($queries));
  1075. $settings['devel'] = array(
  1076. // A random string that is sent to the browser. It enables the AJAX to retrieve queries from this request.
  1077. 'request_id' => $request_id,
  1078. );
  1079. print '<script type="text/javascript">jQuery.extend(Drupal.settings, '. json_encode($settings) .");</script>\n";
  1080. }
  1081. function devel_query_enabled() {
  1082. return method_exists('Database', 'getLog') && variable_get('devel_query_display', FALSE);
  1083. }
  1084. function devel_query_summary($queries) {
  1085. if (variable_get('devel_query_display', FALSE) && is_array($queries)) {
  1086. $sum = 0;
  1087. foreach ($queries as $query) {
  1088. $text[] = $query['query'];
  1089. $sum += $query['time'];
  1090. }
  1091. $counts = array_count_values($text);
  1092. return array($counts, t_safe('Executed @queries queries in @time ms.', array('@queries' => count($queries), '@time' => round($sum * 1000, 2))));
  1093. }
  1094. }
  1095. function t_safe($string, $args) {
  1096. // get_t caused problems here with theme registry after changing on admin/build/modules. the theme_get_registry call is needed.
  1097. if (function_exists('t') && function_exists('theme_get_registry')) {
  1098. theme_get_registry();
  1099. return t($string, $args);
  1100. }
  1101. else {
  1102. strtr($string, $args);
  1103. }
  1104. }
  1105. function devel_get_core_version($version) {
  1106. $version_parts = explode('.', $version);
  1107. // Map from 4.7.10 -> 4.7
  1108. if ($version_parts[0] < 5) {
  1109. return $version_parts[0] .'.'. $version_parts[1];
  1110. }
  1111. // Map from 5.5 -> 5 or 6.0-beta2 -> 6
  1112. else {
  1113. return $version_parts[0];
  1114. }
  1115. }
  1116. // See http://drupal.org/node/126098
  1117. function devel_is_compatible_optimizer() {
  1118. ob_start();
  1119. phpinfo();
  1120. $info = ob_get_contents();
  1121. ob_end_clean();
  1122. // Match the Zend Optimizer version in the phpinfo information
  1123. $found = preg_match('/Zend&nbsp;Optimizer&nbsp;v([0-9])\.([0-9])\.([0-9])/', $info, $matches);
  1124. if ($matches) {
  1125. $major = $matches[1];
  1126. $minor = $matches[2];
  1127. $build = $matches[3];
  1128. if ($major >= 3) {
  1129. if ($minor >= 3) {
  1130. return TRUE;
  1131. }
  1132. elseif ($minor == 2 && $build >= 8) {
  1133. return TRUE;
  1134. }
  1135. else {
  1136. return FALSE;
  1137. }
  1138. }
  1139. else {
  1140. return FALSE;
  1141. }
  1142. }
  1143. else {
  1144. return TRUE;
  1145. }
  1146. }
  1147. /**
  1148. * Generates the execute block form.
  1149. */
  1150. function devel_execute_block_form() {
  1151. $form['execute'] = array(
  1152. '#type' => 'fieldset',
  1153. '#title' => t('Execute PHP Code'),
  1154. '#collapsible' => TRUE,
  1155. '#collapsed' => (!isset($_SESSION['devel_execute_code'])),
  1156. );
  1157. $form['#submit'] = array('devel_execute_form_submit');
  1158. return array_merge_recursive($form, devel_execute_form());
  1159. }
  1160. /**
  1161. * Generates the execute form.
  1162. */
  1163. function devel_execute_form() {
  1164. $form['execute']['code'] = array(
  1165. '#type' => 'textarea',
  1166. '#title' => t('PHP code to execute'),
  1167. '#description' => t('Enter some code. Do not use <code>&lt;?php ?&gt;</code> tags.'),
  1168. '#default_value' => (isset($_SESSION['devel_execute_code']) ? $_SESSION['devel_execute_code'] : ''),
  1169. '#rows' => 20,
  1170. );
  1171. $form['execute']['op'] = array('#type' => 'submit', '#value' => t('Execute'));
  1172. $form['#redirect'] = FALSE;
  1173. if (isset($_SESSION['devel_execute_code'])) {
  1174. unset($_SESSION['devel_execute_code']);
  1175. }
  1176. return $form;
  1177. }
  1178. /**
  1179. * Process PHP execute form submissions.
  1180. */
  1181. function devel_execute_form_submit($form, &$form_state) {
  1182. ob_start();
  1183. print eval($form_state['values']['code']);
  1184. $_SESSION['devel_execute_code'] = $form_state['values']['code'];
  1185. dsm(ob_get_clean());
  1186. }
  1187. /**
  1188. * Switch from original user to another user and back.
  1189. * We don't call session_save_session() because we really want to change users. Usually unsafe!
  1190. *
  1191. * @param $name The username to switch to, or NULL to log out.
  1192. */
  1193. function devel_switch_user($name = NULL) {
  1194. global $user;
  1195. if ($user->uid) {
  1196. module_invoke_all('user_logout', $user);
  1197. }
  1198. if (isset($name) && $account = user_load_by_name($name)) {
  1199. $old_uid = $user->uid;
  1200. $user = $account;
  1201. $user->timestamp = time() - 9999;
  1202. if (!$old_uid) {
  1203. // Switch from anonymous to authorized.
  1204. drupal_session_regenerate();
  1205. }
  1206. $edit = array();
  1207. user_module_invoke('login', $edit, $user);
  1208. }
  1209. elseif ($user->uid) {
  1210. session_destroy();
  1211. }
  1212. drupal_goto();
  1213. }
  1214. /**
  1215. * Print an object or array using either Krumo (if installed) or devel_print_object()
  1216. *
  1217. * @param $object
  1218. * array or object to print
  1219. * @param $prefix
  1220. * prefixing for output items
  1221. */
  1222. function kdevel_print_object($object, $prefix = NULL) {
  1223. return has_krumo() ? krumo_ob($object) : devel_print_object($object, $prefix);
  1224. }
  1225. // Save krumo htlm using output buffering.
  1226. function krumo_ob($object) {
  1227. ob_start();
  1228. krumo($object);
  1229. $output = ob_get_contents();
  1230. ob_end_clean();
  1231. return $output;
  1232. }
  1233. /**
  1234. * Display an object or array
  1235. *
  1236. * @param $object
  1237. * the object or array
  1238. * @param $prefix
  1239. * prefix for the output items (example "$node->", "$user->", "$")
  1240. * @param $header
  1241. * set to FALSE to suppress the output of the h3
  1242. */
  1243. function devel_print_object($object, $prefix = NULL, $header = TRUE) {
  1244. drupal_add_css(drupal_get_path('module', 'devel') .'/devel.css');
  1245. $output = '<div class="devel-obj-output">';
  1246. if ($header) {
  1247. $output .= '<h3>'. t('Display of !type !obj', array('!type' => str_replace(array('$', '->'), '', $prefix), '!obj' => gettype($object))) .'</h3>';
  1248. }
  1249. $output .= _devel_print_object($object, $prefix);
  1250. $output .= '</div>';
  1251. return $output;
  1252. }
  1253. /**
  1254. * Recursive (and therefore magical) function goes through an array or object and
  1255. * returns a nicely formatted listing of its contents.
  1256. *
  1257. * @param $obj
  1258. * array or object to recurse through
  1259. * @param $prefix
  1260. * prefix for the output items (example "$node->", "$user->", "$")
  1261. * @param $parents
  1262. * used by recursion
  1263. * @param $object
  1264. * used by recursion
  1265. * @return
  1266. * fomatted html
  1267. *
  1268. * @todo
  1269. * currently there are problems sending an array with a varname
  1270. */
  1271. function _devel_print_object($obj, $prefix = NULL, $parents = NULL, $object = FALSE) {
  1272. static $root_type, $out_format;
  1273. // TODO: support objects with references. See http://drupal.org/node/234581.
  1274. if (isset($obj->view)) {
  1275. return;
  1276. }
  1277. if (!isset($root_type)) {
  1278. $root_type = gettype($obj);
  1279. if ($root_type == 'object') {
  1280. $object = TRUE;
  1281. }
  1282. }
  1283. if (is_object($obj)) {
  1284. $obj = (array)$obj;
  1285. }
  1286. if (is_array($obj)) {
  1287. $output = "<dl>\n";
  1288. foreach ($obj as $field => $value) {
  1289. if ($field == 'devel_flag_reference') {
  1290. continue;
  1291. }
  1292. if (!is_null($parents)) {
  1293. if ($object) {
  1294. $field = $parents .'->'. $field;
  1295. }
  1296. else {
  1297. if (is_int($field)) {
  1298. $field = $parents .'['. $field .']';
  1299. }
  1300. else {
  1301. $field = $parents .'[\''. $field .'\']';
  1302. }
  1303. }
  1304. }
  1305. $type = gettype($value);
  1306. $show_summary = TRUE;
  1307. $summary = NULL;
  1308. if ($show_summary) {
  1309. switch ($type) {
  1310. case 'string' :
  1311. case 'float' :
  1312. case 'integer' :
  1313. if (strlen($value) == 0) {
  1314. $summary = t("{empty}");
  1315. }
  1316. elseif (strlen($value) < 40) {
  1317. $summary = htmlspecialchars($value);
  1318. }
  1319. else {
  1320. $summary = format_plural(drupal_strlen($value), '1 character', '@count characters');
  1321. }
  1322. break;
  1323. case 'array' :
  1324. case 'object' :
  1325. $summary = format_plural(count((array)$value), '1 element', '@count elements');
  1326. break;
  1327. case 'boolean' :
  1328. $summary = $value ? t('TRUE') : t('FALSE');
  1329. break;
  1330. }
  1331. }
  1332. if (!is_null($summary)) {
  1333. $typesum = '('. $type .', <em>'. $summary .'</em>)';
  1334. }
  1335. else {
  1336. $typesum = '('. $type .')';
  1337. }
  1338. $output .= '<span class="devel-attr">';
  1339. $output .= "<dt><span class=\"field\">{$prefix}{$field}</span> $typesum</dt>\n";
  1340. $output .= "<dd>\n";
  1341. // Check for references.
  1342. if (is_array($value) && isset($value['devel_flag_reference'])) {
  1343. $value['devel_flag_reference'] = TRUE;
  1344. }
  1345. // Check for references to prevent errors from recursions.
  1346. if (is_array($value) && isset($value['devel_flag_reference']) && !$value['devel_flag_reference']) {
  1347. $value['devel_flag_reference'] = FALSE;
  1348. $output .= _devel_print_object($value, $prefix, $field);
  1349. }
  1350. elseif (is_object($value)) {
  1351. $value->devel_flag_reference = FALSE;
  1352. $output .= _devel_print_object((array)$value, $prefix, $field, TRUE);
  1353. }
  1354. else {
  1355. $value = is_bool($value) ? ($value ? 'TRUE' : 'FALSE') : $value;
  1356. $output .= htmlspecialchars(print_r($value, TRUE)) ."\n";
  1357. }
  1358. $output .= "</dd></span>\n";
  1359. }
  1360. $output .= "</dl>\n";
  1361. }
  1362. return $output;
  1363. }
  1364. /**
  1365. * Adds a table at the bottom of the page cataloguing data on all the database queries that were made to
  1366. * generate the page.
  1367. */
  1368. function devel_query_table($queries, $counts) {
  1369. $version = devel_get_core_version(VERSION);
  1370. $header = array ('ms', '#', 'where', 'ops', 'query', 'target');
  1371. $i = 0;
  1372. $api = variable_get('devel_api_url', 'api.drupal.org');
  1373. foreach ($queries as $query) {
  1374. $function = !empty($query['caller']['class']) ? $query['caller']['class'] . '::' : '';
  1375. $function .= $query['caller']['function'];
  1376. $count = isset($counts[$query['query']]) ? $counts[$query['query']] : 0;
  1377. $diff = round($query['time'] * 1000, 2);
  1378. if ($diff > variable_get('devel_execution', 5)) {
  1379. $cell[$i][] = array ('data' => $diff, 'class' => 'marker');
  1380. }
  1381. else {
  1382. $cell[$i][] = $diff;
  1383. }
  1384. $cell[$i][] = $count;
  1385. $cell[$i][] = l($function, "http://$api/api/function/$function/$version");
  1386. $ops[] = l('P', '', array('attributes' => array('title' => 'Show placeholders', 'class' => 'dev-placeholders', 'qid' => $i)));
  1387. $ops[] = l('A', '', array('attributes' => array('title' => 'Show arguments', 'class' => 'dev-arguments', 'qid' => $i)));
  1388. // EXPLAIN only valid for select queries.
  1389. if (strpos($query['query'], 'UPDATE') === FALSE && strpos($query['query'], 'INSERT') === FALSE && strpos($query['query'], 'DELETE') === FALSE) {
  1390. $ops[] = l('E', '', array('attributes' => array('title' => 'Show EXPLAIN', 'class' => 'dev-explain', 'qid' => $i)));
  1391. }
  1392. $cell[$i][] = implode(' ', $ops);
  1393. // 3 divs for each variation of the query. Last 2 are hidden by default.
  1394. $placeholders = '<div class="dev-placeholders">' . check_plain($query['query']) . "</div>\n";
  1395. $args = '<div class="dev-arguments" style="display: none;"></div>' . "\n";
  1396. $explain = '<div class="dev-explain" style="display: none;"></div>' . "\n";
  1397. $cell[$i][] = array(
  1398. 'id' => "devel-query-$i",
  1399. 'data' => $placeholders . $args . $explain,
  1400. );
  1401. $cell[$i][] = $query['target'];
  1402. $i++;
  1403. unset($diff, $count, $ops);
  1404. }
  1405. if (variable_get('devel_query_sort', DEVEL_QUERY_SORT_BY_SOURCE)) {
  1406. usort($cell, '_devel_table_sort');
  1407. }
  1408. return theme('devel_querylog', array('header' => $header, 'rows' => $cell));
  1409. }
  1410. function theme_devel_querylog_row($variables) {
  1411. $row = $variables['row'];
  1412. $i = 0;
  1413. $output = '';
  1414. foreach ($row as $cell) {
  1415. $i++;
  1416. if (is_array($cell)) {
  1417. $data = !empty($cell['data']) ? $cell['data'] : '';
  1418. unset($cell['data']);
  1419. $attr = $cell;
  1420. }
  1421. else {
  1422. $data = $cell;
  1423. $attr = array();
  1424. }
  1425. if (!empty($attr['class'])) {
  1426. $attr['class'] .= " cell cell-$i";
  1427. }
  1428. else {
  1429. $attr['class'] = "cell cell-$i";
  1430. }
  1431. $attr = drupal_attributes($attr);
  1432. $output .= "<div $attr>$data</div>";
  1433. }
  1434. return $output;
  1435. }
  1436. function theme_devel_querylog($variables) {
  1437. $header = $variables['header'];
  1438. $rows = $variables['rows'];
  1439. $output = '';
  1440. if (!empty($header)) {
  1441. $output .= "<div class='devel-querylog devel-querylog-header clear-block'>";
  1442. $output .= theme('devel_querylog_row', array('row' => $header));
  1443. $output .= "</div>";
  1444. }
  1445. if (!empty($rows)) {
  1446. $i = 0;
  1447. foreach ($rows as $row) {
  1448. $i++;
  1449. $zebra = ($i % 2) == 0 ? 'even' : 'odd';
  1450. $output .= "<div class='devel-querylog devel-querylog-$zebra clear-block'>";
  1451. $output .= theme('devel_querylog_row', array('row' => $row));
  1452. $output .= "</div>";
  1453. }
  1454. }
  1455. return $output;
  1456. }
  1457. function _devel_table_sort($a, $b) {
  1458. $a = is_array($a[0]) ? $a[0]['data'] : $a[0];
  1459. $b = is_array($b[0]) ? $b[0]['data'] : $b[0];
  1460. if ($a < $b) {
  1461. return 1;
  1462. }
  1463. if ($a > $b) {
  1464. return -1;
  1465. }
  1466. return 0;
  1467. }
  1468. /**
  1469. * Displays page execution time at the bottom of the page.
  1470. */
  1471. function devel_timer() {
  1472. $time = timer_read('page');
  1473. return t_safe(' Page execution time was @time ms.', array('@time' => $time));
  1474. }
  1475. // An alias for drupal_debug().
  1476. function dd($data, $label = NULL) {
  1477. return drupal_debug($data, $label);
  1478. }
  1479. /**
  1480. * Logs a variable to a drupal_debug.txt in the site's temp directory.
  1481. *
  1482. * @param $data
  1483. * The variable to log to the drupal_debug.txt log file.
  1484. * @param $label
  1485. * (optional) If set, a label to output before $data in the log file.
  1486. *
  1487. * @return
  1488. * No return value if successful, FALSE if the log file could not be written
  1489. * to.
  1490. *
  1491. * @see dd()
  1492. * @see http://drupal.org/node/314112
  1493. */
  1494. function drupal_debug($data, $label = NULL) {
  1495. $out = ($label ? $label . ': ' : '') . print_r($data, TRUE) . "\n";
  1496. // The temp directory does vary across multiple simpletest instances.
  1497. $file = 'temporary://drupal_debug.txt';
  1498. if (file_put_contents($file, $out, FILE_APPEND) === FALSE) {
  1499. drupal_set_message(t('The file could not be written.'), 'error');
  1500. return FALSE;
  1501. }
  1502. }
  1503. /**
  1504. * Prints the arguments passed into the current function
  1505. */
  1506. function dargs($always = TRUE) {
  1507. static $printed;
  1508. if ($always || !$printed) {
  1509. $bt = debug_backtrace();
  1510. print kdevel_print_object($bt[1]['args']);
  1511. $printed = TRUE;
  1512. }
  1513. }
  1514. /**
  1515. * Prints a SQL string from a DBTNG SelectQuery object.
  1516. *
  1517. * Includes quoted arguments.
  1518. *
  1519. * @param $query
  1520. * An object that implements the SelectQueryInterface interface.
  1521. * @param $return
  1522. * Whether to return the string. Default is FALSE, meaning to print it
  1523. * and return $query instead.
  1524. * @param $name
  1525. * Optional name for identifying the output.
  1526. * @return
  1527. * The $query object, or the query string if $return was TRUE.
  1528. */
  1529. function dpq($query, $return = FALSE, $name = NULL) {
  1530. if (user_access('access devel information')) {
  1531. $query->preExecute();
  1532. $sql = (string) $query;
  1533. $quoted = array();
  1534. $connection = Database::getConnection();
  1535. foreach ((array)$query->arguments() as $key => $val) {
  1536. $quoted[$key] = $connection->quote($val);
  1537. }
  1538. $sql = strtr($sql, $quoted);
  1539. if ($return) {
  1540. return $sql;
  1541. }
  1542. dpm($sql, $name);
  1543. }
  1544. return ($return ? NULL : $query);
  1545. }
  1546. /**
  1547. * Print a variable to the 'message' area of the page.
  1548. *
  1549. * Uses drupal_set_message().
  1550. *
  1551. * @param $input
  1552. * An arbitrary value to output.
  1553. * @param $name
  1554. * Optional name for identifying the output.
  1555. * @param $type
  1556. * Optional message type for drupal_set_message(), defaults to 'status'.
  1557. * @return
  1558. * The unaltered input value.
  1559. */
  1560. function dpm($input, $name = NULL, $type = 'status') {
  1561. if (user_access('access devel information')) {
  1562. $export = kprint_r($input, TRUE, $name);
  1563. drupal_set_message($export, $type);
  1564. }
  1565. return $input;
  1566. }
  1567. /**
  1568. * drupal_var_export() a variable to the 'message' area of the page.
  1569. *
  1570. * Uses drupal_set_message().
  1571. *
  1572. * @param $input
  1573. * An arbitrary value to output.
  1574. * @param $name
  1575. * Optional name for identifying the output.
  1576. * @return
  1577. * The unaltered input value.
  1578. */
  1579. function dvm($input, $name = NULL) {
  1580. if (user_access('access devel information')) {
  1581. $export = dprint_r($input, TRUE, $name, 'drupal_var_export', FALSE);
  1582. drupal_set_message($export);
  1583. }
  1584. return $input;
  1585. }
  1586. // legacy function that was poorly named. use dpm() instead, since the 'p' maps to 'print_r'
  1587. function dsm($input, $name = NULL) {
  1588. return dpm($input, $name);
  1589. }
  1590. /**
  1591. * An alias for dprint_r(). Saves carpal tunnel syndrome.
  1592. */
  1593. function dpr($input, $return = FALSE, $name = NULL) {
  1594. return dprint_r($input, $return, $name);
  1595. }
  1596. /**
  1597. * An alias for kprint_r(). Saves carpal tunnel syndrome.
  1598. */
  1599. function kpr($input, $return = FALSE, $name = NULL) {
  1600. return kprint_r($input, $return, $name);
  1601. }
  1602. /**
  1603. * Like dpr, but uses drupal_var_export() instead
  1604. */
  1605. function dvr($input, $return = FALSE, $name = NULL) {
  1606. return dprint_r($input, $return, $name, 'drupal_var_export', FALSE);
  1607. }
  1608. function kprint_r($input, $return = FALSE, $name = NULL, $function = 'print_r') {
  1609. // We do not want to krumo() strings and integers and such
  1610. if (merits_krumo($input)) {
  1611. if (user_access('access devel information')) {
  1612. return $return ? (isset($name) ? $name .' => ' : '') . krumo_ob($input) : krumo($input);
  1613. }
  1614. }
  1615. else {
  1616. return dprint_r($input, $return, $name, $function);
  1617. }
  1618. }
  1619. /**
  1620. * Pretty-print a variable to the browser (no krumo).
  1621. * Displays only for users with proper permissions. If
  1622. * you want a string returned instead of a print, use the 2nd param.
  1623. */
  1624. function dprint_r($input, $return = FALSE, $name = NULL, $function = 'print_r', $check= TRUE) {
  1625. if (user_access('access devel information')) {
  1626. if ($name) {
  1627. $name .= ' => ';
  1628. }
  1629. if ($function == 'drupal_var_export') {
  1630. include_once DRUPAL_ROOT . '/includes/utility.inc';
  1631. $output = drupal_var_export($input);
  1632. }
  1633. else {
  1634. ob_start();
  1635. $function($input);
  1636. $output = ob_get_clean();
  1637. }
  1638. if ($check) {
  1639. $output = check_plain($output);
  1640. }
  1641. if (count($input, COUNT_RECURSIVE) > DEVEL_MIN_TEXTAREA) {
  1642. // don't use fapi here because sometimes fapi will not be loaded
  1643. $printed_value = "<textarea rows=30 style=\"width: 100%;\">\n". $name . $output .'</textarea>';
  1644. }
  1645. else {
  1646. $printed_value = '<pre>'. $name . $output .'</pre>';
  1647. }
  1648. if ($return) {
  1649. return $printed_value;
  1650. }
  1651. else {
  1652. print $printed_value;
  1653. }
  1654. }
  1655. }
  1656. /**
  1657. * Prints a renderable array element to the screen using kprint_r().
  1658. *
  1659. * #pre_render and/or #post_render pass-through callback for kprint_r().
  1660. *
  1661. * @todo Investigate appending to #suffix.
  1662. * @todo Investigate label derived from #id, #title, #name, and #theme.
  1663. */
  1664. function devel_render() {
  1665. $args = func_get_args();
  1666. // #pre_render and #post_render pass the rendered $element as last argument.
  1667. kprint_r(end($args));
  1668. // #pre_render and #post_render expect the first argument to be returned.
  1669. return reset($args);
  1670. }
  1671. /**
  1672. * Print the function call stack.
  1673. */
  1674. function ddebug_backtrace($return = FALSE, $pop = 0) {
  1675. if (user_access('access devel information')) {
  1676. $backtrace = debug_backtrace();
  1677. while ($pop-- > 0) {
  1678. array_shift($backtrace);
  1679. }
  1680. $counter = count($backtrace);
  1681. $path = $backtrace[$counter - 1]['file'];
  1682. $path = substr($path, 0, strlen($path) - 10);
  1683. $paths[$path] = strlen($path) + 1;
  1684. $paths[DRUPAL_ROOT] = strlen(DRUPAL_ROOT) + 1;
  1685. $nbsp = "\xC2\xA0";
  1686. // Show message if error_level is ERROR_REPORTING_DISPLAY_SOME or higher.
  1687. // (This is Drupal's error_level, which is different from $error_level,
  1688. // and we purposely ignore the difference between _SOME and _ALL,
  1689. // see #970688!)
  1690. if (variable_get('error_level', 1) >= 1) {
  1691. while (!empty($backtrace)) {
  1692. $call = array();
  1693. if (isset($backtrace[0]['file'])) {
  1694. $call['file'] = $backtrace[0]['file'];
  1695. foreach ($paths as $path => $len) {
  1696. if (strpos($backtrace[0]['file'], $path) === 0) {
  1697. $call['file'] = substr($backtrace[0]['file'], $len);
  1698. }
  1699. }
  1700. $call['file'] .= ':' . $backtrace[0]['line'];
  1701. }
  1702. if (isset($backtrace[1])) {
  1703. if (isset($backtrace[1]['class'])) {
  1704. $function = $backtrace[1]['class'] . $backtrace[1]['type'] . $backtrace[1]['function'] . '()';
  1705. }
  1706. else {
  1707. $function = $backtrace[1]['function'] . '()';
  1708. }
  1709. $call['args'] = $backtrace[1]['args'];
  1710. }
  1711. else {
  1712. $function = 'main()';
  1713. $call['args'] = $_GET;
  1714. }
  1715. $nicetrace[($counter <= 10 ? $nbsp : '') . --$counter . ': ' . $function] = $call;
  1716. array_shift($backtrace);
  1717. }
  1718. if ($return) {
  1719. return $nicetrace;
  1720. }
  1721. kprint_r($nicetrace);
  1722. }
  1723. }
  1724. }
  1725. // Delete all files in a dir.
  1726. function devel_empty_dir($dir) {
  1727. foreach (new DirectoryIterator($dir) as $fileInfo) {
  1728. if ($fileInfo->isFile()) {
  1729. unlink($fileInfo->getPathname());
  1730. }
  1731. }
  1732. }
  1733. /*
  1734. * migration related functions
  1735. */
  1736. /**
  1737. * Regenerate the data in node_comment_statistics table. Technique comes from
  1738. * http://www.artfulsoftware.com/infotree/queries.php?&bw=1280#101
  1739. *
  1740. * @return void
  1741. **/
  1742. function devel_rebuild_node_comment_statistics() {
  1743. // Empty table
  1744. db_truncate('node_comment_statistics')->execute();
  1745. // TODO: DBTNG. Ignore keyword is Mysql only? Is only used in the rare case when
  1746. // two comments on the same node share same timestamp.
  1747. $sql = "
  1748. INSERT IGNORE INTO {node_comment_statistics} (nid, cid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) (
  1749. SELECT c.nid, c.cid, c.created, c.name, c.uid, c2.comment_count FROM {comment} c
  1750. JOIN (
  1751. SELECT c.nid, MAX(c.created) AS created, COUNT(*) AS comment_count FROM {comment} c WHERE status = 1 GROUP BY c.nid
  1752. ) AS c2 ON c.nid = c2.nid AND c.created = c2.created
  1753. )";
  1754. db_query($sql, array(':published' => COMMENT_PUBLISHED));
  1755. // Insert records into the node_comment_statistics for nodes that are missing.
  1756. $query = db_select('node', 'n');
  1757. $query->leftJoin('node_comment_statistics', 'ncs', 'ncs.nid = n.nid');
  1758. $query->addField('n', 'changed', 'last_comment_timestamp');
  1759. $query->addField('n', 'uid', 'last_comment_uid');
  1760. $query->addField('n', 'nid');
  1761. $query->addExpression('0', 'comment_count');
  1762. $query->addExpression('NULL', 'last_comment_name');
  1763. $query->isNull('ncs.comment_count');
  1764. db_insert('node_comment_statistics')
  1765. ->from($query)
  1766. ->execute();
  1767. }
  1768. /**
  1769. * Implements hook_form_alter().
  1770. *
  1771. * Adds mouse-over hints on the Permissions page to display
  1772. * language-independent machine names and module base names.
  1773. */
  1774. function devel_form_user_admin_permissions_alter(&$form, &$form_state) {
  1775. if (user_access('access devel information') && variable_get('devel_raw_names', FALSE)) {
  1776. foreach ($form['permission'] as $perm => $data) {
  1777. if (is_numeric($perm)) {
  1778. $form['permission'][$perm]['#markup'] = '<span title="' . $form['permission'][$perm]['#id'] . '">' . $form['permission'][$perm]['#markup'] . '</span>';
  1779. }
  1780. else {
  1781. $form['permission'][$perm]['#markup'] = '<span title="' . check_plain($perm) . '">' . $form['permission'][$perm]['#markup'] . '</span>';
  1782. }
  1783. }
  1784. }
  1785. }
  1786. /**
  1787. * Implements hook_form_alter().
  1788. *
  1789. * Adds mouse-over hints on the Modules page to display
  1790. * module base names.
  1791. */
  1792. function devel_form_system_modules_alter(&$form, &$form_state) {
  1793. if (user_access('access devel information') && variable_get('devel_raw_names', FALSE) && isset($form['modules']) && is_array($form['modules'])) {
  1794. foreach (element_children($form['modules']) as $key) {
  1795. if (isset($form['modules'][$key]['name']['#markup'])) {
  1796. $form['modules'][$key]['name']['#markup'] = '<span title="' . $key . '">' . $form['modules'][$key]['name']['#markup'] . '</span>';
  1797. }
  1798. elseif (is_array($form['modules'][$key])) {
  1799. foreach (element_children($form['modules'][$key]) as $key2) {
  1800. if (isset($form['modules'][$key][$key2]['name']['#markup'])) {
  1801. $form['modules'][$key][$key2]['name']['#markup'] = '<span title="' . $key2 . '">' . $form['modules'][$key][$key2]['name']['#markup'] . '</span>';
  1802. }
  1803. }
  1804. }
  1805. }
  1806. }
  1807. }