"Fossies" - the Fresh Open Source Software Archive

Member "phpMyAdmin-5.1.0-all-languages/libraries/classes/Plugins/Export/ExportYaml.php" (24 Feb 2021, 6847 Bytes) of package /linux/www/phpMyAdmin-5.1.0-all-languages.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "ExportYaml.php": 5.0.4-english_vs_5.1.0-english.

    1 <?php
    2 /**
    3  * Set of functions used to build YAML dumps of tables
    4  */
    5 
    6 declare(strict_types=1);
    7 
    8 namespace PhpMyAdmin\Plugins\Export;
    9 
   10 use PhpMyAdmin\DatabaseInterface;
   11 use PhpMyAdmin\Plugins\ExportPlugin;
   12 use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
   13 use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
   14 use PhpMyAdmin\Properties\Options\Items\HiddenPropertyItem;
   15 use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
   16 use function is_numeric;
   17 use function str_replace;
   18 use function stripslashes;
   19 
   20 /**
   21  * Handles the export for the YAML format
   22  */
   23 class ExportYaml extends ExportPlugin
   24 {
   25     public function __construct()
   26     {
   27         parent::__construct();
   28         $this->setProperties();
   29     }
   30 
   31     /**
   32      * Sets the export YAML properties
   33      *
   34      * @return void
   35      */
   36     protected function setProperties()
   37     {
   38         $exportPluginProperties = new ExportPluginProperties();
   39         $exportPluginProperties->setText('YAML');
   40         $exportPluginProperties->setExtension('yml');
   41         $exportPluginProperties->setMimeType('text/yaml');
   42         $exportPluginProperties->setForceFile(true);
   43         $exportPluginProperties->setOptionsText(__('Options'));
   44 
   45         // create the root group that will be the options field for
   46         // $exportPluginProperties
   47         // this will be shown as "Format specific options"
   48         $exportSpecificOptions = new OptionsPropertyRootGroup(
   49             'Format Specific Options'
   50         );
   51 
   52         // general options main group
   53         $generalOptions = new OptionsPropertyMainGroup('general_opts');
   54         // create primary items and add them to the group
   55         $leaf = new HiddenPropertyItem('structure_or_data');
   56         $generalOptions->addProperty($leaf);
   57         // add the main group to the root group
   58         $exportSpecificOptions->addProperty($generalOptions);
   59 
   60         // set the options for the export plugin property item
   61         $exportPluginProperties->setOptions($exportSpecificOptions);
   62         $this->properties = $exportPluginProperties;
   63     }
   64 
   65     /**
   66      * Outputs export header
   67      *
   68      * @return bool Whether it succeeded
   69      */
   70     public function exportHeader()
   71     {
   72         $this->export->outputHandler(
   73             '%YAML 1.1' . $GLOBALS['crlf'] . '---' . $GLOBALS['crlf']
   74         );
   75 
   76         return true;
   77     }
   78 
   79     /**
   80      * Outputs export footer
   81      *
   82      * @return bool Whether it succeeded
   83      */
   84     public function exportFooter()
   85     {
   86         $this->export->outputHandler('...' . $GLOBALS['crlf']);
   87 
   88         return true;
   89     }
   90 
   91     /**
   92      * Outputs database header
   93      *
   94      * @param string $db       Database name
   95      * @param string $db_alias Aliases of db
   96      *
   97      * @return bool Whether it succeeded
   98      */
   99     public function exportDBHeader($db, $db_alias = '')
  100     {
  101         return true;
  102     }
  103 
  104     /**
  105      * Outputs database footer
  106      *
  107      * @param string $db Database name
  108      *
  109      * @return bool Whether it succeeded
  110      */
  111     public function exportDBFooter($db)
  112     {
  113         return true;
  114     }
  115 
  116     /**
  117      * Outputs CREATE DATABASE statement
  118      *
  119      * @param string $db          Database name
  120      * @param string $export_type 'server', 'database', 'table'
  121      * @param string $db_alias    Aliases of db
  122      *
  123      * @return bool Whether it succeeded
  124      */
  125     public function exportDBCreate($db, $export_type, $db_alias = '')
  126     {
  127         return true;
  128     }
  129 
  130     /**
  131      * Outputs the content of a table in JSON format
  132      *
  133      * @param string $db        database name
  134      * @param string $table     table name
  135      * @param string $crlf      the end of line sequence
  136      * @param string $error_url the url to go back in case of error
  137      * @param string $sql_query SQL query for obtaining data
  138      * @param array  $aliases   Aliases of db/table/columns
  139      *
  140      * @return bool Whether it succeeded
  141      */
  142     public function exportData(
  143         $db,
  144         $table,
  145         $crlf,
  146         $error_url,
  147         $sql_query,
  148         array $aliases = []
  149     ) {
  150         global $dbi;
  151 
  152         $db_alias = $db;
  153         $table_alias = $table;
  154         $this->initAlias($aliases, $db_alias, $table_alias);
  155         $result = $dbi->query(
  156             $sql_query,
  157             DatabaseInterface::CONNECT_USER,
  158             DatabaseInterface::QUERY_UNBUFFERED
  159         );
  160 
  161         $columns_cnt = $dbi->numFields($result);
  162         $columns = [];
  163         for ($i = 0; $i < $columns_cnt; $i++) {
  164             $col_as = $dbi->fieldName($result, $i);
  165             if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
  166                 $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
  167             }
  168             $columns[$i] = stripslashes($col_as);
  169         }
  170 
  171         $buffer = '';
  172         $record_cnt = 0;
  173         while ($record = $dbi->fetchRow($result)) {
  174             $record_cnt++;
  175 
  176             // Output table name as comment if this is the first record of the table
  177             if ($record_cnt == 1) {
  178                 $buffer = '# ' . $db_alias . '.' . $table_alias . $crlf;
  179                 $buffer .= '-' . $crlf;
  180             } else {
  181                 $buffer = '-' . $crlf;
  182             }
  183 
  184             for ($i = 0; $i < $columns_cnt; $i++) {
  185                 if (! isset($record[$i])) {
  186                     continue;
  187                 }
  188 
  189                 if ($record[$i] === null) {
  190                     $buffer .= '  ' . $columns[$i] . ': null' . $crlf;
  191                     continue;
  192                 }
  193 
  194                 if (is_numeric($record[$i])) {
  195                     $buffer .= '  ' . $columns[$i] . ': ' . $record[$i] . $crlf;
  196                     continue;
  197                 }
  198 
  199                 $record[$i] = str_replace(
  200                     [
  201                         '\\',
  202                         '"',
  203                         "\n",
  204                         "\r",
  205                     ],
  206                     [
  207                         '\\\\',
  208                         '\"',
  209                         '\n',
  210                         '\r',
  211                     ],
  212                     $record[$i]
  213                 );
  214                 $buffer .= '  ' . $columns[$i] . ': "' . $record[$i] . '"' . $crlf;
  215             }
  216 
  217             if (! $this->export->outputHandler($buffer)) {
  218                 return false;
  219             }
  220         }
  221         $dbi->freeResult($result);
  222 
  223         return true;
  224     }
  225 
  226     /**
  227      * Outputs result raw query in YAML format
  228      *
  229      * @param string $err_url   the url to go back in case of error
  230      * @param string $sql_query the rawquery to output
  231      * @param string $crlf      the end of line sequence
  232      *
  233      * @return bool if succeeded
  234      */
  235     public function exportRawQuery(string $err_url, string $sql_query, string $crlf): bool
  236     {
  237         return $this->exportData('', '', $crlf, $err_url, $sql_query);
  238     }
  239 }