Skip to content

fix(core): recognise all script entry points and veaction in wiki links - #67

Open
sili-sili wants to merge 6 commits into
masterfrom
sili/fix-61-wiki-link-detection
Open

sili-sili wants to merge 6 commits into
masterfrom
sili/fix-61-wiki-link-detection

Conversation

@sili-sili

@sili-sili sili-sili commented Nov 22, 2026 •

Copy link
Copy Markdown

问题

Closes #61

部分 MediaWiki 链接没有出现快速差异或快速编辑按钮,包括使用 /w/i.php 的差异和段落编辑链接,以及使用 veaction=editsource 的段落编辑链接。

改动

  • 从已有的 siteinfo 读取 general.script,用该字段生成 index 入口 URL。按 origin 和路径精确匹配入口,因此译维的 /w/i.php 可以直接识别。
  • 当前访问域名与 siteinfo 的 server 不同时,使用 server 归一化站内识别,保留解析结果中的原始域名、路径和查询参数。
  • 将 veaction=editsource 识别为源码编辑。显式 action 优先,veaction=edit 保持 VisualEditor 行为。

验证

  • 22 个链接识别定向测试通过,浏览器用 UMD 构建通过。
  • 在 MediaWiki.org 的最近更改页面,33 条差异链接全部挂载快速差异按钮。
  • 在同一页面模拟 general.script=/w/i.php 且访问域名与 server 不同,33 条差异链接全部挂载按钮,点击后成功加载真实差异。另验证 api.php、load.php 和站外链接不会被当成站内入口,以及两种 veaction 的分类。
  • translatewiki.net 当次触发了真人验证,改名入口使用上述浏览器模拟验证。
  • 完整类型检查在主分支和本分支均报告 WikiPage 与设置界面的已有错误。

MediaWiki sites may expose entry points other than index.php, e.g.
translatewiki.net serves /w/i.php. isWikiLink() only matched the
hard-coded index.php URL, so quick diff / quick edit buttons never
mounted on such links.

Also treat veaction=editsource / veaction=edit as an edit action, which
is how the 2017 wikitext editor links to section editing.

The link recognition logic is extracted into wikiLinkUrl.ts so it can be
unit tested without booting InPageEdit.

Closes #61
@sili-sili
sili-sili force-pushed the sili/fix-61-wiki-link-detection branch from e37a849 to f355620 Compare November 25, 2026 11:52
@sili-sili

Copy link
Copy Markdown
Author

@sourcery-ai review

@sili-sili

Copy link
Copy Markdown
Author

Friendly ping for an automated review pass on this PR. Summary of the change: isWikiLink() hard-coded the script entry to index.php, so sites that serve the API from a custom entry (e.g. /w/i.php on translatewiki.net) were treated as external links; action also ignored veaction=editsource, which the 2017 wikitext editor uses for section editing.

Changes:

  • new packages/core/src/services/wikiLinkUrl.ts — pure functions, no InPageEdit dependency, so it can be unit tested
  • script entry now matches any .php entry under the script path, with article paths ending in .php excluded
  • veaction=edit / veaction=editsource normalised to edit
  • 18 new unit tests in wikiLinkUrl.spec.ts, all passing; tsc --noEmit clean

@lovelyCARDINAL lovelyCARDINAL left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

总体结论

建议通过(Approve,附少量非阻塞改进建议)

本次 PR 针对 Issue #61 中“快速编辑段落 / 快速差异”按钮在部分链接下不生效的问题,给出了清晰且克制的修复方案:

  1. 将 URL 判定、动作解析与标题提取逻辑从 WikiTitleService 中抽离为纯函数模块 packages/core/src/services/wikiLinkUrl.ts,脱离 InPageEdit 运行时依赖,符合本仓库(@inpageedit/core)模块化、易测试的设计风格。
  2. 完整覆盖了自定义入口脚本(如 i.php)的差异/编辑链接,以及 VisualEditor / 2017 wikitext 编辑器的 veaction=edit / veaction=editsource 链接。
  3. 配套的 wikiLinkUrl.spec.ts 包含 18 个单元测试,覆盖了根目录部署、子目录脚本、.php 结尾条目、veaction 优先级及异常回退等场景。

具体问题与改进建议

