Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/specs/2026-11-25-wiki-link-detection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# MediaWiki 链接识别

## 目标

让快速差异和快速编辑识别 MediaWiki 实际生成的差异链接和源码编辑链接。

## 决定

- 使用已有 siteinfo 中的 `general.script` 生成 index 入口 URL,并精确匹配入口的 origin 和路径。访问 translatewiki 时,项目使用它配置的 `/w/i.php`。其他脚本的 URL 生成沿用 `scriptpath`。
- 用当前访问域名解析页面链接。若它与 siteinfo 的 `server` 不同,识别链接时将当前域名映射到 `server`。解析结果保留页面链接的域名、路径和参数。
- `veaction=editsource` 触发快速编辑。`veaction=edit` 继续交给 VisualEditor。显式 `action` 优先。

## 验收

- 标准 index 入口、改名的 index 入口、短链接和站点首页均能解析。匹配入口时忽略查询参数和锚点。
- 对采用 `/wiki/$1` 条目路径的站点,`api.php`、`load.php` 和其他站外链接均不会挂载快速差异按钮。
- 访问域名和 `server` 不一致时,页面差异链接能挂载快速差异按钮。
7 changes: 6 additions & 1 deletion packages/core/src/services/WikiMetadataService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,13 @@ export class WikiMetadataService extends Service {
}

// utils
/** Use the configured index path from siteinfo, which can differ from `<scriptpath>/index.php`. */
getSciprtUrl(name = 'index') {
return `${this.scriptBaseUrl}/${name.replace(/\.php$/, '')}.php`
const entry = name.replace(/\.php$/, '')
if (entry === 'index') {
return `${this.baseUrl}${this.general.script}`
}
return `${this.scriptBaseUrl}/${entry}.php`
}

/** Get mainpage URL */
Expand Down
69 changes: 34 additions & 35 deletions packages/core/src/services/WikiTitleService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Inject, InPageEdit, Service } from '@/InPageEdit'
import { IWikiPage } from '@/models/WikiPage/index.js'
import { createWikiTitleModel, IWikiTitle, WikiTitleConstructor } from '@/models/WikiTitle/index.js'
import {
extractWikiLinkTitleText,
isWikiLinkUrl,
resolveWikiLinkAction,
toWikiServerOrigin,
type WikiLinkUrlConfig,
} from './wikiLinkUrl.js'

