All files Page.js

83.65% Statements 87/104
86.57% Branches 58/67
56.52% Functions 13/23
83.65% Lines 87/104

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493  1x 1x 1x 1x 1x 1x 1x 1x 1x                             65x 63x   65x 65x 65x   65x     65x 65x 65x 65x 65x 65x 65x 65x     65x     1x                                                                                                                   68x                                                   19x         19x                                                     21x     8x     21x   4x 4x 2x   2x 2x                     17x         17x   5x   5x 5x                 12x 12x                                           4x 2x     2x                                       2x                                                                                                   16x   16x 12x   4x   16x                   8x   8x                 65x 65x 65x   65x                                           9x 9x 9x   9x 9x     9x 7x 7x   7x   7x 7x     7x     2x       7x 3x                   9x   9x                                                                                           1x   2x 2x     2x   2x           2x       2x   2x         2x               2x                             1x   1x  
var
	HTML = mw.html,
	mfExtend = require( './mfExtend' ),
	time = require( './time' ),
	util = require( './util' ),
	Section = require( './Section' ),
	Thumbnail = require( './Thumbnail' ),
	View = require( './View' ),
	HEADING_SELECTOR = mw.config.get( 'wgMFMobileFormatterHeadings' ).join( ',' ),
	BLACKLISTED_THUMBNAIL_CLASS_SELECTORS = [ 'noviewer', 'metadata' ];
 
/**
 * Mobile page view object
 *
 * @class Page
 * @uses Section
 * @extends View
 *
 * @param {Object} options Configuration options
 */
function Page( options ) {
	var thumb;
	// If thumbnail is not passed it should be made false (truthy) so that it
	// renders a placeholder when absent.
	if ( options.thumbnail === undefined ) {
		options.thumbnail = false;
	}
	this.options = options;
	options.languageUrl = mw.util.getUrl( 'Special:MobileLanguages/' + options.title );
	View.call( this, options );
	// Fallback if no displayTitle provided
	options.displayTitle = this.getDisplayTitle();
	// allow usage in templates.
	// FIXME: Should View map all options to properties?
	this.title = options.title;
	this.displayTitle = options.displayTitle;
	this.thumbnail = options.thumbnail;
	this.url = options.url || mw.util.getUrl( options.title );
	this.id = options.id;
	this.isMissing = options.isMissing !== undefined ? options.isMissing : options.id === 0;
	thumb = this.thumbnail;
	Iif ( thumb && thumb.width ) {
		this.thumbnail.isLandscape = thumb.width > thumb.height;
	}
	this.wikidataDescription = options.wikidataDescription;
}
 
