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>
157Syaroslav@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}'),
217Syaroslav@ivinco.com                        array($data['id'], $data['title'], $data['body'], $data['categories'],
2212Syaroslav@ivinco.com                             $data['level'], $data['modified']),
237Syaroslav@ivinco.com                $xmlFormat
247Syaroslav@ivinco.com            );
257Syaroslav@ivinco.com}
267Syaroslav@ivinco.com
277Syaroslav@ivinco.comfunction getDocumentsByHeadings($id, $metadata)
287Syaroslav@ivinco.com{
297Syaroslav@ivinco.com    if (empty($metadata) || empty($metadata['description']['tableofcontents'])) return false;
307Syaroslav@ivinco.com
317Syaroslav@ivinco.com    $sections = array();
32*19Syaroslav@ivinco.com    $level = 1;
33*19Syaroslav@ivinco.com    $previouse_title = '';
347Syaroslav@ivinco.com    foreach($metadata['description']['tableofcontents'] as $row){
357Syaroslav@ivinco.com        $sections[$row['hid']] = array(
367Syaroslav@ivinco.com                                    'section' => getSection($id, $row['title']),
37*19Syaroslav@ivinco.com                                    'level' => $row['level'],
38*19Syaroslav@ivinco.com                                    'title' => $row['title']
397Syaroslav@ivinco.com                                    );
40*19Syaroslav@ivinco.com        if ($row['level'] > $level){
41*19Syaroslav@ivinco.com            $sections[$row['hid']]['title_text'] = $previouse_title . " &raquo; ".$row['title'];
42*19Syaroslav@ivinco.com        } else {
43*19Syaroslav@ivinco.com            $sections[$row['hid']]['title_text'] = $row['title'];
44*19Syaroslav@ivinco.com            $previouse_title = $row['title'];
45*19Syaroslav@ivinco.com        }
467Syaroslav@ivinco.com    }
477Syaroslav@ivinco.com    return $sections;
487Syaroslav@ivinco.com}
497Syaroslav@ivinco.com
507Syaroslav@ivinco.comfunction getSection($id, $header)
517Syaroslav@ivinco.com{
5217Syaroslav@ivinco.com    static $cacheInstructions = null;
5317Syaroslav@ivinco.com    static $cacheDoc = null;
5417Syaroslav@ivinco.com
5518Syaroslav@ivinco.com    if (empty($cacheDoc[$id])){
5617Syaroslav@ivinco.com        // Create the parser
5717Syaroslav@ivinco.com        $Parser = & new Doku_Parser();
587Syaroslav@ivinco.com
5917Syaroslav@ivinco.com        // Add the Handler
6017Syaroslav@ivinco.com        $Parser->Handler = & new Doku_Handler();
6117Syaroslav@ivinco.com
6217Syaroslav@ivinco.com        // Load the header mode to find headers
6317Syaroslav@ivinco.com        $Parser->addMode('header',new Doku_Parser_Mode_Header());
6418Syaroslav@ivinco.com        $Parser->addMode('listblock',new Doku_Parser_Mode_ListBlock());
6517Syaroslav@ivinco.com
6617Syaroslav@ivinco.com        // Loads the raw wiki document
6717Syaroslav@ivinco.com        $doc = io_readFile(wikiFN($id));
687Syaroslav@ivinco.com
6917Syaroslav@ivinco.com        // Get a list of instructions
7017Syaroslav@ivinco.com        $instructions = $Parser->parse($doc);
7117Syaroslav@ivinco.com
7217Syaroslav@ivinco.com        unset($Parser->Handler);
7317Syaroslav@ivinco.com        unset($Parser);
7417Syaroslav@ivinco.com
7517Syaroslav@ivinco.com        //free old cache
7618Syaroslav@ivinco.com        $cacheInstructions = null;
7718Syaroslav@ivinco.com        $cacheDoc = null;
787Syaroslav@ivinco.com
7917Syaroslav@ivinco.com        //initialize new cache
8018Syaroslav@ivinco.com        $cacheInstructions[$id] = $instructions;
8118Syaroslav@ivinco.com        $cacheDoc[$id] = $doc;
8217Syaroslav@ivinco.com    } else {
8317Syaroslav@ivinco.com        $instructions = $cacheInstructions[$id];
8417Syaroslav@ivinco.com        $doc = $cacheDoc[$id];
8518Syaroslav@ivinco.com    }
867Syaroslav@ivinco.com
8717Syaroslav@ivinco.com
887Syaroslav@ivinco.com
897Syaroslav@ivinco.com    // Use this to watch when we're inside the section we want
907Syaroslav@ivinco.com    $inSection = FALSE;
917Syaroslav@ivinco.com    $startPos = 0;
927Syaroslav@ivinco.com    $endPos = 0;
937Syaroslav@ivinco.com
947Syaroslav@ivinco.com    // Loop through the instructions
957Syaroslav@ivinco.com    foreach ( $instructions as $instruction ) {
967Syaroslav@ivinco.com
977Syaroslav@ivinco.com        if ( !$inSection ) {
987Syaroslav@ivinco.com
997Syaroslav@ivinco.com            // Look for the header for the "Lists" heading
1007Syaroslav@ivinco.com            if ( $instruction[0] == 'header' &&
1017Syaroslav@ivinco.com                    trim($instruction[1][0]) == $header ) {
1027Syaroslav@ivinco.com
1037Syaroslav@ivinco.com                $startPos = $instruction[2];
1047Syaroslav@ivinco.com                $inSection = TRUE;
1057Syaroslav@ivinco.com            }
1067Syaroslav@ivinco.com        } else {
1077Syaroslav@ivinco.com
1087Syaroslav@ivinco.com            // Look for the end of the section
1097Syaroslav@ivinco.com            if ( $instruction[0] == 'section_close' ) {
1107Syaroslav@ivinco.com                $endPos = $instruction[2];
1117Syaroslav@ivinco.com                break;
1127Syaroslav@ivinco.com            }
1137Syaroslav@ivinco.com        }
1147Syaroslav@ivinco.com    }
1157Syaroslav@ivinco.com
1167Syaroslav@ivinco.com    // Normalize and pad the document in the same way the parse does
1177Syaroslav@ivinco.com    // so that byte indexes with match
1187Syaroslav@ivinco.com    $doc = "\n".str_replace("\r\n","\n",$doc)."\n";
1197Syaroslav@ivinco.com    $section = substr($doc, $startPos, ($endPos-$startPos));
1207Syaroslav@ivinco.com
1217Syaroslav@ivinco.com    return $section;
1227Syaroslav@ivinco.com}
1237Syaroslav@ivinco.com
1247Syaroslav@ivinco.comfunction getCategories($id)
1257Syaroslav@ivinco.com{
1267Syaroslav@ivinco.com    if (empty($id)) return '';
1277Syaroslav@ivinco.com
1287Syaroslav@ivinco.com    if (false === strpos($id, ":")){
1297Syaroslav@ivinco.com        return $id;
1307Syaroslav@ivinco.com    }
1317Syaroslav@ivinco.com
1327Syaroslav@ivinco.com    $ns = explode(":", $id);
1337Syaroslav@ivinco.com    $nsCount = count($ns);
1347Syaroslav@ivinco.com
1357Syaroslav@ivinco.com    $result = '';
1367Syaroslav@ivinco.com    do{
1377Syaroslav@ivinco.com        for($i = 0; $i < $nsCount; $i++){
1387Syaroslav@ivinco.com            $name = $ns[$i];
1397Syaroslav@ivinco.com            $result .= $name;
1407Syaroslav@ivinco.com            if ($i < $nsCount - 1){
1417Syaroslav@ivinco.com                 $result .= ':';
1427Syaroslav@ivinco.com            }
1437Syaroslav@ivinco.com        }
1447Syaroslav@ivinco.com        $result .= ' ';
1457Syaroslav@ivinco.com    }while($nsCount--);
1467Syaroslav@ivinco.com    return $result;
1477Syaroslav@ivinco.com}
1487Syaroslav@ivinco.com
1497Syaroslav@ivinco.com
1507Syaroslav@ivinco.com /**
1517Syaroslav@ivinco.com  * Method return all wiki page names
1527Syaroslav@ivinco.com  * @global array $conf
1537Syaroslav@ivinco.com  * @return array
1547Syaroslav@ivinco.com  */
1557Syaroslav@ivinco.com function getPagesList()
1567Syaroslav@ivinco.com {
1577Syaroslav@ivinco.com    global $conf;
1587Syaroslav@ivinco.com
1597Syaroslav@ivinco.com    $data = array();
1607Syaroslav@ivinco.com    sort($data);
1617Syaroslav@ivinco.com    search($data,$conf['datadir'],'search_allpages','','');
1627Syaroslav@ivinco.com
1637Syaroslav@ivinco.com    return $data;
1648Syaroslav@ivinco.com}
1658Syaroslav@ivinco.com
16615Syaroslav@ivinco.comfunction getNsLinks($id, $keywords, $search)
1678Syaroslav@ivinco.com{
1688Syaroslav@ivinco.com    global $conf;
1698Syaroslav@ivinco.com    $parts = explode(':', $id);
1708Syaroslav@ivinco.com    $count = count($parts);
17115Syaroslav@ivinco.com
1728Syaroslav@ivinco.com    // print intermediate namespace links
1738Syaroslav@ivinco.com    $part = '';
17410Syaroslav@ivinco.com    $data = array();
17510Syaroslav@ivinco.com    $titles = array();
1768Syaroslav@ivinco.com    for($i=0; $i<$count; $i++){
1778Syaroslav@ivinco.com        $part .= $parts[$i].':';
1788Syaroslav@ivinco.com        $page = $part;
1798Syaroslav@ivinco.com        resolve_pageid('',$page,$exists);
18015Syaroslav@ivinco.com
18115Syaroslav@ivinco.com        if (preg_match("#:start$#", $page) && !preg_match("#:start:$#", $part)) {
18213Syaroslav@ivinco.com            $page = substr($page, 0, strpos($page, ":start"));
18313Syaroslav@ivinco.com        };
1848Syaroslav@ivinco.com
1858Syaroslav@ivinco.com        // output
1868Syaroslav@ivinco.com        if ($exists){
18715Syaroslav@ivinco.com            $titles[wl($page)] = $parts[$i];
1888Syaroslav@ivinco.com        } else {
18918Syaroslav@ivinco.com            $titles[wl($page)] = $parts[$i];
19015Syaroslav@ivinco.com        }
19115Syaroslav@ivinco.com        $data[] = array('link' => "?do=sphinxsearch&id={$keywords}".urlencode(" @categories $page"));
1928Syaroslav@ivinco.com    }
19315Syaroslav@ivinco.com    $titleExcerpt = $search->getExcerpt($titles, $search->starQuery($keywords));
19410Syaroslav@ivinco.com    $i = 0;
19510Syaroslav@ivinco.com    foreach ($data as $key => $notused){
19610Syaroslav@ivinco.com        $data[$key]['title'] = $titleExcerpt[$i++];
19710Syaroslav@ivinco.com    }
19810Syaroslav@ivinco.com    return $data;
1997Syaroslav@ivinco.com}