1. [低] 根目录部署站点下 .php 结尾条目的边界情况

  • 位置:packages/core/src/services/wikiLinkUrl.ts(第 56-59 行)
  • 说明:
    const articlePath = config.articleBaseUrl.replace(/^https?:\/\/[^/]+/, '')
    if (articlePath !== '/' && url.pathname.startsWith(articlePath)) {
      return false
    }
    当站点将条目路径与脚本路径同时部署在根路径时(即 articleBaseUrl 为 https://example.com/,scriptPath 为 ""),articlePath === '/' 会跳过上述排除逻辑。此时若存在标题以 .php 结尾的普通条目(例如 https://example.com/Foo.php),isWikiScriptUrl 会将其判为 true,进而导致 extractWikiLinkTitleText 在第 111-114 行直接返回空字符串 '',无法正确提取标题。
    (注:该配置在生产站点中较少见,且若不跳过 articlePath === '/' 又会反过来把 /index.php 排除,属于两难取舍;若要彻底区分,通常需要结合常见脚本名或查询参数特征。)

2. [低] 非页面入口脚本的匹配范围偏宽(对应“已知取舍”)

  • 位置:packages/core/src/services/wikiLinkUrl.ts(第 64-65 行)
  • 说明:
    /^[^/]+\.php$/i.test(rest) 会将 scriptPath 下的 api.php、load.php 等系统接口同样判定为 Wiki 链接。正如 PR 描述中已主动指出的那样,目前“快速编辑 / 快速差异”不会受到影响;但需要注意 api.php 本身也会携带 action 参数(如 api.php?action=...),如果后续其他插件或模块复用 parseWikiLink() 来识别页面链接,建议将其收窄为白名单,或至少显式排除 api.php、load.php、rest.php 等已知非页面入口。

3. [建议] PATH_INFO 风格的自定义脚本链接

  • 位置:packages/core/src/services/wikiLinkUrl.ts(第 64-65 行、第 69-77 行)
  • 说明:
    当前对于 https://example.com/w/index.php/Foo_Bar 这类链接,由于 href.startsWith(config.indexPhpUrl) 为 true,isWikiLinkUrl 会将其识别为 Wiki 链接;但如果自定义入口站点(如 i.php)存在 /w/i.php/Foo_Bar 形式的 PATH_INFO 链接,由于 rest 为 i.php/Foo_Bar,不满足 /^[^/]+\.php$/i,isWikiScriptUrl 会返回 false。如无此类 URL 需求可忽略,若有需要可考虑兼容 ^[^/]+\.php(?:$|/)。

4. [细节] 重复路径解析与日志保留

  • 位置:packages/core/src/services/wikiLinkUrl.ts(第 56 行、第 115 行)与 packages/core/src/services/WikiTitleService.ts(第 236 行)
  • 说明:
    1. config.articleBaseUrl.replace(/^https?:\/\/[^/]+/, '') 在 isWikiScriptUrl 和 extractWikiLinkTitleText 中出现了两次,可以抽成小辅助函数(或统一通过 new URL(config.articleBaseUrl).pathname 解析,与上方的 new URL(config.baseUrl) 保持一致)。
    2. 原先 WikiTitleService 在 decodeURIComponent 失败时会调用 this.logger.error('parseLink', url, e),抽离后在 extractWikiLinkTitleText 中改为静默返回 ''。这通常无伤大雅,如需保留排查日志可在调用方补充。

验证建议

  • PR 中已完成 wikiLinkUrl.spec.ts(18 passed)及修改文件的 tsc --noEmit。
  • 由于本次尚未在真实浏览器环境中验证按钮挂载,建议在合入前后找一个带有 veaction=editsource(2017 wikitext 编辑器)及自定义入口脚本链接的页面做一次冒烟测试,确认按钮在 DOM 过滤与点击打开编辑器时均表现正常。

Powered by gemini-4-hydrogen

sili-sili and others added 5 commits November 25, 2026 12:33
Wikis with VisualEditor render both an `edit` (veaction=edit) and an
`edit source` (action=edit) link per section. Quick-edit buttons are
mounted per anchor, so normalising veaction=edit to `edit` made the same
section get two buttons. Only veaction=editsource edits wikitext, so
only that one is resolved to `edit` now.

Refs #61
…ers from the wiki server

Pages served from a mirror, mobile domain or proxy resolve relative links
against that host, not siteinfo server, so every link counted as external.
The API comes from the page's own EditURI, so the current host is the same
wiki; move such links onto the server origin before checking them. The
returned url is left as the page has it.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Deploying inpageedit-next with  Cloudflare Pages  Cloudflare Pages

Latest commit: c8692aa
Status:🚫  Build failed.

View logs

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] 添加快速编辑段落/快速差异按钮对部分形式的链接无反应

3 participants