Repository navigation
Conversation
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
force-pushed
the
sili/fix-61-wiki-link-detection
branch
from
November 25, 2026 11:52
e37a849 to
f355620
Compare
Author
|
@sourcery-ai review |
Author
|
Friendly ping for an automated review pass on this PR. Summary of the change: Changes:
|
lovelyCARDINAL
left a comment
Member
There was a problem hiding this comment.
总体结论
建议通过(Approve,附少量非阻塞改进建议)
本次 PR 针对 Issue #61 中“快速编辑段落 / 快速差异”按钮在部分链接下不生效的问题,给出了清晰且克制的修复方案:
- 将 URL 判定、动作解析与标题提取逻辑从
WikiTitleService中抽离为纯函数模块packages/core/src/services/wikiLinkUrl.ts,脱离InPageEdit运行时依赖,符合本仓库(@inpageedit/core)模块化、易测试的设计风格。 - 完整覆盖了自定义入口脚本(如
i.php)的差异/编辑链接,以及 VisualEditor / 2017 wikitext 编辑器的veaction=edit/veaction=editsource链接。 - 配套的
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 行) - 说明:
config.articleBaseUrl.replace(/^https?:\/\/[^/]+/, '')在isWikiScriptUrl和extractWikiLinkTitleText中出现了两次,可以抽成小辅助函数(或统一通过new URL(config.articleBaseUrl).pathname解析,与上方的new URL(config.baseUrl)保持一致)。- 原先
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
lovelyCARDINAL
approved these changes
Nov 25, 2026
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
…r the script path
…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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
Closes #61
部分 MediaWiki 链接没有出现快速差异或快速编辑按钮,包括使用
/w/i.php的差异和段落编辑链接,以及使用veaction=editsource的段落编辑链接。改动
general.script,用该字段生成 index 入口 URL。按 origin 和路径精确匹配入口,因此译维的/w/i.php可以直接识别。server不同时,使用server归一化站内识别,保留解析结果中的原始域名、路径和查询参数。veaction=editsource识别为源码编辑。显式action优先,veaction=edit保持 VisualEditor 行为。验证
general.script=/w/i.php且访问域名与server不同,33 条差异链接全部挂载按钮,点击后成功加载真实差异。另验证api.php、load.php和站外链接不会被当成站内入口,以及两种veaction的分类。WikiPage与设置界面的已有错误。