protected function setUp()
 {
     global $wgContLang;
     parent::setUp();
     $this->testParserOptions = ParserOptions::newFromUserAndLang(new User(), $wgContLang);
     $this->testParser = new Parser();
     $this->testParser->Options($this->testParserOptions);
     $this->testParser->clearState();
     $this->title = Title::newFromText('Preload Test');
 }
 /** setup a basic parser object */
 protected function setUp()
 {
     parent::setUp();
     $contLang = Language::factory('en');
     $this->setMwGlobals(array('wgLanguageCode' => 'en', 'wgContLang' => $contLang));
     $this->testParser = new Parser();
     $this->testParser->Options(ParserOptions::newFromUserAndLang(new User(), $contLang));
     # initialize parser output
     $this->testParser->clearState();
     # Needs a title to do magic word stuff
     $title = Title::newFromText('Tests');
     $title->mRedirect = false;
     # Else it needs a db connection just to check if it's a redirect (when deciding the page language)
     $this->testParser->setTitle($title);
 }
Exemple #3
0
 public function build()
 {
     $article = $this->Agent->getArticle();
     $text = $article->getContent(true);
     # Strip comments and <nowiki>
     $text = preg_replace("/<!--.*?-->/s", "", $text);
     $text = preg_replace("@<nowiki>.*?</nowiki>@s", "", $text);
     # change template usage to substitution; note that this is WRONG
     #$tchars = Title::legalChars();
     #$text = preg_replace("/(?<!{){{([$tchars]+)(\|.*?)?}}(?!})/", "{{subst:$1$2}}", $text);
     $parser = new Parser();
     # so the magic variables work out right
     $parser->mOptions = new ParserOptions();
     $parser->mTitle = $this->Agent->getTitle();
     $parser->mOutputType = OT_WIKI;
     $parser->initialiseVariables();
     $parser->clearState();
     $text = $parser->replaceVariables($text);
     preg_match_all("@<rdf>(.*?)</rdf>@s", $text, $matches, PREG_PATTERN_ORDER);
     $content = $matches[1];
     $rdf = implode(' ', array_values($content));
     $model = MwRdf::Model();
     if (strlen($rdf) > 0) {
         $parser->mOutputType = OT_HTML;
         $rdf = $parser->replaceVariables($rdf);
         $turtle_parser = MwRdf::Parser('turtle');
         $base_uri = $this->Agent->getTitle()->getFullUrl();
         $prelude = MwRdf::getNamespacePrelude();
         $model->loadStatementsFromString($turtle_parser, $prelude . $rdf);
     }
     return $model;
 }
 function setUp()
 {
     global $wgTitle, $wgParser;
     $wgParser = new Parser();
     $wgParser->Options(new ParserOptions());
     $wgParser->clearState();
     $wgParser->setTitle($wgTitle);
 }
Exemple #5
0
 /**
  * @dataProvider parametersProvider
  * @since 2.0
  * @param array $parameters
  */
 public function testRender(array $parameters)
 {
     $parserHook = $this->getInstance();
     $parser = new \Parser();
     $parser->mOptions = new \ParserOptions();
     $parser->clearState();
     $parser->setTitle(\Title::newMainPage());
     $renderResult = $parserHook->renderTag(null, $parameters, $parser);
     $this->assertInternalType('string', $renderResult);
 }
function renderChosen($input, $argv, &$parser)
{
    # Prevent caching
    $parser->disableCache();
    $pick = 1;
    if (isset($argv['pick'])) {
        $pick = intval($argv['pick']);
    }
    # Parse the options and calculate total weight
    $len = preg_match_all("/<option(?:(?:\\s[^>]*?)?\\sweight=[\"']?([^\\s>]+))?" . "(?:\\s[^>]*)?>([\\s\\S]*?)<\\/option>/", $input, $out);
    $tw = 0;
    for ($i = 0; $i < $len; $i++) {
        if (strlen($out[1][$i]) == 0) {
            $out[1][$i] = 1;
        } else {
            $out[1][$i] = intval($out[1][$i]);
        }
        $tw += $out[1][$i];
    }
    $input = "";
    for ($j = 0; $j < $pick; $j++) {
        # Choose an option at random
        if ($tw <= 0) {
            return "";
        }
        $r = mt_rand(1, $tw);
        for ($i = 0; $i < $len; $i++) {
            $r -= $out[1][$i];
            if ($r <= 0) {
                $input .= $out[2][$i];
                $tw -= $out[1][$i];
                $out[1][$i] = 0;
                # Prevents this item being picked twice
                break;
            }
        }
    }
    # If running new parser, take the easy way out
    if (defined('Parser::VERSION') && version_compare(Parser::VERSION, '1.6.1', '>')) {
        return $parser->recursiveTagParse($input);
    }
    # Otherwise, create new parser to handle rendering
    $localParser = new Parser();
    # Initialize defaults, then copy info from parent parser
    $localParser->clearState();
    $localParser->mTagHooks = $parser->mTagHooks;
    $localParser->mTemplates = $parser->mTemplates;
    $localParser->mTemplatePath = $parser->mTemplatePath;
    $localParser->mFunctionHooks = $parser->mFunctionHooks;
    $localParser->mFunctionSynonyms = $parser->mFunctionSynonyms;
    # Render the chosen option
    $output = $localParser->parse($input, $parser->mTitle, $parser->mOptions, false, false);
    return $output->getText();
}
/**
 * @param $input
 * @param $argv
 * @param Parser $parser
 * @return mixed|string
 */
