File: /home/nepi/q.nepi.ir/word.php
<?php
function cleanLabel($label) {
return trim(str_replace(':', '', $label));
}
function extractMeaningsFromHtml($htmlContent) {
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML('<?xml encoding="utf-8" ?>' . $htmlContent);
libxml_clear_errors();
$xpath = new DOMXPath($dom);
$acceptedLabels = [
'مترادف', 'مترادفها',
'متضاد',
'معنی', 'معنی انگلیسی',
'برابر پارسی',
];
$acceptedSources = [
'لغت نامه دهخدا',
'فرهنگ معین',
'فرهنگ عمید',
'فرهنگ فارسی',
'دانشنامه آزاد فارسی',
'ویکی فقه'
];
$results = [];
// بخش اول: پردازش <b> ها
$boldTags = $xpath->query('//b');
foreach ($boldTags as $b) {
$label = trim($b->textContent);
$cleanedLabel = cleanLabel($label);
$match = false;
foreach ($acceptedLabels as $accepted) {
if (strpos($label, $accepted) !== false) {
$match = true;
break;
}
}
if (!$match) continue;
$meaning = '';
$current = $b->nextSibling;
while ($current) {
if ($current->nodeType === XML_TEXT_NODE) {
$meaning .= $current->textContent;
} elseif ($current->nodeType === XML_ELEMENT_NODE && strtolower($current->nodeName) === 'br') {
break;
} elseif ($current->nodeType === XML_ELEMENT_NODE) {
$meaning .= ' ' . $current->textContent;
}
$current = $current->nextSibling;
}
$meaning = trim($meaning);
if (substr($meaning, 0, 1) === ':') {
$meaning = trim(substr($meaning, 1));
}
if (!empty($meaning)) {
$results[] = [
'label' => $cleanedLabel,
'meaning' => $meaning
];
}
}
// بخش دوم: پردازش <div class="lun">
$lunDivs = $xpath->query('//div[contains(@class, "lun")]');
foreach ($lunDivs as $lun) {
if ($lun->hasAttribute('t')) {
$source = trim($lun->getAttribute('t'));
if (in_array($source, $acceptedSources)) {
$article = $xpath->query('.//article', $lun);
if ($article->length > 0) {
$meaning = trim($article[0]->textContent);
if (!empty($meaning)) {
$results[] = [
'label' => $source,
'meaning' => $meaning
];
}
}
}
}
}
return $results;
}
function initDatabase() {
$db = new SQLite3('words.db');
$db->exec("
CREATE TABLE IF NOT EXISTS words (
id INTEGER PRIMARY KEY AUTOINCREMENT,
word TEXT UNIQUE,
json_data TEXT,
search_count INTEGER DEFAULT 1,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
");
return $db;
}
function getWordFromDb($db, $word) {
$stmt = $db->prepare("SELECT json_data FROM words WHERE word = :word");
$stmt->bindValue(':word', $word, SQLITE3_TEXT);
$result = $stmt->execute();
$row = $result->fetchArray(SQLITE3_ASSOC);
if ($row) {
// افزایش شمارش جستجو
$db->exec("UPDATE words SET search_count = search_count + 1 WHERE word = '" . SQLite3::escapeString($word) . "'");
return $row['json_data'];
}
return null;
}
function saveWordToDb($db, $word, $jsonData) {
$stmt = $db->prepare("
INSERT INTO words (word, json_data, search_count)
VALUES (:word, :json_data, 1)
ON CONFLICT(word) DO UPDATE SET
json_data = excluded.json_data,
search_count = words.search_count + 1
");
$stmt->bindValue(':word', $word, SQLITE3_TEXT);
$stmt->bindValue(':json_data', $jsonData, SQLITE3_TEXT);
$stmt->execute();
}
// ======= API Response =======
header('Content-Type: application/json; charset=utf-8');
if (!isset($_GET['word']) || empty($_GET['word'])) {
echo json_encode([
'success' => false,
'message' => 'پارامتر "word" ارسال نشده است.',
'data' => []
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
exit;
}
$word = $_GET['word'];
$encodedWord = urlencode($word);
$db = initDatabase();
$cached = getWordFromDb($db, $word);
if ($cached) {
echo $cached;
exit;
}
// اگر در دیتابیس نبود، از وب بگیر
$url = "https://abadis.ir/fatofa/$encodedWord/";
$htmlContent = @file_get_contents($url);
if ($htmlContent === false) {
echo json_encode([
'success' => false,
'message' => 'دریافت داده از سایت ابادیس با خطا مواجه شد.',
'data' => []
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
exit;
}
$data = extractMeaningsFromHtml($htmlContent);
if (empty($data)) {
$output = json_encode([
'success' => false,
'message' => "هیچ معنی مرتبط برای \"$word\" پیدا نشد.",
'data' => []
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
echo $output;
} else {
$output = json_encode([
'success' => true,
'word' => $word,
'result_count' => count($data),
'data' => $data
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
saveWordToDb($db, $word, $output);
echo $output;
}
?>