xref: /plugin/sphinxsearch-was/functions.php (revision 130:07256766c697)
17Syaroslav@ivinco.com<?php
2*130Syaroslav@ivinco.com/*
37Syaroslav@ivinco.com * To change this template, choose Tools | Templates
47Syaroslav@ivinco.com * and open the template in the editor.
57Syaroslav@ivinco.com */
67Syaroslav@ivinco.com
77Syaroslav@ivinco.comfunction formatXml($data)
87Syaroslav@ivinco.com{
97Syaroslav@ivinco.com    $xmlFormat = '
107Syaroslav@ivinco.com<sphinx:document id="{id}">
117Syaroslav@ivinco.com<title><![CDATA[[{title}]]></title>
127Syaroslav@ivinco.com<body><![CDATA[[{body}]]></body>
1380Syaroslav@ivinco.com<namespace><![CDATA[[{namespace}]]></namespace>
1480Syaroslav@ivinco.com<pagename><![CDATA[[{pagename}]]></pagename>
157Syaroslav@ivinco.com<level>{level}</level>
1637Syaroslav@ivinco.com<modified>{modified}</modified>
177Syaroslav@ivinco.com</sphinx:document>
187Syaroslav@ivinco.com
197Syaroslav@ivinco.com';
207Syaroslav@ivinco.com
2180Syaroslav@ivinco.com    return str_replace(
2280Syaroslav@ivinco.com        array('{id}', '{title}', '{body}', '{namespace}', '{pagename}', '{level}', '{modified}'),
2380Syaroslav@ivinco.com        array(
2480Syaroslav@ivinco.com            $data['id'], escapeTextValue($data['title_to_index']),
2580Syaroslav@ivinco.com            escapeTextValue($data['body']),
2612Syaroslav@ivinco.com            escapeTextValue($data['namespace']),
277Syaroslav@ivinco.com            escapeTextValue($data['pagename']),
287Syaroslav@ivinco.com            $data['level'], $data['modified']
297Syaroslav@ivinco.com        ),
307Syaroslav@ivinco.com        $xmlFormat
3137Syaroslav@ivinco.com    );
3237Syaroslav@ivinco.com}
3337Syaroslav@ivinco.com
3437Syaroslav@ivinco.comfunction escapeTextValue($value)
3537Syaroslav@ivinco.com{
3637Syaroslav@ivinco.com    if ("" === $value) {
3758Syaroslav@ivinco.com        return "";
3837Syaroslav@ivinco.com    }
3937Syaroslav@ivinco.com    //$value = mb_convert_encoding($value,'UTF-8','ISO-8859-1');
4037Syaroslav@ivinco.com    $value = strip_tags($value);
4137Syaroslav@ivinco.com    $value = stripInvalidXml($value);
4237Syaroslav@ivinco.com    return str_replace("]]>", "]]><![CDATA[]]]]><![CDATA[>]]><![CDATA[", $value);
4337Syaroslav@ivinco.com}
4437Syaroslav@ivinco.com
4537Syaroslav@ivinco.comfunction stripInvalidXml($value)
4637Syaroslav@ivinco.com{
4737Syaroslav@ivinco.com    $ret = "";
4837Syaroslav@ivinco.com    if (empty($value)) {
4937Syaroslav@ivinco.com        return $ret;
5037Syaroslav@ivinco.com    }
5137Syaroslav@ivinco.com
5237Syaroslav@ivinco.com    $current = null;
5337Syaroslav@ivinco.com    $length = strlen($value);
5437Syaroslav@ivinco.com    for ($i = 0; $i < $length; $i++) {
5537Syaroslav@ivinco.com        $current = ord($value{
5637Syaroslav@ivinco.com        $i});
5737Syaroslav@ivinco.com        if (($current == 0x9) || ($current == 0xA) || ($current == 0xD) || (($current >= 0x20) && ($current <= 0xD7FF)) || (($current >= 0xE000) && ($current <= 0xFFFD)) || (($current >= 0x10000) && ($current <= 0x10FFFF))
5837Syaroslav@ivinco.com        ) {
5937Syaroslav@ivinco.com            $ret .= chr($current);
6037Syaroslav@ivinco.com        } else {
6137Syaroslav@ivinco.com            $ret .= " ";
6237Syaroslav@ivinco.com        }
6337Syaroslav@ivinco.com    }
6437Syaroslav@ivinco.com    return $ret;
6537Syaroslav@ivinco.com}
6637Syaroslav@ivinco.com
6737Syaroslav@ivinco.comfunction getDocumentsByHeadings($id, $metadata)
6837Syaroslav@ivinco.com{
6937Syaroslav@ivinco.com    if (empty($metadata) || empty($metadata['description']['tableofcontents'])) return false;
7037Syaroslav@ivinco.com
7137Syaroslav@ivinco.com    $sections = array();
7237Syaroslav@ivinco.com    $level = 1;
737Syaroslav@ivinco.com    $previouse_title = '';
747Syaroslav@ivinco.com    $firstSection = true;
757Syaroslav@ivinco.com    foreach ($metadata['description']['tableofcontents'] as $row) {
767Syaroslav@ivinco.com        if ($firstSection) {
777Syaroslav@ivinco.com            $zerocontent  = getZeroSectionContent($id, $row['title']);
7819Syaroslav@ivinco.com            if ($zerocontent) {
7919Syaroslav@ivinco.com                $sections[$id] = array(
80*130Syaroslav@ivinco.com                    'section' => $zerocontent,
817Syaroslav@ivinco.com                    'level' => 0,
82*130Syaroslav@ivinco.com                    'title' => $id,
83*130Syaroslav@ivinco.com                    'title_to_index' => $id
84*130Syaroslav@ivinco.com                );
85*130Syaroslav@ivinco.com            }
86*130Syaroslav@ivinco.com            $firstSection = false;
87*130Syaroslav@ivinco.com        }
88*130Syaroslav@ivinco.com        $sections[$row['hid']] = array(
89*130Syaroslav@ivinco.com            'section' => getSectionByTitleLevel($id, $row['title'], false),
90*130Syaroslav@ivinco.com            'level' => $row['level'],
91*130Syaroslav@ivinco.com            'title' => $row['title']
92*130Syaroslav@ivinco.com        );
93*130Syaroslav@ivinco.com        if ($row['level'] > $level && !empty($previouse_title)) {
947Syaroslav@ivinco.com            $sections[$row['hid']]['title_text'] = $previouse_title . " &raquo; " . $row['title'];
95*130Syaroslav@ivinco.com        } else {
9619Syaroslav@ivinco.com            $sections[$row['hid']]['title_text'] = $row['title'];
9719Syaroslav@ivinco.com            $previouse_title = $row['title'];
987Syaroslav@ivinco.com        }
9956Syaroslav@ivinco.com        $sections[$row['hid']]['title_to_index'] = $row['title'];
10019Syaroslav@ivinco.com    }
10119Syaroslav@ivinco.com    return $sections;
10219Syaroslav@ivinco.com}
10319Syaroslav@ivinco.com
10419Syaroslav@ivinco.comfunction getZeroSectionContent($id, $header)
10556Syaroslav@ivinco.com{
1067Syaroslav@ivinco.com    $headerReg = preg_quote($header, '/');
1077Syaroslav@ivinco.com    $regex = "(={1,6})\s*({$headerReg})\s*(={1,6})";
1087Syaroslav@ivinco.com    $doc = io_readFile(wikiFN($id));
1097Syaroslav@ivinco.com    $matches = array();
110*130Syaroslav@ivinco.com    if (!preg_match("/$regex/i", $doc, $matches)) {
111*130Syaroslav@ivinco.com        return false;
112*130Syaroslav@ivinco.com    }
113*130Syaroslav@ivinco.com    if (empty($matches[1])) {
114*130Syaroslav@ivinco.com        return false;
115*130Syaroslav@ivinco.com    }
116*130Syaroslav@ivinco.com    $end = strpos($doc, $matches[1]);
117*130Syaroslav@ivinco.com    if (!$end) {
118*130Syaroslav@ivinco.com        return false;
119*130Syaroslav@ivinco.com    }
120*130Syaroslav@ivinco.com    $zerocontent = substr($doc, 0, $end);
121*130Syaroslav@ivinco.com    return $zerocontent;
122*130Syaroslav@ivinco.com}
123*130Syaroslav@ivinco.com
124*130Syaroslav@ivinco.comfunction getSectionByTitleLevel($id, $header, $extended = false)
125*130Syaroslav@ivinco.com{
126*130Syaroslav@ivinco.com    $headerReg = preg_quote($header, '/');
127*130Syaroslav@ivinco.com    $doc = io_readFile(wikiFN($id));
128*130Syaroslav@ivinco.com    $regex = "(={1,6})\s*({$headerReg})\s*(={1,6})";
129*130Syaroslav@ivinco.com    $section = '';
13021Syaroslav@ivinco.com    if (preg_match("/$regex/i", $doc, $matches)) {
13120Syaroslav@ivinco.com        $startHeader = $matches[0];
13220Syaroslav@ivinco.com        $startHeaderPos = strpos($doc, $startHeader) + strlen($startHeader);
13320Syaroslav@ivinco.com        $endDoc = substr($doc, $startHeaderPos);
134*130Syaroslav@ivinco.com
13520Syaroslav@ivinco.com        $regex = '(={4,6})(.*?)(={4,6})';
13620Syaroslav@ivinco.com        if (preg_match("/$regex/i", $endDoc, $matches)) {
13720Syaroslav@ivinco.com            $endHeader = $matches[0];
13820Syaroslav@ivinco.com            $endHeaderPos = strpos($doc, $endHeader);
13920Syaroslav@ivinco.com        } else {
14020Syaroslav@ivinco.com            $endHeaderPos = 0;
14196Syaroslav@ivinco.com        }
14220Syaroslav@ivinco.com        if ($endHeaderPos) {
14320Syaroslav@ivinco.com            $section = substr($doc, $startHeaderPos, $endHeaderPos - $startHeaderPos);
14420Syaroslav@ivinco.com        } else {
14520Syaroslav@ivinco.com            $section = substr($doc, $startHeaderPos);
14620Syaroslav@ivinco.com        }
14720Syaroslav@ivinco.com    }
14820Syaroslav@ivinco.com    $section = trim($section);
14920Syaroslav@ivinco.com    //trying to get next section content if body for first section is empty
15020Syaroslav@ivinco.com    //working only for extended mode
15120Syaroslav@ivinco.com    if ($extended && empty($section)) {
152*130Syaroslav@ivinco.com        $startHeaderPos = $endHeaderPos + strlen($endHeader);
15320Syaroslav@ivinco.com        $endDoc = substr($endDoc, $startHeaderPos);
15420Syaroslav@ivinco.com        $regex = '(={4,6})(.*?)(={4,6})';
15521Syaroslav@ivinco.com        if (preg_match("/$regex/i", $endDoc, $matches)) {
15621Syaroslav@ivinco.com            $endHeader = $matches[0];
15721Syaroslav@ivinco.com            $endHeaderPos = strpos($doc, $endHeader);
15820Syaroslav@ivinco.com        } else {
15920Syaroslav@ivinco.com            $endHeaderPos = 0;
16096Syaroslav@ivinco.com        }
16120Syaroslav@ivinco.com        if ($endHeaderPos) {
16220Syaroslav@ivinco.com            $section = substr($doc, $startHeaderPos, $endHeaderPos - $startHeaderPos);
16320Syaroslav@ivinco.com        } else {
16420Syaroslav@ivinco.com            $section = substr($doc, $startHeaderPos);
16520Syaroslav@ivinco.com        }
16620Syaroslav@ivinco.com    }
16720Syaroslav@ivinco.com    $section = trim($section);
16820Syaroslav@ivinco.com    return $section;
16920Syaroslav@ivinco.com}
17020Syaroslav@ivinco.com
17120Syaroslav@ivinco.comfunction getSection($id, $header)
17220Syaroslav@ivinco.com{
17321Syaroslav@ivinco.com    static $cacheInstructions = null;
17420Syaroslav@ivinco.com    static $cacheDoc = null;
17520Syaroslav@ivinco.com
17620Syaroslav@ivinco.com    if (empty($cacheDoc[$id])) {
1777Syaroslav@ivinco.com        // Create the parser
1787Syaroslav@ivinco.com        $Parser = new Doku_Parser();
17917Syaroslav@ivinco.com
18017Syaroslav@ivinco.com        // Add the Handler
18117Syaroslav@ivinco.com        $Parser->Handler = new Doku_Handler();
18218Syaroslav@ivinco.com
18317Syaroslav@ivinco.com        // Load the header mode to find headers
18417Syaroslav@ivinco.com        $Parser->addMode('header', new Doku_Parser_Mode_Header());
1857Syaroslav@ivinco.com        $Parser->addMode('listblock', new Doku_Parser_Mode_ListBlock());
18617Syaroslav@ivinco.com
18717Syaroslav@ivinco.com        // Loads the raw wiki document
18817Syaroslav@ivinco.com        $doc = io_readFile(wikiFN($id));
18917Syaroslav@ivinco.com
19017Syaroslav@ivinco.com        // Get a list of instructions
19118Syaroslav@ivinco.com        $instructions = $Parser->parse($doc);
19217Syaroslav@ivinco.com
19317Syaroslav@ivinco.com        unset($Parser->Handler);
19417Syaroslav@ivinco.com        unset($Parser);
1957Syaroslav@ivinco.com
19617Syaroslav@ivinco.com        //free old cache
19717Syaroslav@ivinco.com        $cacheInstructions = null;
19817Syaroslav@ivinco.com        $cacheDoc = null;
19917Syaroslav@ivinco.com
20017Syaroslav@ivinco.com        //initialize new cache
20117Syaroslav@ivinco.com        $cacheInstructions[$id] = $instructions;
20217Syaroslav@ivinco.com        $cacheDoc[$id] = $doc;
20318Syaroslav@ivinco.com    } else {
20418Syaroslav@ivinco.com        $instructions = $cacheInstructions[$id];
2057Syaroslav@ivinco.com        $doc = $cacheDoc[$id];
20617Syaroslav@ivinco.com    }
20718Syaroslav@ivinco.com
20818Syaroslav@ivinco.com
20917Syaroslav@ivinco.com
21017Syaroslav@ivinco.com    // Use this to watch when we're inside the section we want
21117Syaroslav@ivinco.com    $inSection = FALSE;
212*130Syaroslav@ivinco.com    $startPos = 0;
2137Syaroslav@ivinco.com    $endPos = 0;
214*130Syaroslav@ivinco.com
2157Syaroslav@ivinco.com    // Loop through the instructions
2167Syaroslav@ivinco.com    foreach ($instructions as $instruction) {
2177Syaroslav@ivinco.com
2187Syaroslav@ivinco.com        if (!$inSection) {
2197Syaroslav@ivinco.com
2207Syaroslav@ivinco.com            // Look for the header for the "Lists" heading
2217Syaroslav@ivinco.com            if (
2227Syaroslav@ivinco.com                $instruction[0] == 'header' &&
2237Syaroslav@ivinco.com                trim($instruction[1][0]) == $header
2247Syaroslav@ivinco.com            ) {
2257Syaroslav@ivinco.com
2267Syaroslav@ivinco.com                $startPos = $instruction[2];
2277Syaroslav@ivinco.com                $inSection = TRUE;
2287Syaroslav@ivinco.com            }
2297Syaroslav@ivinco.com        } else {
2307Syaroslav@ivinco.com
2317Syaroslav@ivinco.com            // Look for the end of the section
2327Syaroslav@ivinco.com            if ($instruction[0] == 'section_close') {
2337Syaroslav@ivinco.com                $endPos = $instruction[2];
2347Syaroslav@ivinco.com                break;
2357Syaroslav@ivinco.com            }
2367Syaroslav@ivinco.com        }
2377Syaroslav@ivinco.com    }
2387Syaroslav@ivinco.com
2397Syaroslav@ivinco.com    // Normalize and pad the document in the same way the parse does
2407Syaroslav@ivinco.com    // so that byte indexes with match
2417Syaroslav@ivinco.com    $doc = "\n" . str_replace("\r\n", "\n", $doc) . "\n";
2427Syaroslav@ivinco.com    $section = substr($doc, $startPos, ($endPos - $startPos));
2437Syaroslav@ivinco.com
2447Syaroslav@ivinco.com    return $section;
2457Syaroslav@ivinco.com}
2467Syaroslav@ivinco.com
2477Syaroslav@ivinco.comfunction getCategories($id)
2487Syaroslav@ivinco.com{
2497Syaroslav@ivinco.com    if (empty($id)) return '';
2507Syaroslav@ivinco.com
2517Syaroslav@ivinco.com    if (false === strpos($id, ":")) {
2527Syaroslav@ivinco.com        return '';
2537Syaroslav@ivinco.com    }
2547Syaroslav@ivinco.com
2557Syaroslav@ivinco.com    $ns = explode(":", $id);
25680Syaroslav@ivinco.com    $nsCount = count($ns) - 1;
2577Syaroslav@ivinco.com
2587Syaroslav@ivinco.com    $result = '';
2597Syaroslav@ivinco.com    do {
26080Syaroslav@ivinco.com        for ($i = 0; $i < $nsCount; $i++) {
2617Syaroslav@ivinco.com            $name = $ns[$i];
2627Syaroslav@ivinco.com            $result .= $name;
2637Syaroslav@ivinco.com            if ($i < $nsCount - 1) {
2647Syaroslav@ivinco.com                $result .= ':';
2657Syaroslav@ivinco.com            }
2667Syaroslav@ivinco.com        }
2677Syaroslav@ivinco.com        $result .= ' ';
2687Syaroslav@ivinco.com    } while ($nsCount--);
2697Syaroslav@ivinco.com    return $result;
2707Syaroslav@ivinco.com}
2717Syaroslav@ivinco.com
2727Syaroslav@ivinco.comfunction getPagename($id)
2737Syaroslav@ivinco.com{
2747Syaroslav@ivinco.com    if (empty($id)) return '';
2757Syaroslav@ivinco.com
27680Syaroslav@ivinco.com    if (false === strpos($id, ":")) {
27780Syaroslav@ivinco.com        return $id;
27880Syaroslav@ivinco.com    }
27980Syaroslav@ivinco.com
28080Syaroslav@ivinco.com    $ns = explode(":", $id);
28180Syaroslav@ivinco.com    return $ns[count($ns) - 1];
28280Syaroslav@ivinco.com}
28380Syaroslav@ivinco.com
28480Syaroslav@ivinco.com
28580Syaroslav@ivinco.com
28680Syaroslav@ivinco.com/**
28780Syaroslav@ivinco.com * Method return all wiki page names
28880Syaroslav@ivinco.com * @global array $conf
2897Syaroslav@ivinco.com * @return array
2907Syaroslav@ivinco.com */
2917Syaroslav@ivinco.comfunction getPagesList()
2927Syaroslav@ivinco.com{
2937Syaroslav@ivinco.com    global $conf;
2947Syaroslav@ivinco.com
2957Syaroslav@ivinco.com    $data = array();
2967Syaroslav@ivinco.com    sort($data);
2977Syaroslav@ivinco.com    search($data, $conf['datadir'], 'search_allpages', array('skipacl' => 1), '');
2987Syaroslav@ivinco.com
2997Syaroslav@ivinco.com    return $data;
3007Syaroslav@ivinco.com}
301104Syaroslav@ivinco.com
3027Syaroslav@ivinco.comfunction getNsLinks($id, $keywords, $search)
3037Syaroslav@ivinco.com{
3048Syaroslav@ivinco.com    global $conf;
3058Syaroslav@ivinco.com    $parts = explode(':', $id);
30615Syaroslav@ivinco.com    $count = count($parts);
3078Syaroslav@ivinco.com
3088Syaroslav@ivinco.com    // print intermediate namespace links
3098Syaroslav@ivinco.com    $part = '';
3108Syaroslav@ivinco.com    $data = array();
311*130Syaroslav@ivinco.com    $titles = array();
3128Syaroslav@ivinco.com    for ($i = 0; $i < $count; $i++) {
3138Syaroslav@ivinco.com        $part .= $parts[$i] . ':';
31410Syaroslav@ivinco.com        $page = $part;
31510Syaroslav@ivinco.com        resolve_pageid('', $page, $exists);
3168Syaroslav@ivinco.com
3178Syaroslav@ivinco.com        if (preg_match("#:start$#", $page) && !preg_match("#:start:$#", $part)) {
3188Syaroslav@ivinco.com            $page = substr($page, 0, strpos($page, ":start"));
3198Syaroslav@ivinco.com        };
32015Syaroslav@ivinco.com
32115Syaroslav@ivinco.com        // output
32213Syaroslav@ivinco.com        if ($exists) {
323*130Syaroslav@ivinco.com            $titles[wl($page)] = $parts[$i];
3248Syaroslav@ivinco.com        } else {
3258Syaroslav@ivinco.com            $titles[wl($page)] = $parts[$i];
3268Syaroslav@ivinco.com        }
32715Syaroslav@ivinco.com        $data[] = array('link' => "?do=search&id={$keywords}" . urlencode(" @ns $page"));
3288Syaroslav@ivinco.com    }
32918Syaroslav@ivinco.com    $titleExcerpt = $search->getExcerpt($titles, $search->starQuery($keywords));
33015Syaroslav@ivinco.com    $i = 0;
33182Syaroslav@ivinco.com    foreach ($data as $key => $notused) {
3328Syaroslav@ivinco.com        $data[$key]['title'] = $titleExcerpt[$i++];
33315Syaroslav@ivinco.com    }
33410Syaroslav@ivinco.com    return $data;
33510Syaroslav@ivinco.com}
33610Syaroslav@ivinco.com
33710Syaroslav@ivinco.comfunction printNamespaces($query)
33810Syaroslav@ivinco.com{
33939Syaroslav@ivinco.com    $data = array();
34039Syaroslav@ivinco.com    $query = str_replace(" ", "_", $query);
34139Syaroslav@ivinco.com    $data = ft_pageLookup($query, false);
34239Syaroslav@ivinco.com
34339Syaroslav@ivinco.com    if (!count($data)) return false;
34461Syaroslav@ivinco.com
34569Syaroslav@ivinco.com    print '<ul>';
34639Syaroslav@ivinco.com    $counter = 0;
34753Syaroslav@ivinco.com    foreach ($data as $id) {
34839Syaroslav@ivinco.com        print '<li>';
34953Syaroslav@ivinco.com        $ns = getNS($id);
35039Syaroslav@ivinco.com        if ($ns) {
35148Syaroslav@ivinco.com            $name = shorten(noNS($id), ' (' . $ns . ')', 30);
35239Syaroslav@ivinco.com        } else {
35339Syaroslav@ivinco.com            $name = $id;
35439Syaroslav@ivinco.com        }
35539Syaroslav@ivinco.com        $href = wl($id);
35639Syaroslav@ivinco.com
35739Syaroslav@ivinco.com        tpl_link($href, $id, "class='wikilink1'");
35839Syaroslav@ivinco.com        print '</li>';
35939Syaroslav@ivinco.com        if (++$counter == 20) {
36061Syaroslav@ivinco.com            break;
36161Syaroslav@ivinco.com        }
36250Syaroslav@ivinco.com    }
36339Syaroslav@ivinco.com    print '</ul>';
36448Syaroslav@ivinco.com}
36548Syaroslav@ivinco.com
36648Syaroslav@ivinco.comfunction printNamespacesNew($pageNames)
36739Syaroslav@ivinco.com{
36839Syaroslav@ivinco.com    if (empty($pageNames)) return false;
36968Syaroslav@ivinco.com
37068Syaroslav@ivinco.com    $limit = 10;
37169Syaroslav@ivinco.com    print '<ul>';
37268Syaroslav@ivinco.com    $counter = 0;
37369Syaroslav@ivinco.com    foreach ($pageNames as $id => $header) {
37468Syaroslav@ivinco.com        $ns = getNS($id);
37570Syaroslav@ivinco.com        if ($ns) {
37668Syaroslav@ivinco.com            $name = shorten(noNS($id), ' (' . $ns . ')', 30);
37768Syaroslav@ivinco.com        } else {
37870Syaroslav@ivinco.com            $name = $id;
37977Syaroslav@ivinco.com        }
38076Syaroslav@ivinco.com        print '<li>';
38176Syaroslav@ivinco.com        /*if (!empty($header)){
38276Syaroslav@ivinco.com            print '<a href="'.wl($id).'#'.$header.'" '. "class='wikilink1'>".$id."</a>".'#'.$header;
38376Syaroslav@ivinco.com        } else {
38476Syaroslav@ivinco.com            print '<a href="'.wl($id).'" '. "class='wikilink1'>".$id."</a>";
38576Syaroslav@ivinco.com        }*/
38668Syaroslav@ivinco.com        print '<a href="' . wl($id) . '" ' . "class='wikilink1'>" . $id . "</a>";
38781Syaroslav@ivinco.com        print '</li>';
38877Syaroslav@ivinco.com        if (++$counter == $limit) {
38977Syaroslav@ivinco.com            break;
39077Syaroslav@ivinco.com        }
39181Syaroslav@ivinco.com    }
39281Syaroslav@ivinco.com    print '</ul>';
39368Syaroslav@ivinco.com}
39470Syaroslav@ivinco.com
39570Syaroslav@ivinco.comif (!function_exists('shorten')) {
39670Syaroslav@ivinco.com    /**
39768Syaroslav@ivinco.com     * Shorten a given string by removing data from the middle
39868Syaroslav@ivinco.com     *
39994Syaroslav@ivinco.com     * You can give the string in two parts, teh first part $keep
40094Syaroslav@ivinco.com     * will never be shortened. The second part $short will be cut
40194Syaroslav@ivinco.com     * in the middle to shorten but only if at least $min chars are
40294Syaroslav@ivinco.com     * left to display it. Otherwise it will be left off.
40394Syaroslav@ivinco.com     *
40494Syaroslav@ivinco.com     * @param string $keep   the part to keep
40594Syaroslav@ivinco.com     * @param string $short  the part to shorten
40694Syaroslav@ivinco.com     * @param int    $max    maximum chars you want for the whole string
40794Syaroslav@ivinco.com     * @param int    $min    minimum number of chars to have left for middle shortening
40894Syaroslav@ivinco.com     * @param string $char   the shortening character to use
40994Syaroslav@ivinco.com     */
41094Syaroslav@ivinco.com    function shorten($keep, $short, $max, $min = 9, $char = '⌇')
41194Syaroslav@ivinco.com    {
41294Syaroslav@ivinco.com        $max = $max - utf8_strlen($keep);
41394Syaroslav@ivinco.com        if ($max < $min) return $keep;
41494Syaroslav@ivinco.com        $len = utf8_strlen($short);
41594Syaroslav@ivinco.com        if ($len <= $max) return $keep . $short;
41694Syaroslav@ivinco.com        $half = floor($max / 2);
41794Syaroslav@ivinco.com        return $keep . utf8_substr($short, 0, $half - 1) . $char . utf8_substr($short, $len - $half);
41894Syaroslav@ivinco.com    }
41994Syaroslav@ivinco.com}
42094Syaroslav@ivinco.com