function createAllPages()
 {
     $out = $this->getOutput();
     $req = $this->getRequest();
     $user = $this->getUser();
     $template_name = trim($req->getVal("template_name"));
     $template_multiple = $req->getBool("template_multiple");
     // If this is a multiple-instance template, there
     // shouldn't be a corresponding form or category.
     if ($template_multiple) {
         $form_name = null;
         $category_name = null;
     } else {
         $form_name = trim($req->getVal("form_name"));
         $category_name = trim($req->getVal("category_name"));
     }
     if ($template_name === '' || !$template_multiple && ($form_name === '' || $category_name === '')) {
         $out->addWikiMsg('sf_createclass_missingvalues');
         return;
     }
     $fields = array();
     $jobs = array();
     // Cycle through all the rows passed in.
     for ($i = 1; $req->getVal("field_name_{$i}") != ''; $i++) {
         // Go through the query values, setting the appropriate
         // local variables.
         $field_name = trim($req->getVal("field_name_{$i}"));
         $property_name = trim($req->getVal("property_name_{$i}"));
         $property_type = $req->getVal("property_type_{$i}");
         $allowed_values = $req->getVal("allowed_values_{$i}");
         $is_list = $req->getCheck("is_list_{$i}");
         // Create an SFTemplateField object based on these
         // values, and add it to the $fields array.
         $field = SFTemplateField::create($field_name, $field_name, $property_name, $is_list);
         if (defined('CARGO_VERSION')) {
             $field->setFieldType($property_type);
             // Hopefully it's safe to use a Cargo
             // utility method here.
             $possibleValues = CargoUtils::smartSplit(',', $allowed_values);
             $field->setPossibleValues($possibleValues);
         }
         $fields[] = $field;
         // Create the property, and make a job for it.
         if (defined('SMW_VERSION') && !empty($property_name)) {
             $full_text = SFCreateProperty::createPropertyText($property_type, '', $allowed_values);
             $property_title = Title::makeTitleSafe(SMW_NS_PROPERTY, $property_name);
             $params = array();
             $params['user_id'] = $user->getId();
             $params['page_text'] = $full_text;
             $params['edit_summary'] = wfMessage('sf_createproperty_editsummary', $property_type)->inContentLanguage()->text();
             $jobs[] = new SFCreatePageJob($property_title, $params);
         }
     }
     // Also create the "connecting property", if there is one.
     $connectingProperty = trim($req->getVal('connecting_property'));
     if (defined('SMW_VERSION') && $connectingProperty != '') {
         global $smwgContLang;
         $datatypeLabels = $smwgContLang->getDatatypeLabels();
         $property_type = $datatypeLabels['_wpg'];
         $full_text = SFCreateProperty::createPropertyText($property_type, '', $allowed_values);
         $property_title = Title::makeTitleSafe(SMW_NS_PROPERTY, $connectingProperty);
         $params = array();
         $params['user_id'] = $user->getId();
         $params['page_text'] = $full_text;
         $params['edit_summary'] = wfMessage('sf_createproperty_editsummary', $property_type)->inContentLanguage()->text();
         $jobs[] = new SFCreatePageJob($property_title, $params);
     }
     // Create the template, and save it (might as well save
     // one page, instead of just creating jobs for all of them).
     $template_format = $req->getVal("template_format");
     $sfTemplate = new SFTemplate($template_name, $fields);
     if (defined('CARGO_VERSION')) {
         $sfTemplate->mCargoTable = trim($req->getVal("cargo_table"));
     }
     if (defined('SMW_VERSION') && $template_multiple) {
         $sfTemplate->setConnectingProperty($connectingProperty);
     } else {
         $sfTemplate->setCategoryName($category_name);
     }
     $sfTemplate->setFormat($template_format);
     $full_text = $sfTemplate->createText();
     $template_title = Title::makeTitleSafe(NS_TEMPLATE, $template_name);
     $edit_summary = '';
     if (method_exists('WikiPage', 'doEditContent')) {
         // MW 1.21+
         $template_page = new WikiPage($template_title);
         $content = new WikitextContent($full_text);
         $template_page->doEditContent($content, $edit_summary);
     } else {
         // MW <= 1.20
         $template_article = new Article($template_title);
         $template_article->doEdit($full_text, $edit_summary);
     }
     // Create the form, and make a job for it.
     if ($form_name != '') {
         $form_template = SFTemplateInForm::create($template_name, '', false);
         $form_items = array();
         $form_items[] = array('type' => 'template', 'name' => $form_template->getTemplateName(), 'item' => $form_template);
         $form = SFForm::create($form_name, $form_items);
         $full_text = $form->createMarkup();
         $form_title = Title::makeTitleSafe(SF_NS_FORM, $form_name);
         $params = array();
         $params['user_id'] = $user->getId();
         $params['page_text'] = $full_text;
         $jobs[] = new SFCreatePageJob($form_title, $params);
     }
     // Create the category, and make a job for it.
     if ($category_name != '') {
         $full_text = SFCreateCategory::createCategoryText($form_name, $category_name, '');
         $category_title = Title::makeTitleSafe(NS_CATEGORY, $category_name);
         $params = array();
         $params['user_id'] = $user->getId();
         $params['page_text'] = $full_text;
         $jobs[] = new SFCreatePageJob($category_title, $params);
     }
     if (class_exists('JobQueueGroup')) {
         JobQueueGroup::singleton()->push($jobs);
     } else {
         // MW <= 1.20
         Job::batchInsert($jobs);
     }
     $out->addWikiMsg('sf_createclass_success');
 }
    function printCreateTemplateForm($query)
    {
        $out = $this->getOutput();
        $req = $this->getRequest();
        if (!is_null($query)) {
            $presetTemplateName = str_replace('_', ' ', $query);
            $out->setPageTitle(wfMessage('sf-createtemplate-with-name', $presetTemplateName)->text());
            $template_name = $presetTemplateName;
        } else {
            $presetTemplateName = null;
            $template_name = $req->getVal('template_name');
        }
        $out->addModules('ext.semanticforms.main');
        $this->addJavascript();
        $text = '';
        $save_page = $req->getCheck('wpSave');
        $preview_page = $req->getCheck('wpPreview');
        if ($save_page || $preview_page) {
            $validToken = $this->getUser()->matchEditToken($req->getVal('csrf'), 'CreateTemplate');
            if (!$validToken) {
                $text = "This appears to be a cross-site request forgery; canceling save.";
                $out->addHTML($text);
                return;
            }
            $fields = array();
            // Cycle through the query values, setting the
            // appropriate local variables.
            foreach ($req->getValues() as $var => $val) {
                $var_elements = explode("_", $var);
                // we only care about query variables of the form "a_b"
                if (count($var_elements) != 2) {
                    continue;
                }
                list($field_field, $id) = $var_elements;
                if ($field_field == 'name' && $id != 'starter') {
                    $field = SFTemplateField::create($val, $req->getVal('label_' . $id), $req->getVal('semantic_property_' . $id), $req->getCheck('is_list_' . $id), $req->getVal('delimiter_' . $id));
                    $field->setFieldType($req->getVal('field_type_' . $id));
                    // Fake attribute.
                    $field->mAllowedValuesStr = $req->getVal('allowed_values_' . $id);
                    $fields[] = $field;
                }
            }
            // Assemble the template text, and submit it as a wiki
            // page.
            $out->setArticleBodyOnly(true);
            $title = Title::makeTitleSafe(NS_TEMPLATE, $template_name);
            $category = $req->getVal('category');
            $cargo_table = $req->getVal('cargo_table');
            $aggregating_property = $req->getVal('semantic_property_aggregation');
            $aggregation_label = $req->getVal('aggregation_label');
            $template_format = $req->getVal('template_format');
            $sfTemplate = new SFTemplate($template_name, $fields);
            $sfTemplate->setCategoryName($category);
            $sfTemplate->mCargoTable = $cargo_table;
            $sfTemplate->setAggregatingInfo($aggregating_property, $aggregation_label);
            $sfTemplate->setFormat($template_format);
            $full_text = $sfTemplate->createText();
            $text = SFUtils::printRedirectForm($title, $full_text, "", $save_page, $preview_page, false, false, false, null, null);
            $out->addHTML($text);
            return;
        }
        $text .= '	<form id="createTemplateForm" action="" method="post">' . "\n";
        if (is_null($presetTemplateName)) {
            // Set 'title' field, in case there's no URL niceness
            $text .= Html::hidden('title', $this->getTitle()->getPrefixedText()) . "\n";
            $text .= "\t<p id=\"template_name_p\">" . wfMessage('sf_createtemplate_namelabel')->escaped() . ' <input size="25" id="template_name" name="template_name" /></p>' . "\n";
        }
        $text .= "\t<p>" . wfMessage('sf_createtemplate_categorylabel')->escaped() . ' <input size="25" name="category" /></p>' . "\n";
        if (!defined('SMW_VERSION') && defined('CARGO_VERSION')) {
            $text .= "\t<p>" . wfMessage('sf_createtemplate_cargotablelabel')->escaped() . ' <input size="25" name="cargo_table" /></p>' . "\n";
        }
        $text .= "\t<fieldset>\n";
        $text .= "\t" . Html::element('legend', null, wfMessage('sf_createtemplate_templatefields')->text()) . "\n";
        $text .= "\t" . Html::element('p', null, wfMessage('sf_createtemplate_fieldsdesc')->text()) . "\n";
        if (defined('SMW_VERSION')) {
            $all_properties = self::getAllPropertyNames();
        } else {
            $all_properties = array();
        }
        $text .= '<div id="fieldsList">' . "\n";
        $text .= self::printFieldEntryBox("1", $all_properties);
        $text .= self::printFieldEntryBox("starter", $all_properties, false);
        $text .= "</div>\n";
        $add_field_button = Html::input(null, wfMessage('sf_createtemplate_addfield')->text(), 'button', array('class' => "createTemplateAddField"));
        $text .= Html::rawElement('p', null, $add_field_button) . "\n";
        $text .= "\t</fieldset>\n";
        if (defined('SMW_VERSION')) {
            $text .= "\t<fieldset>\n";
            $text .= "\t" . Html::element('legend', null, wfMessage('sf_createtemplate_aggregation')->text()) . "\n";
            $text .= "\t" . Html::element('p', null, wfMessage('sf_createtemplate_aggregationdesc')->text()) . "\n";
            $text .= "\t<p>" . wfMessage('sf_createtemplate_semanticproperty')->escaped() . ' ' . self::printPropertiesComboBox($all_properties, "aggregation") . "</p>\n";
            $text .= "\t<p>" . wfMessage('sf_createtemplate_aggregationlabel')->escaped() . ' ' . Html::input('aggregation_label', null, 'text', array('size' => '25')) . "</p>\n";
            $text .= "\t</fieldset>\n";
        }
        $text .= self::printTemplateStyleInput('template_format');
        $text .= "\t" . Html::hidden('csrf', $this->getUser()->getEditToken('CreateTemplate')) . "\n";
        $save_button_text = wfMessage('savearticle')->escaped();
        $preview_button_text = wfMessage('preview')->escaped();
        $text .= <<<END
\t<div class="editButtons">
\t<input type="submit" id="wpSave" name="wpSave" value="{$save_button_text}" />
\t<input type="submit" id="wpPreview" name="wpPreview" value="{$preview_button_text}" />
\t</div>
\t</form>

END;
        $out->addHTML($text);
    }