function renderChosen($input, $argv, $parser)
{
    # Prevent caching
    #$parser->disableCache();
    global $wgParserCacheExpireTime;
    $wgParserCacheExpireTime = 60;
    wfDebug("soft disable Cache (choose)\n");
    # Parse the options and calculate total weight
    $len = preg_match_all("/<option(?:(?:\\s[^>]*?)?\\sweight=[\"']?([^\\s>]+))?" . "(?:\\s[^>]*)?>([\\s\\S]*?)<\\/option>/", $input, $out);
    $r = 0;
    for ($i = 0; $i < $len; $i++) {
        if (strlen($out[1][$i]) == 0) {
            $out[1][$i] = 1;
        } else {
            $out[1][$i] = intval($out[1][$i]);
        }
        $r += $out[1][$i];
    }
    # Choose an option at random
    if ($r <= 0) {
        return "";
    }
    $r = mt_rand(1, $r);
    for ($i = 0; $i < $len; $i++) {
        $r -= $out[1][$i];
        if ($r <= 0) {
            $input = $out[2][$i];
            break;
        }
    }
    # If running new parser, take the easy way out
    if (defined('Parser::VERSION') && version_compare(Parser::VERSION, '1.6.1', '>')) {
        return $parser->recursiveTagParse($input);
    }
    # Otherwise, create new parser to handle rendering
    $localParser = new Parser();
    # Initialize defaults, then copy info from parent parser
    $localParser->clearState();
    $localParser->mTagHooks = $parser->mTagHooks;
    $localParser->mTemplates = $parser->mTemplates;
    $localParser->mTemplatePath = $parser->mTemplatePath;
    $localParser->mFunctionHooks = $parser->mFunctionHooks;
    $localParser->mFunctionSynonyms = $parser->mFunctionSynonyms;
    # Render the chosen option
    $output = $localParser->parse($input, $parser->mTitle, $parser->mOptions, false, false);
    return $output->getText();
}
Exemple #8
0
 public function build()
 {
     global $wgContLang;
     $dc = MwRdf::Vocabulary('dc');
     $dcterms = MwRdf::Vocabulary('dcterms');
     $rdfs = MwRdf::Vocabulary('rdfs');
     $model = MwRdf::Model();
     $tr = $this->Agent->titleResource();
     $article = $this->Agent->getArticle();
     $text = $article->getContent(true);
     $parser = new Parser();
     $parser->mOptions = new ParserOptions();
     $parser->mTitle = $this;
     $parser->initialiseVariables();
     $parser->clearState();
     $tags = array('nowiki');
     $m = array();
     $text = $parser->extractTagsAndParams($tags, $text, $m);
     # XXX: maybe it would actually be better to do this at another
     # stage after the parser has already identified interwiki and
     # lang links
     # Find prefixed links
     preg_match_all("/\\[\\[([^|\\]]+:[^|\\]]+)(\\|.*)?\\]\\]/", $text, $m);
     if (!isset($m[0])) {
         return $model;
     }
     // nothing found so nevermind
     foreach ($m[1] as $linktext) {
         $iwlink = Title::newFromText($linktext);
         if (isset($iwlink)) {
             $pfx = $iwlink->getInterwiki();
             if (strlen($pfx) > 0) {
                 $iwlinkmf = MwRdf::ModelingAgent($iwlink);
                 $iwr = $iwlinkmf->titleResource();
                 # XXX: Wikitravel uses some 4+ prefixes for sister site links
                 if ($wgContLang->getLanguageName($pfx) && strlen($pfx) < 4) {
                     $model->addStatement(MwRdf::Statement($tr, $dcterms->hasVersion, $iwr));
                     $model->addStatement(MwRdf::Statement($iwr, $dc->language, MwRdf::Language($pfx)));
                 } else {
                     # XXX: Express the "sister site" relationship better
                     $model->addStatement(MwRdf::Statement($tr, $rdfs->seeAlso, $iwr));
                 }
             }
         }
     }
     return $model;
 }
