Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 132 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| SpecialNewFiles | |
0.00% |
0 / 131 |
|
0.00% |
0 / 5 |
210 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 53 |
|
0.00% |
0 / 1 |
72 | |||
| buildForm | |
0.00% |
0 / 60 |
|
0.00% |
0 / 1 |
6 | |||
| getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setTopText | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Specials; |
| 8 | |
| 9 | use MediaWiki\Cache\LinkBatchFactory; |
| 10 | use MediaWiki\Context\DerivativeContext; |
| 11 | use MediaWiki\Context\IContextSource; |
| 12 | use MediaWiki\Html\FormOptions; |
| 13 | use MediaWiki\Html\Html; |
| 14 | use MediaWiki\HTMLForm\Field\HTMLUserTextField; |
| 15 | use MediaWiki\HTMLForm\HTMLForm; |
| 16 | use MediaWiki\Pager\NewFilesPager; |
| 17 | use MediaWiki\Permissions\GroupPermissionsLookup; |
| 18 | use MediaWiki\Request\DerivativeRequest; |
| 19 | use MediaWiki\SpecialPage\IncludableSpecialPage; |
| 20 | use Wikimedia\Mime\MimeAnalyzer; |
| 21 | use Wikimedia\Rdbms\IConnectionProvider; |
| 22 | |
| 23 | /** |
| 24 | * Implements Special:Newimages |
| 25 | * |
| 26 | * @ingroup SpecialPage |
| 27 | */ |
| 28 | class SpecialNewFiles extends IncludableSpecialPage { |
| 29 | /** @var FormOptions */ |
| 30 | protected $opts; |
| 31 | |
| 32 | /** @var string[] */ |
| 33 | protected $mediaTypes; |
| 34 | |
| 35 | private GroupPermissionsLookup $groupPermissionsLookup; |
| 36 | private IConnectionProvider $dbProvider; |
| 37 | private LinkBatchFactory $linkBatchFactory; |
| 38 | |
| 39 | public function __construct( |
| 40 | MimeAnalyzer $mimeAnalyzer, |
| 41 | GroupPermissionsLookup $groupPermissionsLookup, |
| 42 | IConnectionProvider $dbProvider, |
| 43 | LinkBatchFactory $linkBatchFactory |
| 44 | ) { |
| 45 | parent::__construct( 'Newimages' ); |
| 46 | $this->groupPermissionsLookup = $groupPermissionsLookup; |
| 47 | $this->dbProvider = $dbProvider; |
| 48 | $this->mediaTypes = $mimeAnalyzer->getMediaTypes(); |
| 49 | $this->linkBatchFactory = $linkBatchFactory; |
| 50 | } |
| 51 | |
| 52 | /** @inheritDoc */ |
| 53 | public function execute( $par ) { |
| 54 | $context = new DerivativeContext( $this->getContext() ); |
| 55 | |
| 56 | $this->setHeaders(); |
| 57 | $this->outputHeader(); |
| 58 | |
| 59 | $out = $this->getOutput(); |
| 60 | $this->addHelpLink( 'Help:New images' ); |
| 61 | |
| 62 | $opts = new FormOptions(); |
| 63 | |
| 64 | $opts->add( 'user', '' ); |
| 65 | $opts->add( 'showbots', false ); |
| 66 | $opts->add( 'hidepatrolled', false ); |
| 67 | $opts->add( 'mediatype', $this->mediaTypes ); |
| 68 | $opts->add( 'limit', 50 ); |
| 69 | $opts->add( 'offset', '' ); |
| 70 | $opts->add( 'start', '' ); |
| 71 | $opts->add( 'end', '' ); |
| 72 | |
| 73 | $opts->fetchValuesFromRequest( $this->getRequest() ); |
| 74 | |
| 75 | if ( $par !== null ) { |
| 76 | $opts->setValue( 'limit', $par ); |
| 77 | } |
| 78 | |
| 79 | // If start date comes after end date chronologically, swap them. |
| 80 | // They are swapped in the interface by JS. |
| 81 | $start = $opts->getValue( 'start' ); |
| 82 | $end = $opts->getValue( 'end' ); |
| 83 | if ( $start !== '' && $end !== '' && $start > $end ) { |
| 84 | $temp = $end; |
| 85 | $end = $start; |
| 86 | $start = $temp; |
| 87 | |
| 88 | $opts->setValue( 'start', $start, true ); |
| 89 | $opts->setValue( 'end', $end, true ); |
| 90 | |
| 91 | // also swap values in request object, which is used by HTMLForm |
| 92 | // to pre-populate the fields with the previous input |
| 93 | $request = $context->getRequest(); |
| 94 | $context->setRequest( new DerivativeRequest( |
| 95 | $request, |
| 96 | [ 'start' => $start, 'end' => $end ] + $request->getValues(), |
| 97 | $request->wasPosted() |
| 98 | ) ); |
| 99 | } |
| 100 | |
| 101 | // Avoid unexpected query or query errors to assoc array input, or nested arrays via |
| 102 | // URL query params. Keep only string values (T321133). |
| 103 | $mediaTypes = $opts->getValue( 'mediatype' ); |
| 104 | $mediaTypes = array_filter( $mediaTypes, 'is_string' ); |
| 105 | // Avoid unbounded query size with bogus values. Keep only known types. |
| 106 | $mediaTypes = array_values( array_intersect( $this->mediaTypes, $mediaTypes ) ); |
| 107 | // Optimization: Remove redundant IN() query condition if all types are checked. |
| 108 | if ( !array_diff( $this->mediaTypes, $mediaTypes ) ) { |
| 109 | $mediaTypes = []; |
| 110 | } |
| 111 | $opts->setValue( 'mediatype', $mediaTypes ); |
| 112 | |
| 113 | $opts->validateIntBounds( 'limit', 0, 500 ); |
| 114 | |
| 115 | $this->opts = $opts; |
| 116 | |
| 117 | if ( !$this->including() ) { |
| 118 | $this->setTopText(); |
| 119 | $this->buildForm( $context ); |
| 120 | } |
| 121 | |
| 122 | $pager = new NewFilesPager( |
| 123 | $context, |
| 124 | $this->groupPermissionsLookup, |
| 125 | $this->linkBatchFactory, |
| 126 | $this->getLinkRenderer(), |
| 127 | $this->dbProvider, |
| 128 | $opts |
| 129 | ); |
| 130 | |
| 131 | $out->addHTML( $pager->getBody() ); |
| 132 | if ( !$this->including() ) { |
| 133 | $out->addHTML( $pager->getNavigationBar() ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | protected function buildForm( IContextSource $context ) { |
| 138 | $mediaTypesText = array_map( function ( $type ) { |
| 139 | // mediastatistics-header-unknown, mediastatistics-header-bitmap, |
| 140 | // mediastatistics-header-drawing, mediastatistics-header-audio, |
| 141 | // mediastatistics-header-video, mediastatistics-header-multimedia, |
| 142 | // mediastatistics-header-office, mediastatistics-header-text, |
| 143 | // mediastatistics-header-executable, mediastatistics-header-archive, |
| 144 | // mediastatistics-header-3d, |
| 145 | return $this->msg( 'mediastatistics-header-' . strtolower( $type ) )->escaped(); |
| 146 | }, $this->mediaTypes ); |
| 147 | $mediaTypesOptions = array_combine( $mediaTypesText, $this->mediaTypes ); |
| 148 | ksort( $mediaTypesOptions ); |
| 149 | |
| 150 | $formDescriptor = [ |
| 151 | 'user' => [ |
| 152 | 'class' => HTMLUserTextField::class, |
| 153 | 'label-message' => 'newimages-user', |
| 154 | 'name' => 'user', |
| 155 | ], |
| 156 | |
| 157 | 'showbots' => [ |
| 158 | 'type' => 'check', |
| 159 | 'label-message' => 'newimages-showbots', |
| 160 | 'name' => 'showbots', |
| 161 | ], |
| 162 | |
| 163 | 'hidepatrolled' => [ |
| 164 | 'type' => 'check', |
| 165 | 'label-message' => 'newimages-hidepatrolled', |
| 166 | 'name' => 'hidepatrolled', |
| 167 | ], |
| 168 | |
| 169 | 'mediatype' => [ |
| 170 | 'type' => 'multiselect', |
| 171 | 'flatlist' => true, |
| 172 | 'name' => 'mediatype', |
| 173 | 'label-message' => 'newimages-mediatype', |
| 174 | 'options' => $mediaTypesOptions, |
| 175 | 'default' => $this->mediaTypes, |
| 176 | ], |
| 177 | |
| 178 | 'limit' => [ |
| 179 | 'type' => 'hidden', |
| 180 | 'default' => $this->opts->getValue( 'limit' ), |
| 181 | 'name' => 'limit', |
| 182 | ], |
| 183 | |
| 184 | 'offset' => [ |
| 185 | 'type' => 'hidden', |
| 186 | 'default' => $this->opts->getValue( 'offset' ), |
| 187 | 'name' => 'offset', |
| 188 | ], |
| 189 | |
| 190 | 'start' => [ |
| 191 | 'type' => 'date', |
| 192 | 'label-message' => 'date-range-from', |
| 193 | 'name' => 'start', |
| 194 | ], |
| 195 | |
| 196 | 'end' => [ |
| 197 | 'type' => 'date', |
| 198 | 'label-message' => 'date-range-to', |
| 199 | 'name' => 'end', |
| 200 | ], |
| 201 | ]; |
| 202 | |
| 203 | if ( !$this->getUser()->useFilePatrol() ) { |
| 204 | unset( $formDescriptor['hidepatrolled'] ); |
| 205 | } |
| 206 | |
| 207 | HTMLForm::factory( 'ooui', $formDescriptor, $context ) |
| 208 | // For the 'multiselect' field values to be preserved on submit |
| 209 | ->setFormIdentifier( 'specialnewimages' ) |
| 210 | ->setWrapperLegendMsg( 'newimages-legend' ) |
| 211 | ->setSubmitTextMsg( 'ilsubmit' ) |
| 212 | ->setMethod( 'get' ) |
| 213 | ->prepareForm() |
| 214 | ->displayForm( false ); |
| 215 | } |
| 216 | |
| 217 | /** @inheritDoc */ |
| 218 | protected function getGroupName() { |
| 219 | return 'changes'; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Send the text to be displayed above the options |
| 224 | */ |
| 225 | public function setTopText() { |
| 226 | $message = $this->msg( 'newimagestext' )->inContentLanguage(); |
| 227 | if ( !$message->isDisabled() ) { |
| 228 | $contLang = $this->getContentLanguage(); |
| 229 | $this->getOutput()->addWikiTextAsContent( |
| 230 | Html::rawElement( 'div', |
| 231 | [ |
| 232 | 'lang' => $contLang->getHtmlCode(), |
| 233 | 'dir' => $contLang->getDir() |
| 234 | ], |
| 235 | "\n" . $message->plain() . "\n" |
| 236 | ) |
| 237 | ); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Retain the old class name for backwards compatibility. |
| 244 | * @deprecated since 1.41 |
| 245 | */ |
| 246 | class_alias( SpecialNewFiles::class, 'SpecialNewFiles' ); |