Extension:External Data/Databases/de-formal

You can use External Data to get data from outside databases; most relational DB types are supported, as well as MongoDB. The retrieval is done with either a set of parameters that together represent a SELECT statement, or the name of a prepared statement.

As of version 3.2, the recommended way to retrieve database data is to use one of the display functions (#external_value, #for_external_table, etc.), passing in the necessary parameters for the data retrieval, most notably "source=". You can also retrieve database data by calling the #get_db_data function, or (for version 3.0 and higher) #get_external_data. In all of these cases, you must specify the information, including login information, for the database in the variable $wgExternalDataSources in LocalSettings.php.

For any of these parser functions, you can also call its corresponding Lua function.

A note about security: If you are going to retrieve database data, you should think about the security implications. Configuring a database in LocalSettings.php will allow anyone with edit access to your wiki to run arbitrary SQL statements against that database, unless prepared statements are configured for the database connection. You should use a database user that has the minimum permissions for what you are trying to achieve. It is possible that complex SQL constructions could be passed to this function to cause it to do things vastly different from what it was designed for.

Konfiguration
Each database being accessed needs to be configured separately in LocalSettings.php. For normal databases (i.e., everything except for SQLite), add the following stanza for each database:

Where:


 * ID - a label for this database which is used when calling #get_db_data
 * server URL - the hostname on which the database lives
 * DB type - the type of database, i.e. mysql, postgres, mssql, oracle, sqlite, db2 or mongodb
 * DB name, username, password - details for accessing the database.

An example of a set of values would be:

The following optional settings can also be added:

Example values for these variables are:

Support for database systems
MySQL, Postgres (i.e. PostgreSQL), DB2 and MongoDB should work fully by default (though there are syntax limitations, and differences, for MongoDB - see below). For MS SQL/SQLServer, SQLite and Oracle, you may need to perform some special handling.

Postgres
If you cannot connect to a PostgreSQL database, it may be because your PHP installation is lacking the PostgreSQL database module, php-pgsql. On many Linux systems, you can install it by calling the following, then restarting the web server: This installs a php 5.5 dependency. PHP 5.5 is old and should not be used anymore. Replace php55 with the php version you are running. yum install php55-php-pgsql

Amend the above configuration in LocalSettings.php to change the server type to "postgres":

SQLite
To connect to SQLite, you need something like the following in LocalSettings.php:

Oracle
Connecting to Oracle may work by default. If it doesn't work, the following may help:
 * Make sure that the Oracle client, and the PHP version being used, are using the same architecture: they have to either both be 32-bit, or both be 64-bit.
 * Make sure that the value of $edgDBServer for the installation matches something in the corresponding Oracle client .ora files. The value may need to look like "serverName/dbName", as opposed to "serverName".
 * If none of the above are the issue, you could try using the extension, which should work as well.

MongoDB
For MongoDB, there are no special connection parameters, although the username and password may be optional. There are two optional query parameters:  and. Under PHP 7.*, the extension  and library    is required. Unfortunately, due to the way that MediaWiki continuous integration is built, this library cannot be simply added to  for this extension (see T259743).

MongoDB is a non-SQL (or "NoSQL", if you prefer) database system, with its own querying language. When accessing MongoDB, you can either pass in a standard MongoDB query, or use the standard SQL-like syntax of #get_db_data. To use standard MongoDB querying, pass the query to the parameter  or.

You can also use the standard querying functionality. There are some restrictions and differences, however, for the "where" clause:


 * only "AND"s can be used, not "OR"s
 * for the "LIKE" comparison, no text should be placed around the comparator - it should look like "Username LIKE Jo", not "Username LIKE '%Jo%'".

Because MongoDB returns values in JSON that may be complex, and contain compound values, you can get data that is stored in such a way by separating field names with dots. For instance, if the return data contains a value for a field called "Measurements" that is an array, holding values for fields called "Height" and "Width", then the "data=" parameter to #get_web_data could have a value like "height=Measurements.Height,width=Measurements.Width".

You can do Memcached- or APCU-based caching of values retrieved from MongoDB; to do that, you need the following two lines in LocalSettings.php:

To enable ModgoDB under PHP 7.4,  extension should be enabled (and also   library should be installed with Composer:   (this will be necessary until bug T259743 is resolved).

Microsoft SQL Server
MediaWiki formerly natively supported MS SQL Server; this support went away in version 1.34. However, starting with version 3.1, External Data provides its won support for accessing MS SQL Server via ODBC.

The typical settings for accessing MS SQL Server will be:

Note the  setting and the comma between domain name and port in the   parameter. The odbc PHP extension has to be installed, as well as the Microsoft ODBC driver for SQL Server. The file  should contain

[ODBC Driver 17 for SQL Server] Description=Microsoft ODBC Driver 17 for SQL Server Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.5.so.2.1

Usage
The examples below all refer to #get_db_data. To retrieve DB data using any of the other parser (or Lua) functions, you can use all of the same parameters, but specify "source=", instead of "db=", for the ID stored in LocalSettings.php.

To get data from an external database, call the following:

An explanation of the fields:

An example call, using the "employee database" example from above:

Prepared statements
A safer approach is to define one or more prepared statements for the database connections defined in, in   item of the data source configuration array, which can be a string, containing a SQL query with parameters, for the only statement, or an associative array , for several. Items of this associative array can be either strings holding the prepared statement that take no parameters or only parameters of the default type  (strings), or arrays of the form. See  for parameter types. Setting parameter types is only relevant for mySQL.

Parameters to the prepared statement are passed as a comma-separated list in parser function argument. If there are several prepared statements defined for the same connection, the needed statement ID is passed as  parameter. If prepared statements are defined, arbitrary queries will not be created for the same connection.

PostgreSQL
Download and restore 'dvdrental' example database. Create user 'ED' with password 'ED' and grant to it read rights on 'public' scheme in 'dvdrental' database.

Create the function :

Add 'postgresql' database connection to LocalSettings.php:

Now, you can obtain a list of films shorter than 55 minutes with:

Microsoft SQL Server
It is assumed that Microsoft SQL server and the ODBC driver for it are installed, as well as the odbc extension for PHP; and the Northwind database is imported.

should have the entry: [ODBC Driver 17 for SQL Server] Description=Microsoft ODBC Driver 17 for SQL Server Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.5.so.2.1

Then necessary configuration setting for External Data will be: and the parser function call retrieving and displayng the Washington employees data will be: