Harej has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/335772 )

Change subject: Refactored image- and icon-related functionality.
......................................................................

Refactored image- and icon-related functionality.

This replaces the CollaborationKitIcon class with a new CollaborationKitImage 
class that handles image and icon loading for the whole extension.

This also fixes the bug with list icons.

Bug: T148083
Bug: T148084
Change-Id: If932d2e80ca65c612c6f66c43ab926be00941634
---
M extension.json
M includes/SpecialCreateHubFeature.php
M includes/content/CollaborationHubContent.php
M includes/content/CollaborationHubTOC.php
D includes/content/CollaborationKitIcon.php
A includes/content/CollaborationKitImage.php
M includes/content/CollaborationListContent.php
M includes/content/CollaborationListContentSchema.php
8 files changed, 329 insertions(+), 234 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CollaborationKit 
refs/changes/72/335772/1

diff --git a/extension.json b/extension.json
index a7bafb7..3f334da 100644
--- a/extension.json
+++ b/extension.json
@@ -1,6 +1,6 @@
 {
        "name": "CollaborationKit",
-       "version": "0.3",
+       "version": "0.4",
        "author": [ "Kim Schoonover", "Brian Wolff", "James Hare" ],
        "url": "https://www.mediawiki.org/wiki/Extension:CollaborationKit";,
        "descriptionmsg": "collaborationkit-desc",
@@ -22,7 +22,7 @@
                "CollaborationHubContentHandler": 
"includes/content/CollaborationHubContentHandler.php",
                "CollaborationHubContentEditor": 
"includes/CollaborationHubContentEditor.php",
                "CollaborationHubTOC": 
"includes/content/CollaborationHubTOC.php",
-               "CollaborationKitIcon": 
"includes/content/CollaborationKitIcon.php",
+               "CollaborationKitImage": 
"includes/content/CollaborationKitImage.php",
                "CollaborationListContent": 
"includes/content/CollaborationListContent.php",
                "CollaborationListContentHandler": 
"includes/content/CollaborationListContentHandler.php",
                "CollaborationListContentEditor": 
"includes/CollaborationListContentEditor.php",
diff --git a/includes/SpecialCreateHubFeature.php 
b/includes/SpecialCreateHubFeature.php
index b64176b..7063467 100644
--- a/includes/SpecialCreateHubFeature.php
+++ b/includes/SpecialCreateHubFeature.php
@@ -20,7 +20,7 @@
                $output = $this->getContext()->getOutput();
                $output->addModules( 'ext.CollaborationKit.iconbrowser' );
                $output->addModuleStyles( 'ext.CollaborationKit.edit.styles' );
-               $output->addJsConfigVars( 'wgCollaborationKitIconList', 
CollaborationKitIcon::getCannedIcons() );
+               $output->addJsConfigVars( 'wgCollaborationKitIconList', 
CollaborationKitImage::getCannedIcons() );
                parent::execute( $par );
        }
 
@@ -43,7 +43,7 @@
                        $defaultFeatureName = '';
                }
 
-               $icons = CollaborationKitIcon::getCannedIcons();
+               $icons = CollaborationKitImage::getCannedIcons();
                $iconChoices = array_combine( $icons, $icons );
 
                $fields = [
diff --git a/includes/content/CollaborationHubContent.php 
b/includes/content/CollaborationHubContent.php
index edf956c..e958571 100644
--- a/includes/content/CollaborationHubContent.php
+++ b/includes/content/CollaborationHubContent.php
@@ -356,7 +356,8 @@
                                        'maxItems' => 3,
                                        'defaultSort' => 'random',
                                        'columns' => [ $activeCol ],
-                                       'showColumnHeaders' => false
+                                       'showColumnHeaders' => false,
+                                       'iconWidth' => 32
                                ]
                        );
 
