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();
3219Syaroslav@ivinco.com    $level = 1;
3319Syaroslav@ivinco.com    $previouse_title = '';
347Syaroslav@ivinco.com    foreach($metadata['description']['tableofcontents'] as $row){
357Syaroslav@ivinco.com        $sections[$row['hid']] = array(
3620Syaroslav@ivinco.com                                    'section' => getSectionByTitleLevel($id, $row['title']),
3719Syaroslav@ivinco.com                                    'level' => $row['level'],
3819Syaroslav@ivinco.com                                    'title' => $row['title']
397Syaroslav@ivinco.com                                    );
4019Syaroslav@ivinco.com        if ($row['level'] > $level){
4119Syaroslav@ivinco.com            $sections[$row['hid']]['title_text'] = $previouse_title . " &raquo; ".$row['title'];
4219Syaroslav@ivinco.com        } else {
4319Syaroslav@ivinco.com            $sections[$row['hid']]['title_text'] = $row['title'];
4419Syaroslav@ivinco.com            $previouse_title = $row['title'];
4519Syaroslav@ivinco.com        }
467Syaroslav@ivinco.com    }
477Syaroslav@ivinco.com    return $sections;
487Syaroslav@ivinco.com}
497Syaroslav@ivinco.com
50*21Syaroslav@ivinco.comfunction getSectionByTitleLevel($id, $header, $extended=false)
5120Syaroslav@ivinco.com{
5220Syaroslav@ivinco.com    $headerReg = preg_quote($header, '/');
5320Syaroslav@ivinco.com    $doc = io_readFile(wikiFN($id));
5420Syaroslav@ivinco.com    $regex = "(={1,6})\s*({$headerReg})\s*(={1,6})";
5520Syaroslav@ivinco.com    $section = '';
5620Syaroslav@ivinco.com    if (preg_match("/$regex/i",$doc,$matches)) {
5720Syaroslav@ivinco.com        $startHeader = $matches[0];
5820Syaroslav@ivinco.com        $startHeaderPos = strpos($doc, $startHeader) + strlen($startHeader);
5920Syaroslav@ivinco.com        $endDoc = substr($doc, $startHeaderPos);
6020Syaroslav@ivinco.com
6120Syaroslav@ivinco.com        $regex = '(={1,6})(.*?)(={1,6})';
6220Syaroslav@ivinco.com        if (preg_match("/$regex/i",$endDoc,$matches)) {
6320Syaroslav@ivinco.com            $endHeader = $matches[0];
6420Syaroslav@ivinco.com            $endHeaderPos = strpos($doc, $endHeader);
6520Syaroslav@ivinco.com        } else {
6620Syaroslav@ivinco.com            $endHeaderPos = 0;
6720Syaroslav@ivinco.com        }
6820Syaroslav@ivinco.com        if ($endHeaderPos){
6920Syaroslav@ivinco.com            $section = substr($doc, $startHeaderPos, $endHeaderPos - $startHeaderPos);
7020Syaroslav@ivinco.com        } else {
7120Syaroslav@ivinco.com            $section = substr($doc, $startHeaderPos);
7220Syaroslav@ivinco.com        }
7320Syaroslav@ivinco.com    }
7420Syaroslav@ivinco.com    $section = trim($section);
75*21Syaroslav@ivinco.com    //trying to get next section content if body for first section is empty
76*21Syaroslav@ivinco.com    //working only for extended mode
77*21Syaroslav@ivinco.com    if ($extended && empty($section)){
7820Syaroslav@ivinco.com        $startHeaderPos = $endHeaderPos + strlen($endHeader);
7920Syaroslav@ivinco.com        $endDoc = substr($endDoc, $startHeaderPos);
8020Syaroslav@ivinco.com        $regex = '(={1,6})(.*?)(={1,6})';
8120Syaroslav@ivinco.com        if (preg_match("/$regex/i",$endDoc,$matches)) {
8220Syaroslav@ivinco.com            $endHeader = $matches[0];
8320Syaroslav@ivinco.com            $endHeaderPos = strpos($doc, $endHeader);
8420Syaroslav@ivinco.com        } else {
8520Syaroslav@ivinco.com            $endHeaderPos = 0;
8620Syaroslav@ivinco.com        }
8720Syaroslav@ivinco.com        if ($endHeaderPos){
8820Syaroslav@ivinco.com            $section = substr($doc, $startHeaderPos, $endHeaderPos - $startHeaderPos);
8920Syaroslav@ivinco.com        } else {
9020Syaroslav@ivinco.com            $section = substr($doc, $startHeaderPos);
9120Syaroslav@ivinco.com        }
9220Syaroslav@ivinco.com    }
93*21Syaroslav@ivinco.com    $section = trim($section);
9420Syaroslav@ivinco.com    return $section;
9520Syaroslav@ivinco.com}
9620Syaroslav@ivinco.com
977Syaroslav@ivinco.comfunction getSection($id, $header)
987Syaroslav@ivinco.com{
9917Syaroslav@ivinco.com    static $cacheInstructions = null;
10017Syaroslav@ivinco.com    static $cacheDoc = null;
10117Syaroslav@ivinco.com
10218Syaroslav@ivinco.com    if (empty($cacheDoc[$id])){
10317Syaroslav@ivinco.com        // Create the parser
10417Syaroslav@ivinco.com        $Parser = & new Doku_Parser();
1057Syaroslav@ivinco.com
10617Syaroslav@ivinco.com        // Add the Handler
10717Syaroslav@ivinco.com        $Parser->Handler = & new Doku_Handler();
10817Syaroslav@ivinco.com
10917Syaroslav@ivinco.com        // Load the header mode to find headers
11017Syaroslav@ivinco.com        $Parser->addMode('header',new Doku_Parser_Mode_Header());
11118Syaroslav@ivinco.com        $Parser->addMode('listblock',new Doku_Parser_Mode_ListBlock());
11217Syaroslav@ivinco.com
11317Syaroslav@ivinco.com        // Loads the raw wiki document
11417Syaroslav@ivinco.com        $doc = io_readFile(wikiFN($id));
1157Syaroslav@ivinco.com
11617Syaroslav@ivinco.com        // Get a list of instructions
11717Syaroslav@ivinco.com        $instructions = $Parser->parse($doc);
11817Syaroslav@ivinco.com
11917Syaroslav@ivinco.com        unset($Parser->Handler);
12017Syaroslav@ivinco.com        unset($Parser);
12117Syaroslav@ivinco.com
12217Syaroslav@ivinco.com        //free old cache
12318Syaroslav@ivinco.com        $cacheInstructions = null;
12418Syaroslav@ivinco.com        $cacheDoc = null;
1257Syaroslav@ivinco.com
12617Syaroslav@ivinco.com        //initialize new cache
12718Syaroslav@ivinco.com        $cacheInstructions[$id] = $instructions;
12818Syaroslav@ivinco.com        $cacheDoc[$id] = $doc;
12917Syaroslav@ivinco.com    } else {
13017Syaroslav@ivinco.com        $instructions = $cacheInstructions[$id];
13117Syaroslav@ivinco.com        $doc = $cacheDoc[$id];
13218Syaroslav@ivinco.com    }
1337Syaroslav@ivinco.com
13417Syaroslav@ivinco.com
1357Syaroslav@ivinco.com
1367Syaroslav@ivinco.com    // Use this to watch when we're inside the section we want
1377Syaroslav@ivinco.com    $inSection = FALSE;
1387Syaroslav@ivinco.com    $startPos = 0;
1397Syaroslav@ivinco.com    $endPos = 0;
1407Syaroslav@ivinco.com
1417Syaroslav@ivinco.com    // Loop through the instructions
1427Syaroslav@ivinco.com    foreach ( $instructions as $instruction ) {
1437Syaroslav@ivinco.com
1447Syaroslav@ivinco.com        if ( !$inSection ) {
1457Syaroslav@ivinco.com
1467Syaroslav@ivinco.com            // Look for the header for the "Lists" heading
1477Syaroslav@ivinco.com            if ( $instruction[0] == 'header' &&
1487Syaroslav@ivinco.com                    trim($instruction[1][0]) == $header ) {
1497Syaroslav@ivinco.com
1507Syaroslav@ivinco.com                $startPos = $instruction[2];
1517Syaroslav@ivinco.com                $inSection = TRUE;
1527Syaroslav@ivinco.com            }
1537Syaroslav@ivinco.com        } else {
1547Syaroslav@ivinco.com
1557Syaroslav@ivinco.com            // Look for the end of the section
1567Syaroslav@ivinco.com            if ( $instruction[0] == 'section_close' ) {
1577Syaroslav@ivinco.com                $endPos = $instruction[2];
1587Syaroslav@ivinco.com                break;
1597Syaroslav@ivinco.com            }
1607Syaroslav@ivinco.com        }
1617Syaroslav@ivinco.com    }
1627Syaroslav@ivinco.com
1637Syaroslav@ivinco.com    // Normalize and pad the document in the same way the parse does
1647Syaroslav@ivinco.com    // so that byte indexes with match
1657Syaroslav@ivinco.com    $doc = "\n".str_replace("\r\n","\n",$doc)."\n";
1667Syaroslav@ivinco.com    $section = substr($doc, $startPos, ($endPos-$startPos));
1677Syaroslav@ivinco.com
1687Syaroslav@ivinco.com    return $section;
1697Syaroslav@ivinco.com}
1707Syaroslav@ivinco.com
1717Syaroslav@ivinco.comfunction getCategories($id)
1727Syaroslav@ivinco.com{
1737Syaroslav@ivinco.com    if (empty($id)) return '';
1747Syaroslav@ivinco.com
1757Syaroslav@ivinco.com    if (false === strpos($id, ":")){
1767Syaroslav@ivinco.com        return $id;
1777Syaroslav@ivinco.com    }
1787Syaroslav@ivinco.com
1797Syaroslav@ivinco.com    $ns = explode(":", $id);
1807Syaroslav@ivinco.com    $nsCount = count($ns);
1817Syaroslav@ivinco.com
1827Syaroslav@ivinco.com    $result = '';
1837Syaroslav@ivinco.com    do{
1847Syaroslav@ivinco.com        for($i = 0; $i < $nsCount; $i++){
1857Syaroslav@ivinco.com            $name = $ns[$i];
1867Syaroslav@ivinco.com            $result .= $name;
1877Syaroslav@ivinco.com            if ($i < $nsCount - 1){
1887Syaroslav@ivinco.com                 $result .= ':';
1897Syaroslav@ivinco.com            }
1907Syaroslav@ivinco.com        }
1917Syaroslav@ivinco.com        $result .= ' ';
1927Syaroslav@ivinco.com    }while($nsCount--);
1937Syaroslav@ivinco.com    return $result;
1947Syaroslav@ivinco.com}
1957Syaroslav@ivinco.com
1967Syaroslav@ivinco.com
1977Syaroslav@ivinco.com /**
1987Syaroslav@ivinco.com  * Method return all wiki page names
1997Syaroslav@ivinco.com  * @global array $conf
2007Syaroslav@ivinco.com  * @return array
2017Syaroslav@ivinco.com  */
2027Syaroslav@ivinco.com function getPagesList()
2037Syaroslav@ivinco.com {
2047Syaroslav@ivinco.com    global $conf;
2057Syaroslav@ivinco.com
2067Syaroslav@ivinco.com    $data = array();
2077Syaroslav@ivinco.com    sort($data);
2087Syaroslav@ivinco.com    search($data,$conf['datadir'],'search_allpages','','');
2097Syaroslav@ivinco.com
2107Syaroslav@ivinco.com    return $data;
2118Syaroslav@ivinco.com}
2128Syaroslav@ivinco.com
21315Syaroslav@ivinco.comfunction getNsLinks($id, $keywords, $search)
2148Syaroslav@ivinco.com{
2158Syaroslav@ivinco.com    global $conf;
2168Syaroslav@ivinco.com    $parts = explode(':', $id);
2178Syaroslav@ivinco.com    $count = count($parts);
21815Syaroslav@ivinco.com
2198Syaroslav@ivinco.com    // print intermediate namespace links
2208Syaroslav@ivinco.com    $part = '';
22110Syaroslav@ivinco.com    $data = array();
22210Syaroslav@ivinco.com    $titles = array();
2238Syaroslav@ivinco.com    for($i=0; $i<$count; $i++){
2248Syaroslav@ivinco.com        $part .= $parts[$i].':';
2258Syaroslav@ivinco.com        $page = $part;
2268Syaroslav@ivinco.com        resolve_pageid('',$page,$exists);
22715Syaroslav@ivinco.com
22815Syaroslav@ivinco.com        if (preg_match("#:start$#", $page) && !preg_match("#:start:$#", $part)) {
22913Syaroslav@ivinco.com            $page = substr($page, 0, strpos($page, ":start"));
23013Syaroslav@ivinco.com        };
2318Syaroslav@ivinco.com
2328Syaroslav@ivinco.com        // output
2338Syaroslav@ivinco.com        if ($exists){
23415Syaroslav@ivinco.com            $titles[wl($page)] = $parts[$i];
2358Syaroslav@ivinco.com        } else {
23618Syaroslav@ivinco.com            $titles[wl($page)] = $parts[$i];
23715Syaroslav@ivinco.com        }
23815Syaroslav@ivinco.com        $data[] = array('link' => "?do=sphinxsearch&id={$keywords}".urlencode(" @categories $page"));
2398Syaroslav@ivinco.com    }
24015Syaroslav@ivinco.com    $titleExcerpt = $search->getExcerpt($titles, $search->starQuery($keywords));
24110Syaroslav@ivinco.com    $i = 0;
24210Syaroslav@ivinco.com    foreach ($data as $key => $notused){
24310Syaroslav@ivinco.com        $data[$key]['title'] = $titleExcerpt[$i++];
24410Syaroslav@ivinco.com    }
24510Syaroslav@ivinco.com    return $data;
2467Syaroslav@ivinco.com}