-
Notifications
You must be signed in to change notification settings - Fork 402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return JSON-encoded results from stats.php
#1345
Conversation
@reedy The locale-dependent number formatting changes conflict with my changes. I've resolved the conflicts with |
Arguably most of the formatting should probably be done in the frontend, but the current version is/was such a mix... GitHub doesn't think they are resolved and there are currently conflicts... |
It seems to pull onto master fine... |
Yes, because the last commit here is a merge with master where I throw away (that's the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to work fine! Few related changes we can make to reduce the amount of things being returned by the SQL queries etc
Comments about foramtting mostly left for my reference... I'm happy to rebase to include the lost changes in the merge commit when ther other minor issues have been fixed up.
php/stats.php
Outdated
@@ -22,12 +24,12 @@ | |||
$sth->execute([$trid]); | |||
$row = $sth->fetch(); | |||
if (!$row) { | |||
die('Error;' . _("Trip not found.")); | |||
die(json_encode(["error" => ("Trip not found.")])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Localistion dropped? _(
has become (
; brackets aren't adding anything in the current form
Could potentially use json_error()
from helper.php
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I definitely need to do something here but I'm not trying to remove the translation. There is a gettext
call for this string (for whatever is sent in the error
prop in the UI now (line 2277). It works right now because we already have the string in the catalogue but the missing _
is a problem. However, I don't want to do localization here in this file.
I definitely don't want to use json_error
(and json_success
) because, IMO, data endpoints should not send back localized messages. That's a presentation layer concern.
I'm thinking of defining _
as the identity function in here so strings can be properly marked for localization in the UI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better having message codes, rather than strings passed wholesale to the localisation functions...
But I think that gets a little harder, changes how the defaults are defined (and found in source code) and a whole other host of problems.
So changes the workflow in one way or another too.
I'm thinking of defining
_
as the identity function in here so strings can be properly marked for localization in the UI.
Which is what locale.php
does for if !$OF_USE_LOCALES
... And as long as no other PHP file includes stats.php
, shouldn't cause an issue.
At some point, these want to become proper classes (especially if we go down the route of using a proper router etc), but that's a concern for down the road.
These could return an error code (and an english string? Ideally...), and then have the mapping from code => message in the JS and gettext should pick them up from there instead. And do the localisation there too.
And/or forcing a locale of en_us
in standalone scripts like this, so the localisation function just returns the string anyway.
Not that json_success
/json_error
do any sort of code, just a status number...
I was referring to my previous comment
And is (still) showing
It might just be whatever git implementation github is using, sucking. I know jgit won't do trivial stuff that the "normal" git does fine/trivially with... |
Yeah, it can't be rebased which is why I merged master back here (throwing some changes away as a conflict resolution strategy). If you switch rebase to squash in the UI and you'll see it's happy to merge. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So really only just the localisation string issue from the PHP files to resolve at this point.
Then squash the changes to something you're happy with.
I can then rebase and restore the formatting changes, and should be good to put onto master :)
Which should also make it easier for me to fix a last couple of cases (on a patch ontop) that were left behind too...
table += | ||
"<tr>" + | ||
`<td>${gt.gettext("Around the world")}</td>` + | ||
`<td>${(distance / EARTH_CIRCUMFERENCE).toFixed(2)}x</td>` + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another later TODO is possibly not having these defined different in both PHP and JS.... Possibly have PHP pass the constant value through when needed for calculations...
php/stats.php
Outdated
@@ -22,12 +24,12 @@ | |||
$sth->execute([$trid]); | |||
$row = $sth->fetch(); | |||
if (!$row) { | |||
die('Error;' . _("Trip not found.")); | |||
die(json_encode(["error" => ("Trip not found.")])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better having message codes, rather than strings passed wholesale to the localisation functions...
But I think that gets a little harder, changes how the defaults are defined (and found in source code) and a whole other host of problems.
So changes the workflow in one way or another too.
I'm thinking of defining
_
as the identity function in here so strings can be properly marked for localization in the UI.
Which is what locale.php
does for if !$OF_USE_LOCALES
... And as long as no other PHP file includes stats.php
, shouldn't cause an issue.
At some point, these want to become proper classes (especially if we go down the route of using a proper router etc), but that's a concern for down the road.
These could return an error code (and an english string? Ideally...), and then have the mapping from code => message in the JS and gettext should pick them up from there instead. And do the localisation there too.
And/or forcing a locale of en_us
in standalone scripts like this, so the localisation function just returns the string anyway.
Not that json_success
/json_error
do any sort of code, just a status number...
Made the |
php/stats.php
Outdated
include_once 'db_pdo.php'; | ||
include_once 'helper.php'; | ||
include_once 'filter.php'; | ||
|
||
// Mark strings as localizable but don't localize. | ||
function _($str) { return $str; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This actually causes breakages on my dev install (and therefore presumably would also in production).
"PHP message: PHP Fatal error: Cannot redeclare _() in /var/www/openflights/php/stats.php on line 11
But I haven't explicitly installed the PHP gettext extension, I believe. Just gettext for the CLI commands...
# dpkg -S /usr/lib/php/20220829/gettext.so
php8.2-common: /usr/lib/php/20220829/gettext.so
# dpkg -S /usr/lib/php/20190902/gettext.so
php7.4-common: /usr/lib/php/20190902/gettext.so
And that's from the sury packages..
# dpkg -l | grep php | grep common
ii php-common 2:93+ubuntu22.04.1+deb.sury.org+2 all Common files for PHP packages
ii php7.4-common 1:7.4.33-6+ubuntu22.04.1+deb.sury.org+1 amd64 documentation, examples and common module for PHP
ii php8.2-common 8.2.8-1+ubuntu22.04.1+deb.sury.org+1 amd64 documentation, examples and common module for PHP
I did set $OF_USE_LOCALES = true;
to be able to test things.
If I set $OF_USE_LOCALES = false;
, I get
PHP message: PHP Fatal error: Cannot redeclare _() in /var/www/openflights/php/locale.php on line 28
Looking at my bash history, I don't seem to have explicitly installed it either.
But looking again at the INSTALL and #1206 neither explicitly install any of the php-common
packages...
https://packages.ubuntu.com/jammy/amd64/php8.1 depends on https://packages.ubuntu.com/jammy/amd64/php8.1-common which depends on https://packages.ubuntu.com/jammy/amd64/php-common
# apt-cache --installed rdepends php7.4-common
php7.4-common
Reverse Depends:
php7.4-zip
php7.4-xml
php7.4-readline
php7.4-opcache
php7.4-mysql
php7.4-mbstring
php7.4-json
php7.4-gd
php7.4-fpm
php7.4-curl
php7.4-cli
php7.4
php-common
# apt-cache --installed rdepends php8.2-common
php8.2-common
Reverse Depends:
php8.2-zip
php8.2-xml
php8.2-xdebug
php8.2-readline
php8.2-opcache
php8.2-mysql
php8.2-mbstring
php8.2-gd
php8.2-fpm
php8.2-curl
php8.2-cli
# apt-cache --installed rdepends php-common
php-common
Reverse Depends:
php8.2-zip
php8.2-xml
php8.2-xdebug
php8.2-readline
php8.2-opcache
php8.2-mysql
php8.2-mbstring
php8.2-gd
php8.2-curl
php8.2-common
php7.4-zip
php7.4-xml
php7.4-readline
php7.4-opcache
php7.4-mysql
php7.4-mbstring
php7.4-json
php7.4-gd
php7.4-curl
php7.4-common
php-zip
php-xdebug
php-mbstring
php-json
php-zip
php-xdebug
php-mbstring
php-json
So you should have installed it on your Docker install too...
I'm guessing either something has changed with packaging (since whatever version the site was previously running on), as it's there in the default package on 22.04 (might need to refresh a few times, packages.ubuntu.com
is giving intermittant HTTP 500)...
Does it work for you?
What do you have $OF_USE_LOCALES
set to?
As far as I can tell... any calls to php/stats.php
will be broken whichever way you've got it set...
And almost any other request (just loading the site) will be broken if you have $OF_USE_LOCALES = false;
too...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oof, yeah, it doesn't work. I admit I assumed it was fine based on locale.php
already doing it. Ugh. And turning off locales breaks my local version as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dunno if $OF_USE_LOCALES
should be deprecated/removed, based on the existence of the apparent existence in default PHP...
Not stuff for this commit, but:
openflights/php/config.php.sample
Line 21 in 5e0fae7
// True requires PHP gettext extension, false works only if the extension is *not* installed |
composer runs would actually fail if the "extension" wasn't loaded (unless you tried to use --ignore-platform-reqs
) -
Line 4346 in 5e0fae7
"ext-gettext": "*", |
https://github.com/jpatokal/openflights/blob/0f1adfc/php/locale.php#L28-L30 should either be removed (because of the composer requirement), or guarded with a !function_exists('_')
call for it's definition (I doubt it's possible for ext-gettext
to be apparently existing (ignoring --ignore-platform-reqs
), but the function to not)...
Some distro's do do some weird stuff regarding packaging of php extensions... Some are included, some are pulled out etc...
But either way, both shouldn't be able to be true.
$OF_USE_LOCALES
maybe/probably still has some value, basically unconditionally forcing en_US
for all requests...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, the config.php.sample
comment is from c7b2cdf (May 2013)...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you just set $locale = "en_US.utf8";
at the top of the file?
Which, in theory, should "force" "en_US" for all _()
calls in the file...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you just set
$locale = "en_US.utf8";
at the top of the file?Which, in theory, should "force" "en_US" for all
_()
calls in the file...
How would that work? Wouldn't locale.php
override it with whatever is in $_SESSION
?
The |
What's it complaining about? It should work fine (we shouldn't have duplicate keys to worry about...), and shouldn't need to be using |
|
Won't like you trying to use an array as a key :) |
I figured the Still testing locale stuff now. |
I'm not sure what's going on but my PHP installation is not actually translating any strings right now. JS is fine... |
Anywhere? Or just this file? You did re-add the include/require and drop the session start? |
Anywhere rendered server-side. I restored the |
@@ -1,6 +1,7 @@ | |||
<?php | |||
|
|||
include_once 'locale.php'; | |||
include_once 'config.php'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use $OF_DEMO_UID
so we shouldn't rely on a transitive include from locale.php
.
@@ -22,12 +23,12 @@ | |||
$sth->execute([$trid]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't comment on line 17 because I haven't changed it (I hate this) but I think the existing $uid == 1
check is wrong... I think it's written with an assumption that you can only reach this from the demo screen.
OK, |
Alright, I'm not sure how to disable translation of the error messages but I'm also not super bothered about it happening... for now. It's also consistent with what I've done in |
Not sure which part I'm supposed to look at? In any case, I think we're OK here for now. |
Just call Shouldn't affect any other scope... or change session stuff |
Reworks the changes from 6369e67 Fixes jpatokal#1344
Fixes #1296.