Пример #1
0
function macro_TitleIndex($formatter, $value, $options = array())
{
    global $DBInfo;
    $pc = !empty($DBInfo->titleindex_pagecount) ? intval($DBInfo->titleindex_pagecount) : 100;
    if ($pc < 1) {
        $pc = 100;
    }
    $pg = empty($options['p']) ? 1 : intval($options['p']);
    if ($pg < 1) {
        $pg = 1;
    }
    $group = $formatter->group;
    $key = -1;
    $keys = array();
    if ($value == '' or $value == 'all') {
        $sel = '';
    } else {
        $sel = ucfirst($value);
    }
    // get all keys
    $all_keys = get_keys();
    if (isset($sel[0])) {
        if (!isset($all_keys[$sel])) {
            $sel = key($all_keys);
        }
        // default
    }
    if (@preg_match('/' . $sel . '/i', '') === false) {
        $sel = '';
    }
    $titleindex = array();
    // cache titleindex
    $kc = new Cache_text('titleindex');
    $delay = !empty($DBInfo->default_delaytime) ? $DBInfo->default_delaytime : 0;
    $uid = '';
    if (function_exists('posix_getuid')) {
        $uid = '.' . posix_getuid();
    }
    $lock_file = _fake_lock_file($DBInfo->vartmp_dir, 'titleindex' . $uid);
    $locked = _fake_locked($lock_file, $DBInfo->mtime());
    if ($locked or $kc->exists('key') and $DBInfo->checkUpdated($kc->mtime('key'), $delay)) {
        if (!empty($formatter->use_group) and $formatter->group) {
            $keys = $kc->fetch('key.' . $formatter->group);
            $titleindex = $kc->fetch('titleindex.' . $formatter->group);
        } else {
            $keys = $kc->fetch('key');
            $titleindex = $kc->fetch('titleindex' . $sel);
        }
        if (isset($sel[0]) and isset($titleindex[$sel])) {
            $all_pages = $titleindex[$sel];
        }
        if (empty($titleindex) and $locked) {
            // no cache found
            return _("Please wait...");
        }
    }
    if (empty($all_pages)) {
        $all_pages = array();
        $indexer = $DBInfo->lazyLoad('titleindexer');
        if (!empty($formatter->use_group) and $formatter->group) {
            $group_pages = $indexer->getLikePages('^' . $formatter->group);
            foreach ($group_pages as $page) {
                $all_pages[] = str_replace($formatter->group, '', $page);
            }
        } else {
            $all_pages = $indexer->getLikePages('^' . $all_keys[$sel], 0);
        }
        #natcasesort($all_pages);
        #sort($all_pages,SORT_STRING);
        //usort($all_pages, 'strcasecmp');
        $pages = array_flip($all_pages);
        if (!empty($formatter->use_group)) {
            array_walk($pages, '_setpagekey');
        } else {
            array_walk($pages, create_function('&$p, $k', '$p = $k;'));
        }
        $all_pages = array_flip($pages);
        uksort($all_pages, 'strcasecmp');
    }
    if (empty($keys) or empty($titleindex)) {
        _fake_lock($lock_file);
        foreach ($all_pages as $page => $rpage) {
            $p = ltrim($page);
            $pkey = get_key("{$p}");
            if ($key != $pkey) {
                $key = $pkey;
                //$keys[] = $pkey;
                if (!isset($titleindex[$pkey])) {
                    $titleindex[$pkey] = array();
                }
            }
            $titleindex[$pkey][$page] = $rpage;
        }
        $keys = array_keys($all_keys);
        if (!empty($tlink)) {
            $keys[] = 'all';
        }
        if (!empty($formatter->use_group) and $formatter->group) {
            $kc->update('key.' . $formatter->group, $keys);
            $kc->update('titleindex.' . $formatter->group, $titleindex);
        } else {
            $kc->update('key', $keys);
            $kc->update('titleindex' . $sel, $titleindex);
        }
        if (isset($sel[0]) and isset($titleindex[$sel])) {
            $all_pages = $titleindex[$sel];
        }
        _fake_lock($lock_file, LOCK_UN);
    }
    $pnut = null;
    if (isset($sel[0]) and count($all_pages) > $pc) {
        $pages_number = intval(count($all_pages) / $pc);
        if (count($all_pages) % $pc) {
            $pages_number++;
        }
        $pages = array_keys($all_pages);
        $pages = array_splice($pages, ($pg - 1) * $pc, $pc);
        $selected = array();
        foreach ($pages as $p) {
            $selected[$p] = $all_pages[$p];
        }
        $pages = $selected;
        $pnut = get_pagelist($formatter, $pages_number, '?action=titleindex&amp;sec=' . $sel . '&amp;p=', !empty($pg) ? $pg : 1);
    } else {
        $pages =& $all_pages;
    }
    //print count($all_pages);
    //exit;
    $out = '';
    #  if ($DBInfo->use_titlecache)
    #    $cache=new Cache_text('title');
    $key = '';
    foreach ($pages as $page => $rpage) {
        $p = ltrim($page);
        $pkey = get_key("{$p}");
        if ($key != $pkey) {
            $key = $pkey;
            if (isset($sel[0]) and !preg_match('/^' . $sel . '/i', $pkey)) {
                continue;
            }
            if (!empty($out)) {
                $out .= "</ul>";
            }
            $out .= "<a name='{$key}'></a><h3><a href='#top'>{$key}</a></h3>\n";
            $out .= "<ul>";
        }
        if (isset($sel[0]) and !preg_match('/^' . $sel . '/i', $pkey)) {
            continue;
        }
        #
        #    if ($DBInfo->use_titlecache and $cache->exists($page))
        #      $title=$cache->fetch($page);
        #    else
        $title = get_title($rpage, $page);
        #$out.= '<li>' . $formatter->word_repl('"'.$page.'"',$title,'',0,0);
        $urlname = _urlencode($group . $rpage);
        $out .= '<li>' . $formatter->link_tag($urlname, '', _html_escape($title));
        $keyname = $DBInfo->pageToKeyname(urldecode($rpage));
        if (is_dir($DBInfo->upload_dir . "/{$keyname}") or !empty($DBInfo->use_hashed_upload_dir) and is_dir($DBInfo->upload_dir . '/' . get_hashed_prefix($keyname) . $keyname)) {
            $out .= ' ' . $formatter->link_tag($urlname, "?action=uploadedfiles", $formatter->icon['attach']);
        }
        $out .= "</li>\n";
    }
    if (!empty($pnut)) {
        $out .= '<li style="list-style:none">' . $pnut . '</li>' . "\n";
    }
    $out .= "</ul>\n";
    $index = '';
    $tlink = '';
    if (isset($sel[0])) {
        $tlink = $formatter->link_url($formatter->page->urlname, '?action=titleindex&amp;sec=');
    }
    $index = array();
    foreach ($keys as $key) {
        $name = strval($key);
        $tag = '#' . $key;
        $link = !empty($tlink) ? preg_replace('/sec=/', 'sec=' . _urlencode($key), $tlink) : '';
        if ($name == 'Others') {
            $name = _("Others");
        } else {
            if ($name == 'all') {
                $name = _("Show all");
            }
        }
        $index[] = "<a href='{$link}{$tag}'>{$name}</a>";
    }
    $str = implode(' | ', $index);
    return "<center><a name='top'></a>{$str}</center>\n{$out}";
}
Пример #2
0
function macro_WordIndex($formatter, $value, $params = array())
{
    global $DBInfo;
    $pagelinks = $formatter->pagelinks;
    // save
    $save = $formatter->sister_on;
    $formatter->sister_on = 0;
    if (!empty($DBInfo->use_titlecache)) {
        $cache = new Cache_text('title');
    }
    $word_limit = 50;
    $start = 0;
    $prev = 0;
    if (!empty($params['start']) and is_numeric($params['start'])) {
        $start = $params['start'];
    }
    if (!empty($params['prev']) and is_numeric($params['prev'])) {
        $prev = $params['prev'];
    }
    $value = strval($value);
    if ($value == '' or $value == 'all') {
        $sel = '';
    } else {
        $sel = $value;
    }
    if (@preg_match('/' . $sel . '/i', '') === false) {
        $sel = '';
    }
    $keys = array();
    $dict = array();
    // cache wordindex
    $wc = new Cache_text('wordindex');
    $delay = !empty($DBInfo->default_delaytime) ? $DBInfo->default_delaytime : 0;
    $lock_file = _fake_lock_file($DBInfo->vartmp_dir, 'wordindex');
    $locked = _fake_locked($lock_file, $DBInfo->mtime());
    if ($locked or $wc->exists('key') and $DBInfo->checkUpdated($wc->mtime('key'), $delay)) {
        if ($formatter->group) {
            $keys = $wc->fetch('key.' . $formatter->group);
            $dict = $wc->fetch('wordindex.' . $formatter->group);
        } else {
            $keys = $wc->fetch('key');
            $dict = $wc->fetch('wordindex');
        }
        if (empty($dict) and $locked) {
            // no cache found
            return _("Please wait...");
        }
    }
    if (empty($keys) or empty($dict)) {
        _fake_lock($lock_file);
        $all_pages = array();
        if ($formatter->group) {
            $group_pages = $DBInfo->getLikePages($formatter->group);
            foreach ($group_pages as $page) {
                $all_pages[] = str_replace($formatter->group, '', $page);
            }
        } else {
            $all_pages = $DBInfo->getPageLists();
        }
        foreach ($all_pages as $page) {
            if (!empty($DBInfo->use_titlecache) and $cache->exists($page)) {
                $title = $cache->fetch($page);
            } else {
                $title = $page;
            }
            $tmp = preg_replace("/[\\?!\$%\\.\\^;&\\*()_\\+\\|\\[\\]<>\"' \\-~\\/:]/", " ", $title);
            $tmp = preg_replace("/((?<=[A-Za-z0-9])[A-Z][a-z0-9])/", " \\1", ucwords($tmp));
            $words = preg_split("/\\s+/", $tmp);
            foreach ($words as $word) {
                $word = ltrim($word);
                if (!$word) {
                    continue;
                }
                $key = get_key($word);
                $keys[$key] = $key;
                if (!empty($dict[$key][$word])) {
                    $dict[$key][$word][] = $page;
                } else {
                    if (empty($dict[$key])) {
                        $dict[$key] = array();
                    }
                    $dict[$key][$word] = array($page);
                }
            }
        }
        sort($keys);
        foreach ($keys as $k) {
            #ksort($dict[$k]);
            #ksort($dict[$k], SORT_STRING);
            #uksort($dict[$k], "strnatcasecmp");
            uksort($dict[$k], "strcasecmp");
        }
        if ($formatter->group) {
            $wc->update('key.' . $formatter->group, $keys);
            $wc->update('wordindex.' . $formatter->group, $dict);
        } else {
            $wc->update('key', $keys);
            $wc->update('wordindex', $dict);
        }
        _fake_lock($lock_file, LOCK_UN);
    }
    if (isset($sel[0]) and isset($dict[$sel])) {
        $selected = array($sel);
    } else {
        $selected =& $keys;
    }
    $out = '';
    $key = -1;
    $count = 0;
    $idx = 0;
    foreach ($selected as $k) {
        $words = array_keys($dict[$k]);
        $sz = count($words);
        for ($idx = $start; $idx < $sz; $idx++) {
            $word = $words[$idx];
            $pages =& $dict[$k][$word];
            $pkey = $k;
            if ($key != $pkey) {
                $key = $pkey;
                if (!empty($sel) and !preg_match('/^' . $sel . '/i', $pkey)) {
                    continue;
                }
                if (!empty($out)) {
                    $out .= "</ul>";
                }
                $ukey = urlencode($key);
                $out .= "<a name='{$ukey}'></a><h3><a href='#top'>{$key}</a></h3>\n";
            }
            if (!empty($sel) and !preg_match('/^' . $sel . '/i', $pkey)) {
                continue;
            }
            $out .= "<h4>{$word}</h4>\n";
            $out .= "<ul>\n";
            foreach ($pages as $page) {
                $out .= '<li>' . $formatter->word_repl('"' . $page . '"') . "</li>\n";
            }
            $out .= "</ul>\n";
            $count++;
            if ($count >= $word_limit) {
                break;
            }
        }
    }
    if (isset($sel[0])) {
        $last = count($dict[$sel]);
        $offset = $idx + 1;
        $pager = array();
        if ($start > 0) {
            // get previous start offset.
            $count = 0;
            $idx -= $word_limit - 1;
            if ($idx < 0) {
                $idx = 0;
            }
            $link = $formatter->link_url($formatter->page->name, '?action=wordindex&amp;sec=' . $sel . '&amp;start=' . $idx);
            $pager[] = "<a href='{$link}'>" . _("&#171; Prev") . '</a>';
        }
        if ($offset < $last) {
            $link = $formatter->link_url($formatter->page->name, '?action=wordindex&amp;sec=' . $sel . '&amp;start=' . $offset);
            $pager[] = "<a href='{$link}'>" . _("Next &#187;") . '</a>';
        }
        if (!empty($pager)) {
            $out .= implode(' | ', $pager) . "<br />\n";
        }
    }
    $index = array();
    $tlink = '';
    if (isset($sel[0])) {
        $tlink = $formatter->link_url($formatter->page->name, '?action=wordindex&amp;sec=');
    }
    foreach ($keys as $key) {
        $name = strval($key);
        if ($key == 'Others') {
            $name = _("Others");
        }
        $ukey = urlencode($key);
        $link = !empty($tlink) ? preg_replace('/sec=/', 'sec=' . _urlencode($key), $tlink) : '';
        $index[] = "<a href='{$link}#{$ukey}'>{$name}</a>";
    }
    $str = implode(' | ', $index);
    $formatter->pagelinks = $pagelinks;
    // restore
    $formatter->sister_on = $save;
    return "<center><a name='top'></a>{$str}</center>\n{$out}";
}