lwb5-in-2025/scripts/game/main/manual/library/updateSections.inc

108 lines
3.6 KiB
PHP
Raw Normal View History

2016-01-10 11:01:49 +01:00
<?php
class main_manual_updateSections {
public function __construct($lib) {
2016-01-10 11:01:49 +01:00
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($sections) {
$versions = array();
foreach ($sections as $sName => $sData) {
if (is_null($versions[$sData['version']])) {
2024-12-31 11:40:11 +01:00
$versions[$sData['version']] = "'" . pg_escape_string($sData['version']) . "'";
2016-01-10 11:01:49 +01:00
}
2024-12-31 11:40:11 +01:00
$this->db->query("DELETE FROM man_section WHERE name='" . pg_escape_string($sName)
2016-01-10 11:01:49 +01:00
. "' AND version=" . $versions[$sData['version']]
2024-12-31 11:40:11 +01:00
. " AND lang='" . pg_escape_string($sData['language']) . "'");
2016-01-10 11:01:49 +01:00
}
// Insert the data itself
$now = time();
foreach ($sections as $sName => $sData) {
if (is_null($versions[$sData['version']])) {
continue;
}
$this->db->query("INSERT INTO man_section (version,lang,name,last_update,is_page,in_menu,title,contents) VALUES("
2024-12-31 11:40:11 +01:00
. $versions[$sData['version']] . ",'" . pg_escape_string($sData['language']) . "','"
. pg_escape_string($sName) . "',$now," . dbBool($sData['is_page']) . ","
. dbBool($sData['in_menu']) . ",'" . pg_escape_string($sData['title'])
. "','" . pg_escape_string($sData['contents']) . "')");
$q = $this->db->query("SELECT id FROM man_section WHERE name='" . pg_escape_string($sName)
2016-01-10 11:01:49 +01:00
. "' AND version=" . $versions[$sData['version']]
2024-12-31 11:40:11 +01:00
. " AND lang='" . pg_escape_string($sData['language']) . "'");
2016-01-10 11:01:49 +01:00
list($sections[$sName]['dbid']) = dbFetchArray($q);
}
// Create links between data
foreach ($sections as $sName => $sData) {
if (count($sData['subs'])) {
// In / After section
foreach ($sData['subs'] as $i => $sn) {
$ri = $i - 1;
while ($ri >= 0) {
$sn2 = $sections[$sData['subs'][$ri]];
if ($sn2['is_page'] == $sections[$sn]['is_page']) {
$previous = $sn2['dbid'];
break;
}
$ri --;
}
if ($ri == -1) {
$previous = $sData['dbid'];
}
$this->db->query("UPDATE man_section SET in_section={$sData['dbid']},after_section=$previous WHERE id={$sections[$sn]['dbid']}");
}
}
if ($sData['linkto'] != '') {
if (is_array($sections[$sData['linkto']])) {
$this->db->query("UPDATE man_section SET link_to={$sections[$sData['linkto']]['dbid']} WHERE id={$sData['dbid']}");
} else {
2024-12-31 11:40:11 +01:00
$q = $this->db->query("SELECT id FROM man_section WHERE name='" . pg_escape_string($sData['linkto']) . "' AND version="
. $versions[$sData['version']] . " AND lang='" . pg_escape_string($sData['language']) . "'");
2016-01-10 11:01:49 +01:00
if ($q && dbCount($q) == 1) {
list($toid) = dbFetchArray($q);
$this->db->query("UPDATE man_section SET link_to=$toid WHERE id={$sData['dbid']}");
}
}
}
}
// Update indices
$banwords = array();
foreach ($sections as $sName => $sData) {
if (!is_array($banwords[$sData['language']])) {
$ban = array();
2024-12-31 11:40:11 +01:00
$q = $this->db->query("SELECT word FROM man_index_ban WHERE lang='" . pg_escape_string($sData['language']) . "'");
2016-01-10 11:01:49 +01:00
while ($r = dbFetchArray($q)) {
array_push($ban, $r[0]);
}
$banwords[$sData['lang']] = $ban;
}
$text = preg_replace('/<[^>]+>/', '', $sData['title']) . " ";
$text .= preg_replace('/<[^>]+>/', '', $sData['contents']);
$text = preg_replace('/["\s\.,;:!\(\)<>&]+/', ' ', $text);
$tl = explode(' ', $text);
$rtl = array();
foreach ($tl as $word) {
$word = strtolower($word);
if ($word != '' && !in_array($word, $banwords[$sData['lang']])) {
$rtl[$word] ++;
}
}
foreach ($rtl as $word => $count) {
2024-12-31 11:40:11 +01:00
$this->db->query("INSERT INTO man_index(word,wcount,lang,section) VALUES ('" . pg_escape_string($word)
. "',$count,'" . pg_escape_string($sData['language']) . "',{$sData['dbid']})");
2016-01-10 11:01:49 +01:00
}
}
}
}
?>