public function execute() { global $wgUser; $params = $this->extractRequestParams(); if (is_null($params['page'])) { $this->dieUsage('Must specify page title', 0); } if (is_null($params['tpl'])) { $this->dieUsage('Must specify template name', 1); } if (is_null($params['param'])) { $this->dieUsage('Must specify parameter name', 2); } if (is_null($params['value'])) { $this->dieUsage('Must specify value', 3); } if (is_null($params['summary'])) { $this->dieUsage('Must specify edit summary', 4); } $page = $params['page']; $template = $params['tpl']; $instance = $params['tplinstance']; $parameter = $params['param']; $value = $params['value']; $summary = $params['summary']; $articleTitle = Title::newFromText($page); if (!$articleTitle) { $this->dieUsage("Can't create title object ({$page})", 5); } $errors = $articleTitle->getUserPermissionsErrors('edit', $wgUser); if (!empty($errors)) { $this->dieUsage(wfMsg($errors[0][0], $errors[0][1]), 8); } $article = new Article($articleTitle); if (!$article->exists()) { $this->dieUsage("Article doesn't exist ({$page})", 6); } $pom = new POMPage($article->getContent()); if (array_key_exists($template, $pom->templates) && array_key_exists($instance, $pom->templates[$template])) { $pom->templates[$template][$instance]->setParameter($parameter, $value); } else { $this->dieUsage("This template ({$template}, instance #{$instance}) with this parameter ({$parameter}) doesn't exist within this page ({$page})", 7); } $success = $article->doEdit($pom->asString(), $summary); $result = array(); $result['result'] = $success ? 'Success' : 'Failure'; $this->getResult()->addValue(null, 'pomsettplparam', $result); }
$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;
*/ //$__curdir = getcwd(); //chdir('C:/xampp/htdocs/mw'); //require_once('C:/xampp/htdocs/mw/includes/Webstart.php'); //chdir($__curdir); global $pcpPREFIX, $pcpWSServer; $pcpPREFIX = 'C:\\xampp\\htdocs\\mw\\extensions\\PageCRUD_Plus' . "\\"; $pcpWSServer = false; 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());
private function toArray($data) { if (is_array($data) || is_object($data)) { if (get_class($data) == 'POMPage') { $__pom = new POMPage($data->titel, $data->text); $__result = array(); $__elementsIterator = $__pom->getElements()->listIterator(); $__result["page"] = $this->toArray($__pcpPage); $__cnt = 0; while ($__elementsIterator->hasNext()) { $__element =& $__elementsIterator->getNextNodeValueByReference(); $__array = $this->toArray($__element); $__result["page"][get_class($__element)][get_class($__element) . $__cnt] = $__array; $__cnt++; } return $__result; } $__result = array(); foreach ($data as $__key => $__value) { $__result[$__key] = $this->toArray($__value); } return $__result; } return $data; }
<?php require_once( 'POM.php' ); # Get document from MediaWiki server $pom = new POMPage( join( file( 'http://www.mediawiki.org/w/index.php?title=Extension:Page_Object_Model&action=raw' ) ) ); # Check current version $ver = $pom->templates['Extension'][0]->getParameter( 'version' ); echo "Current version: $ver\n"; # Increase version number by fraction $pom->templates['Extension'][0]->setParameter( 'version', $ver + 0.1 ); # Do whatever you want with result (we'll just display it) echo "Document with increased version:\n" . $pom->asString();
/** * @dataProvider pageProvider */ public function testInputIsTheSameAsOutput( $pagetext ) { $page = new POMPage( $pagetext ); $this->assertEquals( $pagetext, $page->asString() ); }
/** * 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); }