// Kaynakça araması: OpenAlex, Crossref, arXiv ve OSF Preprints sonuçlarını BibTeX kaydına dönüştürür. // Sonuçlar hem "Kaynak ara" panelinde hem de asistanın kaynak arama aracında kullanılır. import { Router } from 'express'; import { HttpError, requireString } from './util.js'; const HEADERS = { 'User-Agent': 'Thoth-LaTeX/0.2' }; const TR_FOLD = { ç: 'c', ğ: 'g', ı: 'i', ö: 'o', ş: 's', ü: 'u' }; const asciiFold = (s) => s.toLowerCase().replace(/[çğıöşü]/g, (c) => TR_FOLD[c]).normalize('NFD').replace(/[^a-z0-9]/g, ''); const STOP_WORDS = new Set(['a', 'an', 'the', 'on', 'of', 'in', 'for', 'and', 'to', 'bir', 've', 'ile', 'icin', 'uzerine']); export function citeKey(authors, year, title) { const lastName = asciiFold((authors[0] ?? 'anonim').split(',')[0].trim().split(/\s+/).pop() ?? ''); const word = title.split(/\s+/).map(asciiFold).find((w) => w.length > 2 && !STOP_WORDS.has(w)) ?? ''; return `${lastName || 'anonim'}${year ?? ''}${word}`; } const escapeTex = (s) => String(s).replace(/([&%#_$])/g, '\\$1'); export function toBibtex(type, key, fields) { const lines = Object.entries(fields) .filter(([, v]) => v != null && v !== '') .map(([k, v]) => ` ${k} = {${k === 'url' || k === 'doi' || k === 'eprint' ? v : escapeTex(v)}}`); return `@${type}{${key},\n${lines.join(',\n')}\n}`; } const stripTags = (s) => s.replace(/<[^>]+>/g, '').replace(/\s+/g, ' ').trim(); const CROSSREF_TYPES = { 'journal-article': 'article', 'proceedings-article': 'inproceedings', book: 'book', monograph: 'book', 'book-chapter': 'incollection', dissertation: 'phdthesis', }; async function searchCrossref(query) { const url = new URL('https://api.crossref.org/works'); url.searchParams.set('query.bibliographic', query); url.searchParams.set('rows', '12'); url.searchParams.set('select', 'DOI,title,author,issued,container-title,type,volume,issue,page,publisher,URL'); const res = await fetch(url, { headers: HEADERS, signal: AbortSignal.timeout(15_000) }); if (!res.ok) throw new Error(`Crossref HTTP ${res.status}`); const { message } = await res.json(); return message.items.filter((it) => it.title?.[0]).map((it) => { const title = stripTags(it.title[0]); const authors = (it.author ?? []).map((a) => (a.family ? `${a.family}, ${a.given ?? ''}`.replace(/, $/, '') : a.name)).filter(Boolean); const year = it.issued?.['date-parts']?.[0]?.[0] ?? null; const venue = it['container-title']?.[0] ? stripTags(it['container-title'][0]) : null; const type = CROSSREF_TYPES[it.type] ?? 'misc'; const key = citeKey(authors, year, title); return { id: it.DOI, key, title, authors, year, venue, url: it.URL, bibtex: toBibtex(type, key, { title, author: authors.join(' and '), [type === 'article' ? 'journal' : 'booktitle']: venue, year, volume: it.volume, number: it.issue, pages: it.page?.replace('-', '--'), publisher: type === 'article' ? null : it.publisher, doi: it.DOI, url: it.URL, }), }; }); } const decodeXml = (s) => s.replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, "'").replace(/&/g, '&'); const xmlTag = (xml, tag) => { const m = xml.match(new RegExp(`<${tag}[^>]*>([\\s\\S]*?)`)); return m ? decodeXml(m[1]).replace(/\s+/g, ' ').trim() : null; }; async function searchArxiv(query) { const url = new URL('https://export.arxiv.org/api/query'); // Her kelime ayrı ayrı aranır; aksi hâlde "all:" yalnızca ilk kelimeye uygulanır. url.searchParams.set('search_query', query.split(/\s+/).map((w) => `all:${w.replace(/[":()]/g, '')}`).join(' AND ')); url.searchParams.set('max_results', '12'); url.searchParams.set('sortBy', 'relevance'); const res = await fetch(url, { headers: HEADERS, signal: AbortSignal.timeout(15_000) }); if (!res.ok) throw new Error(`arXiv HTTP ${res.status}`); const xml = await res.text(); return xml.split('').slice(1).map((entry) => { const absUrl = xmlTag(entry, 'id'); const arxivId = absUrl.replace(/^https?:\/\/arxiv\.org\/abs\//, '').replace(/v\d+$/, ''); const title = xmlTag(entry, 'title'); const authors = [...entry.matchAll(/([\s\S]*?)<\/name>/g)].map((m) => decodeXml(m[1]).trim()); const year = Number(xmlTag(entry, 'published')?.slice(0, 4)) || null; const key = citeKey(authors, year, title); return { id: arxivId, key, title, authors, year, venue: `arXiv:${arxivId}`, url: absUrl, bibtex: toBibtex('misc', key, { title, author: authors.join(' and '), year, eprint: arxivId, archivePrefix: 'arXiv', url: absUrl, }), }; }); } /** OpenAlex'in sözcük -> konum dizininden özet metnini yeniden kurar. */ export function abstractFromIndex(index) { if (!index) return null; const words = []; for (const [word, positions] of Object.entries(index)) for (const p of positions) words[p] = word; return words.filter(Boolean).join(' ') || null; } const OPENALEX_TYPES = { article: 'article', review: 'article', editorial: 'article', letter: 'article', 'book-chapter': 'incollection', book: 'book', dissertation: 'phdthesis', preprint: 'misc', report: 'report', }; async function searchOpenAlex(query, { enCokAtif = false } = {}) { const url = new URL('https://api.openalex.org/works'); url.searchParams.set('search', query); url.searchParams.set('per-page', '12'); if (enCokAtif) url.searchParams.set('sort', 'cited_by_count:desc'); url.searchParams.set('select', 'id,doi,title,authorships,publication_year,primary_location,type,biblio,abstract_inverted_index,language'); // İsteğe bağlı kişisel anahtar: OpenAlex anahtarsız istekleri ortak bir günlük kotayla sınırlar. if (process.env.OPENALEX_API_KEY) url.searchParams.set('api_key', process.env.OPENALEX_API_KEY); const res = await fetch(url, { headers: HEADERS, signal: AbortSignal.timeout(20_000) }); if (!res.ok) throw new Error(`OpenAlex HTTP ${res.status}`); const { results } = await res.json(); return results.filter((w) => w.title).map((w) => { const title = stripTags(w.title); // OpenAlex adları "Ad Soyad" biçimindedir; BibTeX için "Soyad, Ad" yapılır. const authors = (w.authorships ?? []).map((a) => { const name = a.author?.display_name ?? ''; const parts = name.trim().split(/\s+/); return parts.length > 1 ? `${parts.pop()}, ${parts.join(' ')}` : name; }).filter(Boolean); const year = w.publication_year ?? null; const venue = w.primary_location?.source?.display_name ?? null; // Türü bilinmeyen ama bir dergide yayımlanmış kayıtlar makale sayılır. const type = OPENALEX_TYPES[w.type] ?? (w.primary_location?.source?.type === 'journal' ? 'article' : 'misc'); const doi = w.doi ? w.doi.replace(/^https?:\/\/doi\.org\//, '') : null; const key = citeKey(authors, year, title); const b = w.biblio ?? {}; const pages = b.first_page ? (b.last_page && b.last_page !== b.first_page ? `${b.first_page}--${b.last_page}` : b.first_page) : null; return { id: w.id.replace('https://openalex.org/', ''), key, title, authors, year, venue, url: doi ? `https://doi.org/${doi}` : w.id, doi, abstract: abstractFromIndex(w.abstract_inverted_index), bibtex: toBibtex(type, key, { title, author: authors.join(' and '), [type === 'article' ? 'journal' : 'booktitle']: type === 'misc' || type === 'book' || type === 'phdthesis' || type === 'report' ? null : venue, year, volume: b.volume, number: b.issue, pages, doi, url: doi ? null : w.id, langid: w.language === 'tr' ? 'turkish' : w.language === 'en' ? 'english' : null, }), }; }); } async function searchOsf(query) { // OSF yalnızca başlıkta bitişik ifade arar: en uzun anlamlı sözcükle aranır, sonuçlar // diğer sözcükleri de başlığında taşıyanlara göre sıralanır. const words = query.toLocaleLowerCase('tr').split(/[^\p{L}\p{N}-]+/u).filter((w) => w.length > 2 && !STOP_WORDS.has(w)); const anchor = [...words].sort((a, b) => b.length - a.length)[0] ?? query; const url = new URL('https://api.osf.io/v2/preprints/'); url.searchParams.set('filter[title]', anchor); url.searchParams.set('page[size]', '50'); url.searchParams.set('embed', 'contributors'); const res = await fetch(url, { headers: HEADERS, signal: AbortSignal.timeout(20_000) }); if (!res.ok) throw new Error(`OSF HTTP ${res.status}`); const { data } = await res.json(); return data.map((p) => { const a = p.attributes; const title = stripTags(a.title ?? ''); const authors = (p.embeds?.contributors?.data ?? []) .filter((c) => c.attributes?.bibliographic !== false) .map((c) => c.embeds?.users?.data?.attributes) .filter(Boolean) .map((u) => (u.family_name ? `${u.family_name}, ${u.given_name ?? ''}`.replace(/, $/, '') : u.full_name)); const year = Number((a.date_published ?? a.date_created ?? '').slice(0, 4)) || null; const provider = p.relationships?.provider?.data?.id ?? 'osf'; const doi = (a.doi ?? p.links?.preprint_doi ?? '').replace(/^https?:\/\/doi\.org\//, '') || null; const key = citeKey(authors, year, title); return { id: `osf:${p.id}`, key, title, authors, year, venue: `${provider} (önbaskı)`, url: p.links?.html ?? `https://osf.io/${p.id}`, doi, abstract: a.description ? stripTags(a.description) : null, bibtex: toBibtex('misc', key, { title, author: authors.join(' and '), year, howpublished: `${provider} preprint`, doi, url: doi ? null : p.links?.html, }), }; }).filter((r) => r.title) .map((r) => ({ r, score: words.filter((w) => r.title.toLocaleLowerCase('tr').includes(w)).length })) .filter(({ score }) => score >= Math.min(2, words.length)) .sort((a, b) => b.score - a.score || (b.r.year ?? 0) - (a.r.year ?? 0)) .slice(0, 12) .map(({ r }) => r); } export const SOURCES = { openalex: { label: 'OpenAlex', search: searchOpenAlex }, crossref: { label: 'Crossref', search: searchCrossref }, arxiv: { label: 'arXiv', search: searchArxiv }, osf: { label: 'OSF Preprints', search: searchOsf }, }; /** Kaynak araması (panel ve asistan için ortak). */ export async function searchLiterature(query, source = 'openalex', options = {}) { const s = SOURCES[source] ?? SOURCES.openalex; return s.search(query, options); } export const citationsRouter = Router(); citationsRouter.get('/citations/search', async (req, res) => { const query = requireString(req.query.q, 'Arama metni', { min: 2, max: 300 }); try { res.json({ results: await searchLiterature(query, req.query.source) }); } catch (err) { throw new HttpError(502, `Kaynak araması başarısız oldu: ${err.message}`); } });