Skip to content
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

Refactoring of geocache attributes #2304

Merged
merged 2 commits into from
Oct 18, 2021
Merged

Conversation

kojoty
Copy link
Member

@kojoty kojoty commented Oct 17, 2021

HI,

This is general refactoring of attributes for geocaches.
I would like to clean it up, unify, move definition of attributes (config) from DB to files etc.

This is ONLY preparation - after merging this everything works the same - this code is not used at all.

How it works:

  • There is a new class models/geocache/CacheAttributes - this class contains complete definition of all possible attributes used by OCPL-code sites (I mean OCRO/OCNL/OCUS/OCUK/OCPL).
  • Translation for attributes names are moved to site translations and handled by crowdin
  • Each site has it's own config of supported attributes in /config/geocache.* - node config defines the list of displayed attributes and its order
  • I unify the icon names - I made the new order in /public/images/cacheAttributes - assumption is that each node has it's own set of icons but the name are unified for all nodes
  • There is only one icon / per attribute - attribute state is generated by CSS - see live example
  • There is a test site to compare the previous state and the redefined result: https://<node>/test/attributes like: https://opencaching.pl/test/attributes

Still to do:

  • we need to resolve a few details related to attributes not listed at: https://wiki.opencaching.eu/index.php?title=Cache_attributes or these I don't understand:

    • OCUK: ASKOWNER [id=156] - what is this? why do we need this?
    • OCUK: OTHER [id=157] - what is this? why do we need this?
    • OCUK: AIRCRAFT [id=153] - really? how many such caches are there?
    • OCUS: CHALLENGE [id=??] - not listed in attributes index
    • OCUS: BITCACHE [id=??] - not listed in attributes index
    • OCUS: GUESTBOOK [id=??] - not listed in attributes index
  • we should also update OKAPI code to not use DB for attributes dictionary - I will review the OKAPI needs later

