1<?php 2/* 3 * To change this template, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 7function formatXml($data) 8{ 9 $xmlFormat = ' 10<sphinx:document id="{id}"> 11<title><![CDATA[[{title}]]></title> 12<body><![CDATA[[{body}]]></body> 13<categories><![CDATA[[{categories}]]></categories> 14<level>{level}</level> 15<modified>{modified}</modified> 16</sphinx:document> 17 18'; 19 20 return str_replace( array('{id}', '{title}', '{body}', '{categories}', '{level}', '{modified}'), 21 array($data['id'], $data['title'], $data['body'], $data['categories'], 22 $data['level'], $data['modified']), 23 $xmlFormat 24 ); 25} 26 27function getDocumentsByHeadings($id, $metadata) 28{ 29 if (empty($metadata) || empty($metadata['description']['tableofcontents'])) return false; 30 31 $sections = array(); 32 $level = 1; 33 $previouse_title = ''; 34 foreach($metadata['description']['tableofcontents'] as $row){ 35 $sections[$row['hid']] = array( 36 'section' => getSectionByTitleLevel($id, $row['title']), 37 'level' => $row['level'], 38 'title' => $row['title'] 39 ); 40 if ($row['level'] > $level){ 41 $sections[$row['hid']]['title_text'] = $previouse_title . " » ".$row['title']; 42 } else { 43 $sections[$row['hid']]['title_text'] = $row['title']; 44 $previouse_title = $row['title']; 45 } 46 } 47 return $sections; 48} 49 50function getSectionByTitleLevel($id, $header, $extended=false) 51{ 52 $headerReg = preg_quote($header, '/'); 53 $doc = io_readFile(wikiFN($id)); 54 $regex = "(={1,6})\s*({$headerReg})\s*(={1,6})"; 55 $section = ''; 56 if (preg_match("/$regex/i",$doc,$matches)) { 57 $startHeader = $matches[0]; 58 $startHeaderPos = strpos($doc, $startHeader) + strlen($startHeader); 59 $endDoc = substr($doc, $startHeaderPos); 60 61 $regex = '(={1,6})(.*?)(={1,6})'; 62 if (preg_match("/$regex/i",$endDoc,$matches)) { 63 $endHeader = $matches[0]; 64 $endHeaderPos = strpos($doc, $endHeader); 65 } else { 66 $endHeaderPos = 0; 67 } 68 if ($endHeaderPos){ 69 $section = substr($doc, $startHeaderPos, $endHeaderPos - $startHeaderPos); 70 } else { 71 $section = substr($doc, $startHeaderPos); 72 } 73 } 74 $section = trim($section); 75 //trying to get next section content if body for first section is empty 76 //working only for extended mode 77 if ($extended && empty($section)){ 78 $startHeaderPos = $endHeaderPos + strlen($endHeader); 79 $endDoc = substr($endDoc, $startHeaderPos); 80 $regex = '(={1,6})(.*?)(={1,6})'; 81 if (preg_match("/$regex/i",$endDoc,$matches)) { 82 $endHeader = $matches[0]; 83 $endHeaderPos = strpos($doc, $endHeader); 84 } else { 85 $endHeaderPos = 0; 86 } 87 if ($endHeaderPos){ 88 $section = substr($doc, $startHeaderPos, $endHeaderPos - $startHeaderPos); 89 } else { 90 $section = substr($doc, $startHeaderPos); 91 } 92 } 93 $section = trim($section); 94 return $section; 95} 96 97function getSection($id, $header) 98{ 99 static $cacheInstructions = null; 100 static $cacheDoc = null; 101 102 if (empty($cacheDoc[$id])){ 103 // Create the parser 104 $Parser = & new Doku_Parser(); 105 106 // Add the Handler 107 $Parser->Handler = & new Doku_Handler(); 108 109 // Load the header mode to find headers 110 $Parser->addMode('header',new Doku_Parser_Mode_Header()); 111 $Parser->addMode('listblock',new Doku_Parser_Mode_ListBlock()); 112 113 // Loads the raw wiki document 114 $doc = io_readFile(wikiFN($id)); 115 116 // Get a list of instructions 117 $instructions = $Parser->parse($doc); 118 119 unset($Parser->Handler); 120 unset($Parser); 121 122 //free old cache 123 $cacheInstructions = null; 124 $cacheDoc = null; 125 126 //initialize new cache 127 $cacheInstructions[$id] = $instructions; 128 $cacheDoc[$id] = $doc; 129 } else { 130 $instructions = $cacheInstructions[$id]; 131 $doc = $cacheDoc[$id]; 132 } 133 134 135 136 // Use this to watch when we're inside the section we want 137 $inSection = FALSE; 138 $startPos = 0; 139 $endPos = 0; 140 141 // Loop through the instructions 142 foreach ( $instructions as $instruction ) { 143 144 if ( !$inSection ) { 145 146 // Look for the header for the "Lists" heading 147 if ( $instruction[0] == 'header' && 148 trim($instruction[1][0]) == $header ) { 149 150 $startPos = $instruction[2]; 151 $inSection = TRUE; 152 } 153 } else { 154 155 // Look for the end of the section 156 if ( $instruction[0] == 'section_close' ) { 157 $endPos = $instruction[2]; 158 break; 159 } 160 } 161 } 162 163 // Normalize and pad the document in the same way the parse does 164 // so that byte indexes with match 165 $doc = "\n".str_replace("\r\n","\n",$doc)."\n"; 166 $section = substr($doc, $startPos, ($endPos-$startPos)); 167 168 return $section; 169} 170 171function getCategories($id) 172{ 173 if (empty($id)) return ''; 174 175 if (false === strpos($id, ":")){ 176 return $id; 177 } 178 179 $ns = explode(":", $id); 180 $nsCount = count($ns); 181 182 $result = ''; 183 do{ 184 for($i = 0; $i < $nsCount; $i++){ 185 $name = $ns[$i]; 186 $result .= $name; 187 if ($i < $nsCount - 1){ 188 $result .= ':'; 189 } 190 } 191 $result .= ' '; 192 }while($nsCount--); 193 return $result; 194} 195 196 197 /** 198 * Method return all wiki page names 199 * @global array $conf 200 * @return array 201 */ 202 function getPagesList() 203 { 204 global $conf; 205 206 $data = array(); 207 sort($data); 208 search($data,$conf['datadir'],'search_allpages','',''); 209 210 return $data; 211} 212 213function getNsLinks($id, $keywords, $search) 214{ 215 global $conf; 216 $parts = explode(':', $id); 217 $count = count($parts); 218 219 // print intermediate namespace links 220 $part = ''; 221 $data = array(); 222 $titles = array(); 223 for($i=0; $i<$count; $i++){ 224 $part .= $parts[$i].':'; 225 $page = $part; 226 resolve_pageid('',$page,$exists); 227 228 if (preg_match("#:start$#", $page) && !preg_match("#:start:$#", $part)) { 229 $page = substr($page, 0, strpos($page, ":start")); 230 }; 231 232 // output 233 if ($exists){ 234 $titles[wl($page)] = $parts[$i]; 235 } else { 236 $titles[wl($page)] = $parts[$i]; 237 } 238 $data[] = array('link' => "?do=sphinxsearch&id={$keywords}".urlencode(" @categories $page")); 239 } 240 $titleExcerpt = $search->getExcerpt($titles, $search->starQuery($keywords)); 241 $i = 0; 242 foreach ($data as $key => $notused){ 243 $data[$key]['title'] = $titleExcerpt[$i++]; 244 } 245 return $data; 246}