Breaking Changes in v1.10.00: Difference between revisions

From XMBdocs
(Templates)
Line 3: Line 3:
== Queries ==
== Queries ==


There are three main changes that affect all database queries in XMB.
There are two main changes that affect all database queries in XMB.


=== Global $db Service Locator ===
=== Global $db Service Locator ===
Line 62: Line 62:


$query = $db->query("SELECT * FROM " . $vars->tablepre . "restricted ORDER BY id");
$query = $db->query("SELECT * FROM " . $vars->tablepre . "restricted ORDER BY id");
</pre>
== Templates ==
The template system has moved to the <code>templates</code> subdirectory of the website and is no longer stored in the database.
=== Deleted Functions ===
The <code>loadtemplates()</code> and <code>template()</code> functions do not exist in v1.10.00.  They have been replaced by the shared `template` service.  To render a simple template, the code now looks like this:
<pre>
$template = \XMB\Services\template();
$output = $template->process('file_name.php');
</pre>
=== Registered Global Variables Eliminated ===
This will be the first version of XMB where it is impossible to pass a variable directly from user input to template output.
In older versions, it was possible and sometimes adequate to sanitize a global variable such as <code>$fid = (int) $fid;</code> and use it in the template without further consideration.
Starting with v1.10.00 it is required to manually register each variable with the shared <code>template</code> service or a separate instance (discussed later).  Here is how we might handle the old <code>$fid</code> input:
<pre>
$template = \XMB\Services\template();
$template->fid = \XMB\getInt('fid');
</pre>
You might notice there isn't any defined property named <code>fid</code> in the underlying code.  This is called a dynamic property and it was chosen as one of the cleanest styles available for coding.
Of course, most pages need many variables, so you will want to import the XMB function names or the entire namespace.
<pre>
namespace XMB;
$template = Services\template();
$template->fid = getInt('fid');
$template->pid = getInt('pid');
</pre>
</pre>

Revision as of 11:26, 2 January 2025

Customizing and migrating to v1.10.00 will require some new changes.

Queries

There are two main changes that affect all database queries in XMB.

Global $db Service Locator

Instead of providing a global variable named $db, XMB now offers two services named db and sql.

For a direct replacement of the global shared object reference, just add this near the top of your file:

$db = \XMB\Services\db();

If you need to create a 2nd database connection (to a different account or server) within your script, then use the revised class name instead:

$db2 = new \XMB\MySQLiDatabase(debug: false, logErrors: true);
$db2->connect(...);

Internally, XMB is starting to use methods of the shared sql service instead of direct or prepared statements. This results in cleaner and more secure code. For example, to get the shared service and check your inbox:

$sql = \XMB\Services\sql();
$vars = \XMB\Services\vars();

$msgCount = $sql->countU2UInbox($vars->self['username']);

If you wish to write similar query methods, the best practice is to create your own object class rather than try to modify the XMB service.

The X_PREFIX Constant

Instead of providing a global constant named X_PREFIX, XMB now stores the $tablepre variable from config.php as a property of the shared vars service. The direct replacement looks like this:

$vars = \XMB\Services\vars();

$statement = 'SELECT * FROM' . $vars->tablepre . 'ranks';

Example

Here is an actual query from the old cp2.php file:

require 'header.php';

$query = $db->query("SELECT * FROM ".X_PREFIX."restricted ORDER BY id");

Here is how it changed in the new restrictions.php file during development of v1.10.00:

require ROOT . 'header.php';

$db = \XMB\Services\db();
$vars = \XMB\Services\vars();

$query = $db->query("SELECT * FROM " . $vars->tablepre . "restricted ORDER BY id");

Templates

The template system has moved to the templates subdirectory of the website and is no longer stored in the database.

Deleted Functions

The loadtemplates() and template() functions do not exist in v1.10.00. They have been replaced by the shared `template` service. To render a simple template, the code now looks like this:

$template = \XMB\Services\template();

$output = $template->process('file_name.php');

Registered Global Variables Eliminated

This will be the first version of XMB where it is impossible to pass a variable directly from user input to template output.

In older versions, it was possible and sometimes adequate to sanitize a global variable such as $fid = (int) $fid; and use it in the template without further consideration.

Starting with v1.10.00 it is required to manually register each variable with the shared template service or a separate instance (discussed later). Here is how we might handle the old $fid input:

$template = \XMB\Services\template();

$template->fid = \XMB\getInt('fid');

You might notice there isn't any defined property named fid in the underlying code. This is called a dynamic property and it was chosen as one of the cleanest styles available for coding.

Of course, most pages need many variables, so you will want to import the XMB function names or the entire namespace.

namespace XMB;

$template = Services\template();

$template->fid = getInt('fid');
$template->pid = getInt('pid');