xref: /plugin/sphinxsearch-was/functions.php (revision 17:19b3aaf13471)
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    static $cacheInstructions = null;
45    static $cacheDoc = null;
46
47    if (!isset($cacheDoc[$id])){
48        // Create the parser
49        $Parser = & new Doku_Parser();
50
51        // Add the Handler
52        $Parser->Handler = & new Doku_Handler();
53
54        // Load the header mode to find headers
55        $Parser->addMode('header',new Doku_Parser_Mode_Header());
56
57        // Load the modes which could contain markup that might be
58        // mistaken for a header
59
60        // Loads the raw wiki document
61        $doc = io_readFile(wikiFN($id));
62
63        // Get a list of instructions
64        $instructions = $Parser->parse($doc);
65
66        unset($Parser->Handler);
67        unset($Parser);
68
69        //free old cache
70        unset($cacheInstructions);
71        unset($cacheDoc);
72
73        //initialize new cache
74        $cacheInstructions[$id] = &$instructions;
75        $cacheDoc[$id] = &$doc;
76    } else {
77        $instructions = $cacheInstructions[$id];
78        $doc = $cacheDoc[$id];
79    }
80
81
82
83
84
85    // Use this to watch when we're inside the section we want
86    $inSection = FALSE;
87    $startPos = 0;
88    $endPos = 0;
89
90    // Loop through the instructions
91    foreach ( $instructions as $instruction ) {
92
93        if ( !$inSection ) {
94
95            // Look for the header for the "Lists" heading
96            if ( $instruction[0] == 'header' &&
97                    trim($instruction[1][0]) == $header ) {
98
99                $startPos = $instruction[2];
100                $inSection = TRUE;
101            }
102        } else {
103
104            // Look for the end of the section
105            if ( $instruction[0] == 'section_close' ) {
106                $endPos = $instruction[2];
107                break;
108            }
109        }
110    }
111
112    // Normalize and pad the document in the same way the parse does
113    // so that byte indexes with match
114    $doc = "\n".str_replace("\r\n","\n",$doc)."\n";
115    $section = substr($doc, $startPos, ($endPos-$startPos));
116
117    return $section;
118}
119
120function getCategories($id)
121{
122    if (empty($id)) return '';
123
124    if (false === strpos($id, ":")){
125        return $id;
126    }
127
128    $ns = explode(":", $id);
129    $nsCount = count($ns);
130
131    $result = '';
132    do{
133        for($i = 0; $i < $nsCount; $i++){
134            $name = $ns[$i];
135            $result .= $name;
136            if ($i < $nsCount - 1){
137                 $result .= ':';
138            }
139        }
140        $result .= ' ';
141    }while($nsCount--);
142    return $result;
143}
144
145
146 /**
147  * Method return all wiki page names
148  * @global array $conf
149  * @return array
150  */
151 function getPagesList()
152 {
153    global $conf;
154
155    $data = array();
156    sort($data);
157    search($data,$conf['datadir'],'search_allpages','','');
158
159    return $data;
160}
161
162function getNsLinks($id, $keywords, $search)
163{
164    global $conf;
165    $parts = explode(':', $id);
166    $count = count($parts);
167
168    // print intermediate namespace links
169    $part = '';
170    $data = array();
171    $titles = array();
172    for($i=0; $i<$count; $i++){
173        $part .= $parts[$i].':';
174        $page = $part;
175        resolve_pageid('',$page,$exists);
176
177        if (preg_match("#:start$#", $page) && !preg_match("#:start:$#", $part)) {
178            $page = substr($page, 0, strpos($page, ":start"));
179        };
180
181        // output
182        if ($exists){
183            $titles[wl($page)] = $parts[$i];
184        } else {
185            continue; //Skip not exists pages
186        }
187        $data[] = array('link' => "?do=sphinxsearch&id={$keywords}".urlencode(" @categories $page"));
188    }
189    $titleExcerpt = $search->getExcerpt($titles, $search->starQuery($keywords));
190    $i = 0;
191    foreach ($data as $key => $notused){
192        $data[$key]['title'] = $titleExcerpt[$i++];
193    }
194    return $data;
195}