@@ -661,8 +662,7 @@
         */
        protected function getTableOfContents( Title $title, ParserOptions 
$options ) {
                $toc = new CollaborationHubTOC();
-
-               return $toc->renderTOC( $this->content, $this->themeColour );
+               return $toc->renderToC( $this->content );
        }
 
        /**
@@ -673,7 +673,7 @@
         * @return string
         */
        public function getParsedImage( $image, $size = 200 ) {
-               return CollaborationKitIcon::makeIconOrImage( 
$this->getImage(), $size, 'puzzlepiece' );
+               return CollaborationKitImage::makeImage( $image, $size, [ 
'fallback' => 'puzzlepiece' ] );
        }
 
        /**
diff --git a/includes/content/CollaborationHubTOC.php 
b/includes/content/CollaborationHubTOC.php
index 5ebdaf4..20dde3a 100644
--- a/includes/content/CollaborationHubTOC.php
+++ b/includes/content/CollaborationHubTOC.php
@@ -1,7 +1,5 @@
 <?php
 
-use MediaWiki\MediaWikiServices;
-
 class CollaborationHubTOC {
 
        /** @var $tocLinks array ids/links for ToC items that have been used 
already */
@@ -35,7 +33,6 @@
        /**
         * ToC rendering for hub
         * @param $content array block from collaborationhub
-        * @param $colour string variable from collaborationhub content
         * @return string html
         */
        public function renderToC( $content ) {
@@ -58,9 +55,9 @@
                                $displayTitle = $title->getSubpageText();
                        }
                        $linkTarget = Title::newFromText( '#' . 
$this->getToCLinkID( $displayTitle ) );
-                       $image = isset( $item['image'] ) ? $item['image'] : 
$displayTitle;
+                       $image = isset( $item['image'] ) ? $item['image'] : 
null;
 
-                       $link = $this->renderItem( $linkTarget, $displayTitle, 
$image, 50 );
+                       $link = CollaborationKitImage::makeImage( $image, 50, [ 
'link' => $linkTarget, 'label' => $displayTitle ] );
 
                        $html .= Html::rawElement(
                                'li',
@@ -80,8 +77,6 @@
         * @return string html
         */
        public function renderSubpageToC( Title $title ) {
-               $linkRenderer = 
MediaWikiServices::getInstance()->getLinkRenderer();
-
                // We assume $title is sane. This is supposed to be called with 
a $title gotten from CollaborationHubContent::getParentHub, which already 
checks if it is.
                $rev = Revision::newFromTitle( $title );
                $content = $rev->getContent();
@@ -105,7 +100,7 @@
                // hubpage
 
                $name = $content->getDisplayName() == '' ? $title->getText() : 
$content->getDisplayName();
-               $link = $this->renderItem( $title, $name, $image, 16 );
+               $link = CollaborationKitImage::makeImage( $image, 16, [ 'link' 
=> $title, 'label' => $name ] );
 
                $html .= Html::rawElement(
                        'div',
@@ -126,7 +121,11 @@
                        }
                        $itemImage = isset( $item['image'] ) ? $item['image'] : 
$itemDisplayTitle;
 
-                       $itemLink = $this->renderItem( $itemTitle, 
$itemDisplayTitle, $itemImage, $colour, 16 );
+                       $itemLink = CollaborationKitImage::makeImage(
+                               $itemImage,
+                               16,
+                               [ 'link' => $itemTitle, 'label' => 
$itemDisplayTitle, 'colour' => $colour ]
+                       );
 
                        $html .= Html::rawElement(
                                'li',
@@ -139,26 +138,5 @@
                $html .= Html::closeElement( 'div' );
                $html .= Html::closeElement( 'div' );
                return $html;
-       }
-
-       /**
-        * Get item for ToC - link with icon and label as contents
-        * @param $title Title for target
-        * @param $text string diplay text for title
-        * @param $image string seed for makeIconOrImage
-        * @param $imageSize int size
-        * @return string html
-        */
-       protected function renderItem( Title $title, $text, $image, $imageSize 
) {
-               $linkRenderer = 
MediaWikiServices::getInstance()->getLinkRenderer();
-
-               $icon = CollaborationKitIcon::makeIconOrImage( $image, 
$imageSize );
-
-               $linkContent = new HtmlArmor( Html::rawElement(
-                       'div',
-                       [],
-                       $icon . Html::element( 'span', [ 'class' => 
'mw-ck-toc-item-label' ], $text )
-               ) );
-               return $link = $linkRenderer->makeLink( $title, $linkContent );
        }
 }
diff --git a/includes/content/CollaborationKitIcon.php 
b/includes/content/CollaborationKitIcon.php
deleted file mode 100644
index 0432b18..0000000
--- a/includes/content/CollaborationKitIcon.php
+++ /dev/null
@@ -1,154 +0,0 @@
-<?php
-
-class CollaborationKitIcon {
-
-       /**
-        * Generate an in icon based on an on-wiki file or a canned CK icon
-        * @param $icon string icon id, filename, or random seed
-        * @param $size int intended height/width for rendered icon in px
-        * @param $fallback string what to do for no icon; allowed values are 
'random', 'none', or a valid icon id
-        * @return string html
-        */
-       public static function makeIconOrImage( $icon, $size = 50, $colour = 
'black', $fallback = 'random' ) {
-               // We were going to only find files with file name extensions, 
but that's hard to parse, and there's no way to really handle ones that aren't 
uploaded, so we'll just look and see if they're uploaded and be done with it.
-               if ( wfFindFile( $icon ) ) {
-                       // TODO also find if prefixed with 'file:', 'image:', 
etc
-                       return CollaborationKitIcon::makeImage( $icon, $size );
-               } elseif ( $fallback == 'none' ) {
-                       return '';
-               } else {
-                       // canned icons time
-
-                       $iconsPreset = CollaborationKitIcon::getCannedIcons();
-
-                       if ( !in_array( $icon, $iconsPreset ) && in_array( 
$fallback, $iconsPreset ) ) {
-                               return CollaborationKitIcon::makeIcon( 
$fallback, $size, 'lightgrey', '#eee' );
-                       } else {
-                               // makeicon falls back to making a random icon 
anyway, and we've ruled out all the other fallbacks at this point
-                               return CollaborationKitIcon::makeIcon( $icon, 
$size, $colour );
-                       }
-               }
-       }
-
-       /**
-        * Generate an in icon using a canned CK icon
-        * @param $icon string icon id or random seed
-        * @param $size int intended height/width for rendered icon in px
-        * @param $fallback string what to do for no icon; allowed values are 
'random', 'none', or a valid icon id
-        * @return string html
-        */
-       public static function makeIcon( $icon, $size = 50, $colour, 
$background = 'transparent' ) {
-               $iconsPreset = CollaborationKitIcon::getCannedIcons();
-
-               if ( in_array( $icon, $iconsPreset ) ) {
-                       $iconClass = Sanitizer::escapeClass( $icon );
-               } else {
-                       // Random time
-                       // Choose class name using $icon value as seed
-                       $iconClass = $iconsPreset[ hexdec( sha1( $icon )[0] ) % 
27];
-               }
-
-               if ( !isset( $colour ) || $colour == 'black' ) {
-                       $colorSuffix = '';
-               } else {
-                       $colorSuffix = '-' . $colour;
-               }
-               return Html::element(
-                       'div',
-                       [
-                               'class' => [
-                                       'mw-ck-icon',
-                                       'mw-ck-icon-' . $iconClass .  
$colorSuffix
-                               ],
-                               'css' => "height: {$size}px; width: {$size}px; 
background-color: $background;"
-                       ]
-               );
-       }
-
-       /**
-        * Make an image from a file onwiki
-        * Assumes the file exists, and this was already checked. Doesn't work 
if it doesn't.
-        * @param $file string filename
-        * @param $size int width, height in px
-        * @param $background string colour for optional css backgroundstuff
-        * @return string html
-        */
-       public static function makeImage( $file, $size = 50, $background = 
'transparent' ) {
-               return Html::rawElement(
-                       'div',
-                       [
-                               'class' => 'mw-ck-file-image',
-                               'css' => "max-height: {$size}px; 
background-color: $background;"
-                       ],
-                       wfFindFile( $file )->transform( [ 'width' => $size ] 
)->toHtml()
-               );
-       }
-
-       /**
-        * @return array of stupidly many icons
-        */
-       public static function getCannedIcons() {
-               // Keep this synced with the icons listed in the module in 
extension.json
-               return $iconsPreset = [
-                       // Randomly selectable items
-                       'book',
-                       'circlestar',
-                       'clock',
-                       'community',
-                       'contents',
-                       'die',
-                       'edit',
-                       'eye',
-                       'flag',
-                       'funnel',
-                       'gear',
-                       'heart',
-                       'journal',
-                       'key',
-                       'link',
-                       'map',
-                       'menu',
-                       'newspaper',
-                       'ol',
-                       'page',
-                       'paperclip',
-                       'puzzlepiece',
-                       'ribbon',
-                       'rocket',
-                       'star',
-                       'sun',
-                       'ul',
-
-                       'addimage',
-                       'addmapmarker',
-                       'addquote',
-                       'bell',
-                       'circleline',
-                       'circletriangle',
-                       'circlex',
-                       'discussion',
-                       'download',
-                       'editprotected',
-                       'gallery',
-                       'image',
-                       'lock',
-                       'mail',
-                       'mapmarker',
-                       'message',
-                       'messagenew',
-                       'messagescary',
-                       'move',
-                       'nowiki',
-                       'pagechecked',
-                       'pageribbon',
-                       'pagesearch',
-                       'print',
-                       'quotes',
-                       'search',
-                       'starmenu',
-                       'translate',
-                       'trash',
-                       'user'
-               ];
-       }
-}
diff --git a/includes/content/CollaborationKitImage.php 
b/includes/content/CollaborationKitImage.php
new file mode 100644
index 0000000..9551f6b
--- /dev/null
+++ b/includes/content/CollaborationKitImage.php
@@ -0,0 +1,238 @@
+<?php
+
+/**
+ *
+ * Helper class to produce HTML elements containing images for 
CollaborationKit purposes
+ *
+ */
+class CollaborationKitImage {
+       /**
+        * Generate an image element from the wiki or the extension
+        * @param $image string|null The filename (no namespace prefix) or 
CollaborationKit icon
+        *      identifier (or null to use fallback instead)
+        * @param $width int The width of the image in pixels
+        * @param $options array An array with optional parameters
+        * @param $options['classes'] array Array of element classes to assign
+        * @param $options['link'] Title|string|bool Internal link for the 
image; default is true (i.e.
+        *      link to its description page). Pass `false` for no link at all. 
Pass a string to link to a
+        *      page in the manner of an internal wiki link.
+        * @param $options['colour'] string The colour of the icon if using a 
canned icon
+        * @param $options['css'] string In-line style parameters. Avoid if 
possible.
+        * @param $options['renderAsWikitext'] bool Should the output be 
wikitext instead of HTML?
+        *      Defaults to false.
+        * @param $options['label'] string Label to put under image; used for 
ToC icons
+        * @param $options['fallback'] string If the specified image is null or 
doesn't exist. Valid
+        *      options are none', a valid icon ID, or an arbitrary string to 
use a seed. (Note: if you
+        *      specify a label, then that will serve as the fallback.)
+        * @return string HTML elements or wikitext, depending on 
$options['renderAsWikitext']
+        */
+       public static function makeImage( $image, $width, $options = [] ) {
+
+               // Default options
+               if ( !isset( $options[ 'classes' ] ) ) {
+                       $options[ 'classes' ] = [];
+               }
+               if ( !isset( $options[ 'link' ] ) ) {
+                       $options[ 'link' ] = true;
+               }
+               if ( !isset( $options[ 'colour' ] ) ) {
+                       $options[ 'colour' ] = '';
+               }
+               if ( !isset( $options[ 'css' ] ) ) {
+                       $options[ 'css' ] = '';
+               }
+               if ( !isset( $options[ 'renderAsWikitext' ] ) ) {
+                       $options[ 'renderAsWikitext' ] = false;
+               }
+               if ( !isset( $options[ 'label' ] ) ) {
+                       $options[ 'label' ] = '';
+               }
+               if ( !isset( $options[ 'fallback' ] ) ) {
+                       if ( isset( $options[ 'label' ] ) ) {
+                               $options[ 'fallback' ] = $options[ 'label' ];
+                       } else {
+                               $options[ 'fallback' ] = 'none';
+                       }
+               }
+
+               $cannedIcons = self::getCannedIcons();
+
+               // Use fallback icon or random icon if stated image doesn't 
exist
+               if ( $image === null || $image == '' || ( !wfFindFile( $image ) 
&& !in_array( $image, $cannedIcons ) ) ) {
+                       if ( $options[ 'fallback' ] == 'none' ) {
+                               return '';
+                       } elseif ( in_array( $options[ 'fallback' ], 
$cannedIcons ) ) {
+                               $image = $options[ 'fallback' ];
+                       } else {
+                               $image = $cannedIcons[ hexdec( sha1( $options[ 
'fallback' ] )[0] ) % 27];
+                       }
+               }
+
+               // Are we loading an image file or constructing a div based on 
an icon class?
+               if ( wfFindFile( $image ) ) {
+                       $imageCode = self::makeImageFromFile( $image, $options[ 
'classes' ], $width, $options[ 'link' ],
+                               $options[ 'renderAsWikitext' ], $options[ 
'label' ] );
+               } elseif ( in_array( $image, $cannedIcons ) ) {
+                       $imageCode = self::makeImageFromIcon( $image, $options[ 
'classes' ], $width, $options[ 'colour' ],
+                               $options[ 'link' ], $options[ 
'renderAsWikitext' ], $options[ 'label' ] );
+               }
+
+               // Finishing up
+               $wrapperAttributes = [ 'class' => $options[ 'classes' ], 
'style' => $options[ 'css' ] ];
+               $imageBlock = Html::rawElement( 'div', $wrapperAttributes, 
$imageCode );
+               return $imageBlock;
+       }
+
+       protected static function makeImageFromFile( $image, $classes, $width, 
$link,
+               $renderAsWikitext, $label ) {
+               // This assumes that colours cannot be assigned to images.
+               // This is currently true, but who knows what the future might 
hold!
+
+               $imageObj = wfFindFile( $image );
+               $imageFullName = $imageObj->getTitle()->getFullText();
+
+               if ( $renderAsWikitext ) {
+                       $wikitext = "[[{$imageFullName}|{$width}px";
+
+                       if ( $link === false ) {
+                               $wikitext .= "|nolink]]";
+                       } elseif ( is_string( $link ) ) {
+                               $wikitext .= "|link={$link}]]";
+                       } else {
+                               $wikitext .= "]]";
+                       }
+
+                       return $wikitext;
+
+               } else {
+                       $imageHtml = $imageObj->transform( [ 'width' => $width 
] )->toHtml();
+
+                       if ( $label != '' ) {
+                               $imageWrapperCss = "width:{$width}px; 
max-height:{$width}px; overflow:hidden;";
+
+                               $imageHtml = Html::rawElement(
+                                       'div',
+                                       ['class' => 'mw-ck-file-image', 'style' 
=> $imageWrapperCss ],
+                                       $imageHtml
+                               );
+                       }
+
+                       if ( $link !== false ) {
+                               $imageHtml = self::linkFactory( $imageHtml, 
$link, $label, $imageObj );
+                       }
+
+                       return $imageHtml;
+               }
+       }
+
+       protected static function makeImageFromIcon( $image, $classes, $width, 
$colour, $link,
+               $renderAsWikitext, $label ) {
+               // Rendering as wikitext with link is not an option here due to 
unfortunate behavior from Tidy.
+
+               $imageClasses = [ 'mw-ck-icon' ];
+               if ( $colour != '' && $colour != 'black' ) {
+                       $imageClasses[] = 'mw-ck-icon-' . $image . '-' . 
$colour;
+               } else {
+                       $imageClasses[] = 'mw-ck-icon-' . $image;
+               }
+
+               $imageHtml = Html::rawElement(
+                       'div',
+                       [ 'class' => $imageClasses, 'style' => "width: 
{$width}px; height: {$width}px;" ],
+                       ''
+               );
+
+               if ( !$renderAsWikitext && $link !== false ) {
+                       $imageHtml = self::linkFactory( $imageHtml, $link, 
$label );
+               }
+
+               return $imageHtml;
+       }
+
+       protected static function linkFactory( $imageHtml, $link, $label, 
$imageObj = null ) {
+               // Important assumption: image is being rendered as HTML and 
not wikitext.
+
+               if ( $link instanceof Title ) {
+                       $linkHref = $link->getLinkUrl();
+               } elseif ( is_string( $link ) ) {
+                       $linkHref = Title::newFromText( $link )->getLinkUrl();
+               } elseif ( $imageObj !== null ) {
+                       $linkHref = $imageObj->getTitle()->getLinkUrl();
+               } else {
+                       $linkHref = '#';
+               }
+
+               if ( $label != '' ) {
+                       $imageHtml .= Html::rawElement( 'span', [ 'class' => 
'mw-ck-toc-item-label' ], $label );
+               }
+               return Html::rawElement( 'a', [ 'href' => $linkHref ], 
$imageHtml );
+       }
+
+       /**
+        * @return array All the canned icons in CollaborationKit
+        */
+       public static function getCannedIcons() {
+               // Keep this synced with the icons listed in the module in 
extension.json
+               return $iconsPreset = [
+                       // Randomly selectable items
+                       'book',
+                       'circlestar',
+                       'clock',
+                       'community',
+                       'contents',
+                       'die',
+                       'edit',
+                       'eye',
+                       'flag',
+                       'funnel',
+                       'gear',
+                       'heart',
+                       'journal',
+                       'key',
+                       'link',
+                       'map',
+                       'menu',
+                       'newspaper',
+                       'ol',
+                       'page',
+                       'paperclip',
+                       'puzzlepiece',
+                       'ribbon',
+                       'rocket',
+                       'star',
+                       'sun',
+                       'ul',
+
+                       'addimage',
+                       'addmapmarker',
+                       'addquote',
+                       'bell',
+                       'circleline',
+                       'circletriangle',
+                       'circlex',
+                       'discussion',
+                       'download',
+                       'editprotected',
+                       'gallery',
+                       'image',
+                       'lock',
+                       'mail',
+                       'mapmarker',
+                       'message',
+                       'messagenew',
+                       'messagescary',
+                       'move',
+                       'nowiki',
+                       'pagechecked',
+                       'pageribbon',
+                       'pagesearch',
+                       'print',
+                       'quotes',
+                       'search',
+                       'starmenu',
+                       'translate',
+                       'trash',
+                       'user'
+               ];
+       }
+}
diff --git a/includes/content/CollaborationListContent.php 
b/includes/content/CollaborationListContent.php
index cdfea78..c2763be 100644
--- a/includes/content/CollaborationListContent.php
+++ b/includes/content/CollaborationListContent.php
@@ -192,11 +192,27 @@
                if ( !$lang ) {
                        $lang = $title->getPageLanguage();
                }
+
+               // If this is an error-type list (i.e. a schema-violating 
blob), just return the plain JSON.
+               if ( $this->displaymode == 'error' ) {
+                       $errorText = '<div class=errorbox>' .
+                               wfMessage( 'collaborationkit-list-invalid' 
)->inLanguage( $lang )->plain() .
+                               "</div>\n<pre>" .
+                               $this->errortext .
+                               '</pre>';
+                       $output = $wgParser->parse( $errorText, $title, 
$options, true, true, $revId );
+                       return;
+               }
+
                $listOptions = $this->getFullRenderListOptions()
                        + (array)$this->options
                        + $this->getDefaultOptions();
+
+               // Preparing page contents
                $text = $this->convertToWikitext( $lang, $listOptions );
                $output = $wgParser->parse( $text, $title, $options, true, 
true, $revId );
+
+               // Special JS variable if this is a member list
                if ( $this->displaymode == 'members' ) {
                        $isMemberList = true;
                } else {
@@ -234,18 +250,7 @@
                $options = $options + $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' 
)->inLanguage( $lang )->plain() .
-                               "</div>\n<pre>" .
-                               $this->errortext .
-                               '</pre>';
-                       return $errorWikitext;
-               }
+               $iconWidth = $options['iconWidth'];
 
                // Hack to force style loading even when we don't have a Parser 