Exemple #9
0
 /**
  * Triggers the render process with different sets of parameters to see if
  * no errors or notices are thrown and the result indeed is a string.
  *
  * @dataProvider parametersProvider
  * @since 2.0
  * @param array $parameters
  * @param string|null $expected
  */
 public function testRender(array $parameters, $expected = null)
 {
     $parserHook = $this->getInstance();
     $parser = new \Parser();
     $parser->mOptions = new \ParserOptions();
     $parser->clearState();
     $parser->setTitle(\Title::newMainPage());
     $renderResult = call_user_func_array(array($parserHook, 'renderFunction'), array_merge(array(&$parser), $parameters));
     if (is_string($renderResult)) {
         $this->assertTrue(true);
     } else {
         $this->assertInternalType('array', $renderResult);
         $this->assertInternalType('string', $renderResult[0]);
     }
     if ($expected !== null) {
         $this->assertEquals($expected, $renderResult[0]);
     }
 }
Exemple #10
0
 public function execute()
 {
     global $wgUser, $egMapsDefaultGeoService, $egMapsDistanceDecimals, $egMapsDistanceUnit;
     $params = $this->extractRequestParams();
     $geoCoordinateParser = new DataValues\Geo\Parsers\GeoCoordinateParser();
     $results = array();
     if (Maps\Geocoders::canGeocode()) {
         $location = Maps\Geocoders::attemptToGeocode($params['location'], $egMapsDefaultGeoService);
     } else {
         $location = $geoCoordinateParser->parse($params['location']);
     }
     $query = "{{#ask:[[Bundesland::+]][[aktiv::wahr]][[Lage::+]]|?Lage|?=Name|mainlabel=-|format=array|link=none|headers=plain|headersep==|sep=<BV>}}";
     $mainpage = Title::newMainPage();
     $options = new ParserOptions();
     $localparser = new Parser();
     $localparser->Title($mainpage);
     $localparser->Options($options);
     $localparser->clearState();
     $bedarfsverkehre = $localparser->RecursiveTagParse($query);
     $bedarfsverkehre = explode('&lt;BV&gt;', $bedarfsverkehre);
     foreach ($bedarfsverkehre as $key => $props) {
         $props = explode('&lt;PROP&gt;', $props);
         $bedarfsverkehre[$key] = array();
         foreach ($props as $prop) {
             $prop = explode('=', $prop);
             $bedarfsverkehre[$key][$prop[0]] = $prop[1];
         }
         $bvlocation = $geoCoordinateParser->parse($bedarfsverkehre[$key]['Lage']);
         if ($location && $bvlocation) {
             $bedarfsverkehre[$key]['Distanz'] = MapsGeoFunctions::calculateDistance($location, $bvlocation);
         } else {
             // The locations should be valid when this method gets called.
             throw new MWException('Attempt to find the distance between locations of at least one is invalid' . $bedarfsverkehre[$key]['Name']);
         }
     }
     usort($bedarfsverkehre, array("ApiBVdistances", "distanceSort"));
     $results = array_slice($bedarfsverkehre, 0, 10);
     $this->getResult()->addValue(null, 'results', $results);
 }
