ImageGalleryBase.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. /**
  3. * Image gallery.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use MediaWiki\MediaWikiServices;
  23. /**
  24. * Image gallery
  25. *
  26. * Add images to the gallery using add(), then render that list to HTML using toHTML().
  27. *
  28. * @ingroup Media
  29. */
  30. abstract class ImageGalleryBase extends ContextSource {
  31. /**
  32. * @var array Gallery images
  33. */
  34. protected $mImages;
  35. /**
  36. * @var bool Whether to show the filesize in bytes in categories
  37. */
  38. protected $mShowBytes;
  39. /**
  40. * @var bool Whether to show the dimensions in categories
  41. */
  42. protected $mShowDimensions;
  43. /**
  44. * @var bool Whether to show the filename. Default: true
  45. */
  46. protected $mShowFilename;
  47. /**
  48. * @var string Gallery mode. Default: traditional
  49. */
  50. protected $mMode;
  51. /**
  52. * @var bool|string Gallery caption. Default: false
  53. */
  54. protected $mCaption = false;
  55. /**
  56. * Length to truncate filename to in caption when using "showfilename".
  57. * A value of 'true' will truncate the filename to one line using CSS
  58. * and will be the behaviour after deprecation.
  59. *
  60. * @var bool|int
  61. */
  62. protected $mCaptionLength = true;
  63. /**
  64. * @var bool Hide blacklisted images?
  65. */
  66. protected $mHideBadImages;
  67. /**
  68. * @var Parser Registered parser object for output callbacks
  69. */
  70. public $mParser;
  71. /**
  72. * @var Title Contextual title, used when images are being screened against
  73. * the bad image list
  74. */
  75. protected $contextTitle = false;
  76. /** @var array */
  77. protected $mAttribs = [];
  78. /** @var bool */
  79. static private $modeMapping = false;
  80. /**
  81. * Get a new image gallery. This is the method other callers
  82. * should use to get a gallery.
  83. *
  84. * @param string|bool $mode Mode to use. False to use the default
  85. * @param IContextSource|null $context
  86. * @return ImageGalleryBase
  87. * @throws MWException
  88. */
  89. static function factory( $mode = false, IContextSource $context = null ) {
  90. self::loadModes();
  91. if ( !$context ) {
  92. $context = RequestContext::getMainAndWarn( __METHOD__ );
  93. }
  94. if ( !$mode ) {
  95. $galleryOptions = $context->getConfig()->get( 'GalleryOptions' );
  96. $mode = $galleryOptions['mode'];
  97. }
  98. $mode = MediaWikiServices::getInstance()->getContentLanguage()->lc( $mode );
  99. if ( isset( self::$modeMapping[$mode] ) ) {
  100. $class = self::$modeMapping[$mode];
  101. return new $class( $mode, $context );
  102. } else {
  103. throw new MWException( "No gallery class registered for mode $mode" );
  104. }
  105. }
  106. private static function loadModes() {
  107. if ( self::$modeMapping === false ) {
  108. self::$modeMapping = [
  109. 'traditional' => TraditionalImageGallery::class,
  110. 'nolines' => NolinesImageGallery::class,
  111. 'packed' => PackedImageGallery::class,
  112. 'packed-hover' => PackedHoverImageGallery::class,
  113. 'packed-overlay' => PackedOverlayImageGallery::class,
  114. 'slideshow' => SlideshowImageGallery::class,
  115. ];
  116. // Allow extensions to make a new gallery format.
  117. Hooks::run( 'GalleryGetModes', [ &self::$modeMapping ] );
  118. }
  119. }
  120. /**
  121. * Create a new image gallery object.
  122. *
  123. * You should not call this directly, but instead use
  124. * ImageGalleryBase::factory().
  125. * @param string $mode
  126. * @param IContextSource|null $context
  127. */
  128. function __construct( $mode = 'traditional', IContextSource $context = null ) {
  129. if ( $context ) {
  130. $this->setContext( $context );
  131. }
  132. $galleryOptions = $this->getConfig()->get( 'GalleryOptions' );
  133. $this->mImages = [];
  134. $this->mShowBytes = $galleryOptions['showBytes'];
  135. $this->mShowDimensions = $galleryOptions['showDimensions'];
  136. $this->mShowFilename = true;
  137. $this->mParser = false;
  138. $this->mHideBadImages = false;
  139. $this->mPerRow = $galleryOptions['imagesPerRow'];
  140. $this->mWidths = $galleryOptions['imageWidth'];
  141. $this->mHeights = $galleryOptions['imageHeight'];
  142. $this->mCaptionLength = $galleryOptions['captionLength'];
  143. $this->mMode = $mode;
  144. }
  145. /**
  146. * Register a parser object. If you do not set this
  147. * and the output of this gallery ends up in parser
  148. * cache, the javascript will break!
  149. *
  150. * @note This also triggers using the page's target
  151. * language instead of the user language.
  152. *
  153. * @param Parser $parser
  154. */
  155. function setParser( $parser ) {
  156. $this->mParser = $parser;
  157. }
  158. /**
  159. * Set bad image flag
  160. * @param bool $flag
  161. */
  162. function setHideBadImages( $flag = true ) {
  163. $this->mHideBadImages = $flag;
  164. }
  165. /**
  166. * Set the caption (as plain text)
  167. *
  168. * @param string $caption
  169. */
  170. function setCaption( $caption ) {
  171. $this->mCaption = htmlspecialchars( $caption );
  172. }
  173. /**
  174. * Set the caption (as HTML)
  175. *
  176. * @param string $caption
  177. */
  178. public function setCaptionHtml( $caption ) {
  179. $this->mCaption = $caption;
  180. }
  181. /**
  182. * Set how many images will be displayed per row.
  183. *
  184. * @param int $num Integer >= 0; If perrow=0 the gallery layout will adapt
  185. * to screensize invalid numbers will be rejected
  186. */
  187. public function setPerRow( $num ) {
  188. if ( $num >= 0 ) {
  189. $this->mPerRow = (int)$num;
  190. }
  191. }
  192. /**
  193. * Set how wide each image will be, in pixels.
  194. *
  195. * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
  196. * and those below 0 are ignored.
  197. */
  198. public function setWidths( $num ) {
  199. $parsed = Parser::parseWidthParam( $num, false );
  200. if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
  201. $this->mWidths = $parsed['width'];
  202. }
  203. }
  204. /**
  205. * Set how high each image will be, in pixels.
  206. *
  207. * @param string $num Number. Unit other than 'px is invalid. Invalid numbers
  208. * and those below 0 are ignored.
  209. */
  210. public function setHeights( $num ) {
  211. $parsed = Parser::parseWidthParam( $num, false );
  212. if ( isset( $parsed['width'] ) && $parsed['width'] > 0 ) {
  213. $this->mHeights = $parsed['width'];
  214. }
  215. }
  216. /**
  217. * Allow setting additional options. This is meant
  218. * to allow extensions to add additional parameters to
  219. * <gallery> parser tag.
  220. *
  221. * @param array $options Attributes of gallery tag
  222. */
  223. public function setAdditionalOptions( $options ) {
  224. }
  225. /**
  226. * Add an image to the gallery.
  227. *
  228. * @param Title $title Title object of the image that is added to the gallery
  229. * @param string $html Additional HTML text to be shown. The name and size
  230. * of the image are always shown.
  231. * @param string $alt Alt text for the image
  232. * @param string $link Override image link (optional)
  233. * @param array $handlerOpts Array of options for image handler (aka page number)
  234. */
  235. function add( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
  236. if ( $title instanceof File ) {
  237. // Old calling convention
  238. $title = $title->getTitle();
  239. }
  240. $this->mImages[] = [ $title, $html, $alt, $link, $handlerOpts ];
  241. wfDebug( 'ImageGallery::add ' . $title->getText() . "\n" );
  242. }
  243. /**
  244. * Add an image at the beginning of the gallery.
  245. *
  246. * @param Title $title Title object of the image that is added to the gallery
  247. * @param string $html Additional HTML text to be shown. The name and size
  248. * of the image are always shown.
  249. * @param string $alt Alt text for the image
  250. * @param string $link Override image link (optional)
  251. * @param array $handlerOpts Array of options for image handler (aka page number)
  252. */
  253. function insert( $title, $html = '', $alt = '', $link = '', $handlerOpts = [] ) {
  254. if ( $title instanceof File ) {
  255. // Old calling convention
  256. $title = $title->getTitle();
  257. }
  258. array_unshift( $this->mImages, [ &$title, $html, $alt, $link, $handlerOpts ] );
  259. }
  260. /**
  261. * Returns the list of images this gallery contains
  262. * @return array
  263. */
  264. public function getImages() {
  265. return $this->mImages;
  266. }
  267. /**
  268. * isEmpty() returns true if the gallery contains no images
  269. * @return bool
  270. */
  271. function isEmpty() {
  272. return empty( $this->mImages );
  273. }
  274. /**
  275. * Enable/Disable showing of the dimensions of an image in the gallery.
  276. * Enabled by default.
  277. *
  278. * @param bool $f Set to false to disable
  279. */
  280. function setShowDimensions( $f ) {
  281. $this->mShowDimensions = (bool)$f;
  282. }
  283. /**
  284. * Enable/Disable showing of the file size of an image in the gallery.
  285. * Enabled by default.
  286. *
  287. * @param bool $f Set to false to disable
  288. */
  289. function setShowBytes( $f ) {
  290. $this->mShowBytes = (bool)$f;
  291. }
  292. /**
  293. * Enable/Disable showing of the filename of an image in the gallery.
  294. * Enabled by default.
  295. *
  296. * @param bool $f Set to false to disable
  297. */
  298. function setShowFilename( $f ) {
  299. $this->mShowFilename = (bool)$f;
  300. }
  301. /**
  302. * Set arbitrary attributes to go on the HTML gallery output element.
  303. * Should be suitable for a <ul> element.
  304. *
  305. * Note -- if taking from user input, you should probably run through
  306. * Sanitizer::validateAttributes() first.
  307. *
  308. * @param array $attribs Array of HTML attribute pairs
  309. */
  310. function setAttributes( $attribs ) {
  311. $this->mAttribs = $attribs;
  312. }
  313. /**
  314. * Display an html representation of the gallery
  315. *
  316. * @return string The html
  317. */
  318. abstract public function toHTML();
  319. /**
  320. * @return int Number of images in the gallery
  321. */
  322. public function count() {
  323. return count( $this->mImages );
  324. }
  325. /**
  326. * Set the contextual title
  327. *
  328. * @param Title $title Contextual title
  329. */
  330. public function setContextTitle( $title ) {
  331. $this->contextTitle = $title;
  332. }
  333. /**
  334. * Get the contextual title, if applicable
  335. *
  336. * @return Title|bool Title or false
  337. */
  338. public function getContextTitle() {
  339. return is_object( $this->contextTitle ) && $this->contextTitle instanceof Title
  340. ? $this->contextTitle
  341. : false;
  342. }
  343. /**
  344. * Determines the correct language to be used for this image gallery
  345. * @return Language
  346. */
  347. protected function getRenderLang() {
  348. return $this->mParser
  349. ? $this->mParser->getTargetLanguage()
  350. : $this->getLanguage();
  351. }
  352. }