/** * Constructor * @param Title $title * @param File|bool $file */ public function __construct(Title $title, $file = false) { $this->title = $title; // @todo FIXME: check existence if (defined('PAGE_IMAGES_INSTALLED')) { $this->usePageImages = true; $this->file = $file ? $file : PageImages::getPageImage($title); } }
/** * Convert the JSON to wikitext. * * @param $lang Language The (content) language to render the page in. * @param $options Array Options to override the default transclude options * @return string The wikitext */ public function convertToWikitext(Language $lang, $options = []) { $this->decode(); $options = $options + $this->getOverrideOptions() + $this->getDefaultOptions(); $maxItems = $options['maxItems']; $includeDesc = $options['includeDesc']; // If this is an error-type list (i.e. a schema-violating blob), // just return the plain JSON. if ($this->displaymode == 'error') { $errorWikitext = '<div class=errorbox>' . wfMessage('collaborationkit-list-invalid') . "</div>\n<pre>" . $this->errortext . '</pre>'; return $errorWikitext; } // Hack to force style loading even when we don't have a Parser reference. $text = "<collaborationkitloadliststyles/>\n"; if ($includeDesc) { $text .= $this->getDescription() . "\n"; } if (count($this->columns) === 0) { $text .= "\n{{mediawiki:collaborationkit-list-isempty}}\n"; return $text; } $listclass = count($this->columns) > 1 ? 'mw-ck-multilist' : 'mw-ck-singlelist'; $text .= '<div class="mw-ck-list ' . $listclass . '">' . "\n"; $offset = $options['defaultSort'] === 'random' ? 0 : $options['offset']; foreach ($this->columns as $column) { $text .= '<div class="mw-ck-list-column">' . "\n"; if (isset($column->label) && $column->label !== '') { $text .= "=== {$column->label} ===\n"; } if (isset($column->notes) && $column->notes !== '') { $text .= "\n{$column->notes}\n\n"; } if (count($column->items) === 0) { $text .= "\n{{mediawiki:collaborationkit-list-emptycolumn}}\n"; continue; } $curItem = 0; $sortedItems = $column->items; $this->sortList($sortedItems, $options['defaultSort']); foreach ($sortedItems as $item) { if ($offset !== 0) { $offset--; continue; } $curItem++; if ($maxItems !== false && $maxItems < $curItem) { break; } $itemTags = isset($item->tags) ? $item->tags : []; if (!$this->matchesTag($options['tags'], $itemTags)) { continue; } $titleForItem = null; if (!isset($item->link)) { $titleForItem = Title::newFromText($item->title); } elseif ($item->link !== false) { $titleForItem = Title::newFromText($item->link); } $text .= Html::openElement('div', ["class" => "mw-ck-list-item", "data-collabkit-item-title" => $item->title]); if ($options['mode'] !== 'no-img') { $image = null; if (!isset($item->image) && $titleForItem) { if (class_exists('PageImages')) { $image = PageImages::getPageImage($titleForItem); } } elseif (isset($item->image) && is_string($item->image)) { $imageTitle = Title::newFromText($item->image, NS_FILE); if ($imageTitle) { $image = wfFindFile($imageTitle); } } $text .= '<div class="mw-ck-list-image">'; if ($image) { // Important: If you change the width of the image // you also need to change it in the stylesheet. $text .= '[[File:' . $image->getName() . "|left|64px|alt=]]\n"; } else { if ($this->displaymode == 'members') { $placeholderIcon = 'mw-ck-icon-user-grey2'; } else { $placeholderIcon = 'mw-ck-icon-page-grey2'; } $text .= Html::element('div', ["class" => ['mw-ck-list-noimageplaceholder', $placeholderIcon]]); } $text .= '</div>'; } $text .= '<div class="mw-ck-list-container">'; // Question: Arguably it would be more semantically correct to use // an <Hn> element for this. Would that be better? Unclear. $text .= '<div class="mw-ck-list-title">'; if ($titleForItem) { if ($this->displaymode == 'members' && !isset($item->link) && $titleForItem->inNamespace(NS_USER)) { $titleText = $titleForItem->getText(); } else { $titleText = $item->title; } $text .= "[[:" . $titleForItem->getPrefixedDBkey() . "|" . wfEscapeWikiText($titleText) . "]]"; } else { $text .= wfEscapeWikiText($item->title); } $text .= "</div>\n"; $text .= '<div class="mw-ck-list-notes">' . "\n"; if (isset($item->notes) && is_string($item->notes)) { $text .= $item->notes . "\n"; } if (isset($item->tags) && is_array($item->tags) && count($item->tags)) { $text .= "\n<div class='toccolours mw-ck-list-tags'>" . wfMessage('collaborationkit-list-taglist')->inLanguage($lang)->params($lang->commaList(array_map('wfEscapeWikiText', $item->tags)))->numParams(count($item->tags))->text() . "</div>\n"; } $text .= '</div></div></div>' . "\n"; } $text .= "\n</div>"; } $text .= "\n</div>"; return $text; }
/** * Wrapper that returns a page image for a given title * * @param Title $title * @return bool|File */ protected function getPageImage(Title $title) { return PageImages::getPageImage($title); }