mfExtend( Page, View, {
	/**
	 * @memberof Page
	 * @instance
	 * @mixes View#defaults
	 * @property {Object} defaults Default options hash.
	 * @property {number} defaults.id Page ID. The default value of 0 represents a
	 * new or missing page. Be sure to override it to avoid side effects.
	 * @property {string} defaults.title Title of the page. It includes prefix where needed and
	 * is human readable, e.g. Talk:The man who lived.
	 * @property {string} defaults.displayTitle HTML title of the page for display. Falls back
	 * to defaults.title (escaped) if no value is provided. Must be safe HTML!
	 * @property {number} defaults.namespaceNumber the number of the
	 *  namespace the page belongs to
	 * @property {Object} defaults.protection List of permissions as returned by API,
	 * e.g. [{ edit: ['*'] }]
	 * @property {Array} defaults.sections Array of {Section} objects.
	 * @property {boolean} defaults.isMainPage Whether the page is the Main Page.
	 * @property {boolean} defaults.isMissing Whether the page exists in the wiki.
	 * @property {Object} defaults.thumbnail thumbnail definition corresponding to page image
	 * @property {boolean} defaults.thumbnail.isLandscape whether the image is in
	 *  landscape format
	 * @property {number} defaults.thumbnail.width of image in pixels.
	 * @property {number} defaults.thumbnail.height of image in pixels.
	 * @property {string} defaults.thumbnail.source url for image
	 */
	defaults: {
		id: 0,
		title: '',
		displayTitle: '',
		namespaceNumber: 0,
		protection: {
			edit: [ '*' ]
		},
		sections: [],
		isMissing: false,
		isMainPage: false,
		url: undefined,
		thumbnail: {
			isLandscape: undefined,
			source: undefined,
			width: undefined,
			height: undefined
		}
	},
	/**
	 * @inheritdoc
	 * @memberof Page
	 * @instance
	 */
	isBorderBox: false,
	/**
	 * Retrieve the title that should be displayed to the user
	 * @memberof Page
	 * @instance
	 * @return {string} HTML
	 */
	getDisplayTitle: function () {
		return this.options.displayTitle || HTML.escape( this.options.title );
	},
	/**
	 * Determine if current page is in a specified namespace
	 * @memberof Page
	 * @instance
	 * @param {string} namespace Name of namespace
	 * @return {boolean}
	 */
	inNamespace: function ( namespace ) {
		return this.options.namespaceNumber === mw.config.get( 'wgNamespaceIds' )[namespace];
	},
 
	/**
	 * Find the heading in the page.
	 * This has the benefit of excluding any additional h2s and h3s that may
	 * have been added programatically.
	 * @method
	 * @param {number} sectionIndex as defined by the PHP parser.
	 *  It should correspond to the section id
	 *  used in the edit link for the section.
	 *  Note, confusingly, this is different from section "ID" which is
	 * used in methods
	 * @return {jQuery.Object}
	 */
	findSectionHeadingByIndex: function ( sectionIndex ) {
		Iif ( sectionIndex < 1 ) {
			// negative indexes will search from the end, which is behaviour we do not want.
			// return an empty set when this happens.
			return this.$();
		} else {
			return this.$( HEADING_SELECTOR )
				// Headings must strictly be a child element of a section element
				// or the parser-output.
				// Not an ancestor!
				.filter( '.mw-parser-output > *, [class^="mf-section-"] > *' ).eq( sectionIndex - 1 );
		}
	},
	/**
	 * Finds all child elements that match the selector in a given section or subsection.
	 * Returns any direct child elements that match the selector,
	 * (i.e. searches only one level deep)
	 * as well as any elements that match the selector within those children.
	 * If the Page has no headings (e.g. a stub),
	 * then the search will target all nodes within the page.
	 *
	 * This code should work on desktop (PHP parser HTML)
	 * as well as mobile formatted HTML (PHP parser + MobileFormatter)
	 * @method
	 * @param {number} sectionIndex as defined by the PHP parser. It should correspond to
	 *  the section id used in the edit link for the section.
	 *  Note, confusingly, this is different from section "ID" which is
	 *  used in methods
	 * @param {string} selector to match
	 * @return {jQuery.Object}
	 */
	findChildInSectionLead: function ( sectionIndex, selector ) {
		var $heading, $nextHeading, $container, $lead,
			headingSelector = HEADING_SELECTOR;
 
		function withNestedChildren( $matchingNodes ) {
			return $matchingNodes.find( selector ).addBack();
		}
 
		if ( sectionIndex === 0 ) {
			// lead is easy
			$lead = this.getLeadSectionElement();
			if ( $lead && $lead.length ) {
				return withNestedChildren( $lead.children( selector ) );
			} else {
				$heading = this.findSectionHeadingByIndex( 1 );
				return $heading.length ? withNestedChildren( $heading.prevAll( selector ) ) :
					// this page is a stub so search entire page
					this.$( selector );
			}
		}
 
		// find heading associated with the section by looking at its
		// index position in the article
		// section ids relate to the element position in the page and the first heading
		// lead has been dealt with above, so first heading corresponds to section 1,
		// the first heading in the article.
		$heading = this.findSectionHeadingByIndex( sectionIndex );
 
		// If section-heading is present on the heading,
		// then we know the page has been MobileFormatted
		// and that this is a wrapped section
		if ( $heading.hasClass( 'section-heading' ) ) {
			// get content of section
			$container = $heading.next();
			// inside section find the first heading
			$nextHeading = $container.find( headingSelector ).eq( 0 );
			return $nextHeading.length ?
				// find all amboxes before the next heading
				withNestedChildren( $nextHeading.prevAll( selector ) ) :
				// There is no subheadings inside
				// Grab all issues in section
				withNestedChildren( $container.children( selector ) );
		} else {
			// the heading relates to a subsection (or unwrapped desktop section),
			// so grab elements between this and the next one
			$nextHeading = $heading.eq( 0 ).nextAll( headingSelector ).eq( 0 );
			return $heading.nextUntil( $nextHeading, selector );
		}
	},
 
	/**
	 * Get the lead section of the page view.
	 * @memberof Page
	 * @instance
	 * @return {jQuery.Object|null}
	 */
	getLeadSectionElement: function () {
		/*
		 * The page is formatted as follows:
		 * <div id="bodyContent">
		 *   <!-- content of the page.. -->
		 *   <div id="mw-content-text">
		 *     <div class="mf-section-0">lead section</div>
		 *     <h2></h2>
		 *     <div class="mf-section-1">second section</div>
		 *   </div>
		 * </div>
		 */
		if ( this.$( '.mf-section-0' ).length ) {
			return this.$( '.mf-section-0' );
		}
		// no lead section found
		return null;
	},
 
	/**
	 * Determines if content model is wikitext
	 * @memberof Page
	 * @instance
	 * @return {boolean}
	 */
	isWikiText: function () {
		return mw.config.get( 'wgPageContentModel' ) === 'wikitext';
	},
 
	/**
	 * Checks whether the current page is the main page
	 * @memberof Page
	 * @instance
	 * @return {boolean}
	 */
	isMainPage: function () {
		return this.options.isMainPage;
	},
	/**
	 * Checks whether the current page is watched
	 * @memberof Page
	 * @instance
	 * @return {boolean}
	 */
	isWatched: function () {
		return this.options.isWatched;
	},
 
	/**
	 * Return the latest revision id for this page
	 * @memberof Page
	 * @instance
	 * @return {number}
	 */
	getRevisionId: function () {
		return this.options.revId;
	},
 
	/**
	 * Return prefixed page title
	 * @memberof Page
	 * @instance
	 * @return {string}
	 */
	getTitle: function () {
		return this.options.title;
	},
 
	/**
	 * Return page id
	 * @memberof Page
	 * @instance
	 * @return {number}
	 */
	getId: function () {
		return this.options.id;
	},
 
	/**
	 * return namespace id
	 * @memberof Page
	 * @instance
	 * @return {number} namespace Number
	 */
	getNamespaceId: function () {
		var nsId,
			args = this.options.title.split( ':' );
 
		if ( args[1] ) {
			nsId = mw.config.get( 'wgNamespaceIds' )[ args[0].toLowerCase().replace( ' ', '_' ) ] || 0;
		} else {
			nsId = 0;
		}
		return nsId;
	},
 
	/**
	 * Determines if current page is a talk page
	 * @memberof Page
	 * @instance
	 * @return {boolean} Whether the page is a talk page or not
	 */
	isTalkPage: function () {
		var ns = this.getNamespaceId();
		// all talk pages are odd Numbers (except the case of special pages)
		return ns > 0 && ns % 2 === 1;
	},
 
	/**
	 * @inheritdoc
	 * @memberof Page
	 * @instance
	 */
	preRender: function () {
		this.sections = [];
		this._sectionLookup = {};
		this.title = this.options.title;
 
		this.options.sections.forEach( function ( sectionData ) {
			var section = new Section( sectionData );
			this.sections.push( section );
			this._sectionLookup[section.id] = section;
		}.bind( this ) );
	},
 
	/**
	 * Return all the thumbnails in the article.
	 * Images which have a class or link container (.image|.thumbimage)
	 * that matches one of the items of the constant BLACKLISTED_THUMBNAIL_CLASS_SELECTORS
	 * will be excluded.
	 * A thumbnail nested inside one of these classes will still be returned.
	 * e.g. `<div class="noviewer"><a class="image"><img></a></div>` is not a valid thumbnail
	 * `<a class="image noviewer"><img></a>` is not a valid thumbnail
	 * `<a class="image"><img class="noviewer"></a>` is not a valid thumbnail
	 * @memberof Page
	 * @instance
	 * @return {Thumbnail[]}
	 */
	getThumbnails: function () {
		var $thumbs,
			$el = this.$el,
			blacklistSelector = '.' + BLACKLISTED_THUMBNAIL_CLASS_SELECTORS.join( ',.' ),
			thumbs = [];
 
		Eif ( !this._thumbs ) {
			$thumbs = $el.find( 'a.image, a.thumbimage' )
				.not( blacklistSelector );
 
			$thumbs.each( function () {
				var $a = $el.find( this ),
					$lazyImage = $a.find( '.lazy-image-placeholder' ),
					// Parents need to be checked as well.
					valid = $a.parents( blacklistSelector ).length === 0 &&
						$a.find( blacklistSelector ).length === 0,
					legacyMatch = $a.attr( 'href' ).match( /title=([^/&]+)/ ),
					match = $a.attr( 'href' ).match( /[^/]+$/ );
 
				// filter out invalid lazy loaded images if so far image is valid
				if ( $lazyImage.length && valid ) {
					// if the regex matches it means the image has one of the classes
					// thus we must invert the result
					valid = !new RegExp( '\\b(' + BLACKLISTED_THUMBNAIL_CLASS_SELECTORS.join( '|' ) + ')\\b' )
						.test( $lazyImage.data( 'class' ) );
				}
 
				if ( valid && ( legacyMatch || match ) ) {
					thumbs.push(
						new Thumbnail( {
							el: $a,
							filename: decodeURIComponent(
								legacyMatch ? legacyMatch[1] : match[0]
							)
						} )
					);
				}
			} );
			this._thumbs = thumbs;
		}
		return this._thumbs;
	},
 
	/**
	 * FIXME: Change function signature to take the anchor of the heading
	 * @memberof Page
	 * @instance
	 * @param {string} id of the section as defined by MobileFormatter.
	 * Note, that currently, this is different from
	 * the PHP parser in that it relates to top-level sections.
	 * For example, mf-section-1 would relate to section 1. See FIXME.
	 * @return {Section}
	 */
	getSection: function ( id ) {
		return this._sectionLookup[ id ];
	},
 
	/**
	 * Obtain the list of high level (and grouped) sections.
	 * Note that this list will not include subsections.
	 * @memberof Page
	 * @instance
	 * @return {Array} of Section instances
	 */
	getSections: function () {
		return this.sections;
	},
 
	/**
	 * Returns a jQuery object representing all redlinks on the page.
	 * @memberof Page
	 * @instance
	 * @return {jQuery.Object}
	 */
	getRedLinks: function () {
		return this.$( '.new' );
	}
} );
 
