require_once 'C:\\xampp\\htdocs\\mw\\extensions\\PageCRUD_Plus\\PCP.php'; require_once 'C:\\xampp\\htdocs\\mw\\extensions\\PageObjectModel\\POM.php'; # Get document from MediaWiki server $__pageTitle = "Template:Diplomarbeitsthema"; $__pageTitle = "Mobile_Location_Display_Advertising"; $__pageTitle = "Testpage"; if (POMUrlUtil::url_exists('http://localhost/mw/index.php?title=' . $__pageTitle . '&action=raw')) { $pom = new POMPage($__pageTitle, join(file('http://localhost/mw/index.php?title=' . $__pageTitle . '&action=raw')), array('POMExtendedParser')); } else { print "not existing\n"; } # iterate trough the templates //$iterator = $pom->getTemplateByTitle("Diplomarbeit")->listIterator(); //while($iterator->hasNext()){ // $template = &$iterator->getNextNodeValueByReference(); # get reference for direct changes // // var_dump($template); // if($template->getParameter("hat Beschreibung")!== NULL){ // # check if the parameter exists // if ($template->getParameter("hat Beschreibung")->getValue()->getElement() !== NULL){ # a parameter value exists // # get the first subelement of the page object representing the parameter value // // var_dump($template->getParameter("hat Beschreibung")); // $anElement = &$template->getParameter("hat Beschreibung")->getValueByReference()->getElement(); // var_dump($template->getParameter("hat Beschreibung")->getValueByReference()->getElement()); // } // } //} $__property = POMProperty::createProperty("has Test", "Test", " "); $pom->addElement($__property); $pom->sync(); var_dump($pom->getPropertyByName(NULL)->listIterator()->getNextNodeValueByReference()); //print ($pom->text);
$pom = new POMPage('Testeintrag', join(file('http://localhost/wiki/index.php?title=Testeintrag&action=raw')), array('POMExtendedParser')); # iterate trough the templates $iterator = $pom->getTemplateByTitle("Post")->listIterator(); while ($iterator->hasNext()) { $template =& $iterator->getNextNodeValueByReference(); # get reference for direct changes if ($template->getParameter("PARAMETERNAME") !== NULL) { # check if the parameter exists if ($template->getParameter("PARAMETERNAME")->getValue()->getElement() !== NULL) { # a parameter value exists # get the first subelement of the page object representing the parameter value var_dump($template->getParameter("PARAMETERNAME")); $anElement =& $template->getParameter("PARAMETERNAME")->getValueByReference()->getElement(); var_dump($anElement); } else { # add a new text element $parameterValue =& $template->getParameter("PARAMETERNAME")->getValueByReference(); $simpleText = new POMSimpleText("A simple text element!"); $parameterValue->addElement($simpleText); # show the changes $pom->sync(); print $pom->text; print "### Now changing the parameter value .. ###\n"; $parameterValue = new POMPage("A-STRING", ""); $simpleText = new POMSimpleText("String changed!"); $parameterValue->addElement($simpleText); } } } $pom->sync(); print $pom->text;
/** * Parses the text of a given page and creates an element structure. * Supported types of elements include:<br/> * - templates,<br/> * - categories,<br/> * - properties and<br/> * - simple text<br/> * which represent the rest. * * @param POMPage &$page The page for which the POM will be built. */ public function Parse(POMPage &$page) { $wikiText =& $page->text; $this->_page = $page; // was is ein <ref>-tag? if (!$this->doAddWTO($wikiText)) { return $wikiText; } // Treat HTML comments and the like. They may contain dangerous material. $text = $this->maskHTML($wikiText); // Search for templates, template parameters and headings $parts = preg_split('/(\\{)|' . '(\\})|' . '(\\n+)|' . '(^======)|' . '(^=====)|' . '(^====)|' . '(^===)|' . '(^==)|' . '(^=)|' . '(\\[{2,})|' . '(\\]\\])|' . '(\\|)|' . '(<nowiki>)|' . '(<\\/nowiki>)|' . '(<noinclude>)|' . '(<\\/noinclude>)|' . '(<ask)|' . '(<\\/ask>)|' . '(<pre>)|' . '(<\\/pre>)|' . '^$/sm', $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); $markedText = ""; $id = 1; $pos = 0; $braceCount = 0; $templateStart = -1; $ignoreTemplates = -1; $tmplDescr = null; $ignoredLastToken = false; $lastTokenWOT = false; // true, if the last token was an opening tag $numParts = count($parts); $prevPart = null; // the part of the wiki text before the current part $part0 = null; $prefix = ''; $titleOpen = false; // true if a title (e.g. ==Title==) is parsed // annotation vars $aText = ''; // the node text $aStartPos = -1; // the starting position // ~annotation vars // simple text vars $stText = ''; // the node text $stStartPos = -1; // the starting position // ~simple text vars for ($i = 0; $i < $numParts; ++$i) { $part = $parts[$i]; $len = mb_strlen($part, "UTF-8"); if ($part0) { $prevPart = $part0; } $part0 = mb_substr($wikiText, $pos, $len, "UTF-8"); // Is the part a template? if ($templateStart == -1 && $i > $ignoreTemplates) { // no template detected yet. $tmplDescr = $this->parseTemplate($parts, $i); if ($tmplDescr[0] == 't') { // a template has been found => store only its start // It may be ahead of the current parser position. // It will be processed when its start is reached during // normal processing. $templateStart = $tmplDescr[2]; } else { if ($tmplDescr[0] == 'a') { // some tokens have been skipped // => advance parsing without looking for templates $ignoreTemplates = $tmplDescr[2]; } } } if ($i == $templateStart) { // check if a textnode has to be added if ($stStartPos != -1) { // add the text node before the template $page->addElement(new POMSimpleText($stText, $stStartPos)); // reset the simple text variables $stText = ''; // the node text $stStartPos = -1; // the starting position // ~add the text node } // a template has been found. // $tmplDescr[1]: name of the template // $tmplDescr[2]: start index of the template // $tmplDescr[3]: end index of the template // $tmplDescr[4]: content if (ereg('^#', $tmplDescr[1])) { // we have an extension parser function if ($tmplDescr[1] == '#ask:') { $page->addElement(new POMAskFunction($tmplDescr[4])); } else { if ($tmplDescr[1] == '#language:' || $tmplDescr[1] == '#special:' || $tmplDescr[1] == '#tag:') { // we have a built parser function as // described in http://www.mediawiki.org/wiki/Extension:Parser_function_extensions $page->addElement(new POMBuiltInParserFunction($tmplDescr[4])); } else { // we have an extension parser function other than "ask" $page->addElement(new POMExtensionParserFunction($tmplDescr[4])); } } } else { if (preg_match('#\\w\\:.*#', $tmplDescr[1]) !== 0) { // we have a built-in parser function $page->addElement(new POMBuiltInParserFunction($tmplDescr[4])); } else { $page->addElement(new POMTemplate($tmplDescr[4])); } } $templateStart = -1; // The parse can continue after the template $i = $tmplDescr[3]; $pos += mb_strlen($tmplDescr[4], "UTF-8"); } else { // parser is not collecting tokens for a template if ($part0 == '[[') { if ($braceCount == 0) { $aStartPos = $pos; if ($stStartPos != -1) { // add the text node $page->addElement(new POMSimpleText($stText)); // reset the simple text variables $stText = ''; // the node text $stStartPos = -1; // the starting position // ~add the text node } } else { $markedText .= $part0; // TODO: does this mean [[[[ ?? } $braceCount++; } else { if ($part0 == ']]') { if ($braceCount > 0) { $braceCount--; } $markedText .= $part0; $aText = '[[' . $aText . ']]'; if (strpos($aText, '::')) { $page->addElement(new POMProperty($aText)); $aText = ''; } else { if (strpos($aText, ':')) { $page->addElement(new POMCategory($aText)); $aText = ''; } else { // empty } } } else { if ($braceCount > 0) { $markedText .= $part0; $aText .= $part0; } else { // we are not converting into objects anything else than // templates, annotations and simple text if ($stStartPos == -1) { $stStartPos = $pos; } $stText .= $part0; } } } $pos += $len; } } if ($stStartPos != -1) { // add the text node $page->addElement(new POMSimpleText($stText)); // reset the simple text variables $stText = ''; // the node text $stStartPos = -1; // the starting position // ~add the text node } $part0 = mb_substr($wikiText, $pos, 1); }