Skip to content
This repository was archived by the owner on May 28, 2026. It is now read-only.

Commit 10a1e43

Browse files
authored
Ignore query string when testing image URLs
Allow image URLs to have any query string parameters, and split the URL check messages into two so that it's clearer which is being applied when a URL fails. Bug: T422595
1 parent 8a75632 commit 10a1e43

4 files changed

Lines changed: 22 additions & 7 deletions

File tree

i18n/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"form-heading": "Transcribe an image",
66
"image-url": "Image URL",
77
"image-url-help": "Insert an image URL hosted on a Wikimedia server such as: $1",
8-
"image-url-error": "Image URL must begin with {{PLURAL:$1|the following domain name|one of the following domain names}} and end with a valid file extension: $2",
8+
"image-url-error": "Image URL must be for {{PLURAL:$1|the following domain name|one of the following domain names}}: $2",
9+
"image-url-error-extension": "Image URL have one of the following file extensions: $1",
910
"image-alt-text": "The original image",
1011
"language-code": "Languages (optional)",
1112
"engine": "OCR engine",

i18n/qqq.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"form-heading": "Heading for the main form.",
1313
"image-url": "Form label for the 'image' field.",
1414
"image-url-help": "Help text for the 'image' field.",
15-
"image-url-error": "Error message shown when the 'image' field is not valid.",
15+
"image-url-error": "Error message shown when the 'image' field is not for a valid host.\n\nParameter:\n* $1 — Integer number of host names.\n* $2 — List of host names.",
16+
"image-url-error-extension": "Error message shown when the 'image' field is not for a valid file extension.\n\nParameter:\n* $1 — List of permitted file extensions.",
1617
"image-alt-text": "Alt text for the displayed image, next to the OCR text box.",
1718
"language-code": "Form label for the 'languages' field.",
1819
"engine": "Form label for the 'engine' field.\nOCR is a common abbreviation in English for \"Optical Characters Recognition\".",

src/Engine/EngineBase.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,26 @@ public function getImageHosts(): array {
137137
* @throws OcrException
138138
*/
139139
public function checkImageUrl( string $imageUrl ): void {
140-
$hostRegex = implode( '|', array_map( 'preg_quote', $this->getImageHosts() ) );
141-
$formatRegex = implode( '|', self::ALLOWED_FORMATS );
142-
$regex = "/^https?:\/\/($hostRegex)\/.+($formatRegex)$/";
143-
$matches = preg_match( $regex, strtolower( $imageUrl ) );
144-
if ( $matches !== 1 ) {
140+
$parts = parse_url( $imageUrl );
141+
142+
// Check host.
143+
if ( !isset( $parts['host'] ) || !in_array( $parts['host'], $this->getImageHosts(), true ) ) {
145144
$params = [ count( $this->getImageHosts() ), $this->intuition->listToText( $this->getImageHosts() ) ];
146145
throw new OcrException( 'image-url-error', $params );
147146
}
147+
148+
// Check file extension.
149+
$validExt = false;
150+
if ( isset( $parts['path'] ) ) {
151+
$formatRegex = implode( '|', self::ALLOWED_FORMATS );
152+
$regex = "/.+($formatRegex)$/";
153+
$matches = preg_match( $regex, strtolower( $parts['path'] ) );
154+
$validExt = $matches === 1;
155+
}
156+
if ( !$validExt ) {
157+
$params = [ $this->intuition->listToText( self::ALLOWED_FORMATS ) ];
158+
throw new OcrException( 'image-url-error-extension', $params );
159+
}
148160
}
149161

150162
/**

tests/Engine/EngineBaseTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function provideCheckImageUrl(): array {
5656
// Pass:
5757
[ 'https://upload.wikimedia.org/wikipedia/commons/a/a9/Example.jpg', false ],
5858
[ 'https://upload.wikimedia.org/wikipedia/commons/file.jpg', false ],
59+
[ 'https://upload.wikimedia.org/wikipedia/commons/file.jpg?foo=bar', false ],
5960
// Fail:
6061
[ 'https://foo.example.com/wikipedia/commons/a/a9/Example.jpg', true ],
6162
[ 'https://en.wikisource.org/file.mov', true ],

0 commit comments

Comments
 (0)