Last active
March 17, 2020 23:56
-
-
Save jdlrobson/89876628d18413490db85fd56a8ada37 to your computer and use it in GitHub Desktop.
Allow skins to opt out of sortable and collapsible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/includes/skins/Skin.php b/includes/skins/Skin.php | |
index 44a9eb09a9..c90d4b0da2 100644 | |
--- a/includes/skins/Skin.php | |
+++ b/includes/skins/Skin.php | |
@@ -164,6 +164,40 @@ abstract class Skin extends ContextSource { | |
*/ | |
public function initPage( OutputPage $out ) { | |
$this->preloadExistence(); | |
+ $out->addJsConfigVars( [ | |
+ 'wgCollapsible' => $this->supportsFeature( 'table-sorting' ), | |
+ 'wgSortable' => $this->supportsFeature( 'table-sorting' ) | |
+ ] ); | |
+ } | |
+ | |
+ /** | |
+ * Maps features to string tests. | |
+ */ | |
+ private $supportedFeatures = []; | |
+ | |
+ /** | |
+ * Is a certain feature present in the current OutputPage and supported by the skin? | |
+ * @param string $feature | |
+ * @param OutputPage $out | |
+ * @return bool | |
+ */ | |
+ protected function supportsFeature( $feature, $out ) { | |
+ $supported = isset( $this->supportedFeatures[$feature] ); | |
+ if ( $supported ) { | |
+ return $supported; | |
+ } | |
+ switch ( $feature ) { | |
+ case 'table-sorting': | |
+ $supported = strpos( $out->getHTML(), 'sortable' ) !== false; | |
+ break; | |
+ case 'collapsible': | |
+ $supported = strpos( $out->getHTML(), 'mw-collapsible' ) !== false; | |
+ break; | |
+ default: | |
+ throw new MWException( __METHOD__ . " Unknown skin feature $feature" ); | |
+ } | |
+ $this->supportedFeatures[$feature] = $supported; | |
+ return $supported; | |
} | |
/** | |
@@ -213,13 +247,13 @@ abstract class Skin extends ContextSource { | |
]; | |
// Preload jquery.tablesorter for mediawiki.page.ready | |
- if ( strpos( $out->getHTML(), 'sortable' ) !== false ) { | |
+ if ( $this->supportsFeature( 'table-sorting', $out ) ) { | |
$modules['content'][] = 'jquery.tablesorter'; | |
$modules['styles']['content'][] = 'jquery.tablesorter.styles'; | |
} | |
// Preload jquery.makeCollapsible for mediawiki.page.ready | |
- if ( strpos( $out->getHTML(), 'mw-collapsible' ) !== false ) { | |
+ if ( $this->supportsFeature( 'collapsible', $out ) ) { | |
$modules['content'][] = 'jquery.makeCollapsible'; | |
$modules['styles']['content'][] = 'jquery.makeCollapsible.styles'; | |
} | |
diff --git a/resources/src/mediawiki.page.ready/ready.js b/resources/src/mediawiki.page.ready/ready.js | |
index 9b1bb90b44..46e86fa36f 100644 | |
--- a/resources/src/mediawiki.page.ready/ready.js | |
+++ b/resources/src/mediawiki.page.ready/ready.js | |
@@ -38,7 +38,7 @@ mw.hook( 'wikipage.content' ).add( function ( $content ) { | |
var $sortable, $collapsible; | |
$collapsible = $content.find( '.mw-collapsible' ); | |
- if ( $collapsible.length ) { | |
+ if ( $collapsible.length && mw.config.get( 'wgCollapsible' ) ) { | |
// Preloaded by Skin::getDefaultModules() | |
mw.loader.using( 'jquery.makeCollapsible', function () { | |
$collapsible.makeCollapsible(); | |
@@ -46,7 +46,7 @@ mw.hook( 'wikipage.content' ).add( function ( $content ) { | |
} | |
$sortable = $content.find( 'table.sortable' ); | |
- if ( $sortable.length ) { | |
+ if ( $sortable.length && mw.config.get( 'wgSortable' ) ) { | |
// Preloaded by Skin::getDefaultModules() | |
mw.loader.using( 'jquery.tablesorter', function () { | |
$sortable.tablesorter(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment