/** * Equivalent to setSemanticProperty(), but called when using Cargo * instead of SMW. */ function setCargoFieldData($tableName, $fieldName, $fieldDescription = null) { $this->mCargoTable = $tableName; $this->mCargoField = $fieldName; if (is_null($fieldDescription)) { try { $tableSchemas = CargoUtils::getTableSchemas(array($tableName)); } catch (MWException $e) { return; } if (count($tableSchemas) == 0) { return; } $tableSchema = $tableSchemas[$tableName]; $fieldDescriptions = $tableSchema->mFieldDescriptions; if (array_key_exists($fieldName, $fieldDescriptions)) { $fieldDescription = $fieldDescriptions[$fieldName]; } else { return; } } // We have some "pseudo-types", used for setting the correct // form input. if ($fieldDescription->mAllowedValues != null) { $this->mFieldType = 'Enumeration'; } elseif ($fieldDescription->mType == 'Text' && $fieldDescription->mSize != '' && $fieldDescription->mSize <= 100) { $this->mFieldType = 'String'; } else { $this->mFieldType = $fieldDescription->mType; } $this->mIsList = $fieldDescription->mIsList; if (method_exists($fieldDescription, 'getDelimiter')) { // Cargo 0.11+ $this->mDelimiter = $fieldDescription->getDelimiter(); } else { $this->mDelimiter = $fieldDescription->mDelimiter; } $this->mPossibleValues = $fieldDescription->mAllowedValues; }
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 getAllFieldsCargo($templateTitle) { $cargoFieldsOfTemplateParams = array(); $templateFields = array(); // First, get the table name, and fields, declared for this // template. $templatePageID = $templateTitle->getArticleID(); $tableSchemaString = CargoUtils::getPageProp($templatePageID, 'CargoFields'); // See if there even is DB storage for this template - if not, // exit. if (is_null($tableSchemaString)) { return null; } $tableSchema = CargoTableSchema::newFromDBString($tableSchemaString); $tableName = CargoUtils::getPageProp($templatePageID, 'CargoTableName'); // Then, match template params to Cargo table fields, by // parsing call(s) to #cargo_store. $templateText = SFUtils::getPageText($templateTitle); // Ignore 'noinclude' sections and 'includeonly' tags. $templateText = StringUtils::delimiterReplace('<noinclude>', '</noinclude>', '', $templateText); $templateText = strtr($templateText, array('<includeonly>' => '', '</includeonly>' => '')); // Let's find every #cargo_store tag. // Unfortunately, it doesn't seem possible to use a regexp // search for this, because it's hard to know which set of // double brackets represents the end of such a call. Instead, // we'll do some manual parsing. $cargoStoreLocations = array(); $curPos = 0; while (true) { $newPos = strpos($templateText, "#cargo_store:", $curPos); if ($newPos === false) { break; } $curPos = $newPos + 13; $cargoStoreLocations[] = $curPos; } $cargoStoreCalls = array(); foreach ($cargoStoreLocations as $locNum => $startPos) { $numUnclosedBrackets = 2; if ($locNum < count($cargoStoreLocations) - 1) { $lastPos = $cargoStoreLocations[$locNum + 1]; } else { $lastPos = strlen($templateText) - 1; } $curCargoStoreCall = ''; $curPos = $startPos; while ($curPos <= $lastPos) { $curChar = $templateText[$curPos]; $curCargoStoreCall .= $curChar; if ($curChar == '}') { $numUnclosedBrackets--; } elseif ($curChar == '{') { $numUnclosedBrackets++; } if ($numUnclosedBrackets == 0) { break; } $curPos++; } $cargoStoreCalls[] = $curCargoStoreCall; } foreach ($cargoStoreCalls as $cargoStoreCall) { if (preg_match_all('/([^|{]*?)=\\s*{{{([^|}]*)/mis', $cargoStoreCall, $matches)) { foreach ($matches[1] as $i => $cargoFieldName) { $templateParameter = trim($matches[2][$i]); $cargoFieldsOfTemplateParams[$templateParameter] = $cargoFieldName; } } } // Now, combine the two sets of information into an array of // SFTemplateFields objects. $fieldDescriptions = $tableSchema->mFieldDescriptions; foreach ($cargoFieldsOfTemplateParams as $templateParameter => $cargoField) { $templateField = SFTemplateField::create($templateParameter, $templateParameter); if (array_key_exists($cargoField, $fieldDescriptions)) { $fieldDescription = $fieldDescriptions[$cargoField]; $templateField->setCargoFieldData($tableName, $cargoField, $fieldDescription); } $templateFields[] = $templateField; } return $templateFields; }
function getAllFieldsCargo($templateTitle) { $cargoFieldsOfTemplateParams = array(); $templateFields = array(); // First, get the table name, and fields, declared for this // template. $templatePageID = $templateTitle->getArticleID(); $tableSchemaString = CargoUtils::getPageProp($templatePageID, 'CargoFields'); // See if there even is DB storage for this template - if not, // exit. if (is_null($tableSchemaString)) { return null; } $tableSchema = CargoTableSchema::newFromDBString($tableSchemaString); $tableName = CargoUtils::getPageProp($templatePageID, 'CargoTableName'); // Then, match template params to Cargo table fields, by // parsing call(s) to #cargo_store. $templateText = SFUtils::getPageText($templateTitle); // Ignore 'noinclude' sections and 'includeonly' tags. $templateText = StringUtils::delimiterReplace('<noinclude>', '</noinclude>', '', $templateText); $templateText = strtr($templateText, array('<includeonly>' => '', '</includeonly>' => '')); if (preg_match_all('/#cargo_store:(.*?}})\\s*}}/mis', $templateText, $matches)) { foreach ($matches[1] as $match) { if (preg_match_all('/([^|{]*?)=\\s*{{{([^|}]*)/mis', $match, $matches2)) { foreach ($matches2[1] as $i => $cargoFieldName) { $templateParameter = trim($matches2[2][$i]); $cargoFieldsOfTemplateParams[$templateParameter] = $cargoFieldName; } } } } // Now, combine the two sets of information into an array of // SFTemplateFields objects. $fieldDescriptions = $tableSchema->mFieldDescriptions; foreach ($cargoFieldsOfTemplateParams as $templateParameter => $cargoField) { $templateField = SFTemplateField::create($templateParameter, $templateParameter); if (array_key_exists($cargoField, $fieldDescriptions)) { $fieldDescription = $fieldDescriptions[$cargoField]; $templateField->setCargoFieldData($tableName, $cargoField, $fieldDescription); } $templateFields[] = $templateField; } return $templateFields; }