Ejemplo n.º 3
0
 /**
  * Generate pages (form and templates) specified in the list.
  */
 public static function generatePages($pageSchemaObj, $selectedPages)
 {
     global $wgUser;
     $psFormItems = $pageSchemaObj->getFormItemsList();
     $form_items = array();
     $jobs = array();
     $templateHackUsed = false;
     $isCategoryNameSet = false;
     // Generate every specified template
     foreach ($psFormItems as $psFormItem) {
         if ($psFormItem['type'] == 'Template') {
             $psTemplate = $psFormItem['item'];
             $templateName = $psTemplate->getName();
             $templateTitle = Title::makeTitleSafe(NS_TEMPLATE, $templateName);
             $fullTemplateName = PageSchemas::titleString($templateTitle);
             $template_fields = self::getFieldsFromTemplateSchema($psTemplate);
             // Get property for use in either #set_internal
             // or #subobject, defined by either SIO's or
             // SMW's Page Schemas portion. We don't need
             // to record which one it came from, because
             // SF's code to generate the template runs its
             // own, similar check.
             // @TODO - $internalObjProperty should probably
             // have a more generic name.
             if (class_exists('SIOPageSchemas')) {
                 $internalObjProperty = SIOPageSchemas::getInternalObjectPropertyName($psTemplate);
             } elseif (method_exists('SMWPageSchemas', 'getConnectingPropertyName')) {
                 $internalObjProperty = SMWPageSchemas::getConnectingPropertyName($psTemplate);
             } else {
                 $internalObjProperty = null;
             }
             // TODO - actually, the category-setting should be
             // smarter than this: if there's more than one
             // template in the schema, it should probably be only
             // the first non-multiple template that includes the
             // category tag.
             if ($psTemplate->isMultiple()) {
                 $categoryName = null;
             } else {
                 if ($isCategoryNameSet == false) {
                     $categoryName = $pageSchemaObj->getCategoryName();
                     $isCategoryNameSet = true;
                 } else {
                     $categoryName = null;
                 }
             }
             if (method_exists($psTemplate, 'getFormat')) {
                 $templateFormat = $psTemplate->getFormat();
             } else {
                 $templateFormat = null;
             }
             $sfTemplate = new SFTemplate($templateName, $template_fields);
             $sfTemplate->setConnectingProperty($internalObjProperty);
             $sfTemplate->setCategoryName($categoryName);
             $sfTemplate->setFormat($templateFormat);
             // Set Cargo table, if one was set in the schema.
             $cargoArray = $psTemplate->getObject('cargo_TemplateDetails');
             if (!is_null($cargoArray)) {
                 $sfTemplate->mCargoTable = PageSchemas::getValueFromObject($cargoArray, 'Table');
             }
             $templateText = $sfTemplate->createText();
             if (in_array($fullTemplateName, $selectedPages)) {
                 $params = array();
                 $params['user_id'] = $wgUser->getId();
                 $params['page_text'] = $templateText;
                 $jobs[] = new PSCreatePageJob($templateTitle, $params);
                 if (strpos($templateText, '{{!}}') > 0) {
                     $templateHackUsed = true;
                 }
             }
             $templateValues = self::getTemplateValues($psTemplate);
             if (array_key_exists('Label', $templateValues)) {
                 $templateLabel = $templateValues['Label'];
             } else {
                 $templateLabel = null;
             }
             $form_fields = self::getFormFieldInfo($psTemplate, $template_fields);
             // Create template info for form, for use in generating
             // the form (if it will be generated).
             $form_template = SFTemplateInForm::create($templateName, $templateLabel, $psTemplate->isMultiple(), null, $form_fields);
             $form_items[] = array('type' => 'template', 'name' => $form_template->getTemplateName(), 'item' => $form_template);
         } elseif ($psFormItem['type'] == 'Section') {
             $psPageSection = $psFormItem['item'];
             $form_section = self::getPageSection($psPageSection);
             $form_section->setSectionLevel($psPageSection->getSectionLevel());
             $form_items[] = array('type' => 'section', 'name' => $form_section->getSectionName(), 'item' => $form_section);
         }
     }
     // Create the "!" hack template, if it's necessary
     if ($templateHackUsed) {
         $templateTitle = Title::makeTitleSafe(NS_TEMPLATE, '!');
         if (!$templateTitle->exists()) {
             $params = array();
             $params['user_id'] = $wgUser->getId();
             $params['page_text'] = '|';
             $jobs[] = new PSCreatePageJob($templateTitle, $params);
         }
     }
     if (class_exists('JobQueueGroup')) {
         JobQueueGroup::singleton()->push($jobs);
     } else {
         // MW <= 1.20
         Job::batchInsert($jobs);
     }
     // Create form, if it's specified.
     $formName = self::getFormName($pageSchemaObj);
     $categoryName = $pageSchemaObj->getCategoryName();
     if (!empty($formName)) {
         $formInfo = self::getMainFormInfo($pageSchemaObj);
         $formTitle = Title::makeTitleSafe(SF_NS_FORM, $formName);
         $fullFormName = PageSchemas::titleString($formTitle);
         if (in_array($fullFormName, $selectedPages)) {
             self::generateForm($formName, $formTitle, $form_items, $formInfo, $categoryName);
         }
     }
 }