SpecialMIMESearch.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * Implements Special:MIMESearch
  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. * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
  23. */
  24. use MediaWiki\MediaWikiServices;
  25. /**
  26. * Searches the database for files of the requested MIME type, comparing this with the
  27. * 'img_major_mime' and 'img_minor_mime' fields in the image table.
  28. * @ingroup SpecialPage
  29. */
  30. class SpecialMIMESearch extends QueryPage {
  31. protected $major, $minor, $mime;
  32. function __construct( $name = 'MIMEsearch' ) {
  33. parent::__construct( $name );
  34. }
  35. public function isExpensive() {
  36. return false;
  37. }
  38. function isSyndicated() {
  39. return false;
  40. }
  41. function isCacheable() {
  42. return false;
  43. }
  44. function linkParameters() {
  45. return [ 'mime' => "{$this->major}/{$this->minor}" ];
  46. }
  47. public function getQueryInfo() {
  48. $minorType = [];
  49. if ( $this->minor !== '*' ) {
  50. // Allow wildcard searching
  51. $minorType['img_minor_mime'] = $this->minor;
  52. }
  53. $imgQuery = LocalFile::getQueryInfo();
  54. $qi = [
  55. 'tables' => $imgQuery['tables'],
  56. 'fields' => [
  57. 'namespace' => NS_FILE,
  58. 'title' => 'img_name',
  59. // Still have a value field just in case,
  60. // but it isn't actually used for sorting.
  61. 'value' => 'img_name',
  62. 'img_size',
  63. 'img_width',
  64. 'img_height',
  65. 'img_user_text' => $imgQuery['fields']['img_user_text'],
  66. 'img_timestamp'
  67. ],
  68. 'conds' => [
  69. 'img_major_mime' => $this->major,
  70. // This is in order to trigger using
  71. // the img_media_mime index in "range" mode.
  72. // @todo how is order defined? use MimeAnalyzer::getMediaTypes?
  73. 'img_media_type' => [
  74. MEDIATYPE_BITMAP,
  75. MEDIATYPE_DRAWING,
  76. MEDIATYPE_AUDIO,
  77. MEDIATYPE_VIDEO,
  78. MEDIATYPE_MULTIMEDIA,
  79. MEDIATYPE_UNKNOWN,
  80. MEDIATYPE_OFFICE,
  81. MEDIATYPE_TEXT,
  82. MEDIATYPE_EXECUTABLE,
  83. MEDIATYPE_ARCHIVE,
  84. MEDIATYPE_3D,
  85. ],
  86. ] + $minorType,
  87. 'join_conds' => $imgQuery['joins'],
  88. ];
  89. return $qi;
  90. }
  91. /**
  92. * The index is on (img_media_type, img_major_mime, img_minor_mime)
  93. * which unfortunately doesn't have img_name at the end for sorting.
  94. * So tell db to sort it however it wishes (Its not super important
  95. * that this report gives results in a logical order). As an aditional
  96. * note, mysql seems to by default order things by img_name ASC, which
  97. * is what we ideally want, so everything works out fine anyhow.
  98. * @return array
  99. */
  100. function getOrderFields() {
  101. return [];
  102. }
  103. /**
  104. * Generate and output the form
  105. */
  106. function getPageHeader() {
  107. $formDescriptor = [
  108. 'mime' => [
  109. 'type' => 'combobox',
  110. 'options' => $this->getSuggestionsForTypes(),
  111. 'name' => 'mime',
  112. 'label-message' => 'mimetype',
  113. 'required' => true,
  114. 'default' => $this->mime,
  115. ],
  116. ];
  117. HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
  118. ->setSubmitTextMsg( 'ilsubmit' )
  119. ->setAction( $this->getPageTitle()->getLocalURL() )
  120. ->setMethod( 'get' )
  121. ->prepareForm()
  122. ->displayForm( false );
  123. return '';
  124. }
  125. protected function getSuggestionsForTypes() {
  126. $dbr = wfGetDB( DB_REPLICA );
  127. $lastMajor = null;
  128. $suggestions = [];
  129. $result = $dbr->select(
  130. [ 'image' ],
  131. // We ignore img_media_type, but using it in the query is needed for MySQL to choose a
  132. // sensible execution plan
  133. [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ],
  134. [],
  135. __METHOD__,
  136. [ 'GROUP BY' => [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ] ]
  137. );
  138. foreach ( $result as $row ) {
  139. $major = $row->img_major_mime;
  140. $minor = $row->img_minor_mime;
  141. $suggestions[ "$major/$minor" ] = "$major/$minor";
  142. if ( $lastMajor === $major ) {
  143. // If there are at least two with the same major mime type, also include the wildcard
  144. $suggestions[ "$major/*" ] = "$major/*";
  145. }
  146. $lastMajor = $major;
  147. }
  148. ksort( $suggestions );
  149. return $suggestions;
  150. }
  151. public function execute( $par ) {
  152. $this->addHelpLink( 'Help:Managing_files' );
  153. $this->mime = $par ?: $this->getRequest()->getText( 'mime' );
  154. $this->mime = trim( $this->mime );
  155. list( $this->major, $this->minor ) = File::splitMime( $this->mime );
  156. if ( $this->major == '' || $this->minor == '' || $this->minor == 'unknown' ||
  157. !self::isValidType( $this->major )
  158. ) {
  159. $this->setHeaders();
  160. $this->outputHeader();
  161. $this->getPageHeader();
  162. return;
  163. }
  164. parent::execute( $par );
  165. }
  166. /**
  167. * @param Skin $skin
  168. * @param object $result Result row
  169. * @return string
  170. */
  171. function formatResult( $skin, $result ) {
  172. $linkRenderer = $this->getLinkRenderer();
  173. $nt = Title::makeTitle( $result->namespace, $result->title );
  174. $text = MediaWikiServices::getInstance()->getContentLanguage()
  175. ->convert( htmlspecialchars( $nt->getText() ) );
  176. $plink = $linkRenderer->makeLink(
  177. Title::newFromText( $nt->getPrefixedText() ),
  178. new HtmlArmor( $text )
  179. );
  180. $download = Linker::makeMediaLinkObj( $nt, $this->msg( 'download' )->escaped() );
  181. $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
  182. $lang = $this->getLanguage();
  183. $bytes = htmlspecialchars( $lang->formatSize( $result->img_size ) );
  184. $dimensions = $this->msg( 'widthheight' )->numParams( $result->img_width,
  185. $result->img_height )->escaped();
  186. $user = $linkRenderer->makeLink(
  187. Title::makeTitle( NS_USER, $result->img_user_text ),
  188. $result->img_user_text
  189. );
  190. $time = $lang->userTimeAndDate( $result->img_timestamp, $this->getUser() );
  191. $time = htmlspecialchars( $time );
  192. return "$download $plink . . $dimensions . . $bytes . . $user . . $time";
  193. }
  194. /**
  195. * @param string $type
  196. * @return bool
  197. */
  198. protected static function isValidType( $type ) {
  199. // From maintenance/tables.sql => img_major_mime
  200. $types = [
  201. 'unknown',
  202. 'application',
  203. 'audio',
  204. 'image',
  205. 'text',
  206. 'video',
  207. 'message',
  208. 'model',
  209. 'multipart',
  210. 'chemical'
  211. ];
  212. return in_array( $type, $types );
  213. }
  214. public function preprocessResults( $db, $res ) {
  215. $this->executeLBFromResultWrapper( $res );
  216. }
  217. protected function getGroupName() {
  218. return 'media';
  219. }
  220. }