declare module '@/InPageEdit' {
interface InPageEdit {
Expand Down Expand Up @@ -162,31 +169,32 @@ export class WikiTitleService extends Service {
* @example "https://example.com"
*/
private readonly wikiBaseUrl = this.wiki.baseUrl
/**
* Article path, with trailing slash
* @example "/wiki/" (if wgArticlePath is "/wiki/$1")
*/
private readonly wikiArticlePath = this.wiki.articlePath.replace('$1', '')
/**
* Article base URL, with trailing slash
* @example "https://example.com/wiki/" (if wgArticlePath is "/wiki/$1")
*/
private readonly wikiArticleBaseUrl = this.wiki.articleBaseUrl.replace('$1', '')
/**
* Script base URL, **without** trailing slash
* @example "https://example.com/w" (if wgScriptPath is "/w")
*/
private readonly wikiIndexPhpUrl = this.wiki.getSciprtUrl('index.php')
private readonly wikiIndexUrl = this.wiki.getSciprtUrl('index')

private get wikiLinkUrlConfig(): WikiLinkUrlConfig {
return {
baseUrl: this.wikiBaseUrl,
articleBaseUrl: this.wikiArticleBaseUrl,
indexUrl: this.wikiIndexUrl,
currentOrigin: globalThis.location?.origin,
landingPageUrl: this.ctx.wiki.landingPageUrl,
}
}

isWikiLink(url: string): boolean {
return (
url.startsWith(this.wikiArticleBaseUrl) ||
url.startsWith(this.wikiIndexPhpUrl) ||
// Some servers allow index.php to be omitted
url.startsWith(`${this.wikiBaseUrl}/?`) ||
// It's the landing page
url === this.ctx.wiki.landingPageUrl
)
isWikiLink(link: string | URL): boolean {
let url: URL
try {
url = makeURL(link)
} catch {
return false
}
const config = this.wikiLinkUrlConfig
return isWikiLinkUrl(toWikiServerOrigin(url, config), config)
}

static readonly REG_SKIPPED_HREF = /^(#|javascript:|vbscript:|file:)/i
Expand Down Expand Up @@ -214,28 +222,19 @@ export class WikiTitleService extends Service {
return null
}

if (!this.isWikiLink(url.toString())) {
if (!this.isWikiLink(url)) {
return null
}
const config = this.wikiLinkUrlConfig
// For the checks below only; the returned `url` stays as the page has it
const wikiUrl = toWikiServerOrigin(url, config)
const params = url.searchParams
const hash = url.hash.replace('#', '')
const action = params.get('action') || 'view'
const action = resolveWikiLinkAction(params)
const titleParam = params.get('title') || ''
const curid = parseInt(params.get('curid') || '0', 10)
const oldid = parseInt(params.get('oldid') || '0', 10)
let titleText =
titleParam ||
(() => {
try {
return decodeURIComponent(url.pathname.substring(this.wikiArticlePath.length))
} catch (e) {
this.logger.error('parseLink', url, e)
return ''
}
})()
if (titleText.endsWith('index.php')) {
titleText = ''
}
const titleText = extractWikiLinkTitleText(wikiUrl, config, titleParam)

let title: IWikiTitle | undefined = undefined
let pageId: number | undefined = undefined
Expand All @@ -249,7 +248,7 @@ export class WikiTitleService extends Service {
title = this.newTitle(titleText)
} else {
// 无任何有效参数
if (url.origin + url.pathname === this.ctx.wiki.landingPageUrl) {
if (wikiUrl.origin + wikiUrl.pathname === this.ctx.wiki.landingPageUrl) {
// 但 url 是 landingPageUrl,则认为是主页
title = this.newMainPageTitle()
} else {
Expand Down
165 changes: 165 additions & 0 deletions packages/core/src/services/wikiLinkUrl.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { describe, it, expect } from 'vitest'
import {
extractWikiLinkTitleText,
isWikiLinkUrl,
resolveWikiLinkAction,
type WikiLinkUrlConfig,
toWikiServerOrigin,
} from './wikiLinkUrl.js'

/** 大多数 MediaWiki 站点的常见配置 */
function makeConfig(overrides: Partial<WikiLinkUrlConfig> = {}): WikiLinkUrlConfig {
return {
baseUrl: 'https://example.com',
articleBaseUrl: 'https://example.com/wiki/',
indexUrl: 'https://example.com/w/index.php',
landingPageUrl: 'https://example.com/',
...overrides,
}
}

describe('isWikiLinkUrl', () => {
const config = makeConfig()

it('识别标准的 /wiki/ 链接', () => {
expect(isWikiLinkUrl(new URL('https://example.com/wiki/Foo_Bar'), config)).toBe(true)
})

it('识别 index.php 链接', () => {
expect(isWikiLinkUrl(new URL('https://example.com/w/index.php?title=Foo'), config)).toBe(true)
})

it('按 siteinfo 的 script 识别改过名的 index 入口(issue #61)', () => {
// translatewiki.net 的 $wgScript 是 /w/i.php
const twn = makeConfig({ indexUrl: 'https://example.com/w/i.php' })
expect(
isWikiLinkUrl(new URL('https://example.com/w/i.php?title=Foo&curid=1&diff=2&oldid=3'), twn)
).toBe(true)
})

it('不把 api.php 等其他入口当成 wiki 链接', () => {
expect(isWikiLinkUrl(new URL('https://example.com/w/api.php?action=query'), config)).toBe(false)
expect(isWikiLinkUrl(new URL('https://example.com/w/load.php?modules=site'), config)).toBe(
false
)
})

it('识别省略 index.php 的链接', () => {
expect(isWikiLinkUrl(new URL('https://example.com/?title=Foo'), config)).toBe(true)
})

it('识别首页', () => {
expect(isWikiLinkUrl(new URL('https://example.com/'), config)).toBe(true)
})

it('不把站外链接当成 wiki 链接', () => {
expect(isWikiLinkUrl(new URL('https://other.example.org/wiki/Foo'), config)).toBe(false)
expect(isWikiLinkUrl(new URL('https://other.example.org/w/index.php?title=Foo'), config)).toBe(
false
)
expect(isWikiLinkUrl(new URL('https://other.example.org/w/i.php?title=Foo'), config)).toBe(
false
)
})

it('把 article path 下以 .php 结尾的条目当成条目', () => {
expect(isWikiLinkUrl(new URL('https://example.com/wiki/Foo.php'), config)).toBe(true)
expect(extractWikiLinkTitleText(new URL('https://example.com/wiki/Foo.php'), config, '')).toBe(
'Foo.php'
)
})
})

describe('resolveWikiLinkAction', () => {
it('优先使用 action 参数', () => {
expect(resolveWikiLinkAction(new URLSearchParams('action=edit'))).toBe('edit')
expect(resolveWikiLinkAction(new URLSearchParams('action=history'))).toBe('history')
})

it('把 veaction=editsource 视作编辑动作(issue #61)', () => {
expect(resolveWikiLinkAction(new URLSearchParams('veaction=editsource'))).toBe('edit')
expect(
resolveWikiLinkAction(new URLSearchParams('title=Foo&section=2&veaction=editsource'))
).toBe('edit')
})

it('不把 veaction=edit 当成编辑动作', () => {
// VE 的「编辑」按钮与「编辑源代码」按钮会在同一段落并排出现,
// 若把 veaction=edit 也算成编辑,同一个段落会挂两个快速编辑按钮
expect(resolveWikiLinkAction(new URLSearchParams('veaction=edit'))).toBe('view')
expect(resolveWikiLinkAction(new URLSearchParams('title=Foo&section=2&veaction=edit'))).toBe(
'view'
)
})

it('没有动作参数时默认为 view', () => {
expect(resolveWikiLinkAction(new URLSearchParams(''))).toBe('view')
expect(resolveWikiLinkAction(new URLSearchParams('section=2'))).toBe('view')
})

it('不把无法识别的 veaction 当成编辑', () => {
expect(resolveWikiLinkAction(new URLSearchParams('veaction=visualdiff'))).toBe('view')
})
})

describe('extractWikiLinkTitleText', () => {
const config = makeConfig()

it('优先使用 title 参数', () => {
const url = new URL('https://example.com/w/i.php?title=Foo_Bar')
expect(extractWikiLinkTitleText(url, config, 'Foo_Bar')).toBe('Foo_Bar')
})

it('从 article path 中取标题', () => {
const url = new URL('https://example.com/wiki/Foo_Bar')
expect(extractWikiLinkTitleText(url, config, '')).toBe('Foo_Bar')
})

it('index 入口不会把文件名当成标题', () => {
const twn = makeConfig({ indexUrl: 'https://example.com/w/i.php' })
expect(
extractWikiLinkTitleText(new URL('https://example.com/w/i.php?curid=123'), twn, '')
).toBe('')
const indexPath = new URL('https://example.com/w/index.php?curid=123')
expect(extractWikiLinkTitleText(indexPath, config, '')).toBe('')
})

it('能解码百分号转义的标题', () => {
const url = new URL('https://example.com/wiki/Foo%20Bar')
expect(extractWikiLinkTitleText(url, config, '')).toBe('Foo Bar')
})

it('article path 位于 script path 下时,index.php 不会被当成标题', () => {
const nested = makeConfig({ articleBaseUrl: 'https://example.com/w/' })
const url = new URL('https://example.com/w/index.php?curid=1&diff=2&oldid=1')
expect(extractWikiLinkTitleText(url, nested, '')).toBe('')
})

it('站内但不在 article path 下的路径返回空标题', () => {
const url = new URL('https://example.com/special/thing')
expect(extractWikiLinkTitleText(url, config, '')).toBe('')
})
})

describe('toWikiServerOrigin', () => {
const config = makeConfig({ currentOrigin: 'https://m.example.com' })

it('把当前访问域名上的链接换到 server 的域名', () => {
const url = new URL('https://m.example.com/w/index.php?title=Foo&diff=2&oldid=1#bar')
const moved = toWikiServerOrigin(url, config)
expect(moved.href).toBe('https://example.com/w/index.php?title=Foo&diff=2&oldid=1#bar')
expect(isWikiLinkUrl(moved, config)).toBe(true)
})

it('不动真正的站外链接', () => {
const url = new URL('https://other.example.org/wiki/Foo')
expect(toWikiServerOrigin(url, config)).toBe(url)
expect(isWikiLinkUrl(toWikiServerOrigin(url, config), config)).toBe(false)
})

it('当前域名与 server 相同或未知时原样返回', () => {
const url = new URL('https://example.com/wiki/Foo')
expect(toWikiServerOrigin(url, makeConfig({ currentOrigin: 'https://example.com' }))).toBe(url)
expect(toWikiServerOrigin(url, makeConfig())).toBe(url)
})
})
Loading