Fixed table prefix not reliably applied by db adapter regex (affects IPB importer)

Jake Bunce

Well-known member
Affected version
This was initially reported by my customer as a problem with the IPB importer. The users step was failing due to one of the tables not existing. The reason for the error is because the table in question wasn't being properly prefixed in the executed query.

I have tracked this down to a problem with XenForo's db adapter:


This code doesn't handle multiple spaces between the JOIN and the table name. I suggest adding the + to fix this:

Rich (BB code):
	protected function prependPrefixToTables($prefix, $query)
		if ($prefix == '')
			return $query;

		if (in_array('noPrefix', $this->getModifiersFromQuery($query)))
			return $query;

		return preg_replace('/((?:\s|^)(?:UPDATE|INTO|FROM|JOIN|STRAIGHT_JOIN)\s+)([a-z0-9_-]+(?:\s|$))/siU', '$1' . $prefix . '$2$3', $query);

The importer code which triggered this bug is in:


This query has two spaces before the table name which is not properly handled without the code fix above:

Rich (BB code):
		$users = $this->sourceDb->fetchAllKeyed("
				uapr.row_perm_cache AS user_row_perm_cache,
			    gapr.row_perm_cache AS group_row_perm_cache,
			FROM core_members AS members
			LEFT JOIN  core_pfields_content AS pfields_content ON
				(pfields_content.member_id = members.member_id)
			LEFT JOIN core_admin_permission_rows AS uapr ON
				(uapr.row_id = members.member_id AND uapr.row_id_type = 'member')
			LEFT JOIN core_admin_permission_rows AS gapr ON
				(gapr.row_id = members.member_group_id AND gapr.row_id_type = 'group')
			LEFT JOIN core_moderators AS moderator ON
				(members.member_id = AND moderator.type = 'm')
			WHERE members.member_id > ? AND members.member_id <= ?
			ORDER BY members.member_id
			LIMIT {$limit}
		", 'member_id', [$state->startAfter, $state->end]);

XF Bug Bot

XenForo bug fixer bot
Staff member
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.1.6).

Change log:
Fix an issue that would break prepending a table prefix to table names in the database adapter.
There may be a delay before changes are rolled out to the XenForo Community.