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();
327Syaroslav@ivinco.com    foreach($metadata['description']['tableofcontents'] as $row){
337Syaroslav@ivinco.com        $sections[$row['hid']] = array(
347Syaroslav@ivinco.com                                    'section' => getSection($id, $row['title']),
357Syaroslav@ivinco.com                                    'title' => $row['title'],
367Syaroslav@ivinco.com                                    'level' => $row['level']
377Syaroslav@ivinco.com                                    );
387Syaroslav@ivinco.com    }
397Syaroslav@ivinco.com    return $sections;
407Syaroslav@ivinco.com}
417Syaroslav@ivinco.com
427Syaroslav@ivinco.comfunction getSection($id, $header)
437Syaroslav@ivinco.com{
44*17Syaroslav@ivinco.com    static $cacheInstructions = null;
45*17Syaroslav@ivinco.com    static $cacheDoc = null;
46*17Syaroslav@ivinco.com
47*17Syaroslav@ivinco.com    if (!isset($cacheDoc[$id])){
48*17Syaroslav@ivinco.com        // Create the parser
49*17Syaroslav@ivinco.com        $Parser = & new Doku_Parser();
507Syaroslav@ivinco.com
51*17Syaroslav@ivinco.com        // Add the Handler
52*17Syaroslav@ivinco.com        $Parser->Handler = & new Doku_Handler();
53*17Syaroslav@ivinco.com
54*17Syaroslav@ivinco.com        // Load the header mode to find headers
55*17Syaroslav@ivinco.com        $Parser->addMode('header',new Doku_Parser_Mode_Header());
567Syaroslav@ivinco.com
57*17Syaroslav@ivinco.com        // Load the modes which could contain markup that might be
58*17Syaroslav@ivinco.com        // mistaken for a header
59*17Syaroslav@ivinco.com
60*17Syaroslav@ivinco.com        // Loads the raw wiki document
61*17Syaroslav@ivinco.com        $doc = io_readFile(wikiFN($id));
627Syaroslav@ivinco.com
63*17Syaroslav@ivinco.com        // Get a list of instructions
64*17Syaroslav@ivinco.com        $instructions = $Parser->parse($doc);
65*17Syaroslav@ivinco.com
66*17Syaroslav@ivinco.com        unset($Parser->Handler);
67*17Syaroslav@ivinco.com        unset($Parser);
68*17Syaroslav@ivinco.com
69*17Syaroslav@ivinco.com        //free old cache
70*17Syaroslav@ivinco.com        unset($cacheInstructions);
71*17Syaroslav@ivinco.com        unset($cacheDoc);
727Syaroslav@ivinco.com
73*17Syaroslav@ivinco.com        //initialize new cache
74*17Syaroslav@ivinco.com        $cacheInstructions[$id] = &$instructions;
75*17Syaroslav@ivinco.com        $cacheDoc[$id] = &$doc;
76*17Syaroslav@ivinco.com    } else {
77*17Syaroslav@ivinco.com        $instructions = $cacheInstructions[$id];
78*17Syaroslav@ivinco.com        $doc = $cacheDoc[$id];
79*17Syaroslav@ivinco.com    }
807Syaroslav@ivinco.com
81*17Syaroslav@ivinco.com
827Syaroslav@ivinco.com
83*17Syaroslav@ivinco.com
847Syaroslav@ivinco.com
857Syaroslav@ivinco.com    // Use this to watch when we're inside the section we want
867Syaroslav@ivinco.com    $inSection = FALSE;
877Syaroslav@ivinco.com    $startPos = 0;
887Syaroslav@ivinco.com    $endPos = 0;
897Syaroslav@ivinco.com
907Syaroslav@ivinco.com    // Loop through the instructions
917Syaroslav@ivinco.com    foreach ( $instructions as $instruction ) {
927Syaroslav@ivinco.com
937Syaroslav@ivinco.com        if ( !$inSection ) {
947Syaroslav@ivinco.com
957Syaroslav@ivinco.com            // Look for the header for the "Lists" heading
967Syaroslav@ivinco.com            if ( $instruction[0] == 'header' &&
977Syaroslav@ivinco.com                    trim($instruction[1][0]) == $header ) {
987Syaroslav@ivinco.com
997Syaroslav@ivinco.com                $startPos = $instruction[2];
1007Syaroslav@ivinco.com                $inSection = TRUE;
1017Syaroslav@ivinco.com            }
1027Syaroslav@ivinco.com        } else {
1037Syaroslav@ivinco.com
1047Syaroslav@ivinco.com            // Look for the end of the section
1057Syaroslav@ivinco.com            if ( $instruction[0] == 'section_close' ) {
1067Syaroslav@ivinco.com                $endPos = $instruction[2];
1077Syaroslav@ivinco.com                break;
1087Syaroslav@ivinco.com            }
1097Syaroslav@ivinco.com        }
1107Syaroslav@ivinco.com    }
1117Syaroslav@ivinco.com
1127Syaroslav@ivinco.com    // Normalize and pad the document in the same way the parse does
1137Syaroslav@ivinco.com    // so that byte indexes with match
1147Syaroslav@ivinco.com    $doc = "\n".str_replace("\r\n","\n",$doc)."\n";
1157Syaroslav@ivinco.com    $section = substr($doc, $startPos, ($endPos-$startPos));
1167Syaroslav@ivinco.com
1177Syaroslav@ivinco.com    return $section;
1187Syaroslav@ivinco.com}
1197Syaroslav@ivinco.com
1207Syaroslav@ivinco.comfunction getCategories($id)
1217Syaroslav@ivinco.com{
1227Syaroslav@ivinco.com    if (empty($id)) return '';
1237Syaroslav@ivinco.com
1247Syaroslav@ivinco.com    if (false === strpos($id, ":")){
1257Syaroslav@ivinco.com        return $id;
1267Syaroslav@ivinco.com    }
1277Syaroslav@ivinco.com
1287Syaroslav@ivinco.com    $ns = explode(":", $id);
1297Syaroslav@ivinco.com    $nsCount = count($ns);
1307Syaroslav@ivinco.com
1317Syaroslav@ivinco.com    $result = '';
1327Syaroslav@ivinco.com    do{
1337Syaroslav@ivinco.com        for($i = 0; $i < $nsCount; $i++){
1347Syaroslav@ivinco.com            $name = $ns[$i];
1357Syaroslav@ivinco.com            $result .= $name;
1367Syaroslav@ivinco.com            if ($i < $nsCount - 1){
1377Syaroslav@ivinco.com                 $result .= ':';
1387Syaroslav@ivinco.com            }
1397Syaroslav@ivinco.com        }
1407Syaroslav@ivinco.com        $result .= ' ';
1417Syaroslav@ivinco.com    }while($nsCount--);
1427Syaroslav@ivinco.com    return $result;
1437Syaroslav@ivinco.com}
1447Syaroslav@ivinco.com
1457Syaroslav@ivinco.com
1467Syaroslav@ivinco.com /**
1477Syaroslav@ivinco.com  * Method return all wiki page names
1487Syaroslav@ivinco.com  * @global array $conf
1497Syaroslav@ivinco.com  * @return array
1507Syaroslav@ivinco.com  */
1517Syaroslav@ivinco.com function getPagesList()
1527Syaroslav@ivinco.com {
1537Syaroslav@ivinco.com    global $conf;
1547Syaroslav@ivinco.com
1557Syaroslav@ivinco.com    $data = array();
1567Syaroslav@ivinco.com    sort($data);
1577Syaroslav@ivinco.com    search($data,$conf['datadir'],'search_allpages','','');
1587Syaroslav@ivinco.com
1597Syaroslav@ivinco.com    return $data;
1608Syaroslav@ivinco.com}
1618Syaroslav@ivinco.com
16215Syaroslav@ivinco.comfunction getNsLinks($id, $keywords, $search)
1638Syaroslav@ivinco.com{
1648Syaroslav@ivinco.com    global $conf;
1658Syaroslav@ivinco.com    $parts = explode(':', $id);
1668Syaroslav@ivinco.com    $count = count($parts);
16715Syaroslav@ivinco.com
1688Syaroslav@ivinco.com    // print intermediate namespace links
1698Syaroslav@ivinco.com    $part = '';
17010Syaroslav@ivinco.com    $data = array();
17110Syaroslav@ivinco.com    $titles = array();
1728Syaroslav@ivinco.com    for($i=0; $i<$count; $i++){
1738Syaroslav@ivinco.com        $part .= $parts[$i].':';
1748Syaroslav@ivinco.com        $page = $part;
1758Syaroslav@ivinco.com        resolve_pageid('',$page,$exists);
17615Syaroslav@ivinco.com
17715Syaroslav@ivinco.com        if (preg_match("#:start$#", $page) && !preg_match("#:start:$#", $part)) {
17813Syaroslav@ivinco.com            $page = substr($page, 0, strpos($page, ":start"));
17913Syaroslav@ivinco.com        };
1808Syaroslav@ivinco.com
1818Syaroslav@ivinco.com        // output
1828Syaroslav@ivinco.com        if ($exists){
18315Syaroslav@ivinco.com            $titles[wl($page)] = $parts[$i];
1848Syaroslav@ivinco.com        } else {
18510Syaroslav@ivinco.com            continue; //Skip not exists pages
18615Syaroslav@ivinco.com        }
18715Syaroslav@ivinco.com        $data[] = array('link' => "?do=sphinxsearch&id={$keywords}".urlencode(" @categories $page"));
1888Syaroslav@ivinco.com    }
18915Syaroslav@ivinco.com    $titleExcerpt = $search->getExcerpt($titles, $search->starQuery($keywords));
19010Syaroslav@ivinco.com    $i = 0;
19110Syaroslav@ivinco.com    foreach ($data as $key => $notused){
19210Syaroslav@ivinco.com        $data[$key]['title'] = $titleExcerpt[$i++];
19310Syaroslav@ivinco.com    }
19410Syaroslav@ivinco.com    return $data;
1957Syaroslav@ivinco.com}