Page MenuHomePhabricator

Unify WikibaseLexeme vue templates
Closed, ResolvedPublic

Description

In WikibaseLexeme the javascript tests do not use the same source of truth for vue templates as the production code.
This can lead to issues and incidents if the templates are changed and the tests are not updated.

The full process in production is: PHP code (SSR) spits out a component from a template which is they hydrated by the client side JS. So if the JS tests are not using the same templates as PHP, then bad things can happen.

As a programmer, I want one canonical definition per vue template in order to make tests more realistic and avoid mistakes caused by templates out-of-sync with the code working on them.

Acceptance criteria

  • only one definition is used per template for both production and testing
  • templates are shipped in a way that ensures they match the frontend code they were checked in with (may already be done but should be checked)

Info

  • Extensions involved: Wikibase, WikibaseLexeme
  • Currently templates can be found in various places, e.g.
    • src/Presentation/View/*
    • tests/jasmine/* getTemplate()
    • tests/qunit/* getRepresentationWidgetTemplate()

A concrete example would be:

ResourceLoader templates could be used to load the templates from files on the client side.

I believe that this is achievable with ResourceLoader HTML templates. We can extract the vue templates into individual files, and then share them between the PHP and JS code. It looks like MediaWiki currently expects this type of template to be mustache templates, but also provides a mechanism to provide alternative formats via custom template compilers.

this ticket probably also allows us to close T197228

Event Timeline

Addshore triaged this task as Medium priority.Dec 13 2018, 12:18 PM
Addshore moved this task from Incoming to Needs Work on the Wikidata-Campsite board.
Addshore lowered the priority of this task from Medium to Low.Feb 21 2019, 10:35 AM
Addshore removed a project: Wikidata-Campsite.

I believe that this is achievable with ResourceLoader HTML templates. We can extract the vue templates into individual files, and then share them between the PHP and JS code. It looks like MediaWiki currently expects this type of template to be mustache templates, but also provides a mechanism to provide alternative formats via custom template compilers.

Tarrow renamed this task from Unify vue templates to Unify WikibaseLexeme vue templates.Sep 14 2020, 10:06 AM

After looking into this for a bit and considering @Jakob_WMDE 's comment I believe this boils down to two questions:

  1. Which templates are required in order for both JS and PHP unit tests to pass?

    I have collected the following non-exhaustive list of templates needed for each. Please add or remove stuff from this list that may or may not belong there.

    Jasmin/Qunit tests:
    • Raw gloss widget
    • Language lexical category widget
    • Lemma widget
    • Lexeme header widget
    • Representation widget .

      Unit tests:
    • Lexem form view
    • Lexeme gramatical features form view
    • Senses view
  1. How do we integrate ResourceLoader calls in PHP + JS?

The way to use it on the client side is already clearly stated in the ResourceLoader HTML templates link shared above.
This file contains some use of the ResourceLoaderModule, although it's more about injecting templates into JS but maybe it's a good start:

If I understand correctly, the templates are currently loaded by injecting them into the parser-cached HTML:

LexemeView.php
    private function getLemmaVueTemplate() {
        return <<<HTML                                                                                                                                                       
<script id="lemma-widget-vue-template" type="x-template">                                                                                                                    
    {$this->getRawLemmaVueTemplate()}                                                                                                                                        
</script>                                                                                                                                                                    
HTML;
    }
    private function getRawLemmaVueTemplate() {
        return <<<'HTML'                                                                                                                                                     
<div class="lemma-widget">                                                                                                                                                   
    <ul v-if="!inEditMode" class="lemma-widget_lemma-list">                                                                                                                  
...
    </div>                                                                                                                                                                   
</div>                                                                                                                                                                       
HTML;
    }

I think this task can be split into two parts:

  1. Extract those templates into files, make the PHP code read them from there, and make the tests (QUnit or Jasmine – I think they both run in Node and have access to the file system) read them from those files as well. getLemmaVueTemplate() would still exist, but instead of calling $this->getRawLemmaVueTemplate(), it would call something to the effect of file_get_contents(). This would, I believe, not solve T197228 yet, but it should be a useful first step, and already eliminate the duplicate templates in the test.
  2. Now that we have the templates in files, export them as ResourceLoader modules, and then make the JavaScript load them from there instead of from the <script> elements which the PHP code added to the parser-cached HTML. Later, remove the corresponding PHP code, so that we no longer embed the templates in the HTML. The QUnit and Jasmine tests can probably continue to load the templates directly from disk, and skip any ResourceLoader integration.

Change 630639 had a related patch set uploaded (by Rosalie Perside (WMDE); owner: Rosalie Perside (WMDE)):
[mediawiki/extensions/WikibaseLexeme@master] [WIP] Move LexemeView Templates to individual files.

https://gerrit.wikimedia.org/r/630639

while working on this we realised that almost all Qunit test do not run in WikibaseLexeme. See more detail in the task T264207

Change 630901 had a related patch set uploaded (by Rosalie Perside (WMDE); owner: Tobias Andersson):
[mediawiki/extensions/WikibaseLexeme@master] Remove duplicated templates from test files

https://gerrit.wikimedia.org/r/630901

Change 631445 had a related patch set uploaded (by Rosalie Perside (WMDE); owner: Rosalie Perside (WMDE)):
[mediawiki/extensions/WikibaseLexeme@master] [WIP]Register extracted vue templates as resourceloader templates

https://gerrit.wikimedia.org/r/631445

Change 630639 merged by jenkins-bot:
[mediawiki/extensions/WikibaseLexeme@master] Deduplicate vue templates between php implementation and js tests

https://gerrit.wikimedia.org/r/630639

Change 630901 merged by jenkins-bot:
[mediawiki/extensions/WikibaseLexeme@master] Remove duplicated templates from test files

https://gerrit.wikimedia.org/r/630901

Change 631445 merged by jenkins-bot:
[mediawiki/extensions/WikibaseLexeme@master] Register extracted vue template as resourceloader template for Qunit tests

https://gerrit.wikimedia.org/r/631445

Change 631771 had a related patch set uploaded (by Noa wmde; owner: Noa wmde):
[mediawiki/extensions/WikibaseLexeme@master] Replace script injections with ResourceLoader template loading

https://gerrit.wikimedia.org/r/631771

Change 631771 merged by jenkins-bot:
[mediawiki/extensions/WikibaseLexeme@master] Replace script injections with ResourceLoader template loading

https://gerrit.wikimedia.org/r/631771

Change 632486 had a related patch set uploaded (by Lucas Werkmeister (WMDE); owner: Lucas Werkmeister (WMDE)):
[mediawiki/extensions/WikibaseLexeme@master] Reinstate template script injections

https://gerrit.wikimedia.org/r/632486

Change 632487 had a related patch set uploaded (by Lucas Werkmeister (WMDE); owner: Lucas Werkmeister (WMDE)):
[mediawiki/extensions/WikibaseLexeme@master] Revert "Reinstate template script injections"

https://gerrit.wikimedia.org/r/632487

Change 632486 merged by jenkins-bot:
[mediawiki/extensions/WikibaseLexeme@master] Reinstate template script injections

https://gerrit.wikimedia.org/r/632486

This is complete apart from the final removal of template HTML (https://gerrit.wikimedia.org/r/c/mediawiki/extensions/WikibaseLexeme/+/632487/) which is to happen once Wikidata has got 1.36.0-wmf.13 branch code,

Change 632487 merged by jenkins-bot:
[mediawiki/extensions/WikibaseLexeme@master] Revert "Reinstate template script injections"

https://gerrit.wikimedia.org/r/632487