Page MenuHomePhabricator

Reimplement Sanitizer::escapeId in JavaScript
Closed, ResolvedPublic

Description

The PHP function Sanitizer::escapeId is used to generate URL fragments and HTML IDs. It has a special encoding which is MediaWiki specific. This part of the function is needed in JavaScript to generate URLs with fragments and HTML IDs from API responses.

Example:
The API request https://de.wikipedia.org/w/api.php?action=query&format=json&titles=Burgh%C3%B6hle+Dietfurt&redirects=true with the result

{
    "batchcomplete": "",
    "query": {
        "redirects": [
            {
                "from": "Burgh\u00f6hle Dietfurt",
                "to": "Ruine Dietfurt",
                "tofragment": "Burgh\u00f6hle Dietfurt"
            }
        ],
        "pages": {
            "922564": {
                "pageid": 922564,
                "ns": 0,
                "title": "Ruine Dietfurt"
            }
        }
    }
}

has to converted to the URL https://de.wikipedia.org/wiki/Ruine_Dietfurt#Burgh.C3.B6hle_Dietfurt

The JavaScript function should also support wgExperimentalHtmlIds because the encoding in PHP depends on it.

The JavaScript function should not decode HTML entities like ü to ü because entities from wikitext are already decoded in the API responses.

The JavaScript function may optionally support adding a letter as first character.

This function can also used for fragment support in T103553 and T103715 and for ID generating in T87557.

Suggested name for the function: mw.util.escapeId. Alternative: mw.util.fragmentEncode or mw.util.fragmentencode.

Event Timeline

Fomafix raised the priority of this task from to Needs Triage.
Fomafix updated the task description. (Show Details)
Fomafix added a project: JavaScript.
Fomafix subscribed.

Where does this code currently live? In MediaWiki Core (MediaWiki-General)?
Would this be an good first task task that a new contributor could handle, or is this more complex?

Change 221654 had a related patch set uploaded (by Gerrit Patch Uploader):
mediawiki.util: Implement Sanitizer::escapeId as mw.util.escapeId

https://gerrit.wikimedia.org/r/221654

Fomafix set Security to None.
Krinkle renamed this task from Reimplement Sanitizer::escapeId in JavaScript to Query API should format title fragment as valid for use in URL.Jul 13 2015, 5:20 PM
Krinkle added a project: MediaWiki-Action-API.
Krinkle added subscribers: Anomie, bd808.

As newly titled, declined. Feel free to reopen this bug if you change the title back.

Fomafix renamed this task from Query API should format title fragment as valid for use in URL to Reimplement Sanitizer::escapeId in JavaScript.Sep 2 2015, 4:24 AM
Fomafix reopened this task as Open.

Reopened with original title.

In project mediawiki/services/parsoid the Sanitizer's escapeId is reimplemented in JavaScript: https://gerrit.wikimedia.org/r/226032

Why not such a function in core?

Change 221654 abandoned by Krinkle:
mediawiki.util: Implement Sanitizer::escapeId as mw.util.escapeId

https://gerrit.wikimedia.org/r/221654

Change 260328 had a related patch set uploaded (by Gerrit Patch Uploader):
mediawiki.util: Use correct encoding for fragment in getUrl

https://gerrit.wikimedia.org/r/260328

matmarex changed the task status from Declined to Resolved.Dec 21 2015, 4:25 PM
matmarex added subscribers: Krinkle, matmarex.

This was previously declined per @Krinkle's comments at https://gerrit.wikimedia.org/r/221654, but another use case came up now (T103553) and the (less complete) implementation from https://gerrit.wikimedia.org/r/260328 solves it, so I merged that patch.

Change 260328 merged by jenkins-bot:
mediawiki.util: Use correct encoding for fragment in getUrl

https://gerrit.wikimedia.org/r/260328