xref: /plugin/sphinxsearch-was/functions.php (revision 12:0f4db8442a6a)
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    // 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        //if ($page == $conf['start']) continue; // Skip startpage
177
178        // output
179        if ($exists){
180            $titles[wl($page)] = useHeading('navigation') ? p_get_first_heading($page) : $page;
181            if(!$titles[wl($page)]) {
182                $titles[wl($page)] = $parts[$i];
183            }
184        } else {
185            continue; //Skip not exists pages
186            $titles[wl($page)] = $parts[$i];
187        }
188        $data[] = array('link' => '?'. $queryStringValue . urlencode(" @categories $page"));
189    }
190    $titleExcerpt = $search->getExcerpt($titles, $search->starQuery($query));
191    $i = 0;
192    foreach ($data as $key => $notused){
193        $data[$key]['title'] = $titleExcerpt[$i++];
194    }
195    return $data;
196}