/**
 * Create a Page object from an API response.
 *
 * @memberof Page
 * @param {Object} resp as representing a page in the API
 * @return {Page}
 */
Page.newFromJSON = function ( resp ) {
	var revision, displayTitle,
		thumb = resp.thumbnail,
		pageprops = resp.pageprops || {
			displaytitle: HTML.escape( resp.title )
		},
		terms = resp.terms;
 
	Eif ( pageprops || terms ) {
		// The label is either the display title or the label pageprop
		// (the latter used by Wikidata)
		// Long term we want to consolidate these.
		// Note that pageprops.displaytitle is HTML, while
		// terms.label[0] is plain text.
		displayTitle = terms && terms.label ?
			HTML.escape( terms.label[0] ) : pageprops.displaytitle;
	}
	// Add Wikidata descriptions if available (T101719)
	resp.wikidataDescription = resp.description || undefined;
 
	Iif ( thumb ) {
		resp.thumbnail.isLandscape = thumb.width > thumb.height;
	}
 
	// page may or may not exist.
	Iif ( resp.revisions && resp.revisions[0] ) {
		revision = resp.revisions[0];
		resp.lastModified = time.getLastModifiedMessage(
			new Date( revision.timestamp ).getTime() / 1000,
			revision.user
		);
	}
 
	return new Page(
		util.extend( resp, {
			id: resp.pageid,
			isMissing: !!resp.missing,
			url: mw.util.getUrl( resp.title ),
			displayTitle: displayTitle // this is HTML!
		} )
	);
};
 
/**
 * Selector for matching headings
 *
 * @memberof Page
 */
Page.HEADING_SELECTOR = HEADING_SELECTOR;
 
module.exports = Page;