Skip to content

Commit

Permalink
Merge pull request #1209 from samsonasik/xml-convert
Browse files Browse the repository at this point in the history
Fix undefined function xml_convert at Database\BaseUtils
  • Loading branch information
lonnieezell authored Oct 17, 2018
2 parents 51291c9 + 9ffc6b0 commit 648a213
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 1 deletion.
2 changes: 1 addition & 1 deletion system/Database/BaseUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public function getXMLFromResult(ResultInterface $query, $params = [])
extract($params);

// Load the xml helper
// get_instance()->load->helper('xml');
helper('xml');
// Generate the result
$xml = '<' . $root . '>' . $newline;
while ($row = $query->getUnbufferedRow())
Expand Down
74 changes: 74 additions & 0 deletions system/Database/MySQLi/Utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php namespace CodeIgniter\Database\MySQLi;

/**
* CodeIgniter
*
* An open source application development framework for PHP
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2014-2018 British Columbia Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @package CodeIgniter
* @author CodeIgniter Dev Team
* @copyright 2014-2018 British Columbia Institute of Technology (https://bcit.ca/)
* @license https://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com
* @since Version 3.0.0
* @filesource
*/

/**
* Utils for MySQLi
*/
class Utils extends \CodeIgniter\Database\BaseUtils
{

/**
* List databases statement
*
* @var string
*/
protected $listDatabases = 'SHOW DATABASES';

/**
* OPTIMIZE TABLE statement
*
* @var string
*/
protected $optimizeTable = 'OPTIMIZE TABLE %s';

//--------------------------------------------------------------------

/**
* Platform dependent version of the backup function.
*
* @param array|null $prefs
*
* @return mixed
*/
public function _backup(array $prefs = null)
{
throw new DatabaseException('Unsupported feature of the database platform you are using.');
}

//--------------------------------------------------------------------
}
75 changes: 75 additions & 0 deletions system/Helpers/xml_helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* CodeIgniter
*
* An open source application development framework for PHP
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2014-2018 British Columbia Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @package CodeIgniter
* @author CodeIgniter Dev Team
* @copyright 2014-2018 British Columbia Institute of Technology (https://bcit.ca/)
* @license https://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com
* @since Version 1.0.0
* @filesource
*/
if ( ! function_exists('xml_convert'))
{
/**
* Convert Reserved XML characters to Entities
*
* @param string
* @param bool
* @return string
*/
function xml_convert(string $str, $protect_all = FALSE): string
{
$temp = '__TEMP_AMPERSANDS__';

// Replace entities to temporary markers so that
// ampersands won't get messed up
$str = preg_replace('/&#(\d+);/', $temp.'\\1;', $str);

if ($protect_all === TRUE)
{
$str = preg_replace('/&(\w+);/', $temp.'\\1;', $str);
}

$str = str_replace(
['&', '<', '>', '"', "'", '-'],
['&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#45;'],
$str
);

// Decode the temp markers back to entities
$str = preg_replace('/'.$temp.'(\d+);/', '&#\\1;', $str);

if ($protect_all === TRUE)
{
return preg_replace('/'.$temp.'(\w+);/', '&\\1;', $str);
}

return $str;
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/database/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ patterns. The database functions offer clear, simple syntax.
Getting MetaData <metadata>
Custom Function Calls <call_function>
Database Events <events>
Database Utilities <utilities>
37 changes: 37 additions & 0 deletions user_guide_src/source/database/utilities.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
########################
Utilities
########################

The Database Utility Class contains methods that help you manage your database.

.. contents::
:local:
:depth: 2

*******************
Get XML FROM Result
*******************

**getXMLFromResult()**

This method returns the xml result from database result. You can do like this::

$model = new class extends \CodeIgniter\Model {
protected $table = 'foo';
protected $primaryKey = 'id';
};
$db = \Closure::bind(function ($model) {
return $model->db;
}, null, $model)($model);

$util = (new \CodeIgniter\Database\Database())->loadUtils($db);
echo $util->getXMLFromResult($model->get());

and it will get the following xml result::

<root>
<element>
<id>1</id>
<name>bar</name>
</element>
</root>

0 comments on commit 648a213

Please sign in to comment.