Jump to content

Help:Wikifunctions/Images

From mediawiki.org

Wikifunctions can return images from Wikimedia Commons as part of a function's output. When the result of such a function is displayed (for example in Abstract Wikipedia or when running the function), the image appears as a Wikipedia-style thumbnail with attribution back to Commons.

Images in Wikifunctions are one of the features built on the Rich Outputs system: a function returns Z89 content containing a small <ext-wikilambda-image> custom element, and the Wikifunctions PHP renderer replaces it with a trusted <figure> block at render time.

How images are rendered

[edit]
A function output containing an <ext-wikilambda-image> element, rendered as a Wikipedia-style thumbnail.

A function that produces an image returns Z89 output containing one or more custom elements of the form:

<ext-wikilambda-image mid="M68960758" size="thumb" alt="Snowy mountains" />

Attributes:

  • mid — the Commons media ID (an M-number, e.g. M68960758). This is the permanent, stable identifier for a Commons file; it does not change when the file is renamed or moved on Commons.
  • size — currently only "thumb" is supported. Renders as a floating thumbnail on the side of the page opposite to where text starts (right-floating for ltr languages) with a Wikipedia-style border.
  • alt — — alt text describing the image, used by screen readers and other assistive technology. When the image renders, it is placed on the rendered <img>; when the image cannot be loaded, it travels with the broken-image placeholder as an aria-label so the description is still announced to assistive tools.

If the image cannot be rendered (for example because the M-ID is invalid, the file does not exist on Commons, or the file is not a supported image type) an image placeholder is shown in its place, so the rest of the function output still renders.

When an image fails to render an image placeholder is shown in its place so the rest of the function output still renders.

Exemplar function

[edit]
The "Create Commons Thumbnail" function page, with a Commons file selected via the media selector.

Wikifunctions provides one exemplar function for producing image output:

  • Create Commons Thumbnail (Z36038) — takes a Z310 (Commons Image Reference) and an alt-text string, and returns a Z89 fragment containing a single <ext-wikilambda-image> element ready for the renderer.

To use it, visit the function page (Special:ViewObject/Z36038), run it with a Commons M-ID and alt text, and use the output in a composition or as the implementation of another function whose result is displayed where images are supported (for example, soon in Abstract Wikipedia).

Building your own image functions

[edit]

You can write more functions that produce image output. Any function whose Z89 output includes one or more well-formed <ext-wikilambda-image> elements will produce thumbnails when its result is displayed in a context that supports them.

A few patterns worth considering:

  • Image from a Wikidata item — take a Wikidata item and emit an <ext-wikilambda-image> using the M-ID of the item's image (P18) claim. (pending phab:T428224)
  • Image with computed alt text — call another function (for example one that generates a multilingual description from a Wikidata item) to produce the alt attribute, so the alt text travels with the article's render language..

The renderer reads only the mid, size, and alt attributes; other attributes on the custom element are ignored. Your function does not need to (and should not) embed Commons URLs, dimensions, or licensing data — the M-ID is the only stable identifier; everything else is fetched at render time.

V1 limitations

[edit]

The current image support has the following limitations. These are deliberate scoping decisions for v1; some may be relaxed in future iterations.

  • Commons only — images must come from Wikimedia Commons. Files on other wikis (including local file pages) are not supported. There is no integrated upload flow in Wikifunctions. So new files have to be uploaded to Commons separately first.
  • Static images only — only files with image MIME types are rendered. Video, audio, PDFs, and other non-image media are rejected.
  • Single thumbnail size — only size="thumb" is supported. There is no none, center, or pixel-sized variant yet.
  • No srcset — the rendered <img> serves a single fixed thumbnail and has no srcset, so on high-DPI displays the image may appear less sharp than a native MediaWiki thumbnail.
  • No direction control — images always float to the side of the page opposite to where text starts (right-floating for ltr languages); there is no left, right, center, or inline (no-float) option.
  • No figcaption — captions are not yet wired through. The rendered <figure> reserves a <figcaption> slot but does not populate it.
  • No MediaViewer — clicking the rendered thumbnail goes straight to the Commons file page in a new tab; MediaViewer is not invoked.

See also

[edit]