Esempio n. 1
0
 /**
  * Get text before first heading.
  * @param string $text
  * @return string|null
  */
 private function extractHeadingBeforeFirstHeading($text)
 {
     $matches = [];
     if (!preg_match('/<h[123456]>/', $text, $matches, PREG_OFFSET_CAPTURE)) {
         // There isn't a first heading so we interpret this as the article
         // being entirely without heading.
         return null;
     }
     $text = substr($text, 0, $matches[0][1]);
     if (!$text) {
         // There isn't any text before the first heading so we declare there isn't
         // a first heading.
         return null;
     }
     $formatter = new HtmlFormatter($text);
     $formatter->remove($this->excludedElementSelectors);
     $formatter->remove($this->auxiliaryElementSelectors);
     $formatter->filterContent();
     $text = trim(Sanitizer::stripAllTags($formatter->getText()));
     if (!$text) {
         // There isn't any text after filtering before the first heading so we declare
         // that there isn't a first heading.
         return null;
     }
     return $text;
 }
Esempio n. 2
0
 /**
  * Replace Special:ApiHelp links with links to api.php
  *
  * @param string $html
  * @param string|null $helptitle Title to link to rather than api.php, must contain '$1'
  * @param array $localModules Keys are modules to link within the current page, values are ignored
  * @return string
  */
 public static function fixHelpLinks($html, $helptitle = null, $localModules = [])
 {
     $formatter = new HtmlFormatter($html);
     $doc = $formatter->getDoc();
     $xpath = new DOMXPath($doc);
     $nodes = $xpath->query('//a[@href][not(contains(@class,\'apihelp-linktrail\'))]');
     foreach ($nodes as $node) {
         $href = $node->getAttribute('href');
         do {
             $old = $href;
             $href = rawurldecode($href);
         } while ($old !== $href);
         if (preg_match('!Special:ApiHelp/([^&/|#]+)((?:#.*)?)!', $href, $m)) {
             if (isset($localModules[$m[1]])) {
                 $href = $m[2] === '' ? '#' . $m[1] : $m[2];
             } elseif ($helptitle !== null) {
                 $href = Title::newFromText(str_replace('$1', $m[1], $helptitle) . $m[2])->getFullURL();
             } else {
                 $href = wfAppendQuery(wfScript('api'), ['action' => 'help', 'modules' => $m[1]]) . $m[2];
             }
             $node->setAttribute('href', $href);
             $node->removeAttribute('title');
         }
     }
     return $formatter->getText();
 }