|
| 1 | +import { makeApiRequest, fetchPageHtml } from './utils.js'; |
| 2 | +import { getAllWikis, updateWikiConfig } from './config.js'; |
| 3 | +import { WikiDiscoveryError } from './errors.js'; |
| 4 | + |
| 5 | +const COMMON_SCRIPT_PATHS = [ '/w', '' ]; |
| 6 | + |
| 7 | +// TODO: Move these types to a dedicated file if we end up using Action API types elsewhere |
| 8 | +interface MediaWikiActionApiSiteInfoGeneral { |
| 9 | + sitename: string; |
| 10 | + articlepath: string; |
| 11 | + scriptpath: string; |
| 12 | + server: string; |
| 13 | + servername: string; |
| 14 | + // Omitted other fields for now since we don't use them |
| 15 | +} |
| 16 | + |
| 17 | +interface MediaWikiActionApiSiteInfoQuery { |
| 18 | + general: MediaWikiActionApiSiteInfoGeneral; |
| 19 | +} |
| 20 | + |
| 21 | +interface MediaWikiActionApiResponse { |
| 22 | + query?: MediaWikiActionApiSiteInfoQuery; |
| 23 | +} |
| 24 | + |
| 25 | +export interface WikiInfo { |
| 26 | + sitename: string; |
| 27 | + articlepath: string; |
| 28 | + scriptpath: string; |
| 29 | + server: string; |
| 30 | + servername: string; |
| 31 | +} |
| 32 | + |
| 33 | +export async function resolveWiki( wikiUrl: string ): Promise<string> { |
| 34 | + const url = new URL( wikiUrl ); |
| 35 | + const allWikis = getAllWikis(); |
| 36 | + |
| 37 | + if ( allWikis[ url.hostname ] ) { |
| 38 | + return url.hostname; |
| 39 | + } |
| 40 | + |
| 41 | + const wikiServer = parseWikiUrl( wikiUrl ); |
| 42 | + const wikiInfo = await getWikiInfo( wikiServer, wikiUrl ); |
| 43 | + |
| 44 | + if ( wikiInfo !== null ) { |
| 45 | + updateWikiConfig( wikiInfo.servername, { |
| 46 | + sitename: wikiInfo.sitename, |
| 47 | + server: wikiInfo.server, |
| 48 | + articlepath: wikiInfo.articlepath, |
| 49 | + scriptpath: wikiInfo.scriptpath |
| 50 | + } ); |
| 51 | + return wikiInfo.servername; |
| 52 | + } else { |
| 53 | + throw new WikiDiscoveryError( 'Failed to determine wiki info. Please ensure the URL is correct and the wiki is accessible.' ); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +export function parseWikiUrl( wikiUrl: string ): string { |
| 58 | + const url = new URL( wikiUrl ); |
| 59 | + return `${ url.protocol }//${ url.host }`; |
| 60 | +} |
| 61 | + |
| 62 | +export async function getWikiInfo( |
| 63 | + wikiServer: string, originalWikiUrl: string |
| 64 | +): Promise<WikiInfo | null> { |
| 65 | + return ( await fetchUsingCommonScriptPaths( wikiServer ) ) ?? |
| 66 | + ( await fetchUsingScriptPathsFromHtml( wikiServer, originalWikiUrl ) ); |
| 67 | +} |
| 68 | + |
| 69 | +async function fetchWikiInfoFromApi( |
| 70 | + wikiServer: string, scriptPath: string |
| 71 | +): Promise<WikiInfo | null> { |
| 72 | + const baseUrl = `${ wikiServer }${ scriptPath }/api.php`; |
| 73 | + const params = { |
| 74 | + action: 'query', |
| 75 | + meta: 'siteinfo', |
| 76 | + siprop: 'general', |
| 77 | + format: 'json', |
| 78 | + origin: '*' |
| 79 | + }; |
| 80 | + |
| 81 | + let data: MediaWikiActionApiResponse | null = null; |
| 82 | + try { |
| 83 | + data = await makeApiRequest<MediaWikiActionApiResponse>( baseUrl, params ); |
| 84 | + } catch ( error ) { |
| 85 | + console.error( `Error fetching wiki info from ${ baseUrl }:`, error ); |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + if ( data === null || data.query?.general === undefined ) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + const general = data.query.general; |
| 94 | + |
| 95 | + // We don't need to check for every field, the API should be returning the correct values. |
| 96 | + if ( typeof general.scriptpath !== 'string' ) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + return { |
| 101 | + sitename: general.sitename, |
| 102 | + scriptpath: general.scriptpath, |
| 103 | + articlepath: general.articlepath.replace( '/$1', '' ), |
| 104 | + server: general.server, |
| 105 | + servername: general.servername |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +async function fetchUsingCommonScriptPaths( |
| 110 | + wikiServer: string |
| 111 | +): Promise<WikiInfo | null> { |
| 112 | + for ( const candidatePath of COMMON_SCRIPT_PATHS ) { |
| 113 | + const apiResult = await fetchWikiInfoFromApi( wikiServer, candidatePath ); |
| 114 | + if ( apiResult ) { |
| 115 | + return apiResult; |
| 116 | + } |
| 117 | + } |
| 118 | + return null; |
| 119 | +} |
| 120 | + |
| 121 | +async function fetchUsingScriptPathsFromHtml( |
| 122 | + wikiServer: string, |
| 123 | + originalWikiUrl: string |
| 124 | +): Promise<WikiInfo | null> { |
| 125 | + const htmlContent = await fetchPageHtml( originalWikiUrl ); |
| 126 | + const htmlScriptPathCandidates = extractScriptPathsFromHtml( htmlContent, wikiServer ); |
| 127 | + const pathsToTry = htmlScriptPathCandidates.length > 0 ? |
| 128 | + htmlScriptPathCandidates : COMMON_SCRIPT_PATHS; |
| 129 | + |
| 130 | + for ( const candidatePath of pathsToTry ) { |
| 131 | + const apiResult = await fetchWikiInfoFromApi( wikiServer, candidatePath ); |
| 132 | + if ( apiResult ) { |
| 133 | + return apiResult; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return null; |
| 138 | +} |
| 139 | + |
| 140 | +function extractScriptPathsFromHtml( htmlContent: string | null, wikiServer: string ): string[] { |
| 141 | + const candidatesFromHtml: string[] = []; |
| 142 | + if ( htmlContent ) { |
| 143 | + const fromSearchForm = extractScriptPathFromSearchForm( htmlContent, wikiServer ); |
| 144 | + if ( fromSearchForm !== null ) { |
| 145 | + candidatesFromHtml.push( fromSearchForm ); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + const uniqueCandidatesFromHtml = [ ...new Set( candidatesFromHtml ) ]; |
| 150 | + return uniqueCandidatesFromHtml.filter( ( p ) => typeof p === 'string' && ( p === '' || p.trim() !== '' ) ); |
| 151 | +} |
| 152 | + |
| 153 | +function extractScriptPathFromSearchForm( htmlContent: string, wikiServer: string ): string | null { |
| 154 | + const searchFormMatch = htmlContent.match( /<form[^>]+id=['"]searchform['"][^>]+action=['"]([^'"]*index\.php[^'"]*)['"]/i ); |
| 155 | + if ( searchFormMatch && searchFormMatch[ 1 ] ) { |
| 156 | + const actionAttribute = searchFormMatch[ 1 ]; |
| 157 | + try { |
| 158 | + const fullActionUrl = new URL( actionAttribute, wikiServer ); |
| 159 | + const path = fullActionUrl.pathname; |
| 160 | + const indexPathIndex = path.toLowerCase().lastIndexOf( '/index.php' ); |
| 161 | + if ( indexPathIndex !== -1 ) { |
| 162 | + return path.slice( 0, indexPathIndex ); |
| 163 | + } |
| 164 | + } catch ( error ) { |
| 165 | + console.error( `Error extracting script path from search form: ${ error }` ); |
| 166 | + } |
| 167 | + } |
| 168 | + return null; |
| 169 | +} |
0 commit comments