/** * @since 1.9 * * @param ParserParameterProcessor $parameters * * @return string|null */ public function parse(ParserParameterProcessor $parameters) { $count = 0; $template = ''; $subject = $this->parserData->getSemanticData()->getSubject(); $parametersToArray = $parameters->toArray(); if (isset($parametersToArray['template'])) { $template = $parametersToArray['template'][0]; unset($parametersToArray['template']); } foreach ($parametersToArray as $property => $values) { $last = count($values) - 1; // -1 because the key starts with 0 foreach ($values as $key => $value) { $dataValue = DataValueFactory::getInstance()->newDataValueByText($property, $value, false, $subject); if ($this->parserData->canModifySemanticData()) { $this->parserData->addDataValue($dataValue); } $this->messageFormatter->addFromArray($dataValue->getErrors()); $this->addFieldsToTemplate($template, $dataValue, $property, $value, $last == $key, $count); } } $this->parserData->pushSemanticDataToParserOutput(); $html = $this->templateRenderer->render() . $this->messageFormatter->addFromArray($parameters->getErrors())->getHtml(); return array($html, 'noparse' => $template === '', 'isHTML' => false); }
/** * Parse parameters, return concept information box and update the * ParserOutput with the concept object * * @since 1.9 * * @param array $params * * @return string|null */ public function parse(array $rawParams) { $this->parserData->getOutput()->addModules('ext.smw.style'); $title = $this->parserData->getTitle(); $property = new DIProperty('_CONC'); if (!($title->getNamespace() === SMW_NS_CONCEPT)) { return $this->messageFormatter->addFromKey('smw_no_concept_namespace')->getHtml(); } elseif (count($this->parserData->getSemanticData()->getPropertyValues($property)) > 0) { return $this->messageFormatter->addFromKey('smw_multiple_concepts')->getHtml(); } // Remove parser object from parameters array if (isset($rawParams[0]) && $rawParams[0] instanceof Parser) { array_shift($rawParams); } // Use first parameter as concept (query) string $conceptQuery = array_shift($rawParams); // Use second parameter, if any as a description $conceptDocu = array_shift($rawParams); $query = $this->buildQuery($conceptQuery); $conceptQueryString = $query->getDescription()->getQueryString(); $this->parserData->getSemanticData()->addPropertyObjectValue($property, new DIConcept($conceptQueryString, $conceptDocu, $query->getDescription()->getQueryFeatures(), $query->getDescription()->getSize(), $query->getDescription()->getDepth())); $this->messageFormatter->addFromArray($query->getErrors())->addFromArray($this->parserData->getErrors()); $this->parserData->pushSemanticDataToParserOutput(); if ($this->messageFormatter->exists()) { return $this->messageFormatter->getHtml(); } return $this->buildConceptInfoBox($title, $conceptQueryString, $conceptDocu); }
/** * @since 2.1 * * @param PPFrame $frame * @param array $args */ public function parse(PPFrame $frame, array $args) { // @todo Save as metadata if (!$frame->isTemplate()) { return ''; } $this->subject = $this->parserData->getSemanticData()->getSubject(); foreach ($args as $arg) { if (trim($arg) !== '') { $expanded = trim($frame->expand($arg)); $parts = explode('=', $expanded, 2); if (count($parts) == 1) { $propertystring = $expanded; $argumentname = $expanded; } else { $propertystring = $parts[0]; $argumentname = $parts[1]; } $propertyValue = PropertyValue::makeUserProperty($propertystring); $argument = $frame->getArgument($argumentname); $valuestring = $frame->expand($argument); if ($propertyValue->isValid()) { $this->matchValueArgument($propertyValue, $propertystring, $valuestring); } } } $this->parserData->pushSemanticDataToParserOutput(); return ''; }
/** * @dataProvider templateDataProvider */ public function testPreprocessTemplateAndParse($namespace, array $settings, $text, $tmplValue, array $expected) { $parserOutput = new ParserOutput(); $title = Title::newFromText(__METHOD__, $namespace); $outputText = $this->runTemplateTransclusion($title, $text, $tmplValue); $this->applicationFactory->registerObject('Settings', Settings::newFromArray($settings)); $parserData = new ParserData($title, $parserOutput); $instance = new InTextAnnotationParser($parserData, new MagicWordsFinder(), new RedirectTargetFinder()); $instance->parse($outputText); $this->assertContains($expected['resultText'], $outputText); $parserData = new ParserData($title, $parserOutput); $this->assertInstanceOf('\\SMW\\SemanticData', $parserData->getSemanticData()); $this->semanticDataValidator->assertThatPropertiesAreSet($expected, $parserData->getSemanticData()); }
protected function addDataValuesToSubobject(ArrayFormatter $parameters) { $subject = $this->parserData->getSemanticData()->getSubject(); $this->subobject->setEmptyContainerForId($this->createSubobjectId($parameters)); foreach ($this->transformParametersToArray($parameters) as $property => $values) { if ($property === self::PARAM_SORTKEY) { $property = DIProperty::TYPE_SORTKEY; } foreach ($values as $value) { $dataValue = $this->dataValueFactory->newPropertyValue($property, $value, false, $subject); $this->subobject->addDataValue($dataValue); } } }
private function createQueryProfile($query, $format, $duration) { $queryProfileAnnotatorFactory = $this->applicationFactory->newQueryProfileAnnotatorFactory(); $jointProfileAnnotator = $queryProfileAnnotatorFactory->newJointProfileAnnotator($query, $format, $duration); $jointProfileAnnotator->addAnnotation(); $this->parserData->getSemanticData()->addPropertyObjectValue($jointProfileAnnotator->getProperty(), $jointProfileAnnotator->getContainer()); }
private function addProcessingError($errors) { $processingErrorMsgHandler = new ProcessingErrorMsgHandler($this->parserData->getSubject()); foreach ($errors as $error) { $processingErrorMsgHandler->pushTo($this->parserData->getSemanticData(), $processingErrorMsgHandler->getErrorContainerFromMsg($error, new DIProperty('_ASK'))); } }
private function addErrorWithMsg($subject, $errorMsg) { $error = new Error($subject); $this->parserData->getSemanticData()->addPropertyObjectValue($error->getProperty(), $error->getContainerFor(new DIProperty('_SOBJ'), $errorMsg)); $this->parserData->addError($errorMsg); return false; }
protected function addRedirectTargetAnnotation($text) { if ($this->isEnabledNamespace) { $this->redirectTargetFinder->findRedirectTargetFromText($text); $redirectPropertyAnnotator = $this->applicationFactory->newPropertyAnnotatorFactory()->newRedirectPropertyAnnotator($this->parserData->getSemanticData(), $this->redirectTargetFinder); $redirectPropertyAnnotator->addAnnotation(); } }
protected function addRedirectTargetAnnotationFromText($text) { if (!$this->isEnabledNamespace) { return; } $this->redirectTargetFinder->findRedirectTargetFromText($text); $propertyAnnotatorFactory = $this->applicationFactory->singleton('PropertyAnnotatorFactory'); $propertyAnnotator = $propertyAnnotatorFactory->newNullPropertyAnnotator($this->parserData->getSemanticData()); $redirectPropertyAnnotator = $propertyAnnotatorFactory->newRedirectPropertyAnnotator($propertyAnnotator, $this->redirectTargetFinder); $redirectPropertyAnnotator->addAnnotation(); }
private function addQueryProfile($query) { // If the smwgQueryProfiler is marked with FALSE then just don't create a profile. if (ApplicationFactory::getInstance()->getSettings()->get('smwgQueryProfiler') === false) { return; } $query->setContextPage($this->parserData->getSemanticData()->getSubject()); $profileAnnotatorFactory = ApplicationFactory::getInstance()->getQueryFactory()->newProfileAnnotatorFactory(); $descriptionProfileAnnotator = $profileAnnotatorFactory->newDescriptionProfileAnnotator($query); $descriptionProfileAnnotator->pushAnnotationsTo($this->parserData->getSemanticData()); }
public function testEditPageToGetNewRevision() { $this->title = Title::newFromText(__METHOD__); $pageCreator = new PageCreator(); $pageCreator->createPage($this->title)->doEdit('[[EditPageToGetNewRevisionHookTest::Foo]]'); $parserOutput = $pageCreator->getEditInfo()->output; $this->assertInstanceOf('ParserOutput', $parserOutput); $parserData = new ParserData($this->title, $parserOutput); $expected = array('propertyKeys' => array('_SKEY', '_MDAT', 'EditPageToGetNewRevisionHookTest')); $this->semanticDataValidator->assertThatPropertiesAreSet($expected, $parserData->getSemanticData()); }
private function createQueryProfile($query, $format, $duration) { // In case of an query error add a marker to the subject for // discoverability of a failed query if ($query->getErrors() !== array()) { $this->parserData->getSemanticData()->addPropertyObjectValue(new DIProperty('_ERRP'), DIProperty::newFromUserLabel('_ASK')->getDiWikiPage()); } $queryProfileAnnotatorFactory = $this->applicationFactory->newQueryProfileAnnotatorFactory(); $jointProfileAnnotator = $queryProfileAnnotatorFactory->newJointProfileAnnotator($query, $format, $duration); $jointProfileAnnotator->addAnnotation(); $this->parserData->getSemanticData()->addPropertyObjectValue($jointProfileAnnotator->getProperty(), $jointProfileAnnotator->getContainer()); }
/** * @since 1.9 * * @param ParserParameterProcessor $params * * @return string|null */ public function parse(ParserParameterProcessor $parameters) { if ($this->parserData->canModifySemanticData() && $this->addDataValuesToSubobject($parameters) && !$this->subobject->getSemanticData()->isEmpty()) { $this->parserData->getSemanticData()->addSubobject($this->subobject); } $this->parserData->pushSemanticDataToParserOutput(); $html = $this->messageFormatter->addFromArray($this->subobject->getErrors())->addFromArray($this->parserData->getErrors())->addFromArray($parameters->getErrors())->getHtml(); // An empty output in MW forces an extra <br> element. //if ( $html == '' ) { // $html = '<p></p>'; //} return $html; }
private function createQueryProfile($query, $format, $duration) { // In case of an query error add a marker to the subject for // discoverability of a failed query if ($query->getErrors() !== array()) { $error = new Error($this->parserData->getSubject()); $this->parserData->getSemanticData()->addPropertyObjectValue($error->getProperty(), $error->getContainerFor(new DIProperty('_ASK'), $query->getQueryString() . ' (' . implode(' ', $query->getErrors()) . ')')); } $queryProfileAnnotatorFactory = $this->applicationFactory->newQueryProfileAnnotatorFactory(); $jointProfileAnnotator = $queryProfileAnnotatorFactory->newJointProfileAnnotator($query, $format, $duration); $jointProfileAnnotator->addAnnotation(); $this->parserData->getSemanticData()->addPropertyObjectValue($jointProfileAnnotator->getProperty(), $jointProfileAnnotator->getContainer()); }
/** * @dataProvider categoriesDataProvider */ public function testAddCategoriesWithParserDataUpdate(array $parameters, array $expected) { $semanticData = $this->semanticDataFactory->setSubject(new DIWikiPage(__METHOD__, $parameters['namespace'], ''))->newEmptySemanticData(); $title = $semanticData->getSubject()->getTitle(); $parserOutput = new ParserOutput(); $parserData = new ParserData($title, $parserOutput); $this->applicationFactory->registerObject('Settings', Settings::newFromArray($parameters['settings'])); $instance = new CategoryPropertyAnnotator(new NullPropertyAnnotator($parserData->getSemanticData()), $parameters['categories']); $instance->addAnnotation(); $parserData->pushSemanticDataToParserOutput(); $parserDataAfterAnnotation = new ParserData($title, $parserOutput); $this->semanticDataValidator->assertThatPropertiesAreSet($expected, $parserDataAfterAnnotation->getSemanticData()); }
/** * @since 1.9 * * @param ArrayFormatter $parameters * * @return string|null */ public function parse(ArrayFormatter $parameters) { $count = 0; $template = ''; $subject = $this->parserData->getSemanticData()->getSubject(); $parametersToArray = $parameters->toArray(); if (isset($parametersToArray['template'])) { $template = $parametersToArray['template'][0]; unset($parametersToArray['template']); } foreach ($parametersToArray as $property => $values) { foreach ($values as $value) { $dataValue = DataValueFactory::getInstance()->newPropertyValue($property, $value, false, $subject); $this->parserData->addDataValue($dataValue); $this->messageFormatter->addFromArray($dataValue->getErrors()); $this->addFieldsToTemplate($template, $dataValue, $property, $value, $count); } } $this->parserData->pushSemanticDataToParserOutput(); $html = $this->templateRenderer->render() . $this->messageFormatter->addFromArray($parameters->getErrors())->getHtml(); return array($html, 'noparse' => true, 'isHTML' => true); }
/** * @dataProvider categoriesDataProvider */ public function testAddCategoriesWithParserDataUpdate(array $parameters, array $expected) { $semanticData = $this->semanticDataFactory->setSubject(new DIWikiPage(__METHOD__, $parameters['namespace'], ''))->newEmptySemanticData(); $title = $semanticData->getSubject()->getTitle(); $parserOutput = new ParserOutput(); $parserData = new ParserData($title, $parserOutput); $instance = new CategoryPropertyAnnotator(new NullPropertyAnnotator($parserData->getSemanticData()), $parameters['categories']); $instance->setShowHiddenCategoriesState($parameters['settings']['smwgShowHiddenCategories']); $instance->setCategoryInstanceUsageState($parameters['settings']['smwgCategoriesAsInstances']); $instance->setCategoryHierarchyUsageState($parameters['settings']['smwgUseCategoryHierarchy']); $instance->addAnnotation(); $parserData->pushSemanticDataToParserOutput(); $parserDataAfterAnnotation = new ParserData($title, $parserOutput); $this->semanticDataValidator->assertThatPropertiesAreSet($expected, $parserDataAfterAnnotation->getSemanticData()); }
/** * Returns content found for a given ParserOutput object and if the required * custom data was not available then semantic data are retrieved from * the store for a given subject. * * The method checks whether the given setting of $showfactbox requires * displaying the given data at all. * * @since 1.9 * * @return integer $showFactbox * * @return string|null */ protected function fetchContent($showFactbox = SMW_FACTBOX_NONEMPTY) { if ($showFactbox === SMW_FACTBOX_HIDDEN) { return ''; } $semanticData = $this->parserData->getSemanticData(); if ($semanticData === null || $semanticData->stubObject || $this->isEmpty($semanticData)) { $semanticData = $this->store->getSemanticData($this->parserData->getSubject()); } if ($showFactbox === SMW_FACTBOX_SPECIAL && !$semanticData->hasVisibleSpecialProperties()) { // show only if there are special properties return ''; } elseif ($showFactbox === SMW_FACTBOX_NONEMPTY && !$semanticData->hasVisibleProperties()) { // show only if non-empty return ''; } return $this->createTable($semanticData); }
/** * Adds property values to the ParserOutput instance * * @since 1.9 * * @param array $properties * * @return string */ protected function addPropertyValue(array $properties, $value, $valueCaption) { $subject = $this->parserData->getSemanticData()->getSubject(); // Add properties to the semantic container foreach ($properties as $property) { $dataValue = $this->dataValueFactory->newPropertyValue($property, $value, $valueCaption, $subject); if ($this->isEnabledNamespace && $this->isAnnotation) { $this->parserData->addDataValue($dataValue); } } // Return the text representation $result = $dataValue->getShortWikitext(true); // If necessary add an error text if ($this->settings->get('smwgInlineErrors') && $this->isEnabledNamespace && $this->isAnnotation && !$dataValue->isValid()) { $result .= $dataValue->getErrorText(); } return $result; }
/** * Adds property values to the ParserOutput instance * * @since 1.9 * * @param array $properties * * @return string */ protected function addPropertyValue(array $properties, $value, $valueCaption) { $subject = $this->parserData->getSemanticData()->getSubject(); // Set a context to a subject to idenitify the parser run $subject->setContextReference($this->contextReference); // Add properties to the semantic container foreach ($properties as $property) { $dataValue = $this->dataValueFactory->newPropertyValue($property, $value, $valueCaption, $subject); if ($this->isEnabledNamespace && $this->isAnnotation) { $this->parserData->addDataValue($dataValue); } } // Return the text representation $result = $dataValue->getShortWikitext(true); // If necessary add an error text if ($this->settings->get('smwgInlineErrors') && $this->isEnabledNamespace && $this->isAnnotation && !$dataValue->isValid()) { // Encode `:` to avoid a comment block and instead of the nowiki tag // use : as placeholder $result = str_replace(':', ':', $result) . $dataValue->getErrorText(); } return $result; }
protected function assertSemanticDataAfterParse($instance, $expected) { $parserData = new ParserData($instance->getTitle(), $instance->getOutput()); $semanticDataValidator = new SemanticDataValidator(); $semanticDataValidator->assertThatPropertiesAreSet($expected, $parserData->getSemanticData()); }
protected function setupInstanceAndAssertSemanticData(array $parameters, array $expected) { $parserOutput = new ParserOutput(); $title = Title::newFromText(__METHOD__); $subobject = new Subobject($title); $instance = $this->acquireInstance($subobject, $parserOutput); $instance->parse(new ParserParameterFormatter($parameters)); $parserData = new ParserData($title, $parserOutput); $subSemanticData = $parserData->getSemanticData()->getSubSemanticData(); foreach ($subSemanticData as $actualSemanticDataToAssert) { $this->semanticDataValidator->assertThatPropertiesAreSet($expected, $actualSemanticDataToAssert); } }
public function testRedirectAnnotationFromInjectedRedirectTarget() { $namespace = NS_MAIN; $text = ''; $redirectTarget = Title::newFromText('Foo'); $expected = array('propertyCount' => 1, 'property' => new DIProperty('_REDI'), 'propertyValues' => array('Foo')); $settings = array('smwgNamespacesWithSemanticLinks' => array($namespace => true), 'smwgLinksInValues' => false, 'smwgInlineErrors' => true); $this->testEnvironment->registerObject('Settings', Settings::newFromArray($settings)); $parserData = new ParserData(Title::newFromText(__METHOD__, $namespace), new ParserOutput()); $redirectTargetFinder = new RedirectTargetFinder(); $instance = new InTextAnnotationParser($parserData, new MagicWordsFinder(), $redirectTargetFinder); $instance->setRedirectTarget($redirectTarget); $instance->parse($text); $this->semanticDataValidator->assertThatPropertiesAreSet($expected, $parserData->getSemanticData()); }
/** * @dataProvider tableContentDataProvider */ public function testGetTableContent($test, $expected) { $title = Title::newFromText(__METHOD__); $parserData = new ParserData($title, new ParserOutput()); $store = $this->getMockBuilder('\\SMW\\Store')->disableOriginalConstructor()->getMockForAbstractClass(); $message = $this->getMockBuilder('\\Message')->disableOriginalConstructor()->getMock(); $message->expects($this->any())->method('inContentLanguage')->will($this->returnSelf()); $messageBuilder = $this->getMockBuilder('\\SMW\\MediaWiki\\MessageBuilder')->disableOriginalConstructor()->getMock(); $messageBuilder->expects($this->any())->method('getMessage')->will($this->returnValue($message)); $mockDIProperty = $this->mockbuilder->newObject('DIProperty', array('isUserDefined' => $test['isUserDefined'], 'isShown' => $test['isShown'], 'getLabel' => 'Quuey')); $parserData->setSemanticData(new SemanticData(DIWikiPage::newFromTitle($title))); $parserData->getSemanticData()->addPropertyObjectValue($mockDIProperty, DIWikiPage::newFromTitle($title)); $instance = new Factbox($store, $parserData, $messageBuilder); $this->stringValidator->assertThatStringContains($expected, $instance->doBuild()->getContent()); }
/** * @dataProvider getPropertyValueDataProvider */ public function testAddDataValue($propertyName, $value, $errorCount, $propertyCount) { $title = Title::newFromText(__METHOD__); $parserOutput = new ParserOutput(); $instance = new ParserData($title, $parserOutput); $instance->addDataValue($this->dataValueFactory->newPropertyValue($propertyName, $value)); if ($errorCount > 0) { return $this->assertCount($errorCount, $instance->getErrors()); } $expected = array('propertyCount' => $propertyCount, 'propertyLabels' => $propertyName, 'propertyValues' => $value); $this->semanticDataValidator->assertThatPropertiesAreSet($expected, $instance->getSemanticData()); }
protected function setupInstanceAndAssertSemanticData(array $parameters, array $expected) { $title = isset($expected['embeddedTitle']) ? $expected['embeddedTitle'] : __METHOD__; $parserOutput = new ParserOutput(); $title = Title::newFromText($title); $subobject = new Subobject($title); $instance = $this->acquireInstance($subobject, $parserOutput); $instance->parse(new ParserParameterFormatter($parameters)); $parserData = new ParserData($title, $parserOutput); $subSemanticData = $parserData->getSemanticData()->getSubSemanticData(); if ($expected['propertyCount'] == 0) { $this->assertEmpty($subSemanticData); } foreach ($subSemanticData as $key => $semanticData) { if (strpos($semanticData->getSubject()->getSubobjectName(), '_ERR') !== false) { continue; } $this->semanticDataValidator->assertThatPropertiesAreSet($expected, $semanticData); } }
public function testImportFromParserOutput() { $import = new ParserData(Title::newFromText(__METHOD__), new ParserOutput()); $import->addDataValue($this->dataValueFactory->newDataValueByText('Foo', 'Bar')); $import->pushSemanticDataToParserOutput(); $instance = new ParserData(Title::newFromText(__METHOD__), new ParserOutput()); $instance->importFromParserOutput(null); $this->assertNotEquals($import->getSemanticData()->getHash(), $instance->getSemanticData()->getHash()); $instance->importFromParserOutput($import->getOutput()); $this->assertEquals($import->getSemanticData()->getHash(), $instance->getSemanticData()->getHash()); }