Exemple #11
0
function meaneditor_wiki2html($article, $user, &$edit_context, &$wiki_text)
{
    global $wgUploadPath, $wgArticlePath;
    wfLoadExtensionMessages('MeanEditor');
    $meaneditor_page_src = str_replace('$1', '', $wgArticlePath);
    # Detect code sections (lines beginning with whitespace)
    if (preg_match('/^[ \\t]/m', $wiki_text)) {
        return deny_visual_because_of(wfMsg('reason_whitespace'), $edit_context);
    }
    # Detect custom tags: only <br />, super/sub-scripts and references are supported at the moment
    # TODO: expand the safe list
    # Especially problematic tags (do not even think about supporting them):
    #      <p>  (would require special handling to disable normal paragraphing, confusing)
    #      <h*> (for headings not in TOC, strange closing tag)
    #      <b>,<i> (not to be confused with ''emphasis'' as <em>)
    #      <pre>, <nowiki> (if something gets implemented, better be the common leading spaces)
    $wiki_text = str_replace('<br />', '__TEMP__TEMP__br', $wiki_text);
    $wiki_text = str_replace('<br>', '__TEMP__TEMP__br', $wiki_text);
    $wiki_text = str_replace('<references />', '__TEMP__TEMP__allreferences', $wiki_text);
    $wiki_text = str_replace('<ref>', '__TEMP__TEMP__ref', $wiki_text);
    $wiki_text = str_replace('</ref>', '__TEMP__TEMP__cref', $wiki_text);
    $wiki_text = str_replace('<sup>', '__TEMP__TEMP__sup', $wiki_text);
    $wiki_text = str_replace('</sup>', '__TEMP__TEMP__csup', $wiki_text);
    $wiki_text = str_replace('<sub>', '__TEMP__TEMP__sub', $wiki_text);
    $wiki_text = str_replace('</sub>', '__TEMP__TEMP__csub', $wiki_text);
    if (!(strpos($wiki_text, '<') === FALSE && strpos($wiki_text, '>') === FALSE)) {
        return deny_visual_because_of(wfMsg('reason_tag'), $edit_context);
    }
    $wiki_text = str_replace('__TEMP__TEMP__br', '<br />', $wiki_text);
    $wiki_text = str_replace('__TEMP__TEMP__allreferences', 'references_here', $wiki_text);
    $wiki_text = str_replace('__TEMP__TEMP__sup', '<sup>', $wiki_text);
    $wiki_text = str_replace('__TEMP__TEMP__csup', '</sup>', $wiki_text);
    $wiki_text = str_replace('__TEMP__TEMP__sub', '<sub>', $wiki_text);
    $wiki_text = str_replace('__TEMP__TEMP__csub', '</sub>', $wiki_text);
    $wiki_text = str_replace('__TEMP__TEMP__ref', '<ref>', $wiki_text);
    $wiki_text = str_replace('__TEMP__TEMP__cref', '</ref>', $wiki_text);
    # This characters are problematic only at line beginning
    $unwanted_chars_at_beginning = array(':', ';');
    foreach ($unwanted_chars_at_beginning as $uc) {
        if (preg_match('/^' . $uc . '/m', $wiki_text)) {
            return deny_visual_because_of(wfMsg('reason_indent', $uc), $edit_context);
        }
    }
    # <hr>, from Parser.php... TODO: other regexps can be directly stolen from there
    $wiki_text = preg_replace('/(^|\\n)-----*/', '\\1<hr />', $wiki_text);
    #Collapse multiple newlines
    # TODO: Compare Wikipedia:Don't_use_line_breaks
    $wiki_text = preg_replace("/\n\n+/", "\n\n", $wiki_text);
    $wiki_text = preg_replace('/^(.+?)$/m', '<p>$1</p>', $wiki_text);
    #$wiki_text=preg_replace('/\'\'\'(.*?)\'\'\'/','<strong>$1</strong>',$wiki_text);
    #$wiki_text=preg_replace('/\'\'(.*?)\'\'/','<em>$1</em>',$wiki_text);
    $obp = new Parser();
    $obp->clearState();
    $obp->setTitle('');
    $obp->mOptions = new ParserOptions();
    $wiki_text = $obp->doAllQuotes($wiki_text);
    #Substitute ===
    $wiki_text = preg_replace('/(?:<p>|)\\s*===(.*?)===\\s*(?:<\\/p>|)/', '<h3>\\1</h3>', $wiki_text);
    #Substitute ==
    $wiki_text = preg_replace('/(?:<p>|)\\s*==(.*?)==\\s*(?:<\\/p>|)/', '<h2>\\1</h2>', $wiki_text);
    #Substitute [[Image:a]]
    #FIXME: do not require $wgHashedUploadDirectory	= false
    $wiki_text = preg_replace('/\\[\\[Image:(.*?)\\]\\]/', '<img alt="\\1" src="' . $wgUploadPath . '/\\1" />', $wiki_text);
    #Create [[a|b]] syntax for every link
    #TODO: What to do for the [[word (detailed disambiguation)|]] 'pipe trick'?
    $wiki_text = preg_replace('/\\[\\[([^|]*?)\\]\\]/', '[[\\1|\\1]]', $wiki_text);
    #Substitute [[ syntax (internal links)
    if (preg_match('/\\[\\[([^|\\]]*?):(.*?)\\|(.*?)\\]\\]/', $wiki_text, $unwanted_matches)) {
        return deny_visual_because_of(wfMsg('reason_special_link', $unwanted_matches[0]), $edit_context);
    }
    #Preserve #section links from the draconic feature detection
    $wiki_text = preg_replace_callback('/\\[\\[(.*?)\\|(.*?)\\]\\]/', create_function('$matches', 'return "[[".str_replace("#","__TEMP_MEAN_hash",$matches[1])."|".str_replace("#","__TEMP_MEAN_hash",$matches[2])."]]";'), $wiki_text);
    $wiki_text = preg_replace_callback('/<a href="(.*?)">/', create_function('$matches', 'return "<a href=\\"".str_replace("#","__TEMP_MEAN_hash",$matches[1])."\\">";'), $wiki_text);
    $wiki_text = preg_replace('/\\[\\[(.*?)\\|(.*?)\\]\\]/', '<a href="' . $meaneditor_page_src . '\\1">\\2</a>', $wiki_text);
    #Create [a b] syntax for every link
    #(must be here, so that internal links have already been replaced)
    $wiki_text = preg_replace('/\\[([^| ]*?)\\]/', '[\\1 _autonumber_]', $wiki_text);
    #Substitute [ syntax (external links)
    $wiki_text = preg_replace('/\\[(.*?) (.*?)\\]/', '<a href="\\1">\\2</a>', $wiki_text);
    #Lists support
    $wiki_text = preg_replace("/<p># (.*?)<\\/p>/", '<ol><li>\\1</li></ol>', $wiki_text);
    $wiki_text = preg_replace("/<p>\\* (.*?)<\\/p>/", '<ul><li>\\1</li></ul>', $wiki_text);
    $wiki_text = preg_replace("/<\\/ol>\n<ol>/", "\n", $wiki_text);
    $wiki_text = preg_replace("/<\\/ul>\n<ul>/", "\n", $wiki_text);
    # Crude but safe detection of unsupported features
    # In the future, this could be loosened a lot, should also detect harmless uses
    # TODO: Compare with MediaWiki security policy, ensure no mediawiki code can create unsafe HTML in the editor
    # Allow numbered entities, these occur far too often and should be innocous
    $wiki_text = str_replace('&#', '__TEMP__MEAN__nument', $wiki_text);
    $unwanted_chars = array('[', ']', '|', '{', '}', '#', '*');
    foreach ($unwanted_chars as $uc) {
        if (!($unwanted_match = strpos($wiki_text, $uc) === FALSE)) {
            return deny_visual_because_of(wfMsg('reason_forbidden_char', $uc), $edit_context);
        }
    }
    # Restore numbered entities
    $wiki_text = str_replace('__TEMP__MEAN__nument', '&#', $wiki_text);
    #<ref> support
    global $refs_div;
    global $refs_num;
    $refs_div = '';
    $refs_num = 0;
    $wiki_text = preg_replace_callback('/<ref>(.*?)<\\/ref>/', create_function('$matches', 'global $refs_div,$refs_num; $refs_num++; $refs_div=$refs_div."<p id=ref".$refs_num." class=\\"ref\\"> [".$refs_num."] ".
				$matches[1]."</p>"; return "<a href=\\"#ref".$refs_num."\\"> [".$refs_num."] </a>";'), $wiki_text);
    $refs_div = '<div class="ref">' . $refs_div . "</div>";
    # We saved #section links from the sacred detection fury, now restore them
    $wiki_text = str_replace("__TEMP_MEAN_hash", "#", $wiki_text);
    $wiki_text = $wiki_text . $refs_div;
    return false;
}
Exemple #12
0
 /**
  * Render navigations elements that renderNavigation hasn't dealt with
  *
  * @param $buttons array
  * @param $customItems array
  */
 private function renderCustomNavigation(&$buttons, &$customItems)
 {
     /* TODO: check for unintended consequences, there are probably more elegant ways to do this... */
     $options = new ParserOptions();
     $localParser = new Parser();
     $localParser->Title($this->getSkin()->getTitle());
     $localParser->Options($options);
     $localParser->clearState();
     if (count($customItems) !== 0) {
         $newButtons = TweekiHooks::parseButtons(implode(chr(10), $customItems), $localParser, false);
         $buttons = array_merge($buttons, $newButtons);
         $customItems = array();
     }
 }
 /**
  * Clear Parser state
  *
  * @private
  */
 function clearState()
 {
     parent::clearState();
     // don't show TOC in edit mode
     $this->mShowToc = false;
 }