Bug 31380 - Build Mojolicious controller for running Koha plugins
Summary: Build Mojolicious controller for running Koha plugins
Status: Needs Signoff
Alias: None
Product: Koha
Classification: Unclassified
Component: Architecture, internals, and plumbing (show other bugs)
Version: master
Hardware: All All
: P5 - low enhancement (vote)
Assignee: David Cook
QA Contact: Martin Renvoize
URL:
Keywords: release-notes-needed
Depends on: 31389 31390
Blocks:
  Show dependency treegraph
 
Reported: 2022-08-17 06:29 UTC by David Cook
Modified: 2023-01-04 06:05 UTC (History)
6 users (show)

See Also:
Change sponsored?: ---
Patch complexity: Medium patch
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:


Attachments
Bug 31380: Build core functionality for Mojolicious controllers (14.24 KB, patch)
2022-08-18 00:53 UTC, David Cook
Details | Diff | Splinter Review
Koha Plugin with Mojolicious controller (2.55 KB, application/zip)
2022-08-18 01:02 UTC, David Cook
Details
Bug 31380: Add plugin hook for choosing Mojolicious or CGI script controller (4.67 KB, patch)
2022-08-18 04:10 UTC, David Cook
Details | Diff | Splinter Review
Koha Plugin with Mojolicious controller (2.57 KB, application/zip)
2022-08-18 04:11 UTC, David Cook
Details
Bug 31380: Build core functionality for Mojolicious controllers (13.83 KB, patch)
2022-08-18 04:23 UTC, David Cook
Details | Diff | Splinter Review
Bug 31380: Add plugin hook for choosing Mojolicious or CGI script controller (4.67 KB, patch)
2022-08-18 04:23 UTC, David Cook
Details | Diff | Splinter Review
Bug 31380: Allow Koha Plugins to run using Mojolicious controllers (15.00 KB, patch)
2022-08-18 05:51 UTC, David Cook
Details | Diff | Splinter Review
Bug 31380: Add plugin hook for choosing Mojolicious or CGI script controller (4.67 KB, patch)
2022-08-18 05:51 UTC, David Cook
Details | Diff | Splinter Review
Bug 31380: Allow Koha Plugins to run using Mojolicious controllers (15.65 KB, patch)
2022-08-18 05:52 UTC, David Cook
Details | Diff | Splinter Review
Bug 31380: Add plugin hook for choosing Mojolicious or CGI script controller (4.67 KB, patch)
2022-08-18 05:52 UTC, David Cook
Details | Diff | Splinter Review
Bug 31380: Allow Koha Plugins to run using Mojolicious controllers (15.75 KB, patch)
2022-08-18 06:00 UTC, David Cook
Details | Diff | Splinter Review
Bug 31380: Add plugin hook for choosing Mojolicious or CGI script controller (4.67 KB, patch)
2022-08-18 06:01 UTC, David Cook
Details | Diff | Splinter Review
Bug 31380: Allow Koha Plugins to run using Mojolicious controllers (16.55 KB, patch)
2022-08-18 06:12 UTC, David Cook
Details | Diff | Splinter Review
Bug 31380: Add plugin hook for choosing Mojolicious or CGI script controller (4.67 KB, patch)
2022-08-18 06:12 UTC, David Cook
Details | Diff | Splinter Review
Bug 31380: Allow Koha Plugins to run using Mojolicious controllers (16.57 KB, patch)
2022-08-18 06:18 UTC, David Cook
Details | Diff | Splinter Review
Bug 31380: Add plugin hook for choosing Mojolicious or CGI script controller (4.67 KB, patch)
2022-08-18 06:18 UTC, David Cook
Details | Diff | Splinter Review
Bug 31380: Add unit tests (3.28 KB, patch)
2022-08-18 06:58 UTC, David Cook
Details | Diff | Splinter Review
Bug 31380: Add unit tests (3.27 KB, patch)
2022-08-18 07:00 UTC, David Cook
Details | Diff | Splinter Review
Bug 31380: Allow Koha Plugins to run using Mojolicious controllers (16.62 KB, patch)
2022-08-18 15:07 UTC, Kyle M Hall
Details | Diff | Splinter Review
Bug 31380: Add plugin hook for choosing Mojolicious or CGI script controller (4.72 KB, patch)
2022-08-18 15:07 UTC, Kyle M Hall
Details | Diff | Splinter Review
Bug 31380: Add unit tests (3.32 KB, patch)
2022-08-18 15:07 UTC, Kyle M Hall
Details | Diff | Splinter Review
Bug 31380: Allow Koha Plugins to run using Mojolicious controllers (16.62 KB, patch)
2022-08-26 09:07 UTC, Paul Derscheid
Details | Diff | Splinter Review
Bug 31380: Add plugin hook for choosing Mojolicious or CGI script controller (4.72 KB, patch)
2022-08-26 09:08 UTC, Paul Derscheid
Details | Diff | Splinter Review
Bug 31380: Add unit tests (3.38 KB, patch)
2022-08-26 09:08 UTC, Paul Derscheid
Details | Diff | Splinter Review
Bug 31380: Allow Koha Plugins to run using Mojolicious controllers (16.75 KB, patch)
2022-09-15 14:20 UTC, Martin Renvoize
Details | Diff | Splinter Review
Bug 31380: Add plugin hook for choosing Mojolicious or CGI script controller (4.84 KB, patch)
2022-09-15 14:20 UTC, Martin Renvoize
Details | Diff | Splinter Review
Bug 31380: Add unit tests (3.44 KB, patch)
2022-09-15 14:20 UTC, Martin Renvoize
Details | Diff | Splinter Review
Koha Plugin with Mojolicious controller (2.65 KB, application/zip)
2023-01-04 04:08 UTC, David Cook
Details
Bug 31380: Allow Koha Plugins to run using Mojolicious controllers (29.62 KB, patch)
2023-01-04 05:59 UTC, David Cook
Details | Diff | Splinter Review
Bug 31380: Add plugin hook for choosing Mojolicious or CGI script controller (5.54 KB, patch)
2023-01-04 05:59 UTC, David Cook
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description David Cook 2022-08-17 06:29:10 UTC
At the moment, Koha plugins run via Plack::App::CGIBin, which buffers responses and otherwise prevents plugins from fully leveraging the power of Plack and Mojolicious.
Comment 1 David Cook 2022-08-18 00:53:05 UTC Comment hidden (obsolete)
Comment 2 David Cook 2022-08-18 01:02:31 UTC Comment hidden (obsolete)
Comment 3 David Cook 2022-08-18 01:10:40 UTC Comment hidden (obsolete)
Comment 4 David Cook 2022-08-18 04:10:02 UTC Comment hidden (obsolete)
Comment 5 David Cook 2022-08-18 04:11:41 UTC Comment hidden (obsolete)
Comment 6 David Cook 2022-08-18 04:18:45 UTC Comment hidden (obsolete)
Comment 7 David Cook 2022-08-18 04:23:36 UTC Comment hidden (obsolete)
Comment 8 David Cook 2022-08-18 04:23:40 UTC Comment hidden (obsolete)
Comment 9 David Cook 2022-08-18 05:51:13 UTC Comment hidden (obsolete)
Comment 10 David Cook 2022-08-18 05:51:17 UTC Comment hidden (obsolete)
Comment 11 David Cook 2022-08-18 05:52:42 UTC Comment hidden (obsolete)
Comment 12 David Cook 2022-08-18 05:52:46 UTC Comment hidden (obsolete)
Comment 13 David Cook 2022-08-18 06:00:58 UTC Comment hidden (obsolete)
Comment 14 David Cook 2022-08-18 06:01:03 UTC Comment hidden (obsolete)
Comment 15 David Cook 2022-08-18 06:12:12 UTC Comment hidden (obsolete)
Comment 16 David Cook 2022-08-18 06:12:17 UTC Comment hidden (obsolete)
Comment 17 David Cook 2022-08-18 06:18:46 UTC
Created attachment 139324 [details] [review]
Bug 31380: Allow Koha Plugins to run using Mojolicious controllers

