Merge "Show protection log on creation-protected pages"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / mw.rcfilters.UriProcessor.js
1 ( function ( mw, $ ) {
2 /* eslint no-underscore-dangle: "off" */
3 /**
4 * URI Processor for RCFilters
5 *
6 * @param {mw.rcfilters.dm.FiltersViewModel} filtersModel Filters view model
7 */
8 mw.rcfilters.UriProcessor = function MwRcfiltersController( filtersModel ) {
9 this.emptyParameterState = {};
10 this.filtersModel = filtersModel;
11
12 // Initialize
13 this._buildEmptyParameterState();
14 };
15
16 /* Initialization */
17 OO.initClass( mw.rcfilters.UriProcessor );
18
19 /* Static methods */
20
21 /**
22 * Replace the url history through replaceState
23 *
24 * @param {mw.Uri} newUri New URI to replace
25 */
26 mw.rcfilters.UriProcessor.static.replaceState = function ( newUri ) {
27 window.history.replaceState(
28 { tag: 'rcfilters' },
29 document.title,
30 newUri.toString()
31 );
32 };
33
34 /**
35 * Push the url to history through pushState
36 *
37 * @param {mw.Uri} newUri New URI to push
38 */
39 mw.rcfilters.UriProcessor.static.pushState = function ( newUri ) {
40 window.history.pushState(
41 { tag: 'rcfilters' },
42 document.title,
43 newUri.toString()
44 );
45 };
46
47 /* Methods */
48
49 /**
50 * Get the version that this URL query is tagged with.
51 *
52 * @param {Object} [uriQuery] URI query
53 * @return {number} URL version
54 */
55 mw.rcfilters.UriProcessor.prototype.getVersion = function ( uriQuery ) {
56 uriQuery = uriQuery || new mw.Uri().query;
57
58 return Number( uriQuery.urlversion || 1 );
59 };
60
61 /**
62 * Update the filters model based on the URI query
63 * This happens on initialization, and from this moment on,
64 * we consider the system synchronized, and the model serves
65 * as the source of truth for the URL.
66 *
67 * This methods should only be called once on initialiation.
68 * After initialization, the model updates the URL, not the
69 * other way around.
70 *
71 * @param {Object} [uriQuery] URI query
72 */
73 mw.rcfilters.UriProcessor.prototype.updateModelBasedOnQuery = function ( uriQuery ) {
74 var parameters;
75
76 uriQuery = uriQuery || new mw.Uri().query;
77
78 // For arbitrary numeric single_option values, check the uri and see if it's beyond the limit
79 $.each( this.filtersModel.getFilterGroups(), function ( groupName, groupModel ) {
80 if (
81 groupModel.getType() === 'single_option' &&
82 groupModel.isAllowArbitrary()
83 ) {
84 if (
85 groupModel.getMaxValue() !== null &&
86 uriQuery[ groupName ] > groupModel.getMaxValue()
87 ) {
88 // Change the value to the actual max value
89 uriQuery[ groupName ] = String( groupModel.getMaxValue() );
90 } else if (
91 groupModel.getMinValue() !== null &&
92 uriQuery[ groupName ] < groupModel.getMinValue()
93 ) {
94 // Change the value to the actual min value
95 uriQuery[ groupName ] = String( groupModel.getMinValue() );
96 }
97 }
98 } );
99
100 // Normalize
101 parameters = this._getNormalizedQueryParams( uriQuery );
102
103 // Update filter states
104 this.filtersModel.toggleFiltersSelected(
105 this.filtersModel.getFiltersFromParameters(
106 parameters
107 )
108 );
109
110 this.filtersModel.toggleInvertedNamespaces( !!Number( parameters.invert ) );
111
112 // Update highlight state
113 this.filtersModel.toggleHighlight( !!Number( parameters.highlight ) );
114 this.filtersModel.getItems().forEach( function ( filterItem ) {
115 var color = parameters[ filterItem.getName() + '_color' ];
116 if ( color ) {
117 filterItem.setHighlightColor( color );
118 } else {
119 filterItem.clearHighlightColor();
120 }
121 } );
122
123 // Check all filter interactions
124 this.filtersModel.reassessFilterInteractions();
125 };
126
127 /**
128 * Get parameters representing the current state of the model
129 *
130 * @return {Object} Uri query parameters
131 */
132 mw.rcfilters.UriProcessor.prototype.getUriParametersFromModel = function () {
133 return $.extend(
134 true,
135 {},
136 this.filtersModel.getParametersFromFilters(),
137 this.filtersModel.getHighlightParameters(),
138 {
139 highlight: String( Number( this.filtersModel.isHighlightEnabled() ) ),
140 invert: String( Number( this.filtersModel.areNamespacesInverted() ) )
141 }
142 );
143 };
144
145 /**
146 * Build the full parameter representation based on given query parameters
147 *
148 * @private
149 * @param {Object} uriQuery Given URI query
150 * @return {Object} Full parameter state representing the URI query
151 */
152 mw.rcfilters.UriProcessor.prototype._expandModelParameters = function ( uriQuery ) {
153 var filterRepresentation = this.filtersModel.getFiltersFromParameters( uriQuery );
154
155 return $.extend( true,
156 {},
157 uriQuery,
158 this.filtersModel.getParametersFromFilters( filterRepresentation ),
159 this.filtersModel.extractHighlightValues( uriQuery ),
160 {
161 highlight: String( Number( uriQuery.highlight ) ),
162 invert: String( Number( uriQuery.invert ) )
163 }
164 );
165 };
166
167 /**
168 * Compare two URI queries to decide whether they are different
169 * enough to represent a new state.
170 *
171 * @param {Object} currentUriQuery Current Uri query
172 * @param {Object} updatedUriQuery Updated Uri query
173 * @return {boolean} This is a new state
174 */
175 mw.rcfilters.UriProcessor.prototype.isNewState = function ( currentUriQuery, updatedUriQuery ) {
176 var currentParamState, updatedParamState,
177 notEquivalent = function ( obj1, obj2 ) {
178 var keys = Object.keys( obj1 ).concat( Object.keys( obj2 ) );
179 return keys.some( function ( key ) {
180 return obj1[ key ] != obj2[ key ]; // eslint-disable-line eqeqeq
181 } );
182 };
183
184 // Compare states instead of parameters
185 // This will allow us to always have a proper check of whether
186 // the requested new url is one to change or not, regardless of
187 // actual parameter visibility/representation in the URL
188 currentParamState = this._expandModelParameters( currentUriQuery );
189 updatedParamState = this._expandModelParameters( updatedUriQuery );
190
191 return notEquivalent( currentParamState, updatedParamState );
192 };
193
194 /**
195 * Check whether the given query has parameters that are
196 * recognized as parameters we should load the system with
197 *
198 * @param {mw.Uri} [uriQuery] Given URI query
199 * @return {boolean} Query contains valid recognized parameters
200 */
201 mw.rcfilters.UriProcessor.prototype.doesQueryContainRecognizedParams = function ( uriQuery ) {
202 var anyValidInUrl,
203 validParameterNames = Object.keys( this._getEmptyParameterState() )
204 .filter( function ( param ) {
205 // Remove 'highlight' parameter from this check;
206 // if it's the only parameter in the URL we still
207 // want to consider the URL 'empty' for defaults to load
208 return param !== 'highlight';
209 } );
210
211 uriQuery = uriQuery || new mw.Uri().query;
212
213 anyValidInUrl = Object.keys( uriQuery ).some( function ( parameter ) {
214 return validParameterNames.indexOf( parameter ) > -1;
215 } );
216
217 // URL version 2 is allowed to be empty or within nonrecognized params
218 return anyValidInUrl || this.getVersion( uriQuery ) === 2;
219 };
220
221 /**
222 * Remove all parameters that have the same value as the base state
223 * This method expects uri queries of the urlversion=2 format
224 *
225 * @private
226 * @param {Object} uriQuery Current uri query
227 * @return {Object} Minimized query
228 */
229 mw.rcfilters.UriProcessor.prototype.minimizeQuery = function ( uriQuery ) {
230 var baseParams = this._getEmptyParameterState(),
231 uriResult = $.extend( true, {}, uriQuery );
232
233 $.each( uriResult, function ( paramName, paramValue ) {
234 if (
235 baseParams[ paramName ] !== undefined &&
236 baseParams[ paramName ] === paramValue
237 ) {
238 // Remove parameter from query
239 delete uriResult[ paramName ];
240 }
241 } );
242
243 return uriResult;
244 };
245
246 /**
247 * Get the adjusted URI params based on the url version
248 * If the urlversion is not 2, the parameters are merged with
249 * the model's defaults.
250 *
251 * @private
252 * @param {Object} uriQuery Current URI query
253 * @return {Object} Normalized parameters
254 */
255 mw.rcfilters.UriProcessor.prototype._getNormalizedQueryParams = function ( uriQuery ) {
256 // Check whether we are dealing with urlversion=2
257 // If we are, we do not merge the initial request with
258 // defaults. Not having urlversion=2 means we need to
259 // reproduce the server-side request and merge the
260 // requested parameters (or starting state) with the
261 // wiki default.
262 // Any subsequent change of the URL through the RCFilters
263 // system will receive 'urlversion=2'
264 var hiddenParamDefaults = {},
265 base = this.getVersion( uriQuery ) === 2 ?
266 {} :
267 this.filtersModel.getDefaultParams();
268
269 // Go over the model and get all hidden parameters' defaults
270 // These defaults should be applied regardless of the urlversion
271 // but be overridden by the URL params if they exist
272 $.each( this.filtersModel.getFilterGroups(), function ( groupName, groupModel ) {
273 if ( groupModel.isHidden() ) {
274 $.extend( true, hiddenParamDefaults, groupModel.getDefaultParams() );
275 }
276 } );
277
278 return this.minimizeQuery(
279 $.extend( true, {}, hiddenParamDefaults, base, uriQuery, { urlversion: '2' } )
280 );
281 };
282
283 /**
284 * Get the representation of an empty parameter state
285 *
286 * @private
287 * @return {Object} Empty parameter state
288 */
289 mw.rcfilters.UriProcessor.prototype._getEmptyParameterState = function () {
290 // Override empty parameter state with the sticky parameter values
291 return $.extend( true, {}, this.emptyParameterState, this.filtersModel.getStickyParams() );
292 };
293
294 /**
295 * Build an empty representation of the parameters, where all parameters
296 * are either set to '0' or '' depending on their type.
297 * This must run during initialization, before highlights are set.
298 *
299 * @private
300 */
301 mw.rcfilters.UriProcessor.prototype._buildEmptyParameterState = function () {
302 var emptyParams = this.filtersModel.getParametersFromFilters( {} ),
303 emptyHighlights = this.filtersModel.getHighlightParameters();
304
305 this.emptyParameterState = $.extend(
306 true,
307 {},
308 emptyParams,
309 emptyHighlights,
310 { highlight: '0', invert: '0' }
311 );
312 };
313 }( mediaWiki, jQuery ) );