/**
  * Returns predefined text for a newly created page (only via Halo AJAX API)
  *
  * @param Title $title current Title
  * @param string $categoryTemplate template name (without namespace prefix)
  * @param hash array $parameterMappings (name=>value)
  * @return string predefined text 
  */
 public static function getPredefinitions($title, $template, $parameterMappings)
 {
     self::$title = $title;
     // parse template parameters
     $rev = Revision::newFromTitle(Title::newFromText($template, NS_TEMPLATE));
     if (is_null($rev)) {
         return "";
     }
     $text = $rev->getText();
     preg_match_all('/\\{\\{\\{([^}]+)\\}\\}\\}/', $text, $matches);
     // serialize template with predefined values
     $resultText = "\n{{" . $template;
     $parameters = array_unique($matches[1]);
     for ($i = 0; $i < count($parameters); $i++) {
         $parameters[$i] = trim(str_replace("|", "", $parameters[$i]));
     }
     foreach ($parameters as $m) {
         if ($m == '') {
             continue;
         }
         if (array_key_exists($m, $parameterMappings)) {
             $mappedValue = self::mappedValue($parameterMappings[$m]);
             $resultText .= "\n|{$m}=" . $mappedValue;
         } else {
             $resultText .= "\n|{$m}=";
         }
     }
     // adds additional parameters not found in the template
     $additionalParameters = array_diff(array_keys($parameterMappings), $parameters);
     foreach ($additionalParameters as $sp) {
         $mappedValue = self::mappedValue($parameterMappings[$sp]);
         if ($sp != '') {
             $resultText .= "\n|{$sp}=" . $mappedValue;
         }
     }
     $resultText .= "\n}}\n";
     return $resultText;
 }
/**
 * Creates a new article or appends some text if it already
 * exists. This function is invoked by an ajax call.
 *
 * @param string $title
 * 			The name of the article
 * @param string $user
 * 			The name of the user
 * @param string $content
 * 			The initial content of the article. It is only set if the article
 * 			is newly created.
 * @param string optionalText
 * 			This text is appended to the article, if it is not already part
 * 			of it. The text may contain variables of the language files
 * 			that are replaced by their representation.
 * @param string creationComment
 * 			This text describes why the article has been created.
 *
 * @return string Comma separated list:
 * 			bool success
 * 	 			<true> if the operation was successful.
 * 			bool created
 * 				<true> if the article was created,
 * 				<false> if it was only modified
 * 				<denied> if the permission was denied
 * 			string title
 * 				Title of the (new) article
 *
 */
function smwf_om_CreateArticle($title, $user, $content, $optionalText, $creationComment)
{
    global $smwgContLang, $smwgHaloContLang;
    $success = false;
    $created = true;
    $title = strip_tags($title);
    if ($title == '') {
        return "false";
    }
    if (smwf_om_userCan($title, 'create') === "false") {
        return "false,denied,{$title}";
    }
    $title = Title::newFromText($title);
    // add predefined content if configured
    global $smwhgAutoTemplates, $smwhgAutoTemplatesParameters;
    if (isset($smwhgAutoTemplates)) {
        if (array_key_exists($title->getNamespace(), $smwhgAutoTemplates)) {
            require_once 'SMW_Predefinitions.php';
            $mappings = array_key_exists($title->getNamespace(), $smwhgAutoTemplatesParameters) ? $smwhgAutoTemplatesParameters[$title->getNamespace()] : array();
            $metadataText = SMWPredefinitions::getPredefinitions($title, $smwhgAutoTemplates[$title->getNamespace()], $mappings);
            $content = $metadataText . $content;
        }
    }
    $article = new Article($title);
    if ($article->exists()) {
        // The article exists => get its current content. The passed content
        // will be ignored.
        $text = $article->getContent();
        if ($text === false) {
            return "false,false," . $title . getText();
        }
        $content = $text;
        $created = false;
    }
    if (!empty($optionalText)) {
        $supportedConstants = array("_SUBP", "SMW_SSP_HAS_DOMAIN_AND_RANGE_HINT", "SMW_SSP_HAS_MAX_CARD", "SMW_SSP_HAS_MIN_CARD", "_TYPE", "_rec", "_LIST");
        // Some optional text is given
        $sp = $smwgContLang->getPropertyLabels() + $smwgContLang->getDatatypeLabels();
        $ssp = $smwgHaloContLang->getSpecialSchemaPropertyArray();
        $num = count($supportedConstants);
        for ($i = 0; $i < $num; $i++) {
            $constant = $supportedConstants[$i];
            $pos = strpos($optionalText, $constant);
            if ($pos) {
                $langString = "Unsupported constant";
                if (strpos($constant, "SMW_SSP_") !== false) {
                    $langString = $ssp[constant($constant)];
                } else {
                    $langString = $sp[$constant];
                }
                $optionalText = str_replace($constant, $langString, $optionalText);
            }
        }
        // does the optional text already exist?
        if ($article->exists()) {
            $pos = strpos($content, $optionalText);
            if ($pos === false) {
                // optional text not found => append it
                $content .= $optionalText;
            }
        } else {
            // The article will be created with content and optional text
            $content .= $optionalText;
        }
    }
    // Set the article's content
    $success = $article->doEdit($content, $creationComment);
    return ($success ? "true," : "false,") . ($created ? "true," : "false,") . $title->getNsText() . ":" . $title->getText();
}