This patch adds a new mount point in plack.psgi, which makes Koha
able to run Mojolicious controllers. For example:
http://localhost:8081/cgi-bin/koha/staff/plugins/run

0a) Apply patch
0b) cp debian/templates/plack.psgi /etc/koha/sites/kohadev/plack.psgi
0c) koha-plack --restart
1) Upload Koha Plugin koha-plugin-mojolicious.kpz
2) koha-plack --restart
3) Go to
http://localhost:8081/cgi-bin/koha/plugins/plugins-home.pl
4) Next to "Mojolicious Test", click "Actions", then click "Run tool"
5) Note that the web page looks perfect
6) Note that the URL is a bit different to normal:
http://localhost:8081/cgi-bin/koha/staff/plugins/run?class=Koha%3A%3APlugin%3A%3AProsentient%3A%3AMojolicious&method=tool
instead of:
http://localhost:8081/cgi-bin/koha/plugins/run.pl?class=Koha%3A%3APlugin%3A%3AProsentient%3A%3AMojolicious&method=tool

Note: The test Koha plugin will only work with a Mojolicious controller,
but it could be easily made to work with both
Comment 18 David Cook 2022-08-18 06:18:52 UTC
Created attachment 139325 [details] [review]
Bug 31380: Add plugin hook for choosing Mojolicious or CGI script controller

