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