Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 216
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImageMap
0.00% covered (danger)
0.00%
0 / 216
0.00% covered (danger)
0.00%
0 / 5
6806
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onParserFirstCallInit
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 render
0.00% covered (danger)
0.00%
0 / 203
0.00% covered (danger)
0.00%
0 / 1
5256
 tokenizeCoords
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
56
 error
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Main file for extension ImageMap.
4 *
5 * @file
6 * @ingroup Extensions
7 *
8 * Syntax:
9 * <imagemap>
10 * Image:Foo.jpg | 100px | picture of a foo
11 *
12 * rect    0  0  50 50  [[Foo type A]]
13 * circle  50 50 20     [[Foo type B]]
14 *
15 * desc bottom-left
16 * </imagemap>
17 *
18 * Coordinates are relative to the source image, not the thumbnail.
19 */
20
21namespace MediaWiki\Extension\ImageMap;
22
23use MediaWiki\Config\Config;
24use MediaWiki\FileRepo\RepoGroup;
25use MediaWiki\Hook\ParserFirstCallInitHook;
26use MediaWiki\Html\Html;
27use MediaWiki\MainConfigNames;
28use MediaWiki\Page\File\BadFileLookup;
29use MediaWiki\Parser\Parser;
30use MediaWiki\Parser\Sanitizer;
31use MediaWiki\Title\Title;
32use Wikimedia\Assert\Assert;
33use Wikimedia\Parsoid\Core\DOMCompat;
34use Wikimedia\Parsoid\DOM\Element;
35use Wikimedia\Parsoid\Ext\DOMUtils;
36use Wikimedia\Parsoid\Ext\WTUtils;
37
38class ImageMap implements ParserFirstCallInitHook {
39
40    private const TOP_RIGHT = 0;
41    private const BOTTOM_RIGHT = 1;
42    private const BOTTOM_LEFT = 2;
43    private const TOP_LEFT = 3;
44    private const NONE = 4;
45
46    private const DESC_TYPE_MAP = [
47        'top-right', 'bottom-right', 'bottom-left', 'top-left'
48    ];
49
50    public function __construct(
51        private readonly BadFileLookup $badFileLookup,
52        private readonly Config $config,
53        private readonly RepoGroup $repoGroup,
54    ) {
55    }
56
57    /**
58     * @param Parser $parser
59     */
60    public function onParserFirstCallInit( $parser ) {
61        $parser->setHook( 'imagemap', [ $this, 'render' ] );
62    }
63
64    /**
65     * @param string|null $input
66     * @param array $params
67     * @param Parser $parser
68     * @return string HTML (Image map, or error message)
69     */
70    public function render( $input, $params, Parser $parser ) {
71        if ( $input === null ) {
72            return '';
73        }
74
75        $urlProtocols = $this->config->get( MainConfigNames::UrlProtocols );
76
77        $lines = explode( "\n", $input );
78
79        $first = true;
80        $scale = 1;
81        $imageNode = null;
82        $domDoc = DOMCompat::newDocument( true );
83        $domFragment = null;
84        $thumbWidth = 0;
85        $thumbHeight = 0;
86        $imageTitle = null;
87        $mapHTML = '';
88        $links = [];
89        $explicitNone = false;
90
91        // Define canonical desc types to allow i18n of 'imagemap_desc_types'
92        $descTypesCanonical = 'top-right, bottom-right, bottom-left, top-left, none';
93        $descType = self::BOTTOM_RIGHT;
94        $defaultLinkAttribs = false;
95        $realMap = true;
96        $extLinks = [];
97        foreach ( $lines as $lineNum => $line ) {
98            $lineNum++;
99            $externLink = false;
100
101            $line = trim( $line );
102            if ( $line === '' || $line[0] === '#' ) {
103                continue;
104            }
105
106            if ( $first ) {
107                $first = false;
108
109                // The first line should have an image specification on it
110                // Extract it and render the HTML
111                $bits = explode( '|', $line, 2 );
112                $image = $bits[0];
113                $options = $bits[1] ?? '';
114                $imageTitle = Title::newFromText( $image );
115                if ( !$imageTitle || !$imageTitle->inNamespace( NS_FILE ) ) {
116                    return $this->error( 'imagemap_no_image' );
117                }
118                if ( $this->badFileLookup->isBadFile( $imageTitle->getDBkey(), $parser->getTitle() ) ) {
119                    return $this->error( 'imagemap_bad_image' );
120                }
121                // Parse the options so we can use links and the like in the caption
122                $parsedOptions = $options === '' ? '' : $parser->recursiveTagParse( $options );
123
124                $explicitNone = preg_match( '/(^|\|)none(\||$)/D', $parsedOptions );
125                if ( !$explicitNone ) {
126                    $parsedOptions .= '|none';
127                }
128
129                $imageHTML = $parser->makeImageHtml( $imageTitle, $parsedOptions );
130                $imageHTML = $parser->getStripState()->unstripBoth( $imageHTML );
131                $imageHTML = Sanitizer::normalizeCharReferences( $imageHTML );
132
133                $domFragment = $domDoc->createDocumentFragment();
134                DOMUtils::setFragmentInnerHTML( $domFragment, $imageHTML );
135                $imageNode = DOMCompat::querySelector( $domFragment, 'img' );
136                if ( !$imageNode ) {
137                    return $this->error( 'imagemap_invalid_image' );
138                }
139                $thumbWidth = (int)( DOMCompat::getAttribute( $imageNode, 'width' ) ?? '0' );
140                $thumbHeight = (int)( DOMCompat::getAttribute( $imageNode, 'height' ) ?? '0' );
141
142                $imageObj = $this->repoGroup->findFile( $imageTitle );
143                if ( !$imageObj || !$imageObj->exists() ) {
144                    return $this->error( 'imagemap_invalid_image' );
145                }
146                // Add the linear dimensions to avoid inaccuracy in the scale
147                // factor when one is much larger than the other
148                // (sx+sy)/(x+y) = s
149                $denominator = $imageObj->getWidth() + $imageObj->getHeight();
150                $numerator = $thumbWidth + $thumbHeight;
151                if ( $denominator <= 0 || $numerator <= 0 ) {
152                    return $this->error( 'imagemap_invalid_image' );
153                }
154                $scale = $numerator / $denominator;
155                continue;
156            }
157
158            // Handle desc spec
159            $cmd = strtok( $line, " \t" );
160            if ( $cmd === 'desc' ) {
161                $typesText = wfMessage( 'imagemap_desc_types' )->inContentLanguage()->text();
162                if ( $descTypesCanonical !== $typesText ) {
163                    // i18n desc types exists
164                    $typesText = $descTypesCanonical . ', ' . $typesText;
165                }
166                $types = array_map( 'trim', explode( ',', $typesText ) );
167                $type = trim( strtok( '' ) ?: '' );
168                $descType = array_search( $type, $types );
169                if ( $descType > 4 ) {
170                    // A localized descType is used. Subtract 5 to reach the canonical desc type.
171                    $descType -= 5;
172                }
173                // <0? In theory never, but paranoia...
174                if ( $descType === false || $descType < 0 ) {
175                    return $this->error( 'imagemap_invalid_desc', $typesText );
176                }
177                continue;
178            }
179
180            $title = false;
181            $alt = '';
182            // Find the link
183            $link = trim( strstr( $line, '[' ) );
184            $m = [];
185            if ( preg_match( '/^ \[\[  ([^|]*+)  \|  ([^\]]*+)  \]\] \w* $ /x', $link, $m ) ) {
186                $title = Title::newFromText( $m[1] );
187                $alt = trim( $m[2] );
188            } elseif ( preg_match( '/^ \[\[  ([^\]]*+) \]\] \w* $ /x', $link, $m ) ) {
189                $title = Title::newFromText( $m[1] );
190                if ( $title === null ) {
191                    return $this->error( 'imagemap_invalid_title', $lineNum );
192                }
193                $alt = $title->getFullText();
194            } elseif ( in_array( substr( $link, 1, strpos( $link, '//' ) + 1 ), $urlProtocols )
195                || in_array( substr( $link, 1, strpos( $link, ':' ) ), $urlProtocols )
196            ) {
197                if ( preg_match( '/^ \[  ([^\s]*+)  \s  ([^\]]*+)  \] \w* $ /x', $link, $m ) ) {
198                    $title = $m[1];
199                    $alt = trim( $m[2] );
200                    $externLink = true;
201                } elseif ( preg_match( '/^ \[  ([^\]]*+) \] \w* $ /x', $link, $m ) ) {
202                    $title = $alt = trim( $m[1] );
203                    $externLink = true;
204                }
205            } else {
206                return $this->error( 'imagemap_no_link', $lineNum );
207            }
208            if ( !$title ) {
209                return $this->error( 'imagemap_invalid_title', $lineNum );
210            }
211
212            $shapeSpec = substr( $line, 0, -strlen( $link ) );
213
214            // Tokenize shape spec
215            $shape = strtok( $shapeSpec, " \t" );
216            switch ( $shape ) {
217                case 'default':
218                    $coords = [];
219                    break;
220                case 'rect':
221                    $coords = $this->tokenizeCoords( $lineNum, 4 );
222                    if ( !is_array( $coords ) ) {
223                        return $coords;
224                    }
225                    break;
226                case 'circle':
227                    $coords = $this->tokenizeCoords( $lineNum, 3 );
228                    if ( !is_array( $coords ) ) {
229                        return $coords;
230                    }
231                    break;
232                case 'poly':
233                    $coords = $this->tokenizeCoords( $lineNum, 1, true );
234                    if ( !is_array( $coords ) ) {
235                        return $coords;
236                    }
237                    if ( count( $coords ) % 2 !== 0 ) {
238                        return $this->error( 'imagemap_poly_odd', $lineNum );
239                    }
240                    break;
241                default:
242                    return $this->error( 'imagemap_unrecognised_shape', $lineNum );
243            }
244
245            // Scale the coords using the size of the source image
246            foreach ( $coords as $i => $c ) {
247                $coords[$i] = (int)round( $c * $scale );
248            }
249
250            // Construct the area tag
251            $attribs = [];
252            if ( $externLink ) {
253                // Get the 'target' and 'rel' attributes for external link.
254                $attribs = $parser->getExternalLinkAttribs( $title );
255
256                $attribs['href'] = $title;
257                $attribs['class'] = 'plainlinks';
258            } elseif ( $title->getFragment() !== '' && $title->getPrefixedDBkey() === '' ) {
259                // XXX: kluge to handle [[#Fragment]] links, should really fix getLocalURL()
260                // in Title.php to return an empty string in this case
261                $attribs['href'] = $title->getFragmentForURL();
262            } else {
263                $attribs['href'] = $title->getLocalURL() . $title->getFragmentForURL();
264            }
265            if ( $shape !== 'default' ) {
266                $attribs['shape'] = $shape;
267            }
268            if ( $coords ) {
269                $attribs['coords'] = implode( ',', $coords );
270            }
271            if ( $alt !== '' ) {
272                if ( $shape !== 'default' ) {
273                    $attribs['alt'] = $alt;
274                }
275                $attribs['title'] = $alt;
276            }
277            if ( $shape === 'default' ) {
278                $defaultLinkAttribs = $attribs;
279            } else {
280                // @phan-suppress-next-line SecurityCheck-DoubleEscaped
281                $mapHTML .= Html::element( 'area', $attribs ) . "\n";
282            }
283            if ( $externLink ) {
284                $extLinks[] = $title;
285            } else {
286                $links[] = $title;
287            }
288        }
289
290        if ( !$imageNode || !$domFragment ) {
291            return $this->error( 'imagemap_no_image' );
292        }
293
294        if ( $mapHTML === '' ) {
295            // no areas defined, default only. It's not a real imagemap, so we do not need some tags
296            $realMap = false;
297        }
298
299        if ( $realMap ) {
300            // Construct the map
301            // Add a hash of the map HTML to avoid breaking cached HTML fragments that are
302            // later joined together on the one page (T18471).
303            // The only way these hashes can clash is if the map is identical, in which
304            // case it wouldn't matter that the "wrong" map was used.
305            $mapName = 'ImageMap_' . substr( md5( $mapHTML ), 0, 16 );
306            $mapHTML = "<map name=\"$mapName\">\n$mapHTML</map>\n";
307
308            // Alter the image tag
309            $imageNode->setAttribute( 'usemap', "#$mapName" );
310        }
311
312        if ( $mapHTML !== '' ) {
313            $mapFragment = $domDoc->createDocumentFragment();
314            DOMUtils::setFragmentInnerHTML( $mapFragment, $mapHTML );
315            $mapNode = $mapFragment->firstChild;
316        }
317
318        $div = null;
319
320        $anchor = $imageNode->parentNode;
321        $wrapper = $anchor->parentNode;
322        Assert::precondition( $wrapper instanceof Element, 'Anchor node has a parent' );
323        '@phan-var Element $anchor';
324
325        $classes = DOMCompat::getAttribute( $wrapper, 'class' ) ?? '';
326
327        // For T22030
328        $classes .= ( $classes ? ' ' : '' ) . 'noresize';
329
330        // Remove that class if it was only added while forcing a block
331        if ( !$explicitNone ) {
332            $classes = trim( preg_replace( '/ ?mw-halign-none/', '', $classes ) );
333        }
334
335        $wrapper->setAttribute( 'class', $classes );
336
337        if ( $defaultLinkAttribs ) {
338            $imageParent = $domDoc->createElement( 'a' );
339            foreach ( $defaultLinkAttribs as $name => $value ) {
340                $imageParent->setAttribute( $name, $value );
341            }
342        } else {
343            $imageParent = $domDoc->createElement( 'span' );
344        }
345        $wrapper->insertBefore( $imageParent, $anchor );
346
347        if ( !WTUtils::hasVisibleCaption( $wrapper ) ) {
348            $caption = DOMCompat::querySelector( $domFragment, 'figcaption' );
349            $captionText = trim( WTUtils::textContentFromCaption( $caption ) );
350            if ( $captionText ) {
351                $imageParent->setAttribute( 'title', $captionText );
352            }
353        }
354
355        if ( isset( $mapNode ) ) {
356            $wrapper->insertBefore( $mapNode, $anchor );
357        }
358
359        $imageParent->appendChild( $imageNode->cloneNode( true ) );
360        $wrapper->removeChild( $anchor );
361
362        $parserOutput = $parser->getOutput();
363
364        if ( DOMUtils::hasTypeOf( $wrapper, 'mw:File/Thumb' ) ) {
365            // $imageNode was cloned above
366            $img = DOMCompat::getFirstElementChild( $imageParent );
367            if ( $img !== null && !$img->hasAttribute( 'resource' ) ) {
368                $img->setAttribute( 'resource', $imageTitle->getLocalURL() );
369            }
370        } elseif ( $descType !== self::NONE ) {
371            // The following classes are used here:
372            // * mw-ext-imagemap-desc-top-right
373            // * mw-ext-imagemap-desc-bottom-right
374            // * mw-ext-imagemap-desc-bottom-left
375            // * mw-ext-imagemap-desc-top-left
376            DOMCompat::getClassList( $wrapper )->add(
377                'mw-ext-imagemap-desc-' . self::DESC_TYPE_MAP[$descType]
378            );
379            // $imageNode was cloned above
380            $img = DOMCompat::getFirstElementChild( $imageParent );
381            if ( $img !== null && !$img->hasAttribute( 'resource' ) ) {
382                $img->setAttribute( 'resource', $imageTitle->getLocalURL() );
383            }
384            $parserOutput->addModules( [ 'ext.imagemap' ] );
385            $parserOutput->addModuleStyles( [ 'ext.imagemap.styles' ] );
386        }
387
388        // Output the result (XHTML-compliant)
389        $output = DOMUtils::getFragmentInnerHTML( $domFragment );
390
391        // Register links
392        foreach ( $links as $title ) {
393            if ( $title->isExternal() || $title->getNamespace() === NS_SPECIAL ) {
394                // Don't register special or interwiki links...
395            } elseif ( $title->getNamespace() === NS_MEDIA ) {
396                // Regular Media: links are recorded as image usages
397                $parserOutput->addImage( $title->getDBkey() );
398            } else {
399                // Plain ol' link
400                $parserOutput->addLink( $title );
401            }
402        }
403        foreach ( $extLinks as $title ) {
404            $parserOutput->addExternalLink( $title );
405        }
406        // Armour output against broken parser
407        return str_replace( "\n", '', $output );
408    }
409
410    /**
411     * @param int|string $lineNum Line number, for error reporting
412     * @param int $minCount Minimum token count
413     * @param bool $allowNegative
414     * @return array|string String with error (HTML), or array of coordinates
415     */
416    private function tokenizeCoords( $lineNum, $minCount = 0, $allowNegative = false ) {
417        $coords = [];
418        $coord = strtok( " \t" );
419        while ( $coord !== false ) {
420            if ( !is_numeric( $coord ) || $coord > 1e9 || ( !$allowNegative && $coord < 0 ) ) {
421                return $this->error( 'imagemap_invalid_coord', $lineNum );
422            }
423            $coords[] = $coord;
424            $coord = strtok( " \t" );
425        }
426        if ( count( $coords ) < $minCount ) {
427            // TODO: Should this also check there aren't too many coords?
428            return $this->error( 'imagemap_missing_coord', $lineNum );
429        }
430        return $coords;
431    }
432
433    /**
434     * @param string $name
435     * @param string|int|bool $line
436     * @return string HTML
437     */
438    private function error( $name, $line = false ) {
439        return '<p class="error">' . wfMessage( $name, $line )->parse() . '</p>';
440    }
441}