[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/database/src/ -> DatabaseExporter.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Database Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\Database;
  10  
  11  /**
  12   * Joomla Framework Database Exporter Class
  13   *
  14   * @since  1.0
  15   */
  16  abstract class DatabaseExporter
  17  {
  18      /**
  19       * The type of output format.
  20       *
  21       * @var    string
  22       * @since  1.0
  23       */
  24      protected $asFormat = 'xml';
  25  
  26      /**
  27       * An array of cached data.
  28       *
  29       * @var    array
  30       * @since  1.0
  31       */
  32      protected $cache = ['columns' => [], 'keys' => []];
  33  
  34      /**
  35       * The database connector to use for exporting structure and/or data.
  36       *
  37       * @var    DatabaseInterface
  38       * @since  1.0
  39       */
  40      protected $db;
  41  
  42      /**
  43       * An array input sources (table names).
  44       *
  45       * @var    string[]
  46       * @since  1.0
  47       */
  48      protected $from = [];
  49  
  50      /**
  51       * An array of options for the exporter.
  52       *
  53       * @var    \stdClass
  54       * @since  1.0
  55       */
  56      protected $options;
  57  
  58      /**
  59       * Constructor.
  60       *
  61       * Sets up the default options for the exporter.
  62       *
  63       * @since   1.0
  64       */
  65  	public function __construct()
  66      {
  67          $this->options = new \stdClass;
  68  
  69          // Set up the class defaults:
  70  
  71          // Export not only structure
  72          $this->withStructure();
  73          $this->withData();
  74  
  75          // Export as xml.
  76          $this->asXml();
  77  
  78          // Default destination is a string using $output = (string) $exporter;
  79      }
  80  
  81      /**
  82       * Magic function to exports the data to a string.
  83       *
  84       * @return  string
  85       *
  86       * @since   1.0
  87       */
  88  	public function __toString()
  89      {
  90          $buffer = '';
  91  
  92          try
  93          {
  94              // Check everything is ok to run first.
  95              $this->check();
  96  
  97              // Get the format.
  98              switch ($this->asFormat)
  99              {
 100                  case 'xml':
 101                  default:
 102                      $buffer = $this->buildXml();
 103  
 104                      break;
 105              }
 106          }
 107          catch (\Exception $e)
 108          {
 109              // Do nothing
 110          }
 111  
 112          return $buffer;
 113      }
 114  
 115      /**
 116       * Set the output option for the exporter to XML format.
 117       *
 118       * @return  $this
 119       *
 120       * @since   1.0
 121       */
 122  	public function asXml()
 123      {
 124          $this->asFormat = 'xml';
 125  
 126          return $this;
 127      }
 128  
 129      /**
 130       * Builds the XML data for the tables to export.
 131       *
 132       * @return  string  An XML string
 133       *
 134       * @since   1.0
 135       * @throws  \Exception if an error occurs.
 136       */
 137      abstract protected function buildXml();
 138  
 139      /**
 140       * Builds the XML structure to export.
 141       *
 142       * @return  array  An array of XML lines (strings).
 143       *
 144       * @since   1.0
 145       * @throws  \Exception if an error occurs.
 146       */
 147      abstract protected function buildXmlStructure();
 148  
 149      /**
 150       * Checks if all data and options are in order prior to exporting.
 151       *
 152       * @return  $this
 153       *
 154       * @since   1.0
 155       * @throws  \Exception if an error is encountered.
 156       */
 157      abstract public function check();
 158  
 159      /**
 160       * Specifies a list of table names to export.
 161       *
 162       * @param   string[]|string  $from  The name of a single table, or an array of the table names to export.
 163       *
 164       * @return  $this
 165       *
 166       * @since   1.0
 167       * @throws  \InvalidArgumentException
 168       */
 169  	public function from($from)
 170      {
 171          if (\is_string($from))
 172          {
 173              $this->from = [$from];
 174          }
 175          elseif (\is_array($from))
 176          {
 177              $this->from = $from;
 178          }
 179          else
 180          {
 181              throw new \InvalidArgumentException('The exporter requires either a single table name or array of table names');
 182          }
 183  
 184          return $this;
 185      }
 186  
 187      /**
 188       * Get the generic name of the table, converting the database prefix to the wildcard string.
 189       *
 190       * @param   string  $table  The name of the table.
 191       *
 192       * @return  string  The name of the table with the database prefix replaced with #__.
 193       *
 194       * @since   1.0
 195       */
 196  	protected function getGenericTableName($table)
 197      {
 198          $prefix = $this->db->getPrefix();
 199  
 200          // Replace the magic prefix if found.
 201          return preg_replace("|^$prefix|", '#__', $table);
 202      }
 203  
 204      /**
 205       * Sets the database connector to use for importing structure and/or data.
 206       *
 207       * @param   DatabaseInterface  $db  The database connector.
 208       *
 209       * @return  $this
 210       *
 211       * @since   1.0
 212       */
 213  	public function setDbo(DatabaseInterface $db)
 214      {
 215          $this->db = $db;
 216  
 217          return $this;
 218      }
 219  
 220      /**
 221       * Sets an internal option to export the structure of the input table(s).
 222       *
 223       * @param   boolean  $setting  True to export the structure, false to not.
 224       *
 225       * @return  $this
 226       *
 227       * @since   1.0
 228       */
 229  	public function withStructure($setting = true)
 230      {
 231          $this->options->withStructure = (boolean) $setting;
 232  
 233          return $this;
 234      }
 235  
 236      /**
 237       * Sets an internal option to export the data of the input table(s).
 238       *
 239       * @param   boolean  $setting  True to export the data, false to not.
 240       *
 241       * @return  $this
 242       *
 243       * @since   2.0.0
 244       */
 245  	public function withData($setting = false)
 246      {
 247          $this->options->withData = (boolean) $setting;
 248  
 249          return $this;
 250      }
 251  
 252      /**
 253       * Builds the XML data to export.
 254       *
 255       * @return  array  An array of XML lines (strings).
 256       *
 257       * @since   2.0.0
 258       * @throws  \Exception if an error occurs.
 259       */
 260  	protected function buildXmlData()
 261      {
 262          $buffer = [];
 263  
 264          foreach ($this->from as $table)
 265          {
 266              // Replace the magic prefix if found.
 267              $table = $this->getGenericTableName($table);
 268  
 269              // Get the details columns information.
 270              $fields  = $this->db->getTableColumns($table, false);
 271              $colblob = [];
 272  
 273              foreach ($fields as $field)
 274              {
 275                  // Catch blob for conversion xml
 276                  if ($field->Type == 'mediumblob')
 277                  {
 278                      $colblob[] = $field->Field;
 279                  }
 280              }
 281  
 282              $this->db->setQuery(
 283                  $this->db->getQuery(true)
 284                      ->select($this->db->quoteName(array_keys($fields)))
 285                      ->from($this->db->quoteName($table))
 286              );
 287  
 288              $rows = $this->db->loadObjectList();
 289  
 290              if (!count($rows))
 291              {
 292                  continue;
 293              }
 294  
 295              $buffer[] = '  <table_data name="' . $table . '">';
 296  
 297              foreach ($rows as $row)
 298              {
 299                  $buffer[] = '   <row>';
 300  
 301                  foreach ($row as $key => $value)
 302                  {
 303                      if (!in_array($key, $colblob))
 304                      {
 305                          $buffer[] = '    <field name="' . $key . '">' . htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . '</field>';
 306                      }
 307                      else
 308                      {
 309                          $buffer[] = '    <field name="' . $key . '">' . base64_encode($value) . '</field>';
 310                      }
 311                  }
 312  
 313                  $buffer[] = '   </row>';
 314              }
 315  
 316              $buffer[] = '  </table_data>';
 317          }
 318  
 319          return $buffer;
 320      }
 321  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer