17Syaroslav@ivinco.com<?php 27Syaroslav@ivinco.com/* 37Syaroslav@ivinco.com * To change this template, choose Tools | Templates 47Syaroslav@ivinco.com * and open the template in the editor. 57Syaroslav@ivinco.com */ 67Syaroslav@ivinco.com 77Syaroslav@ivinco.comfunction formatXml($data) 87Syaroslav@ivinco.com{ 97Syaroslav@ivinco.com $xmlFormat = ' 107Syaroslav@ivinco.com<sphinx:document id="{id}"> 117Syaroslav@ivinco.com<title><![CDATA[[{title}]]></title> 127Syaroslav@ivinco.com<body><![CDATA[[{body}]]></body> 137Syaroslav@ivinco.com<categories><![CDATA[[{categories}]]></categories> 147Syaroslav@ivinco.com<level>{level}</level> 1537Syaroslav@ivinco.com<modified>{modified}</modified> 167Syaroslav@ivinco.com</sphinx:document> 177Syaroslav@ivinco.com 187Syaroslav@ivinco.com'; 197Syaroslav@ivinco.com 2012Syaroslav@ivinco.com return str_replace( array('{id}', '{title}', '{body}', '{categories}', '{level}', '{modified}'), 2137Syaroslav@ivinco.com array($data['id'], escapeTextValue($data['title']), escapeTextValue($data['body']), escapeTextValue($data['categories']), 2212Syaroslav@ivinco.com $data['level'], $data['modified']), 237Syaroslav@ivinco.com $xmlFormat 247Syaroslav@ivinco.com ); 257Syaroslav@ivinco.com} 267Syaroslav@ivinco.com 2737Syaroslav@ivinco.comfunction escapeTextValue($value) 2837Syaroslav@ivinco.com{ 2937Syaroslav@ivinco.com if ("" === $value) 3037Syaroslav@ivinco.com { 3137Syaroslav@ivinco.com return ""; 3237Syaroslav@ivinco.com } 3337Syaroslav@ivinco.com $value = mb_convert_encoding($value,'UTF-8','ISO-8859-1'); 3437Syaroslav@ivinco.com $value = strip_tags($value); 3537Syaroslav@ivinco.com $value = stripInvalidXml($value); 3637Syaroslav@ivinco.com return str_replace("]]>", "]]><![CDATA[]]]]><![CDATA[>]]><![CDATA[", $value); 3737Syaroslav@ivinco.com } 3837Syaroslav@ivinco.com 3937Syaroslav@ivinco.comfunction stripInvalidXml($value) 4037Syaroslav@ivinco.com{ 4137Syaroslav@ivinco.com $ret = ""; 4237Syaroslav@ivinco.com if (empty($value)) 4337Syaroslav@ivinco.com { 4437Syaroslav@ivinco.com return $ret; 4537Syaroslav@ivinco.com } 4637Syaroslav@ivinco.com 4737Syaroslav@ivinco.com $current = null; 4837Syaroslav@ivinco.com $length = strlen($value); 4937Syaroslav@ivinco.com for ($i=0; $i < $length; $i++) 5037Syaroslav@ivinco.com { 5137Syaroslav@ivinco.com $current = ord($value{$i}); 5237Syaroslav@ivinco.com if (($current == 0x9) || 5337Syaroslav@ivinco.com ($current == 0xA) || 5437Syaroslav@ivinco.com ($current == 0xD) || 5537Syaroslav@ivinco.com (($current >= 0x20) && ($current <= 0xD7FF)) || 5637Syaroslav@ivinco.com (($current >= 0xE000) && ($current <= 0xFFFD)) || 5737Syaroslav@ivinco.com (($current >= 0x10000) && ($current <= 0x10FFFF))) 5837Syaroslav@ivinco.com { 5937Syaroslav@ivinco.com $ret .= chr($current); 6037Syaroslav@ivinco.com } 6137Syaroslav@ivinco.com else 6237Syaroslav@ivinco.com { 6337Syaroslav@ivinco.com $ret .= " "; 6437Syaroslav@ivinco.com } 6537Syaroslav@ivinco.com } 6637Syaroslav@ivinco.com return $ret; 6737Syaroslav@ivinco.com } 6837Syaroslav@ivinco.com 697Syaroslav@ivinco.comfunction getDocumentsByHeadings($id, $metadata) 707Syaroslav@ivinco.com{ 717Syaroslav@ivinco.com if (empty($metadata) || empty($metadata['description']['tableofcontents'])) return false; 727Syaroslav@ivinco.com 737Syaroslav@ivinco.com $sections = array(); 7419Syaroslav@ivinco.com $level = 1; 7519Syaroslav@ivinco.com $previouse_title = ''; 767Syaroslav@ivinco.com foreach($metadata['description']['tableofcontents'] as $row){ 777Syaroslav@ivinco.com $sections[$row['hid']] = array( 7820Syaroslav@ivinco.com 'section' => getSectionByTitleLevel($id, $row['title']), 7919Syaroslav@ivinco.com 'level' => $row['level'], 8019Syaroslav@ivinco.com 'title' => $row['title'] 817Syaroslav@ivinco.com ); 8219Syaroslav@ivinco.com if ($row['level'] > $level){ 8319Syaroslav@ivinco.com $sections[$row['hid']]['title_text'] = $previouse_title . " » ".$row['title']; 8419Syaroslav@ivinco.com } else { 8519Syaroslav@ivinco.com $sections[$row['hid']]['title_text'] = $row['title']; 8619Syaroslav@ivinco.com $previouse_title = $row['title']; 8719Syaroslav@ivinco.com } 887Syaroslav@ivinco.com } 897Syaroslav@ivinco.com return $sections; 907Syaroslav@ivinco.com} 917Syaroslav@ivinco.com 9221Syaroslav@ivinco.comfunction getSectionByTitleLevel($id, $header, $extended=false) 9320Syaroslav@ivinco.com{ 9420Syaroslav@ivinco.com $headerReg = preg_quote($header, '/'); 9520Syaroslav@ivinco.com $doc = io_readFile(wikiFN($id)); 9635Syaroslav@ivinco.com $regex = "(={1,6})\s*({$headerReg})\s*(={1,6})"; 9720Syaroslav@ivinco.com $section = ''; 9820Syaroslav@ivinco.com if (preg_match("/$regex/i",$doc,$matches)) { 9920Syaroslav@ivinco.com $startHeader = $matches[0]; 10020Syaroslav@ivinco.com $startHeaderPos = strpos($doc, $startHeader) + strlen($startHeader); 10120Syaroslav@ivinco.com $endDoc = substr($doc, $startHeaderPos); 10220Syaroslav@ivinco.com 10336Syaroslav@ivinco.com $regex = '(={3,6})(.*?)(={3,6})'; 10420Syaroslav@ivinco.com if (preg_match("/$regex/i",$endDoc,$matches)) { 10520Syaroslav@ivinco.com $endHeader = $matches[0]; 10620Syaroslav@ivinco.com $endHeaderPos = strpos($doc, $endHeader); 10720Syaroslav@ivinco.com } else { 10820Syaroslav@ivinco.com $endHeaderPos = 0; 10920Syaroslav@ivinco.com } 11020Syaroslav@ivinco.com if ($endHeaderPos){ 11120Syaroslav@ivinco.com $section = substr($doc, $startHeaderPos, $endHeaderPos - $startHeaderPos); 11220Syaroslav@ivinco.com } else { 11320Syaroslav@ivinco.com $section = substr($doc, $startHeaderPos); 11420Syaroslav@ivinco.com } 11520Syaroslav@ivinco.com } 11620Syaroslav@ivinco.com $section = trim($section); 11721Syaroslav@ivinco.com //trying to get next section content if body for first section is empty 11821Syaroslav@ivinco.com //working only for extended mode 11921Syaroslav@ivinco.com if ($extended && empty($section)){ 12020Syaroslav@ivinco.com $startHeaderPos = $endHeaderPos + strlen($endHeader); 12120Syaroslav@ivinco.com $endDoc = substr($endDoc, $startHeaderPos); 12236Syaroslav@ivinco.com $regex = '(={3,6})(.*?)(={3,6})'; 12320Syaroslav@ivinco.com if (preg_match("/$regex/i",$endDoc,$matches)) { 12420Syaroslav@ivinco.com $endHeader = $matches[0]; 12520Syaroslav@ivinco.com $endHeaderPos = strpos($doc, $endHeader); 12620Syaroslav@ivinco.com } else { 12720Syaroslav@ivinco.com $endHeaderPos = 0; 12820Syaroslav@ivinco.com } 12920Syaroslav@ivinco.com if ($endHeaderPos){ 13020Syaroslav@ivinco.com $section = substr($doc, $startHeaderPos, $endHeaderPos - $startHeaderPos); 13120Syaroslav@ivinco.com } else { 13220Syaroslav@ivinco.com $section = substr($doc, $startHeaderPos); 13320Syaroslav@ivinco.com } 13420Syaroslav@ivinco.com } 13521Syaroslav@ivinco.com $section = trim($section); 13620Syaroslav@ivinco.com return $section; 13720Syaroslav@ivinco.com} 13820Syaroslav@ivinco.com 1397Syaroslav@ivinco.comfunction getSection($id, $header) 1407Syaroslav@ivinco.com{ 14117Syaroslav@ivinco.com static $cacheInstructions = null; 14217Syaroslav@ivinco.com static $cacheDoc = null; 14317Syaroslav@ivinco.com 14418Syaroslav@ivinco.com if (empty($cacheDoc[$id])){ 14517Syaroslav@ivinco.com // Create the parser 14617Syaroslav@ivinco.com $Parser = & new Doku_Parser(); 1477Syaroslav@ivinco.com 14817Syaroslav@ivinco.com // Add the Handler 14917Syaroslav@ivinco.com $Parser->Handler = & new Doku_Handler(); 15017Syaroslav@ivinco.com 15117Syaroslav@ivinco.com // Load the header mode to find headers 15217Syaroslav@ivinco.com $Parser->addMode('header',new Doku_Parser_Mode_Header()); 15318Syaroslav@ivinco.com $Parser->addMode('listblock',new Doku_Parser_Mode_ListBlock()); 15417Syaroslav@ivinco.com 15517Syaroslav@ivinco.com // Loads the raw wiki document 15617Syaroslav@ivinco.com $doc = io_readFile(wikiFN($id)); 1577Syaroslav@ivinco.com 15817Syaroslav@ivinco.com // Get a list of instructions 15917Syaroslav@ivinco.com $instructions = $Parser->parse($doc); 16017Syaroslav@ivinco.com 16117Syaroslav@ivinco.com unset($Parser->Handler); 16217Syaroslav@ivinco.com unset($Parser); 16317Syaroslav@ivinco.com 16417Syaroslav@ivinco.com //free old cache 16518Syaroslav@ivinco.com $cacheInstructions = null; 16618Syaroslav@ivinco.com $cacheDoc = null; 1677Syaroslav@ivinco.com 16817Syaroslav@ivinco.com //initialize new cache 16918Syaroslav@ivinco.com $cacheInstructions[$id] = $instructions; 17018Syaroslav@ivinco.com $cacheDoc[$id] = $doc; 17117Syaroslav@ivinco.com } else { 17217Syaroslav@ivinco.com $instructions = $cacheInstructions[$id]; 17317Syaroslav@ivinco.com $doc = $cacheDoc[$id]; 17418Syaroslav@ivinco.com } 1757Syaroslav@ivinco.com 17617Syaroslav@ivinco.com 1777Syaroslav@ivinco.com 1787Syaroslav@ivinco.com // Use this to watch when we're inside the section we want 1797Syaroslav@ivinco.com $inSection = FALSE; 1807Syaroslav@ivinco.com $startPos = 0; 1817Syaroslav@ivinco.com $endPos = 0; 1827Syaroslav@ivinco.com 1837Syaroslav@ivinco.com // Loop through the instructions 1847Syaroslav@ivinco.com foreach ( $instructions as $instruction ) { 1857Syaroslav@ivinco.com 1867Syaroslav@ivinco.com if ( !$inSection ) { 1877Syaroslav@ivinco.com 1887Syaroslav@ivinco.com // Look for the header for the "Lists" heading 1897Syaroslav@ivinco.com if ( $instruction[0] == 'header' && 1907Syaroslav@ivinco.com trim($instruction[1][0]) == $header ) { 1917Syaroslav@ivinco.com 1927Syaroslav@ivinco.com $startPos = $instruction[2]; 1937Syaroslav@ivinco.com $inSection = TRUE; 1947Syaroslav@ivinco.com } 1957Syaroslav@ivinco.com } else { 1967Syaroslav@ivinco.com 1977Syaroslav@ivinco.com // Look for the end of the section 1987Syaroslav@ivinco.com if ( $instruction[0] == 'section_close' ) { 1997Syaroslav@ivinco.com $endPos = $instruction[2]; 2007Syaroslav@ivinco.com break; 2017Syaroslav@ivinco.com } 2027Syaroslav@ivinco.com } 2037Syaroslav@ivinco.com } 2047Syaroslav@ivinco.com 2057Syaroslav@ivinco.com // Normalize and pad the document in the same way the parse does 2067Syaroslav@ivinco.com // so that byte indexes with match 2077Syaroslav@ivinco.com $doc = "\n".str_replace("\r\n","\n",$doc)."\n"; 2087Syaroslav@ivinco.com $section = substr($doc, $startPos, ($endPos-$startPos)); 2097Syaroslav@ivinco.com 2107Syaroslav@ivinco.com return $section; 2117Syaroslav@ivinco.com} 2127Syaroslav@ivinco.com 2137Syaroslav@ivinco.comfunction getCategories($id) 2147Syaroslav@ivinco.com{ 2157Syaroslav@ivinco.com if (empty($id)) return ''; 2167Syaroslav@ivinco.com 2177Syaroslav@ivinco.com if (false === strpos($id, ":")){ 2187Syaroslav@ivinco.com return $id; 2197Syaroslav@ivinco.com } 2207Syaroslav@ivinco.com 2217Syaroslav@ivinco.com $ns = explode(":", $id); 2227Syaroslav@ivinco.com $nsCount = count($ns); 2237Syaroslav@ivinco.com 2247Syaroslav@ivinco.com $result = ''; 2257Syaroslav@ivinco.com do{ 2267Syaroslav@ivinco.com for($i = 0; $i < $nsCount; $i++){ 2277Syaroslav@ivinco.com $name = $ns[$i]; 2287Syaroslav@ivinco.com $result .= $name; 2297Syaroslav@ivinco.com if ($i < $nsCount - 1){ 2307Syaroslav@ivinco.com $result .= ':'; 2317Syaroslav@ivinco.com } 2327Syaroslav@ivinco.com } 2337Syaroslav@ivinco.com $result .= ' '; 2347Syaroslav@ivinco.com }while($nsCount--); 2357Syaroslav@ivinco.com return $result; 2367Syaroslav@ivinco.com} 2377Syaroslav@ivinco.com 2387Syaroslav@ivinco.com 2397Syaroslav@ivinco.com /** 2407Syaroslav@ivinco.com * Method return all wiki page names 2417Syaroslav@ivinco.com * @global array $conf 2427Syaroslav@ivinco.com * @return array 2437Syaroslav@ivinco.com */ 2447Syaroslav@ivinco.com function getPagesList() 2457Syaroslav@ivinco.com { 2467Syaroslav@ivinco.com global $conf; 2477Syaroslav@ivinco.com 2487Syaroslav@ivinco.com $data = array(); 2497Syaroslav@ivinco.com sort($data); 2507Syaroslav@ivinco.com search($data,$conf['datadir'],'search_allpages','',''); 2517Syaroslav@ivinco.com 2527Syaroslav@ivinco.com return $data; 2538Syaroslav@ivinco.com} 2548Syaroslav@ivinco.com 25515Syaroslav@ivinco.comfunction getNsLinks($id, $keywords, $search) 2568Syaroslav@ivinco.com{ 2578Syaroslav@ivinco.com global $conf; 2588Syaroslav@ivinco.com $parts = explode(':', $id); 2598Syaroslav@ivinco.com $count = count($parts); 26015Syaroslav@ivinco.com 2618Syaroslav@ivinco.com // print intermediate namespace links 2628Syaroslav@ivinco.com $part = ''; 26310Syaroslav@ivinco.com $data = array(); 26410Syaroslav@ivinco.com $titles = array(); 2658Syaroslav@ivinco.com for($i=0; $i<$count; $i++){ 2668Syaroslav@ivinco.com $part .= $parts[$i].':'; 2678Syaroslav@ivinco.com $page = $part; 2688Syaroslav@ivinco.com resolve_pageid('',$page,$exists); 26915Syaroslav@ivinco.com 27015Syaroslav@ivinco.com if (preg_match("#:start$#", $page) && !preg_match("#:start:$#", $part)) { 27113Syaroslav@ivinco.com $page = substr($page, 0, strpos($page, ":start")); 27213Syaroslav@ivinco.com }; 2738Syaroslav@ivinco.com 2748Syaroslav@ivinco.com // output 2758Syaroslav@ivinco.com if ($exists){ 27615Syaroslav@ivinco.com $titles[wl($page)] = $parts[$i]; 2778Syaroslav@ivinco.com } else { 27818Syaroslav@ivinco.com $titles[wl($page)] = $parts[$i]; 27915Syaroslav@ivinco.com } 28027Syaroslav@ivinco.com $data[] = array('link' => "?do=search&id={$keywords}".urlencode(" @categories $page")); 2818Syaroslav@ivinco.com } 28215Syaroslav@ivinco.com $titleExcerpt = $search->getExcerpt($titles, $search->starQuery($keywords)); 28310Syaroslav@ivinco.com $i = 0; 28410Syaroslav@ivinco.com foreach ($data as $key => $notused){ 28510Syaroslav@ivinco.com $data[$key]['title'] = $titleExcerpt[$i++]; 28610Syaroslav@ivinco.com } 28710Syaroslav@ivinco.com return $data; 28839Syaroslav@ivinco.com} 28939Syaroslav@ivinco.com 29039Syaroslav@ivinco.comfunction printNamespaces($query) 29139Syaroslav@ivinco.com{ 29239Syaroslav@ivinco.com $data = array(); 29339Syaroslav@ivinco.com $data = ft_pageLookup($query); 29439Syaroslav@ivinco.com 29539Syaroslav@ivinco.com if(!count($data)) return; 29639Syaroslav@ivinco.com 29739Syaroslav@ivinco.com print '<strong>'.$lang['quickhits'].'</strong>'; 29839Syaroslav@ivinco.com print '<ul>'; 299*48Syaroslav@ivinco.com $counter = 0; 30039Syaroslav@ivinco.com foreach($data as $id){ 30139Syaroslav@ivinco.com print '<li>'; 30239Syaroslav@ivinco.com $ns = getNS($id); 30339Syaroslav@ivinco.com if($ns){ 30439Syaroslav@ivinco.com $name = shorten(noNS($id), ' ('.$ns.')',30); 30539Syaroslav@ivinco.com }else{ 30639Syaroslav@ivinco.com $name = $id; 30739Syaroslav@ivinco.com } 30839Syaroslav@ivinco.com $href = ("?do=search&id={$query}".urlencode(" @categories {$id}")); 30939Syaroslav@ivinco.com tpl_link($href,$id); 31039Syaroslav@ivinco.com print '</li>'; 311*48Syaroslav@ivinco.com if(++$counter == 20){ 312*48Syaroslav@ivinco.com break; 313*48Syaroslav@ivinco.com } 31439Syaroslav@ivinco.com } 31539Syaroslav@ivinco.com print '</ul>'; 3167Syaroslav@ivinco.com}