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