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}'),
2156Syaroslav@ivinco.com                        array($data['id'], escapeTextValue($data['title_to_index']), 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    }
33*58Syaroslav@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                                    );
8256Syaroslav@ivinco.com        if ($row['level'] > $level && !empty($previouse_title)){
8319Syaroslav@ivinco.com            $sections[$row['hid']]['title_text'] = $previouse_title . " &raquo; ".$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        }
8856Syaroslav@ivinco.com        $sections[$row['hid']]['title_to_index'] = $row['title'];
897Syaroslav@ivinco.com    }
907Syaroslav@ivinco.com    return $sections;
917Syaroslav@ivinco.com}
927Syaroslav@ivinco.com
9321Syaroslav@ivinco.comfunction getSectionByTitleLevel($id, $header, $extended=false)
9420Syaroslav@ivinco.com{
9520Syaroslav@ivinco.com    $headerReg = preg_quote($header, '/');
9620Syaroslav@ivinco.com    $doc = io_readFile(wikiFN($id));
9735Syaroslav@ivinco.com    $regex = "(={1,6})\s*({$headerReg})\s*(={1,6})";
9820Syaroslav@ivinco.com    $section = '';
9920Syaroslav@ivinco.com    if (preg_match("/$regex/i",$doc,$matches)) {
10020Syaroslav@ivinco.com        $startHeader = $matches[0];
10120Syaroslav@ivinco.com        $startHeaderPos = strpos($doc, $startHeader) + strlen($startHeader);
10220Syaroslav@ivinco.com        $endDoc = substr($doc, $startHeaderPos);
10320Syaroslav@ivinco.com
10436Syaroslav@ivinco.com        $regex = '(={3,6})(.*?)(={3,6})';
10520Syaroslav@ivinco.com        if (preg_match("/$regex/i",$endDoc,$matches)) {
10620Syaroslav@ivinco.com            $endHeader = $matches[0];
10720Syaroslav@ivinco.com            $endHeaderPos = strpos($doc, $endHeader);
10820Syaroslav@ivinco.com        } else {
10920Syaroslav@ivinco.com            $endHeaderPos = 0;
11020Syaroslav@ivinco.com        }
11120Syaroslav@ivinco.com        if ($endHeaderPos){
11220Syaroslav@ivinco.com            $section = substr($doc, $startHeaderPos, $endHeaderPos - $startHeaderPos);
11320Syaroslav@ivinco.com        } else {
11420Syaroslav@ivinco.com            $section = substr($doc, $startHeaderPos);
11520Syaroslav@ivinco.com        }
11620Syaroslav@ivinco.com    }
11720Syaroslav@ivinco.com    $section = trim($section);
11821Syaroslav@ivinco.com    //trying to get next section content if body for first section is empty
11921Syaroslav@ivinco.com    //working only for extended mode
12021Syaroslav@ivinco.com    if ($extended && empty($section)){
12120Syaroslav@ivinco.com        $startHeaderPos = $endHeaderPos + strlen($endHeader);
12220Syaroslav@ivinco.com        $endDoc = substr($endDoc, $startHeaderPos);
12336Syaroslav@ivinco.com        $regex = '(={3,6})(.*?)(={3,6})';
12420Syaroslav@ivinco.com        if (preg_match("/$regex/i",$endDoc,$matches)) {
12520Syaroslav@ivinco.com            $endHeader = $matches[0];
12620Syaroslav@ivinco.com            $endHeaderPos = strpos($doc, $endHeader);
12720Syaroslav@ivinco.com        } else {
12820Syaroslav@ivinco.com            $endHeaderPos = 0;
12920Syaroslav@ivinco.com        }
13020Syaroslav@ivinco.com        if ($endHeaderPos){
13120Syaroslav@ivinco.com            $section = substr($doc, $startHeaderPos, $endHeaderPos - $startHeaderPos);
13220Syaroslav@ivinco.com        } else {
13320Syaroslav@ivinco.com            $section = substr($doc, $startHeaderPos);
13420Syaroslav@ivinco.com        }
13520Syaroslav@ivinco.com    }
13621Syaroslav@ivinco.com    $section = trim($section);
13720Syaroslav@ivinco.com    return $section;
13820Syaroslav@ivinco.com}
13920Syaroslav@ivinco.com
1407Syaroslav@ivinco.comfunction getSection($id, $header)
1417Syaroslav@ivinco.com{
14217Syaroslav@ivinco.com    static $cacheInstructions = null;
14317Syaroslav@ivinco.com    static $cacheDoc = null;
14417Syaroslav@ivinco.com
14518Syaroslav@ivinco.com    if (empty($cacheDoc[$id])){
14617Syaroslav@ivinco.com        // Create the parser
14717Syaroslav@ivinco.com        $Parser = & new Doku_Parser();
1487Syaroslav@ivinco.com
14917Syaroslav@ivinco.com        // Add the Handler
15017Syaroslav@ivinco.com        $Parser->Handler = & new Doku_Handler();
15117Syaroslav@ivinco.com
15217Syaroslav@ivinco.com        // Load the header mode to find headers
15317Syaroslav@ivinco.com        $Parser->addMode('header',new Doku_Parser_Mode_Header());
15418Syaroslav@ivinco.com        $Parser->addMode('listblock',new Doku_Parser_Mode_ListBlock());
15517Syaroslav@ivinco.com
15617Syaroslav@ivinco.com        // Loads the raw wiki document
15717Syaroslav@ivinco.com        $doc = io_readFile(wikiFN($id));
1587Syaroslav@ivinco.com
15917Syaroslav@ivinco.com        // Get a list of instructions
16017Syaroslav@ivinco.com        $instructions = $Parser->parse($doc);
16117Syaroslav@ivinco.com
16217Syaroslav@ivinco.com        unset($Parser->Handler);
16317Syaroslav@ivinco.com        unset($Parser);
16417Syaroslav@ivinco.com
16517Syaroslav@ivinco.com        //free old cache
16618Syaroslav@ivinco.com        $cacheInstructions = null;
16718Syaroslav@ivinco.com        $cacheDoc = null;
1687Syaroslav@ivinco.com
16917Syaroslav@ivinco.com        //initialize new cache
17018Syaroslav@ivinco.com        $cacheInstructions[$id] = $instructions;
17118Syaroslav@ivinco.com        $cacheDoc[$id] = $doc;
17217Syaroslav@ivinco.com    } else {
17317Syaroslav@ivinco.com        $instructions = $cacheInstructions[$id];
17417Syaroslav@ivinco.com        $doc = $cacheDoc[$id];
17518Syaroslav@ivinco.com    }
1767Syaroslav@ivinco.com
17717Syaroslav@ivinco.com
1787Syaroslav@ivinco.com
1797Syaroslav@ivinco.com    // Use this to watch when we're inside the section we want
1807Syaroslav@ivinco.com    $inSection = FALSE;
1817Syaroslav@ivinco.com    $startPos = 0;
1827Syaroslav@ivinco.com    $endPos = 0;
1837Syaroslav@ivinco.com
1847Syaroslav@ivinco.com    // Loop through the instructions
1857Syaroslav@ivinco.com    foreach ( $instructions as $instruction ) {
1867Syaroslav@ivinco.com
1877Syaroslav@ivinco.com        if ( !$inSection ) {
1887Syaroslav@ivinco.com
1897Syaroslav@ivinco.com            // Look for the header for the "Lists" heading
1907Syaroslav@ivinco.com            if ( $instruction[0] == 'header' &&
1917Syaroslav@ivinco.com                    trim($instruction[1][0]) == $header ) {
1927Syaroslav@ivinco.com
1937Syaroslav@ivinco.com                $startPos = $instruction[2];
1947Syaroslav@ivinco.com                $inSection = TRUE;
1957Syaroslav@ivinco.com            }
1967Syaroslav@ivinco.com        } else {
1977Syaroslav@ivinco.com
1987Syaroslav@ivinco.com            // Look for the end of the section
1997Syaroslav@ivinco.com            if ( $instruction[0] == 'section_close' ) {
2007Syaroslav@ivinco.com                $endPos = $instruction[2];
2017Syaroslav@ivinco.com                break;
2027Syaroslav@ivinco.com            }
2037Syaroslav@ivinco.com        }
2047Syaroslav@ivinco.com    }
2057Syaroslav@ivinco.com
2067Syaroslav@ivinco.com    // Normalize and pad the document in the same way the parse does
2077Syaroslav@ivinco.com    // so that byte indexes with match
2087Syaroslav@ivinco.com    $doc = "\n".str_replace("\r\n","\n",$doc)."\n";
2097Syaroslav@ivinco.com    $section = substr($doc, $startPos, ($endPos-$startPos));
2107Syaroslav@ivinco.com
2117Syaroslav@ivinco.com    return $section;
2127Syaroslav@ivinco.com}
2137Syaroslav@ivinco.com
2147Syaroslav@ivinco.comfunction getCategories($id)
2157Syaroslav@ivinco.com{
2167Syaroslav@ivinco.com    if (empty($id)) return '';
2177Syaroslav@ivinco.com
2187Syaroslav@ivinco.com    if (false === strpos($id, ":")){
2197Syaroslav@ivinco.com        return $id;
2207Syaroslav@ivinco.com    }
2217Syaroslav@ivinco.com
2227Syaroslav@ivinco.com    $ns = explode(":", $id);
2237Syaroslav@ivinco.com    $nsCount = count($ns);
2247Syaroslav@ivinco.com
2257Syaroslav@ivinco.com    $result = '';
2267Syaroslav@ivinco.com    do{
2277Syaroslav@ivinco.com        for($i = 0; $i < $nsCount; $i++){
2287Syaroslav@ivinco.com            $name = $ns[$i];
2297Syaroslav@ivinco.com            $result .= $name;
2307Syaroslav@ivinco.com            if ($i < $nsCount - 1){
2317Syaroslav@ivinco.com                 $result .= ':';
2327Syaroslav@ivinco.com            }
2337Syaroslav@ivinco.com        }
2347Syaroslav@ivinco.com        $result .= ' ';
2357Syaroslav@ivinco.com    }while($nsCount--);
2367Syaroslav@ivinco.com    return $result;
2377Syaroslav@ivinco.com}
2387Syaroslav@ivinco.com
2397Syaroslav@ivinco.com
2407Syaroslav@ivinco.com /**
2417Syaroslav@ivinco.com  * Method return all wiki page names
2427Syaroslav@ivinco.com  * @global array $conf
2437Syaroslav@ivinco.com  * @return array
2447Syaroslav@ivinco.com  */
2457Syaroslav@ivinco.com function getPagesList()
2467Syaroslav@ivinco.com {
2477Syaroslav@ivinco.com    global $conf;
2487Syaroslav@ivinco.com
2497Syaroslav@ivinco.com    $data = array();
2507Syaroslav@ivinco.com    sort($data);
2517Syaroslav@ivinco.com    search($data,$conf['datadir'],'search_allpages','','');
2527Syaroslav@ivinco.com
2537Syaroslav@ivinco.com    return $data;
2548Syaroslav@ivinco.com}
2558Syaroslav@ivinco.com
25615Syaroslav@ivinco.comfunction getNsLinks($id, $keywords, $search)
2578Syaroslav@ivinco.com{
2588Syaroslav@ivinco.com    global $conf;
2598Syaroslav@ivinco.com    $parts = explode(':', $id);
2608Syaroslav@ivinco.com    $count = count($parts);
26115Syaroslav@ivinco.com
2628Syaroslav@ivinco.com    // print intermediate namespace links
2638Syaroslav@ivinco.com    $part = '';
26410Syaroslav@ivinco.com    $data = array();
26510Syaroslav@ivinco.com    $titles = array();
2668Syaroslav@ivinco.com    for($i=0; $i<$count; $i++){
2678Syaroslav@ivinco.com        $part .= $parts[$i].':';
2688Syaroslav@ivinco.com        $page = $part;
2698Syaroslav@ivinco.com        resolve_pageid('',$page,$exists);
27015Syaroslav@ivinco.com
27115Syaroslav@ivinco.com        if (preg_match("#:start$#", $page) && !preg_match("#:start:$#", $part)) {
27213Syaroslav@ivinco.com            $page = substr($page, 0, strpos($page, ":start"));
27313Syaroslav@ivinco.com        };
2748Syaroslav@ivinco.com
2758Syaroslav@ivinco.com        // output
2768Syaroslav@ivinco.com        if ($exists){
27715Syaroslav@ivinco.com            $titles[wl($page)] = $parts[$i];
2788Syaroslav@ivinco.com        } else {
27918Syaroslav@ivinco.com            $titles[wl($page)] = $parts[$i];
28015Syaroslav@ivinco.com        }
28127Syaroslav@ivinco.com        $data[] = array('link' => "?do=search&id={$keywords}".urlencode(" @categories $page"));
2828Syaroslav@ivinco.com    }
28315Syaroslav@ivinco.com    $titleExcerpt = $search->getExcerpt($titles, $search->starQuery($keywords));
28410Syaroslav@ivinco.com    $i = 0;
28510Syaroslav@ivinco.com    foreach ($data as $key => $notused){
28610Syaroslav@ivinco.com        $data[$key]['title'] = $titleExcerpt[$i++];
28710Syaroslav@ivinco.com    }
28810Syaroslav@ivinco.com    return $data;
28939Syaroslav@ivinco.com}
29039Syaroslav@ivinco.com
29139Syaroslav@ivinco.comfunction printNamespaces($query)
29239Syaroslav@ivinco.com{
29339Syaroslav@ivinco.com  $data = array();
29439Syaroslav@ivinco.com  $data = ft_pageLookup($query);
29539Syaroslav@ivinco.com
29653Syaroslav@ivinco.com  if(!count($data)) return false;
29739Syaroslav@ivinco.com
29853Syaroslav@ivinco.com  print '<h3>Matching pagenames</h3>';
29939Syaroslav@ivinco.com  print '<ul>';
30048Syaroslav@ivinco.com  $counter = 0;
30139Syaroslav@ivinco.com  foreach($data as $id){
30239Syaroslav@ivinco.com    print '<li>';
30339Syaroslav@ivinco.com    $ns = getNS($id);
30439Syaroslav@ivinco.com    if($ns){
30539Syaroslav@ivinco.com      $name = shorten(noNS($id), ' ('.$ns.')',30);
30639Syaroslav@ivinco.com    }else{
30739Syaroslav@ivinco.com      $name = $id;
30839Syaroslav@ivinco.com    }
30939Syaroslav@ivinco.com    $href = ("?do=search&id={$query}".urlencode(" @categories {$id}"));
31050Syaroslav@ivinco.com    tpl_link($href,$id, "class='wikilink1'");
31139Syaroslav@ivinco.com    print '</li>';
31248Syaroslav@ivinco.com    if(++$counter == 20){
31348Syaroslav@ivinco.com        break;
31448Syaroslav@ivinco.com    }
31539Syaroslav@ivinco.com  }
31639Syaroslav@ivinco.com  print '</ul>';
3177Syaroslav@ivinco.com}