Page MenuHomePhabricator

Remote autocompletion does not work when specifying cargo field=_pageName
Closed, ResolvedPublicBUG REPORT

Description

I was investigating the previous bug that I submitted and I found this other bug.

When using cargo, if we make a form enabling autocompletion with the option cargo field=_pageName, for example:

{{{field|My_input|input type=tokens|cargo table=My_table|cargo field=_pageName}}}

and force remote autocompletion by setting the following option in LocalSettings

$wgPageFormsMaxLocalAutocompleteValues = 20;

(Replacing 20 by a number less than the total number of rows of the cargo table), then autocompletion does not work at all. On the other hand, if we change the option so local autocompletion is used, then the autocompletion works.


The problem is in the PFAutocompleteAPI class, getAllValuesForCargoField function. When executing the sql query (using CargoSQLQuery class), the array returned wil have several rows, each being another array with the key _pageName. However, the code executes the following line to obtain the key for the rows returned:

$cargoFieldAlias = str_replace( '_', ' ', $cargoField );

which will give the string " pageName", then, when the code

foreach ( $queryResults as $row ) {
	// @TODO - this check should not be necessary.
	if ( ( $value = $row[$cargoFieldAlias] ) != '' ) {
		$values[] = $value;
	}
}

is executed, the $values variable will be empty.

Actually, I think that the fix for this is simple, because the cargo alias is already stored in the $sqlQuery variable, which gives the correct value for _pageName too, so this patch should solve the problem

diff --git a/includes/PF_AutocompleteAPI.php b/includes/PF_AutocompleteAPI.php
index bf4cf9e2..3c1d3fa4 100644
--- a/includes/PF_AutocompleteAPI.php
+++ b/includes/PF_AutocompleteAPI.php
@@ -356,7 +356,7 @@ class PFAutocompleteAPI extends ApiBase {
                        $wgPageFormsMaxAutocompleteValues,
                        $offsetStr = 0
                );
-               $cargoFieldAlias = str_replace( '_', ' ', $cargoField );
+               $cargoFieldAlias = key($sqlQuery->mAliasedFieldNames);
                $queryResults = $sqlQuery->run();
 
                foreach ( $queryResults as $row ) {

Event Timeline

Okay, I just checked in a fix for this. It's different from what you have, but hopefully it works!

Tombolano claimed this task.

Thank you!, yes, now the code works as expected, so I am marking this bug as resolved.