Benutzer:TMg/advancedSearch.js
Erscheinungsbild
Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.
- Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
- Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
- Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
( function ( mw, $ ) {
'use strict';
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'Search' ) {
return;
}
/**
* @param {string} val
* @return {string}
*/
function trimQuotes( val ) {
return val.replace( /^"([^"]+)"$/, '$1' );
}
/**
* @param {string} val
* @return {string}
*/
function addQuotes( val ) {
return trimQuotes( val ).replace( /^([^"]*\s[^"]*)$/, '"$1"' );
}
var advancedOptions = [
// Text
{
group: 'text',
id: 'phrase',
formatter: function ( val ) {
return addQuotes( val );
},
parser: /(?:^| +)"([^"]+)"(?= |$)/gi
},
{
group: 'text',
id: 'fuzzy',
formatter: function ( val ) {
return val + '~';
},
parser: /(?:^| +)("[^"]+"|[^\s":]+)~(?= |$)/gi
},
{
group: 'text',
id: 'not',
formatter: function ( val ) {
return '-' + val;
},
parser: /(?:^| +)-("[^"]+"|[^\s":]+)(?= |$)/gi
},
{
group: 'text',
id: 'hastemplate',
formatter: function ( val ) {
return 'hastemplate:' + addQuotes( val );
},
parser: /(?:^| +)\bhastemplate:("[^"]+"|[^\s"]+)/gi
},
{
group: 'text',
id: 'insource',
formatter: function ( val ) {
return 'insource:' + ( /^\/.*\/$/.test( val ) ? val : addQuotes( val ) );
},
parser: /(?:^| +)\binsource:(\/\S+\/|"[^"]+"|[^\s"]+)/gi
},
// Titles and headlines
// local:
{
group: 'title',
id: 'prefix',
formatter: function ( val ) {
return 'prefix:' + addQuotes( val );
},
parser: /(?:^| +)\bprefix:("[^"]+"|[^\s"]+)/gi
},
{
group: 'title',
id: 'intitle',
formatter: function ( val ) {
return 'intitle:' + val;
},
parser: /(?:^| +)\bintitle:("[^"]+"|[^\s"]+)/gi
},
// Categories
{
group: 'categories',
id: 'deepcat',
enabled: function () {
return !!mw.libs.deepCat;
},
formatter: function ( val ) {
return 'deepcat:' + addQuotes( val );
},
parser: /(?:^| +)\bdeepcat:("[^"]+"|\S+)/gi
},
{
group: 'categories',
id: 'incategory',
formatter: function ( val ) {
return 'incategory:' + addQuotes( val );
},
parser: /(?:^| +)\bincategory:("[^"]+"|\S+)/gi
},
// Files
// filebits
// filesize
{
group: 'files',
id: 'filetype',
formatter: function ( val ) {
var types = '';
val.split( ',' ).forEach( function ( type ) {
type = $.trim( type ).replace( /^\w*\W+(?=\w)/, '' ).toLowerCase();
switch ( type ) {
case '':
break;
// Individual file types with non-standard alternatives
case 'bitmap':
case 'image':
case 'bild':
types += 'filetype:bitmap';
break;
case 'audio':
case 'music':
case 'musik':
types += 'filetype:audio';
break;
case 'drawing':
case 'vector':
case 'vektor':
case 'zeichnung':
types += 'filetype:drawing';
break;
// Other known file types
case 'multimedia':
case 'office':
case 'video':
types += 'filetype:' + type;
break;
// Individual MIME types with non-standard alternatives
case 'flac':
types += 'filemime:audio/flac';
break;
case 'midi':
case 'mid':
types += 'filemime:audio/midi';
break;
case 'wav':
case 'wave':
types += 'filemime:audio/wav';
break;
case 'jpg':
types += 'filemime:image/jpeg';
break;
case 'tif':
types += 'filemime:image/tiff';
break;
case 'svg':
case 'xml':
types += 'filemime:xml/svg';
break;
// Other known MIME types
case 'ogg':
case 'pdf':
types += 'filemime:application/' + type;
break;
default:
types += 'filemime:image/' + type;
break;
}
} );
return types;
},
parser: /(?:^| +)\bfile(?:mime|type):(?:\w+\/)?(\w+)/gi
},
{
group: 'files',
id: 'filew',
formatter: function ( val ) {
return 'filew:' + val;
},
parser: /(?:^| +)\bfilew(?:idth)?\b:?([<>\d.,]+)/gi
},
{
group: 'files',
id: 'fileh',
formatter: function ( val ) {
return 'fileh:' + val;
},
parser: /(?:^| +)\bfileh(?:eight)?\b:?([<>\d.,]+)/gi
},
{
group: 'files',
id: 'fileres',
formatter: function ( val ) {
return 'fileres:' + val;
},
parser: /(?:^| +)\bfileres\b:?([<>\d.,]+)/gi
}
// Ordering
// prefer-recent:
// boost-templates:
// Meta
// linksto:
// neartitle:
// morelike:
];
var i18n = {
'de': {
'advanced-search': 'Erweiterte Suchoptionen',
text: 'Text',
phrase: 'Genau diese Wortgruppe:',
fuzzy: 'Ungefähr dieses Wort:',
not: 'Nicht dieses Wort:',
hastemplate: 'Nur Seiten mit dieser Vorlage:',
insource: 'Suche im Wikitext:',
title: 'Titel',
prefix: 'Seitentitel beginnt mit:',
intitle: 'Seitentitel enthält:',
categories: 'Kategorien',
deepcat: 'In dieser oder tieferer Kategorie:',
incategory: 'Nur direkt in dieser Kategorie:',
files: 'Dateien',
filetype: 'Dateien dieses Typs:',
filew: 'Dateibreite in Pixel:',
fileh: 'Dateihöhe in Pixel:',
fileres: 'Diagonalauflösung in Pixel:'
},
'en': {
'advanced-search': 'Advanced search options',
text: 'Text',
phrase: 'Exactly this phrase:',
fuzzy: 'Approximately this word:',
not: 'Not this word:',
hastemplate: 'Only pages with this template:',
insource: 'Search in wikitext source:',
title: 'Title',
prefix: 'Page title starts with:',
intitle: 'Page title contains:',
categories: 'Categories',
deepcat: 'In this or deeper category:',
incategory: 'Only in this category:',
files: 'Files',
filetype: 'File type:',
filew: 'File width in pixels:',
fileh: 'File height in pixels:',
fileres: 'Diagonal resolution in pixels:'
}
};
/**
* @param {string} key
* @return {string}
*/
function msg( key ) {
var lang = mw.config.get( 'wgUserLanguage' );
return i18n[lang] && i18n[lang][key] || i18n.en[key] || '<' + key + '>';
}
/**
* @param {RegExp|undefined} regexp
* @param {string} val
* @return {Array|boolean}
*/
function lastMatch( regexp, val ) {
var match,
lastMatch = false;
while ( regexp && ( match = regexp.exec( val ) ) ) {
lastMatch = match;
}
return lastMatch;
}
/**
* @param {string} val
* @return {string}
*/
function parseSearchOptions( val ) {
for ( var i = advancedOptions.length; i--; ) {
var option = advancedOptions[i],
$field = $( '#advancedSearchOption-' + option.id );
if ( !$field.length || $.trim( $field.val() ) !== '' ) {
continue;
}
var match = lastMatch( option.parser, val );
if ( match ) {
val = val.slice( 0, match.index ) + val.slice( match.index + match[0].length );
$field.val( trimQuotes( match[1] ) );
}
}
return $.trim( val );
}
/**
* @param {string} allValues
* @return {string}
*/
function formatSearchOptions( allValues ) {
advancedOptions.forEach( function ( option ) {
var $field = $( '#advancedSearchOption-' + option.id );
if ( !$field.length ) {
return;
}
var val = $.trim( $field.val() );
if ( val !== '' ) {
allValues += ' ' + option.formatter( val );
$field.val( '' );
}
} );
return $.trim( allValues );
}
mw.loader.using( [ 'oojs-ui' ], function () {
var $search = $( 'form#search, form#powersearch' ),
optionSets = {};
advancedOptions.forEach( function ( option ) {
if ( option.enabled && !option.enabled() ) {
return;
}
var id = 'advancedSearchOption-' + option.id;
if ( !optionSets[option.group] ) {
optionSets[option.group] = [];
}
optionSets[option.group].push(
$( '<div>' )
.css( { display: 'table-row' } )
.append(
$( '<label>' )
.prop( { 'for': id } )
.css( {
display: 'table-cell',
'text-align': 'right',
'padding-right': '0.5em',
width: '20em'
} )
.text( msg( option.id ) ),
$( '<input>' )
.prop( { id: id } )
.css( { display: 'table-cell' } )
)
);
} );
var $allOptions = $( '<fieldset>' )
.append( $( '<legend>' ).text( msg( 'advanced-search' ) ) )
.hide();
for ( var group in optionSets ) {
var $optionSet = $( '<fieldset>' )
.css( {
border: 'none',
'border-top': 'solid 1px #ccc',
margin: 0
} )
.append( $( '<legend>' )
.css( { color: '#666' } )
.text( msg( group ) )
);
optionSets[group].forEach( function ( $option ) {
$optionSet.append( $option );
} );
$allOptions.append( $optionSet );
}
var advancedButton = new OO.ui.ButtonWidget( {
icon: 'advanced',
label: msg( 'advanced-search' ),
title: msg( 'advanced-search' ),
indicator: 'down'
} ).on( 'click', function () {
$allOptions.toggle();
if ( $allOptions.is(':visible') ) {
var $field = $search.find( 'input[name="search"]' );
$field.val( parseSearchOptions( $field.val() ) );
}
} );
var $advancedButton = advancedButton.$element.css( {
clear: 'both',
display: 'block'
} );
$( '#mw-search-top-table').after( $advancedButton, $allOptions );
$search.on( 'submit', function () {
var $field = $( this ).find( 'input[name="search"]' );
$field.val( formatSearchOptions( $field.val() ) );
} );
} );
} )( mediaWiki, jQuery );