This change checks for a "use_mojolicious" method in the plugin in order to
determine whether it should use a Mojolicious controller or a CGI script
controller.

Note that the check is for the existence of the method, so it is backwards
compatible with all existing Koha Plugins.
Comment 19 David Cook 2022-08-18 06:20:01 UTC
I'm pretty happy with these patches. The Koha::Mojo::Staff class will evolve over time, but I think that it's more than sufficient for running Koha Plugins now.
Comment 20 David Cook 2022-08-18 06:58:31 UTC
Created attachment 139327 [details] [review]
Bug 31380: Add unit tests
Comment 21 David Cook 2022-08-18 07:00:31 UTC
Created attachment 139328 [details] [review]
Bug 31380: Add unit tests
Comment 22 Kyle M Hall 2022-08-18 15:07:04 UTC
Created attachment 139394 [details] [review]
Bug 31380: Allow Koha Plugins to run using Mojolicious controllers

This patch adds a new mount point in plack.psgi, which makes Koha
able to run Mojolicious controllers. For example:
http://localhost:8081/cgi-bin/koha/staff/plugins/run

0a) Apply patch
0b) cp debian/templates/plack.psgi /etc/koha/sites/kohadev/plack.psgi
0c) koha-plack --restart
1) Upload Koha Plugin koha-plugin-mojolicious.kpz
2) koha-plack --restart
3) Go to
http://localhost:8081/cgi-bin/koha/plugins/plugins-home.pl
4) Next to "Mojolicious Test", click "Actions", then click "Run tool"
5) Note that the web page looks perfect
6) Note that the URL is a bit different to normal:
http://localhost:8081/cgi-bin/koha/staff/plugins/run?class=Koha%3A%3APlugin%3A%3AProsentient%3A%3AMojolicious&method=tool
instead of:
http://localhost:8081/cgi-bin/koha/plugins/run.pl?class=Koha%3A%3APlugin%3A%3AProsentient%3A%3AMojolicious&method=tool

Note: The test Koha plugin will only work with a Mojolicious controller,
but it could be easily made to work with both

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 23 Kyle M Hall 2022-08-18 15:07:13 UTC
Created attachment 139395 [details] [review]
Bug 31380: Add plugin hook for choosing Mojolicious or CGI script controller

This change checks for a "use_mojolicious" method in the plugin in order to
determine whether it should use a Mojolicious controller or a CGI script
controller.

Note that the check is for the existence of the method, so it is backwards
compatible with all existing Koha Plugins.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 24 Kyle M Hall 2022-08-18 15:07:16 UTC
Created attachment 139396 [details] [review]
Bug 31380: Add unit tests

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 25 Paul Derscheid 2022-08-26 09:07:56 UTC
Created attachment 139818 [details] [review]
Bug 31380: Allow Koha Plugins to run using Mojolicious controllers

This patch adds a new mount point in plack.psgi, which makes Koha
able to run Mojolicious controllers. For example:
http://localhost:8081/cgi-bin/koha/staff/plugins/run

0a) Apply patch
0b) cp debian/templates/plack.psgi /etc/koha/sites/kohadev/plack.psgi
0c) koha-plack --restart
1) Upload Koha Plugin koha-plugin-mojolicious.kpz
2) koha-plack --restart
3) Go to
http://localhost:8081/cgi-bin/koha/plugins/plugins-home.pl
4) Next to "Mojolicious Test", click "Actions", then click "Run tool"
5) Note that the web page looks perfect
6) Note that the URL is a bit different to normal:
http://localhost:8081/cgi-bin/koha/staff/plugins/run?class=Koha%3A%3APlugin%3A%3AProsentient%3A%3AMojolicious&method=tool
instead of:
http://localhost:8081/cgi-bin/koha/plugins/run.pl?class=Koha%3A%3APlugin%3A%3AProsentient%3A%3AMojolicious&method=tool

Note: The test Koha plugin will only work with a Mojolicious controller,
but it could be easily made to work with both

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 26 Paul Derscheid 2022-08-26 09:08:00 UTC
Created attachment 139819 [details] [review]
Bug 31380: Add plugin hook for choosing Mojolicious or CGI script controller

This change checks for a "use_mojolicious" method in the plugin in order to
determine whether it should use a Mojolicious controller or a CGI script
controller.

Note that the check is for the existence of the method, so it is backwards
compatible with all existing Koha Plugins.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Comment 27 Paul Derscheid 2022-08-26 09:08:05 UTC
Created attachment 139820 [details] [review]
Bug 31380: Add unit tests

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
Comment 28 Paul Derscheid 2022-08-26 09:09:19 UTC
Ha, meant to sign-off on all three but Kyle already did, so.
Just tested it out and I love this. Great work!
Comment 29 Marcel de Rooy 2022-08-29 11:59:31 UTC
(In reply to Paul Derscheid from comment #28)
> Ha, meant to sign-off on all three but Kyle already did, so.
> Just tested it out and I love this. Great work!

An additional signoff line only provides more evidence of testing. So never hesitate to add one :)
Comment 30 Marcel de Rooy 2022-08-29 12:07:34 UTC
David, just glancing over but this code looks very good.
Just thinking that Koha::Auth may be quite ambitiously named for now.
How long will we have C4::Auth and Koha::Auth side by side?
Do we actually need the 'staff' script or was it jusy easy while testing?
Comment 31 David Cook 2022-08-30 07:01:19 UTC
(In reply to Marcel de Rooy from comment #30)
> David, just glancing over but this code looks very good.

Thanks, Marcel :)

> Just thinking that Koha::Auth may be quite ambitiously named for now.
> How long will we have C4::Auth and Koha::Auth side by side?

Probably for a while, but Koha::Auth can be a target for any new auth work or for refactoring work. 

In Bug 31389, I've created Koha::Auth::Permissions, which contains a function to be used in C4::Auth. 

I suppose we could always use Koha::Mojo::Auth or some other Koha::Auth::* module instead of Koha::Auth.

> Do we actually need the 'staff' script or was it jusy easy while testing?

The "staff" script is for non-Plack enabled Koha instances. "api/v1/app.pl" works similarly. Although, in plack.psgi, the API gets loaded by loading that file whereas I just load the Koha::Mojo::Staff module as I think that's cleaner.
Comment 32 David Cook 2022-09-08 02:35:14 UTC
Does anyone need anything more from me on this one?
Comment 33 Martin Renvoize 2022-09-15 14:20:47 UTC
Created attachment 140673 [details] [review]
Bug 31380: Allow Koha Plugins to run using Mojolicious controllers

This patch adds a new mount point in plack.psgi, which makes Koha
able to run Mojolicious controllers. For example:
http://localhost:8081/cgi-bin/koha/staff/plugins/run

0a) Apply patch
0b) cp debian/templates/plack.psgi /etc/koha/sites/kohadev/plack.psgi
0c) koha-plack --restart
1) Upload Koha Plugin koha-plugin-mojolicious.kpz
2) koha-plack --restart
3) Go to
http://localhost:8081/cgi-bin/koha/plugins/plugins-home.pl
4) Next to "Mojolicious Test", click "Actions", then click "Run tool"
5) Note that the web page looks perfect
6) Note that the URL is a bit different to normal:
http://localhost:8081/cgi-bin/koha/staff/plugins/run?class=Koha%3A%3APlugin%3A%3AProsentient%3A%3AMojolicious&method=tool
instead of:
http://localhost:8081/cgi-bin/koha/plugins/run.pl?class=Koha%3A%3APlugin%3A%3AProsentient%3A%3AMojolicious&method=tool

Note: The test Koha plugin will only work with a Mojolicious controller,
but it could be easily made to work with both

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 34 Martin Renvoize 2022-09-15 14:20:52 UTC
Created attachment 140674 [details] [review]
Bug 31380: Add plugin hook for choosing Mojolicious or CGI script controller

This change checks for a "use_mojolicious" method in the plugin in order to
determine whether it should use a Mojolicious controller or a CGI script
controller.

Note that the check is for the existence of the method, so it is backwards
compatible with all existing Koha Plugins.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 35 Martin Renvoize 2022-09-15 14:20:56 UTC
Created attachment 140675 [details] [review]
Bug 31380: Add unit tests

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 36 Martin Renvoize 2022-09-15 14:22:01 UTC
This one took me a little while to wrap my head around.. probably just too many bugs on the mind.

Anywho, now I have it looks great and all works as expected.. I think we'll need to carefully document it somewhere to showcase it.

Tests are passing and the changes are covered nicely.

Passing QA
Comment 37 Tomás Cohen Arazi 2022-09-22 12:08:28 UTC
We certainly need docs.
Comment 38 Tomás Cohen Arazi 2022-09-22 12:23:05 UTC
I like where this is headed to.

I think we need some more tests here, particularly for the 'staff_authorize' and 'prepare_template', unless I'm missing something. I noticed at first glance a subtle issue with __koha__user__ vs. __koha_user__ that is not caught by the provided tests.

Otherwise this is great!

It would be great (also) if you could perltidy some of this code. I usually select the code I want to tidy, and just ctrl+k ctrl+f on VScode. Please do it inline. Thanks!
Comment 39 David Cook 2022-10-04 01:48:49 UTC
(In reply to Tomás Cohen Arazi from comment #38)
> I think we need some more tests here, particularly for the 'staff_authorize'
> and 'prepare_template', unless I'm missing something. I noticed at first
> glance a subtle issue with __koha__user__ vs. __koha_user__ that is not
> caught by the provided tests.

Yep, can certainly do that, when I have some time. 

> It would be great (also) if you could perltidy some of this code. I usually
> select the code I want to tidy, and just ctrl+k ctrl+f on VScode. Please do
> it inline. Thanks!

What's the issue? I think it passed the QA tools, so that should've pointed out any perltidy issues?

Because I'm on Windows, I need to do all my editing within the Docker container, so no VScode for me. But it looks like KTD has a ":Tidy" command in Vim, so I could use that. 

But this is fresh code that I thought I was writing in accordance with the Koha Coding Guidelines? Do we need to update those or am I just out of date?
Comment 40 David Cook 2023-01-04 01:52:14 UTC
(In reply to Tomás Cohen Arazi from comment #38)
> I think we need some more tests here, particularly for the 'staff_authorize'
> and 'prepare_template', unless I'm missing something. I noticed at first
> glance a subtle issue with __koha__user__ vs. __koha_user__ that is not
> caught by the provided tests.

Good catch with __koha__user__. I hadn't used that return value anywhere yet. 

Following my unit tests on Bug 30962 and looking at t/Koha/REST/Plugin/Query.t, I think I now know how to write the tests you're talking about. I'll give it a go. 
 
> It would be great (also) if you could perltidy some of this code. I usually
> select the code I want to tidy, and just ctrl+k ctrl+f on VScode. Please do
> it inline. Thanks!

I think there's a problem with the perltidy vim macro in KTD but otherwise I'm open to it...
Comment 41 David Cook 2023-01-04 04:08:22 UTC
Created attachment 144968 [details]
Koha Plugin with Mojolicious controller

Updating attached plugin to use updated staff interface templates and to use some code updates
Comment 42 David Cook 2023-01-04 05:59:21 UTC
Created attachment 144969 [details] [review]
Bug 31380: Allow Koha Plugins to run using Mojolicious controllers

This patch adds a new mount point in plack.psgi, which makes Koha
able to run Mojolicious controllers. For example:
http://localhost:8081/cgi-bin/koha/staff/plugins/run

0a) Apply patch
0b) cp debian/templates/plack.psgi /etc/koha/sites/kohadev/plack.psgi
0c) koha-plack --restart kohadev
1) Upload Koha Plugin koha-plugin-mojolicious.kpz
2) koha-plack --restart kohadev
3) Go to
http://localhost:8081/cgi-bin/koha/plugins/plugins-home.pl
4) Next to "Mojolicious Test", click "Actions", then click "Run tool"
5) Note that the web page looks perfect
6) Note that the URL is a bit different to normal:
http://localhost:8081/cgi-bin/koha/staff/plugins/run?class=Koha%3A%3APlugin%3A%3AProsentient%3A%3AMojolicious&method=tool
instead of:
http://localhost:8081/cgi-bin/koha/plugins/run.pl?class=Koha%3A%3APlugin%3A%3AProsentient%3A%3AMojolicious&method=tool

Note: The test Koha plugin will only work with a Mojolicious controller,
but it could be easily made to work with both

7) Run the above test using an unauthenticated user,
use a staff user without plugin permissions, try with a staff
user with plugin permissions, and try with a superlibrarian

8) Run tests
prove t/db_dependent/Koha/Auth.t
prove t/db_dependent/Koha/Mojo/Plugins/Core.t
prove t/db_dependent/Koha/Template.t
Comment 43 David Cook 2023-01-04 05:59:26 UTC
Created attachment 144970 [details] [review]
Bug 31380: Add plugin hook for choosing Mojolicious or CGI script controller

This change checks for a "use_mojolicious" method in the plugin in order to
determine whether it should use a Mojolicious controller or a CGI script
controller.

Note that the check is for the existence of the method, so it is backwards
compatible with all existing Koha Plugins.
Comment 44 David Cook 2023-01-04 06:05:11 UTC
I've added a lot more tests, streamlined the code more, and resolved an issue with the authorization process.

I could probably add a couple more end-to-end tests but I've run out of time today and I'm away the next 5 days. 

Moving this back to "Needs Signoff" to get eyes on it in the meantime...