reference.
                $text = "<collaborationkitloadliststyles/>";
@@ -322,37 +327,12 @@
                                        "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";
+                                       if ( isset( $item->image ) ) {
+                                               $text .= $this->generateImage( 
$item->image, $this->displaymode, $titleForItem, $iconWidth );
                                        } else {
-                                               if ( $this->displaymode == 
'members' ) {
-                                                       $placeholderIcon = 
'mw-ck-icon-user-lightgrey';
-                                               } else {
-                                                       $placeholderIcon = 
'mw-ck-icon-page-lightgrey';
-                                               }
-                                               $text .= Html::element( 'div', [
-                                                       "class" => [
-                                                               
'mw-ck-list-noimageplaceholder',
-                                                               $placeholderIcon
-                                                       ]
-                                               ] );
+                                               // Use fallback mechanisms
+                                               $text .= $this->generateImage( 
null, $this->displaymode, $titleForItem, $iconWidth );
                                        }
-                                       $text .= '</div>';
                                }
 
                                $text .= '<div class="mw-ck-list-container">';
@@ -399,6 +379,54 @@
                return $text;
        }
 
+       /**
+        * Invokes CollaborationKitImage::makeImage with fallback criteria
+        *
+        * @param string $definedImage The filename given in the list item
+        * @param string $displayMode Type of list (members or otherwise)
+        * @param Title $title Title object of the list item
+        * @param int $size The width of the icon image. Default is 64px;
+        * @return string HTML
+        */
+       protected static function generateImage( $definedImage, $displayMode, 
$title, $size = 64 ) {
+               $image = null;
+               $iconColour = '';
+               $linkOrNot = true;
+
+               // Step 1: Use the defined image, assuming it's valid
+               if ( $definedImage !== null && is_string( $definedImage ) ) {
+                       $imageTitle = Title::newFromText( $definedImage, 
NS_FILE );
+                       if ( $imageTitle ) {
+                               $image = wfFindFile( $imageTitle )->getName();
+                       }
+               }
+
+               // Step 2: No defined image / invalid defined image? Use 
PageImages if possible.
+               if ( $image === null && $title && class_exists( 'PageImages' ) 
) {
+                       $queryPageImages = PageImages::getPageImage( $title );
+                       if ( $queryPageImages !== false ) {
+                               $image = $queryPageImages->getName();
+                       }
+               }
+
+               // Step 3: None of the above work? Time for fallback icons.
+               if ( $image === null ) {
+                       $iconColour = "lightgrey";
+                       $linkOrNot = false;
+                       if ( $displayMode == 'members' ) {
+                               $image = 'user';
+                       } else {
+                               $image = 'page';
+                       }
+               }
+
+               return CollaborationKitImage::makeImage(
+                       $image,
+                       $size,
+                       [ 'classes' => [ 'mw-ck-list-image' ], 'colour' => 
$iconColour, 'link' => $linkOrNot, 'renderAsWikitext' => true ]
+               );
+       }
+
        public function convert( $toModel, $lossy = '' ) {
                if ( $toModel === CONTENT_MODEL_WIKITEXT && $lossy === 'lossy' 
) {
                        global $wgContLang;
@@ -437,7 +465,8 @@
                        'tags' => [],
                        'mode' => 'normal',
                        'columns' => [],
-                       'showColumnHeaders' => true
+                       'showColumnHeaders' => true,
+                       'iconWidth' => 64
                ];
        }
 
diff --git a/includes/content/CollaborationListContentSchema.php 
b/includes/content/CollaborationListContentSchema.php
index 6a49a16..871c77a 100644
--- a/includes/content/CollaborationListContentSchema.php
+++ b/includes/content/CollaborationListContentSchema.php
@@ -94,6 +94,10 @@
                                'tags' => [
                                        'type' => 'array',
                                        'items' => [ [] ]
+                               ],
+                               'iconWidth' => [
+                                       'type' => 'number',
+                                       'default' => 64
                                ]
                        ]
                ]

-- 
To view, visit https://gerrit.wikimedia.org/r/335772
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If932d2e80ca65c612c6f66c43ab926be00941634
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CollaborationKit
Gerrit-Branch: master
Gerrit-Owner: Harej <jamesmh...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to