I'm going to merge it soon, as this not affects the working code.
If we decide it's OK I will deliver the changes to use the new approach in views with attributes.
Next after delivering OKAPI changes we will be able to finally remove cache_attrib table from DB :)

  This method uses HTMLPurifier to clean up input text with html tags but this is take a lot of memory.
  OCCookie can use just htmlspecialchars instead (it saves a half of the memory of each OCPL code call as asmost always OCCookie is in use.
@kojoty kojoty force-pushed the pju-attributes branch 2 times, most recently from 676629a to 4341c00 Compare October 17, 2021 18:14
@andrixnet
Copy link
Contributor

This is great news. I've been working on attribute definitions the previous years (dataset) a lot and also created an attribute set (graphics). I paused because of the code changes it required.
I am happy to share my work.

Please include attribute theme configuration directive.

Also note that OC UK has requested at least one new attribute (I had extensive talks with them in 2019) and the removal of several. I'll look it up and post it.

My work also included opencaching.eu documentation and a partial redefining of the attribute IDs from the current mess to a consistent, cross node uniform set.

IIRC I developed some SQL queries to help make the transition for cache - attribute mappings.

Don't forget OC US has some particular attribs as well, since they dropped some cache types in favour of attributes.

@andrixnet
Copy link
Contributor

I'll recheck my attribute sheets and update the wiki.

@andrixnet
Copy link
Contributor

Checking https://wiki.opencaching.eu/index.php?title=Cache_attributes against my master table. I think I have to update it with several of the OCUS attribs (derived from cache type fallback).

  • OCUS: CHALLENGE [id=??] - not listed in attributes index

  • OCUS: BITCACHE [id=??] - not listed in attributes index

  • OCUS: GUESTBOOK [id=??] - not listed in attributes index
    It looks like I have to document these first. Please allow me to do it first.

  • OCUK: ASKOWNER [id=156] - what is this? why do we need this?
    to be removed ( OCDE inspired legacy maybe (?) )

  • OCUK: OTHER [id=157] - what is this? why do we need this?
    to be removed

  • OCUK: AIRCRAFT [id=153] - really? how many such caches are there?
    to be removed from OCUK

Also, OCUK would like to add at least new attrib 217.

Before applying SQL updates on data (per note designed SQL queries) I think it best that I recheck them. (also in light with OCUS changes)
See email in that regard.

@andrixnet
Copy link
Contributor

Reading the code looks like I should make additional updates to the wiki (and my tables).
Just to bring things in sync. Thank you very much for tackling this very large component.

@andrixnet
Copy link
Contributor

Issues reference: #462 #806 #1251 #2013 #2076 #2277 at least.

@deg-pl
Copy link
Member

deg-pl commented Oct 18, 2021

From Facebook UK technical group:

I've had a look and it seems there are just 3 questions for us ...
OCUK: ASKOWNER [id=156] - what is this? why do we need this?
I have no idea !
OCUK: OTHER [id=157] - what is this? why do we need this?
I have no idea on this one either
OCUK: AIRCRAFT [id=153] - really? how many such caches are there?
It seems unlikely there are any in the UK
I have no problem with dropping all of these.

@kojoty could you check if any OC UK caches has these attributes?

Copy link
Member

@deg-pl deg-pl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Thanks! This is a very good idea to have attribs in one class, not database - consistent on all OC PL sites!
I've wrote some feedback notes.


/**
* List of attributes supported by the node. The order is significant - the same order is used on sites.
* Use CacheAttribute::AT_* notation for more clear definition.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no AT_* notation in CacheAttribute class.

@@ -3136,6 +3136,86 @@
'vote_elListThEnd' => 'End of election',
'vote_optionsToChoose' => 'Options to choose from',


'at_fee' => 'Access or parking fee',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not clear. I suggest rename it to attr_*

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know - this is just a prefix. Shorter is easier to write...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still I myself support @deg-pl - better use attr_* - much more clear and human readable. at_ can easily be confused with references using the english word "at".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simple find&replace should do the trick, while it's fresh


// attribute name = attribute ID
/** Access or parking fee */
const FEE = 2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It violates PSR-12:

Visibility MUST be declared on all constants if your project PHP minimum version supports constant visibilities (PHP 7.1 or later).

Should be:
public const FEE = 2;

/** Climbing gear requried */
const RAPPELING = 3;
/** Boat required */
const BOAT = 4;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Napiszę po polsku, bo boję się, że mój angielski wprowadzi tu zamieszanie ;)
Zdecydowanie mam wrażenie, że lepszym wyjściem jest wykorzystanie wprowadzonej przez PHP 7 możliwości definiowania stałych jako tablic. W zaproponowanej przez Ciebie formie już teraz definicja jednego atrybutu jest w minimum 3 miejscach, a kod może się rozrosnąć i będzie jeszcze trudniejszy do serwisowania. Proponuję taką konstrukcję:

public const BOAT = [
    'id' => 4,
    'translation' => 'at_boat',
    'icon' => 'at_boat',
];

