/**
 * This function is called, when a <WebService>-tag for a WWSD has been
 * found in an article. If the content of the definition is correct, and
 * if the namespace of the article is the WebService namespace, the WWSD
 * will be stored in the database.
 *
 * @param string $input
 * 		The content of the tag
 * @param array $args
 * 		Array of attributes in the tag
 * @param Parser $parser
 * 		The wiki text parser
 * @return string
 * 		The text to be rendered
 */
function wwsdParserHook($input, $args, $parser)
{
    global $smwgDIIP;
    require_once "{$smwgDIIP}/specials/WebServices/SMW_WebService.php";
    $wwsd = WebService::newFromID($parser->getTitle()->getArticleID());
    WebServiceManager::rememberWWSD($wwsd);
    $attr = "";
    foreach ($args as $k => $v) {
        $attr .= " " . $k . '="' . $v . '"';
    }
    $completeWWSD = "<WebService{$attr}>" . $input . "</WebService>\n";
    $notice = '';
    $name = $parser->mTitle->getText();
    $id = $parser->mTitle->getArticleID();
    $ws = WebService::newFromWWSD($name, $completeWWSD);
    $errors = null;
    $warnings = null;
    if (is_array($ws)) {
        $errors = $ws;
    } else {
        // A web service object was returned. Validate the definition
        // with respect to the WSDL.
        $res = $ws->validateWWSD();
        if (is_array($res)) {
            // Warnings were returned
            $warnings = $res;
        } else {
            if (is_string($res)) {
                $errors = array($res);
            }
        }
    }
    $msg = "";
    if ($errors != null || $warnings != null) {
        // Errors within the WWSD => show them as a bullet list
        $ew = $errors ? $errors : $warnings;
        $msg = '<h4><span class="mw-headline">' . wfMsg('smw_wws_wwsd_errors') . '</h4><ul>';
        foreach ($ew as $err) {
            $msg .= '<li>' . $err . '</li>';
        }
        $msg .= '</ul>';
        if ($errors) {
            return '<h4><span class="mw-headline">Web Service Definition</span></h4>' . "<pre>\n" . htmlspecialchars($completeWWSD) . "\n</pre><br />" . $msg;
        }
    }
    if ($parser->mTitle->getNamespace() == SMW_NS_WEB_SERVICE) {
        // store the WWSD in the database in the hook function <articleSavedHook>.
        WebServiceManager::$mNewWebService = $ws;
    } else {
        // add message: namespace webService needed.
        $notice = "<b>" . wfMsg('smw_wws_wwsd_needs_namespace') . "</b>";
    }
    //	global $wgArticlePath;
    //	if(strpos($wgArticlePath, "?") > 0){
    //		$url = Title::makeTitleSafe(NS_SPECIAL, "DefineWebService")->getFullURL()."&wwsdId=".$ws->getArticleID();
    //	} else {
    //		$url = Title::makeTitleSafe(NS_SPECIAL, "DefineWebService")->getFullURL()."?wwsdId=".$ws->getArticleID();
    //	}
    //	$linkToDefGui = '<h4><span class="mw-headline"><a href="'.$url.'">'.wfMsg('smw_wws_edit_in_gui').'</a></h4>';
    return '<h4><span class="mw-headline">Web Service Definition</span></h4>' . "<pre>\n" . htmlspecialchars($completeWWSD) . "\n</pre>" . $notice . $msg;
    //.$linkToDefGui;
}