Jdlrobson has uploaded a new change for review.
https://gerrit.wikimedia.org/r/70736
Change subject: Define explodeBodyHtml function to allow us to action section
info from template
......................................................................
Define explodeBodyHtml function to allow us to action section info from template
Change-Id: Ifdc88084346efd3da0764a34992c2e690b62a089
---
M MobileFrontend.php
M includes/MobileFrontend.hooks.php
M includes/skins/MinervaTemplate.php
M includes/skins/SkinBootstrap.php
M includes/skins/SkinMobile.php
M includes/skins/SkinMobileWML.php
6 files changed, 127 insertions(+), 51 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend
refs/changes/36/70736/1
diff --git a/MobileFrontend.php b/MobileFrontend.php
index a2d8ed9..96173ea 100644
--- a/MobileFrontend.php
+++ b/MobileFrontend.php
@@ -94,6 +94,8 @@
$wgHooks['LinksUpdate'][] = 'MobileFrontendHooks::onLinksUpdate';
+$wgHooks['OutputPageParserOutput'][] =
'MobileFrontendHooks::onOutputPageParserOutput';
+
$wgHooks['RequestContextCreateSkin'][] =
'MobileFrontendHooks::onRequestContextCreateSkin';
$wgHooks['SkinTemplateOutputPageBeforeExec'][] =
'MobileFrontendHooks::onSkinTemplateOutputPageBeforeExec';
$wgHooks['BeforePageRedirect'][] = 'MobileFrontendHooks::onBeforePageRedirect';
diff --git a/includes/MobileFrontend.hooks.php
b/includes/MobileFrontend.hooks.php
index b981ce3..35ba466 100644
--- a/includes/MobileFrontend.hooks.php
+++ b/includes/MobileFrontend.hooks.php
@@ -33,6 +33,19 @@
}
/**
+ * OutputPageParserOutput hook handler
+ * @see
https://www.mediawiki.org/wiki/Manual:Hooks/OutputPageParserOutput
+ * FIXME [ParserOutput]: upstream to core
+ * @param $out OutputPage
+ * @param $parserOutput ParserOutput
+ * @return bool
+ */
+ public static function onOutputPageParserOutput( $out, $parserOutput ) {
+ $out->setProperty( 'parserOutput', $parserOutput );
+ return true;
+ }
+
+ /**
* RequestContextCreateSkin hook handler
* @see
https://www.mediawiki.org/wiki/Manual:Hooks/RequestContextCreateSkin
*
diff --git a/includes/skins/MinervaTemplate.php
b/includes/skins/MinervaTemplate.php
index 2397f11..b1ec06b 100644
--- a/includes/skins/MinervaTemplate.php
+++ b/includes/skins/MinervaTemplate.php
@@ -89,6 +89,50 @@
?></ul><?php
}
+ protected function renderHeading( $section ) {
+ $level = $section['level'];
+ $tag = 'h' . $level;
+ $className = $level === '2' ? 'section_heading' : '';
+ echo Html::openElement( $tag, array( 'class' => $className,
'id' => 'section_' . $section['index'] ) );
+ echo Html::element( 'span', array( 'id'=> $section['anchor'] ),
$section['line'] );
+ echo Html::closeElement( $tag );
+ }
+
+ protected function renderContent( $data ) {
+ $open = False;
+ if ( isset( $data['sections'] ) ) {
+ foreach( $data['sections'] as $section ) {
+ // deal with h2s
+ if ( $section['toclevel'] === 1 ) {
+ if ( $open ) {
+ // close last content block
+ echo Html::closeElement( 'div'
);
+ // close last section
+ echo Html::closeElement( 'div'
);
+ }
+ $open = True;
+ echo Html::openElement( 'div', array(
'class' => 'section' ), $section['line'] );
+ // heading
+ $this->renderHeading( $section );
+ // content
+ echo Html::openElement( 'div', array(
'class' => 'content_block', 'id' => 'content_' . $section['index'] ) );
+ echo $section['text'];
+ } else {
+ $this->renderHeading( $section );
+ echo $section['text'];
+ }
+ }
+ if ( $open ) {
+ // close last content block
+ echo Html::closeElement( 'div' );
+ // close last section
+ echo Html::closeElement( 'div' );
+ }
+ } else {
+ // FIXME: for legacy purposes (Minerva desktop skin
uses this)
+ echo $data[ 'bodytext' ];
+ }
+ }
protected function renderContentWrapper( $data ) {
$isSpecialPage = $this->getSkin()->getTitle()->isSpecialPage();
?>
@@ -99,7 +143,7 @@
echo $data['prebodytext'];
$this->renderPageActions( $data
);
}
- echo $data[ 'bodytext' ];
+ $this->renderContent( $data );
$this->renderLanguages();
echo $data['postbodytext'];
?>
diff --git a/includes/skins/SkinBootstrap.php b/includes/skins/SkinBootstrap.php
index 88e5153..736a6cc 100644
--- a/includes/skins/SkinBootstrap.php
+++ b/includes/skins/SkinBootstrap.php
@@ -6,55 +6,21 @@
* @ingroup Skins
*/
abstract class SkinBootstrap extends SkinTemplate {
- protected $headings = 0;
-
- /**
- * Callback for headingTransform()
- * @param array $matches
- * @return string
- */
- protected abstract function headingTransformCallback( $matches );
-
- /**
- * generates a back top link for a given section number
- * @param int $headingNumber: The number corresponding to the section
heading
- * @return string
- */
- protected function backToTopLink( $headingNumber ) {
- return Html::rawElement( 'a',
- array( 'id' => 'anchor_' . $headingNumber,
- 'href' => '#section_' . $headingNumber,
- 'class' => 'section_anchors' ),
- '↑' . $this->msg(
'mobile-frontend-back-to-top-of-section' ) );
- }
-
- /**
- * Prepares headings in WML mode, makes sections expandable in HTML mode
- * FIXME [ParserOutput] This should be done by the Parser
- * @param string $s
- * @return string
- */
- protected function headingTransform( $s ) {
- wfProfileIn( __METHOD__ );
-
- // Closures are a PHP 5.3 feature.
- // MediaWiki currently requires PHP 5.2.3 or higher.
- // So, using old style for now.
- $s = '<div id="content_0" class="content_block openSection">'
- . preg_replace_callback(
- '%<h2(.*)<span class="mw-headline"
[^>]*>(.+)</span>[\s\r\n]*</h2>%sU',
- array( $this, 'headingTransformCallback' ),
- $s
+ // FIXME [ParserOutput] This should be done by the Parser
+ protected function explodeBodyHtml() {
+ $data = array();
+ $parserOutput = $this->getOutput()->getProperty( 'parserOutput'
);
+ $sections = $parserOutput->getSections();
+ $html = $parserOutput->getText();
+ $chunks = preg_split( '/<h(?=[1-6]\b)/i', $html );
+ $data['lead'] = $chunks[1]; // table of contents has an h2 in it
+ foreach ( $sections as $key => $section ) {
+ $sections[$key]['text'] = trim(
+ preg_replace( '#<(h[1-6])\b.*?<\s*/\s*\\1>#',
'',
+ '<h' . $chunks[ $key + 2 ] ) // return the h
that we split on, remove heading from output.
);
-
- // if we had any, make sure to close the whole thing!
- if ( $this->headings > 0 ) {
- $bt = $this->backToTopLink( intval( $this->headings ) );
- $s .= '</div>' // <div class="content_block">
- . $bt
- . "\n</div>"; // <div class="section">
}
- wfProfileOut( __METHOD__ );
- return $s;
+ $data['sections'] = $sections;
+ return $data;
}
}
diff --git a/includes/skins/SkinMobile.php b/includes/skins/SkinMobile.php
index 4841402..c4c6ac4 100644
--- a/includes/skins/SkinMobile.php
+++ b/includes/skins/SkinMobile.php
@@ -46,12 +46,17 @@
}
}
$html = $this->extMobileFrontend->DOMParse( $out );
- $html = $this->headingTransform( $html );
wfProfileIn( __METHOD__ . '-tpl' );
$tpl = $this->prepareTemplate();
+ $data = $this->explodeBodyHtml( $html );
+ foreach ( $data as $key => $value ) {
+ $tpl->set( $key, $value );
+ }
+ // reset for mobile now it has been exploded
+ $tpl->set( 'bodytext', '' );
+
$tpl->set( 'headelement', $out->headElement( $this ) );
- $tpl->set( 'bodytext', $html );
$notice = '';
wfRunHooks( 'GetMobileNotice', array( $this, &$notice ) );
$tpl->set( 'notice', $notice );
diff --git a/includes/skins/SkinMobileWML.php b/includes/skins/SkinMobileWML.php
index 166f526..d88e135 100644
--- a/includes/skins/SkinMobileWML.php
+++ b/includes/skins/SkinMobileWML.php
@@ -6,6 +6,7 @@
public $skinname = 'SkinMobileWML';
public $stylename = 'SkinMobileWML';
public $template = 'MobileTemplateWML';
+ protected $headings = 0;
public function __construct( ExtMobileFrontend $extMobileFrontend ) {
$this->setContext( $extMobileFrontend );
@@ -106,6 +107,51 @@
return $card;
}
+ /**
+ * FIXME: use explodeBodyHtml instead
+ * generates a back top link for a given section number
+ * @param int $headingNumber: The number corresponding to the section
heading
+ * @return string
+ */
+ protected function backToTopLink( $headingNumber ) {
+ return Html::rawElement( 'a',
+ array( 'id' => 'anchor_' . $headingNumber,
+ 'href' => '#section_' . $headingNumber,
+ 'class' => 'section_anchors' ),
+ '↑' . $this->msg(
'mobile-frontend-back-to-top-of-section' ) );
+ }
+
+ /**
+ * Prepares headings in WML mode, makes sections expandable in HTML mode
+ * FIXME: use explodeBodyHtml instead
+ * @param string $s
+ * @return string
+ */
+ protected function headingTransform( $s ) {
+ wfProfileIn( __METHOD__ );
+
+ // Closures are a PHP 5.3 feature.
+ // MediaWiki currently requires PHP 5.2.3 or higher.
+ // So, using old style for now.
+ $s = '<div id="content_0" class="content_block openSection">'
+ . preg_replace_callback(
+ '%<h2(.*)<span class="mw-headline"
[^>]*>(.+)</span>[\s\r\n]*</h2>%sU',
+ array( $this, 'headingTransformCallback' ),
+ $s
+ );
+
+ // if we had any, make sure to close the whole thing!
+ if ( $this->headings > 0 ) {
+ $bt = $this->backToTopLink( intval( $this->headings ) );
+ $s .= '</div>' // <div class="content_block">
+ . $bt
+ . "\n</div>"; // <div class="section">
+ }
+ wfProfileOut( __METHOD__ );
+ return $s;
+ }
+
+ // FIXME: use explodeBodyHtml instead
protected function headingTransformCallback( $matches ) {
wfProfileIn( __METHOD__ );
$this->headings++;
--
To view, visit https://gerrit.wikimedia.org/r/70736
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifdc88084346efd3da0764a34992c2e690b62a089
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits