Fix: handle array input for engine parameter in OcrController#167
Conversation
|
See above for the CI failures. Have you managed to get this running locally? You should run |
Thanks for the feedback! I have now tested the changes locally by running I also verified the behavior in the browser with:
The application no longer throws errors and correctly handles array inputs for the engine parameter. Please let me know if any further improvements are needed. |
|
There's no need to open a new PR (#168), you can make changes to this one. When running the tests locally do you get any phpcs errors? |
thanks for the guidance |
| // Apply the tesseract-specific settings | ||
| // NOTE: Intentionally excluding `oem`, see T285262 | ||
| if ( TesseractEngine::getId() === static::$params['engine'] ) { | ||
| if ( $this->engine instanceof TesseractEngine && $this->engine->getId() === static::$params['engine'] ) { |
There was a problem hiding this comment.
This change (and the one on line 150) is not correct. It is now checking the selected engine against itself, rather than checking whether it is tesseract.
|
|
||
| static::$params['engine'] = $this->engine::getId(); | ||
| $this->setEngineOptions(); | ||
| // 🔧 Fix: handle array input |
There was a problem hiding this comment.
There's no need to comment that this is a fix.
| try { | ||
| $this->engine = $this->engineFactory->get( $requestedEngine ); | ||
| } catch ( EngineNotFoundException $e ) { | ||
| $this->addFlash( 'error', $this->intuition->msg( | ||
| 'engine-not-found-warning', | ||
| [ 'variables' => [ $requestedEngine, static::DEFAULT_ENGINE ] ] | ||
| ) ); | ||
| $this->engine = $this->engineFactory->get( static::DEFAULT_ENGINE ); | ||
| } | ||
|
|
||
| static::$params['engine'] = $this->engine->getId(); | ||
| static::$params['image'] = (string)$this->request->query->get( 'image' ); | ||
| if ( substr( static::$params['image'], 0, 2 ) === '//' ) { | ||
| static::$params['image'] = "https:" . static::$params['image']; | ||
| } |
There was a problem hiding this comment.
It looks like this part (and the setEngineOptions below) is mostly unchanged other than changing the order. What is the reasoning for changing it like this? A couple of comments have been separated from their code.
| ) ); | ||
| $this->engine = $this->engineFactory->get( static::DEFAULT_ENGINE ); | ||
| } | ||
| $requestedEngine = $this->request->query->get( 'engine', static::$params['engine'] ); |
There was a problem hiding this comment.
This an the following lines are all missing an indent level. I'm not quite sure why this isn't being caught by phpcs (if you want to dig into that, and add a phpcs rule for it, feel free!).
There was a problem hiding this comment.
This an the following lines are all missing an indent level. I'm not quite sure why this isn't being caught by phpcs (if you want to dig into that, and add a phpcs rule for it, feel free!).
Hi @samwilson
I’ve addressed all the comments you made on the PR:
Fixed the array input handling for engine.
Corrected the Tesseract and Transkribus checks in setEngineOptions.
Removed unnecessary comments and fixed indent/whitespace issues.
Verified everything with composer test — all tests pass, no PHPCS errors.
Please let me know if anything else needs adjustment.
Thanks for your guidance!
| if ( is_array( $requestedEngine ) ) { | ||
| $requestedEngine = $requestedEngine[0] ?? static::$params['engine']; | ||
| } |
There was a problem hiding this comment.
It looks like this is the only actual change needed here. What are the other changes below aiming to do? (They don't look wrong necessarily, but don't seem related to the task at hand.)
There was a problem hiding this comment.
I’ve removed the unrelated changes and kept only the array handling fix. Please let me know if anything else is needed.
There was a problem hiding this comment.
Sorry, it looks like you've added even more changes. If you run e.g. git diff origin/main before pushing you should be able to see the changes you're making in this branch.
There was a problem hiding this comment.
I’ve confirmed the only real change here is the array input fix in OcrController.php. composer.phar is local and not tracked.
There was a problem hiding this comment.
Where are you looking to see your changes? If you go to https://github.com/wikimedia/wikimedia-ocr/pull/167/changes you can see all the changes that you're making in this PR. You can see there are a bunch of unrelated things going on below here (e.g. you're changing a comment from "Change protocol-relative URLs…" to "Convert protocol-relative URLs…").
Are you using a GenAI tool to make these changes, by any chance? It may be doing more than you think it is.
There was a problem hiding this comment.
Thanks a lot for the encouragement
I’ll continue working on this and share a clean PR soon.
There was a problem hiding this comment.
You're nearly there! If you want to keep working on it, I'm happy to keep helping you. You're not slowing anything down, this bug has existed for years. :-)
Hi @samwilson, thanks again for your guidance 🙂
I’ve reset my branch to match upstream/main and re-applied only the required change for handling array input for the engine parameter. I made sure there are no unrelated changes in the diff.
I also ran composer test locally — all PHPUnit tests are passing. There is a small issue with bin/console on Windows, but it seems unrelated to this change.
Please let me know if everything looks correct now or if any further changes are needed!
There was a problem hiding this comment.
Looks good! Thanks for persisting.
Feel free to open a bug report on Phabricator about the issue with bin/console. Always good to track things even if they're not going to be fixed straight away.
There was a problem hiding this comment.
I’ll open a bug report on Phabricator for the bin/console issue.
Thanks for the suggestion!
There was a problem hiding this comment.
Looks good! Thanks for persisting.
Feel free to open a bug report on Phabricator about the issue with
bin/console. Always good to track things even if they're not going to be fixed straight away.
Hi @samwilson thank you again for reviewing and merging my contribution—I really appreciate your guidance.
I’m planning to apply for GSoC 2026 and wanted to ask if you have any suggestions on how I can improve my contributions or prepare better within Wikimedia projects. Any advice would mean a lot.
Thanks again for your support!
a821942 to
d756589
Compare
Fixes error when 'engine' parameter is passed as an array.
Previously, passing engine[]=tesseract caused a TypeError because EngineFactory::get() expects a string.
This change ensures that if the 'engine' parameter is an array, the first value is extracted before passing it to the factory.
Tested locally using PHP built-in server.