Exemplo n.º 1
0
 /**
  * Renders an image gallery from a text with one line per image.
  * text labels may be given by using |-style alternative text. E.g.
  *   Image:one.jpg|The number "1"
  *   Image:tree.jpg|A tree
  * given as text will return the HTML of a gallery with two images,
  * labeled 'The number "1"' and
  * 'A tree'.
  *
  * @param string $text
  * @param array $params
  * @return string HTML
  */
 function renderImageGallery($text, $params)
 {
     wfProfileIn(__METHOD__);
     $mode = false;
     if (isset($params['mode'])) {
         $mode = $params['mode'];
     }
     try {
         $ig = ImageGalleryBase::factory($mode);
     } catch (MWException $e) {
         // If invalid type set, fallback to default.
         $ig = ImageGalleryBase::factory(false);
     }
     $ig->setContextTitle($this->mTitle);
     $ig->setShowBytes(false);
     $ig->setShowFilename(false);
     $ig->setParser($this);
     $ig->setHideBadImages();
     $ig->setAttributes(Sanitizer::validateTagAttributes($params, 'table'));
     if (isset($params['showfilename'])) {
         $ig->setShowFilename(true);
     } else {
         $ig->setShowFilename(false);
     }
     if (isset($params['caption'])) {
         $caption = $params['caption'];
         $caption = htmlspecialchars($caption);
         $caption = $this->replaceInternalLinks($caption);
         $ig->setCaptionHtml($caption);
     }
     if (isset($params['perrow'])) {
         $ig->setPerRow($params['perrow']);
     }
     if (isset($params['widths'])) {
         $ig->setWidths($params['widths']);
     }
     if (isset($params['heights'])) {
         $ig->setHeights($params['heights']);
     }
     $ig->setAdditionalOptions($params);
     wfRunHooks('BeforeParserrenderImageGallery', array(&$this, &$ig));
     $lines = StringUtils::explode("\n", $text);
     foreach ($lines as $line) {
         # match lines like these:
         # Image:someimage.jpg|This is some image
         $matches = array();
         preg_match("/^([^|]+)(\\|(.*))?\$/", $line, $matches);
         # Skip empty lines
         if (count($matches) == 0) {
             continue;
         }
         if (strpos($matches[0], '%') !== false) {
             $matches[1] = rawurldecode($matches[1]);
         }
         $title = Title::newFromText($matches[1], NS_FILE);
         if (is_null($title)) {
             # Bogus title. Ignore these so we don't bomb out later.
             continue;
         }
         # We need to get what handler the file uses, to figure out parameters.
         # Note, a hook can overide the file name, and chose an entirely different
         # file (which potentially could be of a different type and have different handler).
         $options = array();
         $descQuery = false;
         wfRunHooks('BeforeParserFetchFileAndTitle', array($this, $title, &$options, &$descQuery));
         # Don't register it now, as ImageGallery does that later.
         $file = $this->fetchFileNoRegister($title, $options);
         $handler = $file ? $file->getHandler() : false;
         wfProfileIn(__METHOD__ . '-getMagicWord');
         $paramMap = array('img_alt' => 'gallery-internal-alt', 'img_link' => 'gallery-internal-link');
         if ($handler) {
             $paramMap = $paramMap + $handler->getParamMap();
             // We don't want people to specify per-image widths.
             // Additionally the width parameter would need special casing anyhow.
             unset($paramMap['img_width']);
         }
         $mwArray = new MagicWordArray(array_keys($paramMap));
         wfProfileOut(__METHOD__ . '-getMagicWord');
         $label = '';
         $alt = '';
         $link = '';
         $handlerOptions = array();
         if (isset($matches[3])) {
             // look for an |alt= definition while trying not to break existing
             // captions with multiple pipes (|) in it, until a more sensible grammar
             // is defined for images in galleries
             // FIXME: Doing recursiveTagParse at this stage, and the trim before
             // splitting on '|' is a bit odd, and different from makeImage.
             $matches[3] = $this->recursiveTagParse(trim($matches[3]));
             $parameterMatches = StringUtils::explode('|', $matches[3]);
             foreach ($parameterMatches as $parameterMatch) {
                 list($magicName, $match) = $mwArray->matchVariableStartToEnd($parameterMatch);
                 if ($magicName) {
                     $paramName = $paramMap[$magicName];
                     switch ($paramName) {
                         case 'gallery-internal-alt':
                             $alt = $this->stripAltText($match, false);
                             break;
                         case 'gallery-internal-link':
                             $linkValue = strip_tags($this->replaceLinkHoldersText($match));
                             $chars = self::EXT_LINK_URL_CLASS;
                             $prots = $this->mUrlProtocols;
                             //check to see if link matches an absolute url, if not then it must be a wiki link.
                             if (preg_match("/^({$prots}){$chars}+\$/u", $linkValue)) {
                                 $link = $linkValue;
                             } else {
                                 $localLinkTitle = Title::newFromText($linkValue);
                                 if ($localLinkTitle !== null) {
                                     $link = $localLinkTitle->getLocalURL();
                                 }
                             }
                             break;
                         default:
                             // Must be a handler specific parameter.
                             if ($handler->validateParam($paramName, $match)) {
                                 $handlerOptions[$paramName] = $match;
                             } else {
                                 // Guess not. Append it to the caption.
                                 wfDebug("{$parameterMatch} failed parameter validation\n");
                                 $label .= '|' . $parameterMatch;
                             }
                     }
                 } else {
                     // concatenate all other pipes
                     $label .= '|' . $parameterMatch;
                 }
             }
             // remove the first pipe
             $label = substr($label, 1);
         }
         $ig->add($title, $label, $alt, $link, $handlerOptions);
     }
     $html = $ig->toHTML();
     wfProfileOut(__METHOD__);
     return $html;
 }