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