SpecialNewFiles.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * Implements Special:Newimages
  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. * @ingroup SpecialPage
  22. */
  23. use MediaWiki\MediaWikiServices;
  24. class SpecialNewFiles extends IncludableSpecialPage {
  25. /** @var FormOptions */
  26. protected $opts;
  27. /** @var string[] */
  28. protected $mediaTypes;
  29. public function __construct() {
  30. parent::__construct( 'Newimages' );
  31. }
  32. public function execute( $par ) {
  33. $context = new DerivativeContext( $this->getContext() );
  34. $this->setHeaders();
  35. $this->outputHeader();
  36. $mimeAnalyzer = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
  37. $this->mediaTypes = $mimeAnalyzer->getMediaTypes();
  38. $out = $this->getOutput();
  39. $this->addHelpLink( 'Help:New images' );
  40. $opts = new FormOptions();
  41. $opts->add( 'like', '' );
  42. $opts->add( 'user', '' );
  43. $opts->add( 'showbots', false );
  44. $opts->add( 'hidepatrolled', false );
  45. $opts->add( 'mediatype', $this->mediaTypes );
  46. $opts->add( 'limit', 50 );
  47. $opts->add( 'offset', '' );
  48. $opts->add( 'start', '' );
  49. $opts->add( 'end', '' );
  50. $opts->fetchValuesFromRequest( $this->getRequest() );
  51. if ( $par !== null ) {
  52. $opts->setValue( is_numeric( $par ) ? 'limit' : 'like', $par );
  53. }
  54. // If start date comes after end date chronologically, swap them.
  55. // They are swapped in the interface by JS.
  56. $start = $opts->getValue( 'start' );
  57. $end = $opts->getValue( 'end' );
  58. if ( $start !== '' && $end !== '' && $start > $end ) {
  59. $temp = $end;
  60. $end = $start;
  61. $start = $temp;
  62. $opts->setValue( 'start', $start, true );
  63. $opts->setValue( 'end', $end, true );
  64. // also swap values in request object, which is used by HTMLForm
  65. // to pre-populate the fields with the previous input
  66. $request = $context->getRequest();
  67. $context->setRequest( new DerivativeRequest(
  68. $request,
  69. [ 'start' => $start, 'end' => $end ] + $request->getValues(),
  70. $request->wasPosted()
  71. ) );
  72. }
  73. // if all media types have been selected, wipe out the array to prevent
  74. // the pointless IN(...) query condition (which would have no effect
  75. // because every possible type has been selected)
  76. $missingMediaTypes = array_diff( $this->mediaTypes, $opts->getValue( 'mediatype' ) );
  77. if ( empty( $missingMediaTypes ) ) {
  78. $opts->setValue( 'mediatype', [] );
  79. }
  80. $opts->validateIntBounds( 'limit', 0, 500 );
  81. $this->opts = $opts;
  82. if ( !$this->including() ) {
  83. $this->setTopText();
  84. $this->buildForm( $context );
  85. }
  86. $pager = new NewFilesPager( $context, $opts, $this->getLinkRenderer() );
  87. $out->addHTML( $pager->getBody() );
  88. if ( !$this->including() ) {
  89. $out->addHTML( $pager->getNavigationBar() );
  90. }
  91. }
  92. protected function buildForm( IContextSource $context ) {
  93. $mediaTypesText = array_map( function ( $type ) {
  94. // mediastatistics-header-unknown, mediastatistics-header-bitmap,
  95. // mediastatistics-header-drawing, mediastatistics-header-audio,
  96. // mediastatistics-header-video, mediastatistics-header-multimedia,
  97. // mediastatistics-header-office, mediastatistics-header-text,
  98. // mediastatistics-header-executable, mediastatistics-header-archive,
  99. // mediastatistics-header-3d,
  100. return $this->msg( 'mediastatistics-header-' . strtolower( $type ) )->text();
  101. }, $this->mediaTypes );
  102. $mediaTypesOptions = array_combine( $mediaTypesText, $this->mediaTypes );
  103. ksort( $mediaTypesOptions );
  104. $formDescriptor = [
  105. 'like' => [
  106. 'type' => 'text',
  107. 'label-message' => 'newimages-label',
  108. 'name' => 'like',
  109. ],
  110. 'user' => [
  111. 'class' => 'HTMLUserTextField',
  112. 'label-message' => 'newimages-user',
  113. 'name' => 'user',
  114. ],
  115. 'showbots' => [
  116. 'type' => 'check',
  117. 'label-message' => 'newimages-showbots',
  118. 'name' => 'showbots',
  119. ],
  120. 'hidepatrolled' => [
  121. 'type' => 'check',
  122. 'label-message' => 'newimages-hidepatrolled',
  123. 'name' => 'hidepatrolled',
  124. ],
  125. 'mediatype' => [
  126. 'type' => 'multiselect',
  127. 'flatlist' => true,
  128. 'name' => 'mediatype',
  129. 'label-message' => 'newimages-mediatype',
  130. 'options' => $mediaTypesOptions,
  131. 'default' => $this->mediaTypes,
  132. ],
  133. 'limit' => [
  134. 'type' => 'hidden',
  135. 'default' => $this->opts->getValue( 'limit' ),
  136. 'name' => 'limit',
  137. ],
  138. 'offset' => [
  139. 'type' => 'hidden',
  140. 'default' => $this->opts->getValue( 'offset' ),
  141. 'name' => 'offset',
  142. ],
  143. 'start' => [
  144. 'type' => 'date',
  145. 'label-message' => 'date-range-from',
  146. 'name' => 'start',
  147. ],
  148. 'end' => [
  149. 'type' => 'date',
  150. 'label-message' => 'date-range-to',
  151. 'name' => 'end',
  152. ],
  153. ];
  154. if ( $this->getConfig()->get( 'MiserMode' ) ) {
  155. unset( $formDescriptor['like'] );
  156. }
  157. if ( !$this->getUser()->useFilePatrol() ) {
  158. unset( $formDescriptor['hidepatrolled'] );
  159. }
  160. HTMLForm::factory( 'ooui', $formDescriptor, $context )
  161. // For the 'multiselect' field values to be preserved on submit
  162. ->setFormIdentifier( 'specialnewimages' )
  163. ->setWrapperLegendMsg( 'newimages-legend' )
  164. ->setSubmitTextMsg( 'ilsubmit' )
  165. ->setMethod( 'get' )
  166. ->prepareForm()
  167. ->displayForm( false );
  168. }
  169. protected function getGroupName() {
  170. return 'changes';
  171. }
  172. /**
  173. * Send the text to be displayed above the options
  174. */
  175. function setTopText() {
  176. $message = $this->msg( 'newimagestext' )->inContentLanguage();
  177. if ( !$message->isDisabled() ) {
  178. $contLang = MediaWikiServices::getInstance()->getContentLanguage();
  179. $this->getOutput()->addWikiTextAsContent(
  180. Html::rawElement( 'div',
  181. [
  182. 'lang' => $contLang->getHtmlCode(),
  183. 'dir' => $contLang->getDir()
  184. ],
  185. "\n" . $message->plain() . "\n"
  186. )
  187. );
  188. }
  189. }
  190. }