Esempio n. 1
0
 public function parse(Node $node)
 {
     $result = self::nodeToString($node);
     //Clean text
     $result = WikiUtil::removeWikiEmphasis($result);
     $result = StringUtil::htmlDecode($result);
     if (strlen($result) > 0) {
         return $result;
     }
 }
Esempio n. 2
0
 public function generate($baseUri, $node)
 {
     if (isset($node)) {
         //Retrieve text
         $text = $this->nodeToText($node);
         //Normalize text
         $text = WikiUtil::removeWikiEmphasis($text);
         $text = StringUtil::htmlDecode($text);
         $text = preg_replace('/ +/', ' ', $text);
         //remove duplicate spaces
         $text = str_replace('(', ' ', $text);
         $text = str_replace(')', ' ', $text);
         $text = strip_tags($text);
         $text = substr($text, 0, 50);
         $text = trim($text);
         $text = str_replace(' ', '_', $text);
         $text = urlencode($text);
         //Test if the base URI ends with a prefix of text
         $baseLen = strlen($baseUri);
         $textLen = strlen($text);
         for ($i = $baseLen - 1; $i > 0 && $baseLen - $i < $textLen; $i--) {
             if (substr_compare($baseUri, $text, $i, $textLen, true) === 0) {
                 $text = substr($text, $baseLen - $i);
                 break;
             }
         }
         //Remove leading underscore
         if (!empty($text) && $text[0] === '_') {
             $text = substr($text, 1);
         }
         //Generate URI
         $uri = $baseUri . '__' . $text;
     } else {
         $uri = $baseUri;
     }
     //Resolve collisions
     if (!isset($this->uris[$uri])) {
         //No collision
         $this->uris[$uri] = 1;
     } else {
         //Collision found
         $index = $this->uris[$uri];
         $this->uris[$uri] = $index + 1;
         $uri .= '__' . $index;
     }
     return $uri;
 }
Esempio n. 3
0
 /**
  * @param $nsCode namespace code
  * @param $decoded decoded page name. URL-decoded, using normalized spaces (not underscores), first letter uppercase.
  */
 public function __construct($nsCode, $decoded)
 {
     PhpUtil::assertInteger($nsCode, 'namespace code');
     if (!isset(self::$nsNames[$nsCode])) {
         throw new \InvalidArgumentException('unknown namespace code ' . $nsCode);
     }
     PhpUtil::assertString($decoded, 'page name');
     if (strlen($decoded) === 0) {
         throw new WikiParserException('page name must not be empty');
     }
     $this->nsCode = $nsCode;
     $this->nsName = self::$nsNames[$nsCode][0];
     $this->encoded = WikiUtil::wikiEncode($decoded);
     // re-decode to make sure name is normalized
     $this->decoded = WikiUtil::wikiDecode($this->encoded);
 }
Esempio n. 4
0
 private function parseTemplate($templateNode, $source)
 {
     while (true) {
         $propertyNode = new PropertyNode($source->getLine());
         $this->parseProperty($propertyNode, $source);
         //The first entry denotes the name of the template
         if ($templateNode->getTitle() == null) {
             $templateName = $propertyNode->getText();
             if (!$templateName) {
                 throw new WikiParserException("Template name contains invalid elements", $templateNode->getLine(), $source->findLine($templateNode->getLine()));
             }
             $templateNode->setTitle(new WikiTitle(WikiTitle::NS_TEMPLATE, WikiUtil::wikiDecode(trim($templateName))));
         } else {
             $templateNode->addProperty($propertyNode);
         }
         //Reached template end?
         if ($source->lastTag('}}')) {
             return $templateNode;
         }
     }
 }