"Subsections", 'also' => "See also...", 'sib' => "Also here...", 'rand' => "Random Links"); session_start(); dbconnect("existencia", "db connection"); if (empty($_SESSION['visited'])) $_SESSION['visited'] = array(); if (empty($_GET['q'])) RenderPage(1); if ($_GET['q'] == 'random') { $result = dbquery("select id, title, description, name, parent_id from pages where parent_id != 0 order by rand() limit %d", count($_SESSION['visited']) + 1); while ($row = dbfetch($result, DB_ASSOC)) { if (array_search($row['id'], $_SESSION['visited']) === false) { header('Location: ' . array_shift(array_keys(MakeBreadcrumb($row, 0)))); exit(0); } } // uh-oh, they visited everything! $_SESSION['visited'] = array(); header('Location: ' . array_shift(array_keys(MakeBreadcrumb($row, 0)))); exit(0); } $parent_id = 1; $path = explode('/', $_GET['q']); foreach ($path as $name) { if ($name == '') continue; $parent_id = dbgetvalue("select id from pages where parent_id = %d and name = %s", $parent_id, $name); if ($parent_id == null) RenderPage(ERROR_PAGE); } RenderPage($parent_id); function RenderPage($id) { global $kTypeTitles; // update counters if (!empty($_SESSION['lastpage'])) dbquery("update links set uses = uses + 1 where source_id = %d and dest_id = %d", $_SESSION['lastpage'], $id); dbquery("update pages set uses = uses + 1 where id = %d", $id); // update link array_push($_SESSION['visited'], $id); $_SESSION['lastpage'] = $id; // get page info list($parent_id, $title, $image, $body) = dbgetrow("select parent_id, title, image, body from pages where id = %d", $id); $page = array('title' => $title, 'image' => MyFormatText($image), 'body' => MyFormatText($body)); // get links $result = dbquery("select pages.id, pages.title, pages.description, pages.name, pages.parent_id, links.type, links.salience from links left join pages on links.dest_id = pages.id where links.source_id = %d order by salience desc", $id); $groups = array(); $pendings = array(); $num_full = 0; while ($row = dbfetch($result, DB_ASSOC)) { // Can we fit this in easily? if ($num_full < 10 && isset($groups[$row['type']])) { AddLink($groups, $row); $num_full++; } else if ($num_full < 9 && !isset($groups[$row['type']])) { AddLink($groups, $row); $num_full += 2; } else if (count($groups) > 1) { AddLink($pendings, $row); $rows_saved = 0; if (isset($groups[$row['type']])) { // we can save a row! $rows_saved = 1; } $best_improve = 0; $best_replace = null; foreach ($groups as $key => $rows) { $improve = TotalSalience($pending[$row['type']], count($rows) + $rows_saved) - TotalSalience($rows, 0); if ($improve > $best_improve) { $best_improve = $improve; $best_replace = $key; } } if ($best_replace != null) { $old_rows = $groups[$best_replace]; unset($groups[$best_replace]); foreach ($old_rows as $ii => $old_row) AddLink($groups, array_shift($pending[$row['type']])); // num_fill can't change, because using at least as many } } else { // no zeros left to replace-- can't improve! break; } } if ($num_full < 8 && $parent_id != 1 && $parent_id != 0) { // add some siblings $result = dbquery("select id, title, description, name, parent_id from pages where parent_id = %d and id != %d order by rand() limit 10", $parent_id, $id); $siblings = array(); while ($num_full < 10) { $totry = dbfetch($result, DB_ASSOC); if (!$totry) break; // did we already include this? $found = false; foreach ($groups as $key => $rows) { foreach ($rows as $ii => $row) if ($totry['id'] == $row['id']) { $found = true; break; } if ($found) break; } if (!$found) { array_push($siblings, $totry); $num_full++; } } if (!empty($siblings)) $groups['sib'] = $siblings; } if ($num_full < 8) { // fill with random links $result = dbquery("select id, title, description, name, parent_id from pages where parent_id != 0 and id != %d and id != %d order by rand() limit 20", $id, $parent_id); $groups['rand'] = array(); $num_full++; while ($num_full < 10) { $totry = dbfetch($result, DB_ASSOC); // did we already include this? $found = false; foreach ($groups as $key => $rows) { foreach ($rows as $ii => $row) if ($totry['id'] == $row['id']) { $found = true; break; } if ($found) break; } if (!$found) { array_push($groups['rand'], $totry); $num_full++; } } } $links = array(); $ii = 0; foreach ($groups as $type => $rows) { if ($type == 'sib') $links[$ii++ * 2 + 1] = '' . $kTypeTitles[$type] . ''; else $links[$ii++ * 2 + 1] = '' . $kTypeTitles[$type] . ''; foreach ($rows as $row) { $links[$ii * 2] = MakeLink($row); $links[$ii++ * 2 + 1] = $row['description']; } } $page['links'] = $links; if ($id == 1) { $page['bottomcrumbs'] = 'Go to a random page'; $page['topcrumbs'] = ""; } else { $bottomcrumbs = $topcrumbs = ""; if ($parent_id != 0 && $parent_id != 1) { $urls = MakeBreadcrumb(dbfetch(dbquery("select title, name, parent_id from pages where id = %d", $parent_id), 1), DB_ASSOC); foreach ($urls as $url => $title) { $bottomcrumbs = ' < ' . $title . '' . $bottomcrumbs; $topcrumbs = $title . ' » ' . $topcrumbs; } } $page['bottomcrumbs'] = 'Home' . $bottomcrumbs; $page['topcrumbs'] = $topcrumbs; } echo FileTemplateReplace("template.html", $page); exit(0); } function MyFormatText($text) { //$text = FormatText($text); $text = nl2br($text); $text = preg_replace('/==([^=]+)==/', '

$1

', $text); $text = preg_replace_callback('/\[img:\s*([^\]]+)\]/', "ImageReplace", $text); $text = preg_replace('/\[(\S+)\s+([^\]]+)\]/', '$2', $text); return $text; } function ImageReplace($match) { if (is_array($match)) $match = $match[1]; if ($match == 'random') return ImageReplace(dbgetvalue("select name from images order by rand() + salience desc limit 1")); if ($match == 'frompics') { return 'random image'; } list($url, $alt) = dbgetrow("select url, alt from images where name = %s", $match); return '' . $alt . ''; } function AddLink(&$coll, $row) { if (isset($coll[$row['type']])) array_push($coll[$row['type']], $row); else if (!isset($coll[$row['type']])) $coll[$row['type']] = array($row); } function TotalSalience($rows, $count) { if ($count == 0 || $count > count($rows)) $count = count($rows); $total = 0; for ($ii = 0; $ii < $count; $ii++) $total += $rows[$ii]['salience']; return $total; } function MakeBreadcrumb($row, $num_back) { $urls = array($row['name'] => $row['title']); $parent_id = $row['parent_id']; while ($parent_id != 1 && $parent_id != 0) { list($parent_id, $name, $title) = dbgetrow("select parent_id, name, title from pages where id = %d", $parent_id); $newurls = array(); foreach ($urls as $url => $title) $newurls[$name . '/' . $url] = $title; $urls = $newurls; if (count($urls) < $num_back + 1) $urls[$name] = $title; } $newurls = array(); foreach ($urls as $url => $title) $newurls[PAGE_ROOT . $url] = $title; return $newurls; } function MakeLink($row) { $url = array_shift(array_keys(MakeBreadcrumb($row, 0))); return '' . $row['title'] . ''; } ?>