Text_Plain_Json.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Text Plain JSON Transformations plugin for phpMyAdmin
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Transformations\Output;
  7. use PhpMyAdmin\FieldMetadata;
  8. use PhpMyAdmin\Plugins\TransformationsPlugin;
  9. use PhpMyAdmin\ResponseRenderer;
  10. use function __;
  11. use function htmlspecialchars;
  12. /**
  13. * Handles the json transformation for text plain
  14. */
  15. class Text_Plain_Json extends TransformationsPlugin
  16. {
  17. public function __construct()
  18. {
  19. if (empty($GLOBALS['cfg']['CodemirrorEnable'])) {
  20. return;
  21. }
  22. $response = ResponseRenderer::getInstance();
  23. $scripts = $response->getHeader()
  24. ->getScripts();
  25. $scripts->addFile('vendor/codemirror/lib/codemirror.js');
  26. $scripts->addFile('vendor/codemirror/mode/javascript/javascript.js');
  27. $scripts->addFile('vendor/codemirror/addon/runmode/runmode.js');
  28. $scripts->addFile('transformations/json.js');
  29. }
  30. /**
  31. * Gets the transformation description of the specific plugin
  32. *
  33. * @return string
  34. */
  35. public static function getInfo()
  36. {
  37. return __('Formats text as JSON with syntax highlighting.');
  38. }
  39. /**
  40. * Does the actual work of each specific transformations plugin.
  41. *
  42. * @param string $buffer text to be transformed
  43. * @param array $options transformation options
  44. * @param FieldMetadata|null $meta meta information
  45. *
  46. * @return string
  47. */
  48. public function applyTransformation($buffer, array $options = [], ?FieldMetadata $meta = null)
  49. {
  50. return '<code class="json"><pre>' . "\n"
  51. . htmlspecialchars($buffer) . "\n"
  52. . '</pre></code>';
  53. }
  54. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  55. /**
  56. * Gets the plugin`s MIME type
  57. *
  58. * @return string
  59. */
  60. public static function getMIMEType()
  61. {
  62. return 'Text';
  63. }
  64. /**
  65. * Gets the plugin`s MIME subtype
  66. *
  67. * @return string
  68. */
  69. public static function getMIMESubtype()
  70. {
  71. return 'Plain';
  72. }
  73. /**
  74. * Gets the transformation name of the specific plugin
  75. *
  76. * @return string
  77. */
  78. public static function getName()
  79. {
  80. return 'JSON';
  81. }
  82. }