xref: /plugin/sphinxsearch-was/functions.php (revision 14:90866a3ce289)
1<?php
2/*
3 * To change this template, choose Tools | Templates
4 * and open the template in the editor.
5 */
6
7function formatXml($data)
8{
9    $xmlFormat = '
10<sphinx:document id="{id}">
11<title><![CDATA[[{title}]]></title>
12<body><![CDATA[[{body}]]></body>
13<categories><![CDATA[[{categories}]]></categories>
14<level>{level}</level>
15<modified>{modified}</modified>
16</sphinx:document>
17
18';
19
20    return str_replace( array('{id}', '{title}', '{body}', '{categories}', '{level}', '{modified}'),
21                        array($data['id'], $data['title'], $data['body'], $data['categories'],
22                             $data['level'], $data['modified']),
23                $xmlFormat
24            );
25}
26
27function getDocumentsByHeadings($id, $metadata)
28{
29    if (empty($metadata) || empty($metadata['description']['tableofcontents'])) return false;
30
31    $sections = array();
32    foreach($metadata['description']['tableofcontents'] as $row){
33        $sections[$row['hid']] = array(
34                                    'section' => getSection($id, $row['title']),
35                                    'title' => $row['title'],
36                                    'level' => $row['level']
37                                    );
38    }
39    return $sections;
40}
41
42function getSection($id, $header)
43{
44    // Create the parser
45    $Parser = & new Doku_Parser();
46
47    // Add the Handler
48    $Parser->Handler = & new Doku_Handler();
49
50    // Load the header mode to find headers
51    $Parser->addMode('header',new Doku_Parser_Mode_Header());
52
53    // Load the modes which could contain markup that might be
54    // mistaken for a header
55    $Parser->addMode('listblock',new Doku_Parser_Mode_ListBlock());
56    $Parser->addMode('preformatted',new Doku_Parser_Mode_Preformatted());
57    $Parser->addMode('table',new Doku_Parser_Mode_Table());
58    $Parser->addMode('unformatted',new Doku_Parser_Mode_Unformatted());
59    $Parser->addMode('php',new Doku_Parser_Mode_PHP());
60    $Parser->addMode('html',new Doku_Parser_Mode_HTML());
61    $Parser->addMode('code',new Doku_Parser_Mode_Code());
62    $Parser->addMode('file',new Doku_Parser_Mode_File());
63    $Parser->addMode('quote',new Doku_Parser_Mode_Quote());
64    $Parser->addMode('footnote',new Doku_Parser_Mode_Footnote());
65    $Parser->addMode('internallink',new Doku_Parser_Mode_InternalLink());
66    $Parser->addMode('media',new Doku_Parser_Mode_Media());
67    $Parser->addMode('externallink',new Doku_Parser_Mode_ExternalLink());
68    $Parser->addMode('windowssharelink',new Doku_Parser_Mode_WindowsShareLink());
69    $Parser->addMode('filelink',new Doku_Parser_Mode_FileLink());
70
71    // Loads the raw wiki document
72    $doc = io_readFile(wikiFN($id));
73
74    // Get a list of instructions
75    $instructions = $Parser->parse($doc);
76
77    unset($Parser);
78
79    // Use this to watch when we're inside the section we want
80    $inSection = FALSE;
81    $startPos = 0;
82    $endPos = 0;
83
84    // Loop through the instructions
85    foreach ( $instructions as $instruction ) {
86
87        if ( !$inSection ) {
88
89            // Look for the header for the "Lists" heading
90            if ( $instruction[0] == 'header' &&
91                    trim($instruction[1][0]) == $header ) {
92
93                $startPos = $instruction[2];
94                $inSection = TRUE;
95            }
96        } else {
97
98            // Look for the end of the section
99            if ( $instruction[0] == 'section_close' ) {
100                $endPos = $instruction[2];
101                break;
102            }
103        }
104    }
105
106    // Normalize and pad the document in the same way the parse does
107    // so that byte indexes with match
108    $doc = "\n".str_replace("\r\n","\n",$doc)."\n";
109    $section = substr($doc, $startPos, ($endPos-$startPos));
110
111    return $section;
112}
113
114function getCategories($id)
115{
116    if (empty($id)) return '';
117
118    if (false === strpos($id, ":")){
119        return $id;
120    }
121
122    $ns = explode(":", $id);
123    $nsCount = count($ns);
124
125    $result = '';
126    do{
127        for($i = 0; $i < $nsCount; $i++){
128            $name = $ns[$i];
129            $result .= $name;
130            if ($i < $nsCount - 1){
131                 $result .= ':';
132            }
133        }
134        $result .= ' ';
135    }while($nsCount--);
136    return $result;
137}
138
139
140 /**
141  * Method return all wiki page names
142  * @global array $conf
143  * @return array
144  */
145 function getPagesList()
146 {
147    global $conf;
148
149    $data = array();
150    sort($data);
151    search($data,$conf['datadir'],'search_allpages','','');
152
153    return $data;
154}
155
156function getNsLinks($id, $query, $search, $queryString)
157{
158    global $conf;
159    $parts = explode(':', $id);
160    $count = count($parts);
161
162    $queryStringValue = $queryString;
163
164    if (false !== ($pos = strpos($queryStringValue, "+%40categories"))){;
165        $queryStringValue = substr($queryStringValue, 0, $pos);
166    }
167
168    if (false !== ($pos = strpos($queryStringValue, " @categories"))){;
169        $queryStringValue = substr($queryStringValue, 0, $pos);
170    }
171
172    // print intermediate namespace links
173    $part = '';
174    $data = array();
175    $titles = array();
176    for($i=0; $i<$count; $i++){
177        $part .= $parts[$i].':';
178        $page = $part;
179        resolve_pageid('',$page,$exists);
180        if (preg_match("#:start$#", $page)) {
181            $page = substr($page, 0, strpos($page, ":start"));
182        };
183
184        // output
185        if ($exists){
186            //$titles[wl($page)] = useHeading('navigation') ? p_get_first_heading($page) : $page;
187            if(!$titles[wl($page)]) {
188                $titles[wl($page)] = $parts[$i];
189            }
190        } else {
191            continue; //Skip not exists pages
192            //$titles[wl($page)] = $parts[$i];
193        }
194        $data[] = array('link' => '?'. $queryStringValue . urlencode(" @categories $page"));
195    }
196    $titleExcerpt = $search->getExcerpt($titles, $search->starQuery($query));
197    $i = 0;
198    foreach ($data as $key => $notused){
199        $data[$key]['title'] = $titleExcerpt[$i++];
200    }
201    return $data;
202}