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<creator>{creator}</creator>
177Syaroslav@ivinco.com</sphinx:document>
187Syaroslav@ivinco.com
197Syaroslav@ivinco.com';
207Syaroslav@ivinco.com
217Syaroslav@ivinco.com    return str_replace( array('{id}', '{title}', '{body}', '{categories}', '{level}', '{modified}', '{creator}'),
227Syaroslav@ivinco.com                        array($data['id'], $data['title'], $data['body'], $data['categories'],
237Syaroslav@ivinco.com                             $data['level'], $data['modified'], $data['creator']),
247Syaroslav@ivinco.com                $xmlFormat
257Syaroslav@ivinco.com            );
267Syaroslav@ivinco.com}
277Syaroslav@ivinco.com
287Syaroslav@ivinco.comfunction getDocumentsByHeadings($id, $metadata)
297Syaroslav@ivinco.com{
307Syaroslav@ivinco.com    if (empty($metadata) || empty($metadata['description']['tableofcontents'])) return false;
317Syaroslav@ivinco.com
327Syaroslav@ivinco.com    $sections = array();
337Syaroslav@ivinco.com    foreach($metadata['description']['tableofcontents'] as $row){
347Syaroslav@ivinco.com        $sections[$row['hid']] = array(
357Syaroslav@ivinco.com                                    'section' => getSection($id, $row['title']),
367Syaroslav@ivinco.com                                    'title' => $row['title'],
377Syaroslav@ivinco.com                                    'level' => $row['level']
387Syaroslav@ivinco.com                                    );
397Syaroslav@ivinco.com    }
407Syaroslav@ivinco.com    return $sections;
417Syaroslav@ivinco.com}
427Syaroslav@ivinco.com
437Syaroslav@ivinco.comfunction getSection($id, $header)
447Syaroslav@ivinco.com{
457Syaroslav@ivinco.com    // Create the parser
467Syaroslav@ivinco.com    $Parser = & new Doku_Parser();
477Syaroslav@ivinco.com
487Syaroslav@ivinco.com    // Add the Handler
497Syaroslav@ivinco.com    $Parser->Handler = & new Doku_Handler();
507Syaroslav@ivinco.com
517Syaroslav@ivinco.com    // Load the header mode to find headers
527Syaroslav@ivinco.com    $Parser->addMode('header',new Doku_Parser_Mode_Header());
537Syaroslav@ivinco.com
547Syaroslav@ivinco.com    // Load the modes which could contain markup that might be
557Syaroslav@ivinco.com    // mistaken for a header
567Syaroslav@ivinco.com    $Parser->addMode('listblock',new Doku_Parser_Mode_ListBlock());
577Syaroslav@ivinco.com    $Parser->addMode('preformatted',new Doku_Parser_Mode_Preformatted());
587Syaroslav@ivinco.com    $Parser->addMode('table',new Doku_Parser_Mode_Table());
597Syaroslav@ivinco.com    $Parser->addMode('unformatted',new Doku_Parser_Mode_Unformatted());
607Syaroslav@ivinco.com    $Parser->addMode('php',new Doku_Parser_Mode_PHP());
617Syaroslav@ivinco.com    $Parser->addMode('html',new Doku_Parser_Mode_HTML());
627Syaroslav@ivinco.com    $Parser->addMode('code',new Doku_Parser_Mode_Code());
637Syaroslav@ivinco.com    $Parser->addMode('file',new Doku_Parser_Mode_File());
647Syaroslav@ivinco.com    $Parser->addMode('quote',new Doku_Parser_Mode_Quote());
657Syaroslav@ivinco.com    $Parser->addMode('footnote',new Doku_Parser_Mode_Footnote());
667Syaroslav@ivinco.com    $Parser->addMode('internallink',new Doku_Parser_Mode_InternalLink());
677Syaroslav@ivinco.com    $Parser->addMode('media',new Doku_Parser_Mode_Media());
687Syaroslav@ivinco.com    $Parser->addMode('externallink',new Doku_Parser_Mode_ExternalLink());
697Syaroslav@ivinco.com    $Parser->addMode('windowssharelink',new Doku_Parser_Mode_WindowsShareLink());
707Syaroslav@ivinco.com    $Parser->addMode('filelink',new Doku_Parser_Mode_FileLink());
717Syaroslav@ivinco.com
727Syaroslav@ivinco.com    // Loads the raw wiki document
737Syaroslav@ivinco.com    $doc = io_readFile(wikiFN($id));
747Syaroslav@ivinco.com
757Syaroslav@ivinco.com    // Get a list of instructions
767Syaroslav@ivinco.com    $instructions = $Parser->parse($doc);
777Syaroslav@ivinco.com
787Syaroslav@ivinco.com    unset($Parser);
797Syaroslav@ivinco.com
807Syaroslav@ivinco.com    // Use this to watch when we're inside the section we want
817Syaroslav@ivinco.com    $inSection = FALSE;
827Syaroslav@ivinco.com    $startPos = 0;
837Syaroslav@ivinco.com    $endPos = 0;
847Syaroslav@ivinco.com
857Syaroslav@ivinco.com    // Loop through the instructions
867Syaroslav@ivinco.com    foreach ( $instructions as $instruction ) {
877Syaroslav@ivinco.com
887Syaroslav@ivinco.com        if ( !$inSection ) {
897Syaroslav@ivinco.com
907Syaroslav@ivinco.com            // Look for the header for the "Lists" heading
917Syaroslav@ivinco.com            if ( $instruction[0] == 'header' &&
927Syaroslav@ivinco.com                    trim($instruction[1][0]) == $header ) {
937Syaroslav@ivinco.com
947Syaroslav@ivinco.com                $startPos = $instruction[2];
957Syaroslav@ivinco.com                $inSection = TRUE;
967Syaroslav@ivinco.com            }
977Syaroslav@ivinco.com        } else {
987Syaroslav@ivinco.com
997Syaroslav@ivinco.com            // Look for the end of the section
1007Syaroslav@ivinco.com            if ( $instruction[0] == 'section_close' ) {
1017Syaroslav@ivinco.com                $endPos = $instruction[2];
1027Syaroslav@ivinco.com                break;
1037Syaroslav@ivinco.com            }
1047Syaroslav@ivinco.com        }
1057Syaroslav@ivinco.com    }
1067Syaroslav@ivinco.com
1077Syaroslav@ivinco.com    // Normalize and pad the document in the same way the parse does
1087Syaroslav@ivinco.com    // so that byte indexes with match
1097Syaroslav@ivinco.com    $doc = "\n".str_replace("\r\n","\n",$doc)."\n";
1107Syaroslav@ivinco.com    $section = substr($doc, $startPos, ($endPos-$startPos));
1117Syaroslav@ivinco.com
1127Syaroslav@ivinco.com    return $section;
1137Syaroslav@ivinco.com}
1147Syaroslav@ivinco.com
1157Syaroslav@ivinco.comfunction getCategories($id)
1167Syaroslav@ivinco.com{
1177Syaroslav@ivinco.com    if (empty($id)) return '';
1187Syaroslav@ivinco.com
1197Syaroslav@ivinco.com    if (false === strpos($id, ":")){
1207Syaroslav@ivinco.com        return $id;
1217Syaroslav@ivinco.com    }
1227Syaroslav@ivinco.com
1237Syaroslav@ivinco.com    $ns = explode(":", $id);
1247Syaroslav@ivinco.com    $nsCount = count($ns);
1257Syaroslav@ivinco.com
1267Syaroslav@ivinco.com    $result = '';
1277Syaroslav@ivinco.com    do{
1287Syaroslav@ivinco.com        for($i = 0; $i < $nsCount; $i++){
1297Syaroslav@ivinco.com            $name = $ns[$i];
1307Syaroslav@ivinco.com            $result .= $name;
1317Syaroslav@ivinco.com            if ($i < $nsCount - 1){
1327Syaroslav@ivinco.com                 $result .= ':';
1337Syaroslav@ivinco.com            }
1347Syaroslav@ivinco.com        }
1357Syaroslav@ivinco.com        $result .= ' ';
1367Syaroslav@ivinco.com    }while($nsCount--);
1377Syaroslav@ivinco.com    return $result;
1387Syaroslav@ivinco.com}
1397Syaroslav@ivinco.com
1407Syaroslav@ivinco.com
1417Syaroslav@ivinco.com /**
1427Syaroslav@ivinco.com  * Method return all wiki page names
1437Syaroslav@ivinco.com  * @global array $conf
1447Syaroslav@ivinco.com  * @return array
1457Syaroslav@ivinco.com  */
1467Syaroslav@ivinco.com function getPagesList()
1477Syaroslav@ivinco.com {
1487Syaroslav@ivinco.com    global $conf;
1497Syaroslav@ivinco.com
1507Syaroslav@ivinco.com    $data = array();
1517Syaroslav@ivinco.com    sort($data);
1527Syaroslav@ivinco.com    search($data,$conf['datadir'],'search_allpages','','');
1537Syaroslav@ivinco.com
1547Syaroslav@ivinco.com    return $data;
1558Syaroslav@ivinco.com}
1568Syaroslav@ivinco.com
157*10Syaroslav@ivinco.comfunction getNsLinks($id, $query, $search, $queryString)
1588Syaroslav@ivinco.com{
1598Syaroslav@ivinco.com    global $conf;
1608Syaroslav@ivinco.com    $parts = explode(':', $id);
1618Syaroslav@ivinco.com    $count = count($parts);
1628Syaroslav@ivinco.com
163*10Syaroslav@ivinco.com    $queryStringValue = $queryString;
164*10Syaroslav@ivinco.com
165*10Syaroslav@ivinco.com    if (false !== ($pos = strpos($queryStringValue, "+%40categories"))){;
166*10Syaroslav@ivinco.com        $queryStringValue = substr($queryStringValue, 0, $pos);
167*10Syaroslav@ivinco.com    }
168*10Syaroslav@ivinco.com
1698Syaroslav@ivinco.com    // print intermediate namespace links
1708Syaroslav@ivinco.com    $part = '';
171*10Syaroslav@ivinco.com    $data = array();
172*10Syaroslav@ivinco.com    $titles = array();
1738Syaroslav@ivinco.com    for($i=0; $i<$count; $i++){
1748Syaroslav@ivinco.com        $part .= $parts[$i].':';
1758Syaroslav@ivinco.com        $page = $part;
1768Syaroslav@ivinco.com        resolve_pageid('',$page,$exists);
1778Syaroslav@ivinco.com        if ($page == $conf['start']) continue; // Skip startpage
1788Syaroslav@ivinco.com
1798Syaroslav@ivinco.com        // output
1808Syaroslav@ivinco.com        if ($exists){
181*10Syaroslav@ivinco.com            $titles[wl($page)] = useHeading('navigation') ? p_get_first_heading($page) : $page;
182*10Syaroslav@ivinco.com            if(!$titles[wl($page)]) {
183*10Syaroslav@ivinco.com                $titles[wl($page)] = $parts[$i];
184*10Syaroslav@ivinco.com            }
1858Syaroslav@ivinco.com        } else {
186*10Syaroslav@ivinco.com            continue; //Skip not exists pages
187*10Syaroslav@ivinco.com            $titles[wl($page)] = $parts[$i];
188*10Syaroslav@ivinco.com        }
189*10Syaroslav@ivinco.com        $data[] = array('link' => '?'. $queryStringValue . urlencode(" @categories $page"));
1908Syaroslav@ivinco.com    }
191*10Syaroslav@ivinco.com    $titleExcerpt = $search->getExcerpt($titles, $search->starQuery($query));
192*10Syaroslav@ivinco.com    $i = 0;
193*10Syaroslav@ivinco.com    foreach ($data as $key => $notused){
194*10Syaroslav@ivinco.com        $data[$key]['title'] = $titleExcerpt[$i++];
195*10Syaroslav@ivinco.com    }
196*10Syaroslav@ivinco.com    return $data;
1977Syaroslav@ivinco.com}