Jeśli chcesz dodać nowy atrybut - robisz to tylko w jednym miejscu, wszystko jest czytelne, nie zostają śmieci po usuniętych atrybutach (wyobrażasz sobie za 10 lat przeglądanie wszystkich Twoich case'ów czy nie ma w nich atrybutów-widmo?
Takie rozwiązanie dodatkowo trywializuje getIconFileName() i getTrKey() - to mogą być krótkie, proste metody, zamiast długich tasiemców.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm.. no ciekawe - nie wiedziałem, że tak już można w PHP - muszę się przyjrzeć...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, przemyślałem i jednak bym został z tym co jest.
Ogólnie to to by było super gdyby dało się definiować "stałe obiekty" - tj mieć jakiś rodzaj interfejsu czy klasy i definiować to inline np tak:

class Attrib {
    
    public $id;
    public $trKey;
    
    __construct Attrib($id, $trKey) (...)
}

class Attribs {
    
    public const FEE = new (1,'at_fee');
    ...
}

ale to na razie nie możliwe...

Za to problem z definicją jako tablica jest to, że nie można wtedy uciec od tego indeksu
i musisz żeby dostać id pisać:

$supportedAttribsIds = [CacheAtt::FEE['id']]

a mi się to mało podoba... te jawne indeksy jakieś takie mało sexy...

Za to te moje switch-case'y są dość stabilne, bo:

  • jeśli dodasz atrybut a nie dodasz go do switch-case w którejś z metod - wpadniesz w default i widzisz error w logu
  • jeśli usuniesz atrybut a nie usuniesz w switch-case - masz błąd od razu bo php wychwyci to
  • zgadzam się, że te długie listy też są mało fajne, ale na razie nie mam prostszego pomysłu - można to polaczyc w jedną tablicę w stylu:
const ATTR_CONF = [
  self::FEE => ['trKey' => 'at_fee', 'icon' => 'at_fee'],
  ...
]

ale nie wiem czy to coś zmienia... No może mamy to w jednym miejscu... OK..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatywnie można byłoby wczytywać te stałe w postaci tablic i przerabiać je pętlą for na obiekty, ale to chyba byłby przerost formy nad treścią.

Twój kod
$supportedAttribsIds = [CacheAtt::FEE['id']]
można też ewentualnie ugryźć czymś w rodzaju gettera i odwoływać się w stylu:
$attribId = CacheAttribute::getId(CacheAttribute::FEE)
ale to chyba też przerost formy. Taka forma pseudogettera ma tą zaletę, że ukrywa logikę przed pozostałym kodem (hermetyzacja) i docelowo czymkolwiek będzie CacheAttribute:FEE - zawsze będzie działało poprawnie i ewentualny refactoring kodu będzie dotyczył tylko jednego miejsca.

Jako ciekawostkę dorzucę, że w PHP8 kod typu:

class Attrib {
    
    public int $id;
    public string $trKey;
    
    __construct Attrib($id, $trKey)
   {
        $this->id = $id;
        $this->trKey = $trKey;
    }
}

można skrócić do:

class Attrib {

    __construct Attrib(public int $id, public string $trKey)
   {}
}

*/
public static function getTrKey(int $attr): string
{
switch ($attr) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Informational:
In PHP8 there is wonderfull instruction "match". For example:

return match ($attr) {
self:FEE => 'at_fee'
(...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's interesting, but now we had php-7.4 on board...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please let's not rush to PHP8 yet

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrixnet this was only for informational purpose. We don't plan to change PHP version currently.

*/
public static function getSupportedAttributes(): array
{
return self::getKeyFromGeoCacheConfig('reactivationRulesPredefinedOpts');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"reactivationRulesPredefinedOpts"? I think it is a mistake.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YES!

@kojoty
Copy link
Member Author

kojoty commented Oct 18, 2021

OK I deliver the current version of the code and we will work on this.
For now this is harmless as this code is not used anywhere.

@kojoty kojoty merged commit ec970ed into opencaching:master Oct 18, 2021
@kojoty
Copy link
Member Author

kojoty commented Oct 18, 2021

BTW. Please see https://opencaching.pl/test/attributes for debug current config debug purpose :)

@kojoty
Copy link
Member Author

kojoty commented Oct 18, 2021

From Facebook UK technical group:

I've had a look and it seems there are just 3 questions for us ...
OCUK: ASKOWNER [id=156] - what is this? why do we need this?
I have no idea !
OCUK: OTHER [id=157] - what is this? why do we need this?
I have no idea on this one either
OCUK: AIRCRAFT [id=153] - really? how many such caches are there?
It seems unlikely there are any in the UK
I have no problem with dropping all of these.

@kojoty could you check if any OC UK caches has these attributes?

There is no Aircraft nor AskOwner, 2 caches with attrib. OTHER.

@kojoty
Copy link
Member Author

kojoty commented Oct 19, 2021 via email

@andrixnet
Copy link
Contributor

There is no Aircraft nor AskOwner, 2 caches with attrib. OTHER.
just remove the association between that attrib and the respective caches. Attribute is nonesense. (was some legacy from OCDE inspiration)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants