예제 #1
0
/**
 * Parses additinal semantic data need for a triple store:
 *
 *  1. categories
 *  2. rules (optional)
 */
function smwfTripleStoreParserHook(&$parser, &$text, &$strip_state = null)
{
    global $smwgIP, $smwgTripleStoreGraph;
    global $wgContLang;
    include_once $smwgIP . '/includes/SMW_Factbox.php';
    SMWTripleStore::$fullSemanticData = new SMWFullSemanticData();
    $categoryText = $wgContLang->getNsText(NS_CATEGORY);
    // parse categories:
    $categoryLinkPattern = '/\\[\\[\\s*                   # Beginning of the link
                            ' . $categoryText . '\\s*:      # category link (case insensitive!)
                            ([^\\[\\]]*)                 # category
                            \\]\\]                       # End of link
                            /ixu';
    # case-insensitive, ignore whitespaces, UTF-8 compatible
    $categories = array();
    $matches = array();
    preg_match_all($categoryLinkPattern, $text, $matches);
    if (isset($matches[1])) {
        foreach ($matches[1] as $m) {
            $labelIndex = strpos($m, '|');
            $m = $labelIndex !== false ? substr($m, 0, $labelIndex) : $m;
            $categories[] = Title::newFromText(trim($m), NS_CATEGORY);
        }
    }
    // parse redirects
    $redirectLinkPattern = '/\\#REDIRECT          # REDIRECT command
                            \\[\\[                # Beginning of the link
                            ([^]]+)               # target
                            \\]\\]                # End of link
                            /ixu';
    # case-insensitive, ignore whitespaces, UTF-8 compatible
    $redirects = array();
    $matches = array();
    preg_match_all($redirectLinkPattern, $text, $matches);
    if (isset($matches[1])) {
        foreach ($matches[1] as $m) {
            $redirects[] = Title::newFromText($m);
        }
    }
    SMWTripleStore::$fullSemanticData->setCategories($categories);
    SMWTripleStore::$fullSemanticData->setRedirects($redirects);
    return true;
}