Jdlrobson has uploaded a new change for review.
https://gerrit.wikimedia.org/r/313611
Change subject: POC: MobileFrontend should be able to use MobileContentService
......................................................................
POC: MobileFrontend should be able to use MobileContentService
This allows the site admin to choose between Restbase or MobileFormatter
a la apps. It should demonstrate the power of such an approach - we can
now test locally on real content.
The idea is to move away from the blob of rendering a blob of HTML and
allow skins to have more control over the components within a page and be
more dumb about where they come from.
Looking at the MobileFormatter and ApiMobileView code, it feels like we
probably want to slowly move away from using MobileFormatter as a public
interface and instead use ApiMobileView directly
TODO:
* Avoid multiple parallel RESTBase requests (read caching)
* Edit icons/reference sections do not work
Bug: T145219
Change-Id: Ibfd1d728f8e1c155858b9bc657b6d16712619968
---
M includes/MobileFrontend.body.php
M includes/MobileFrontend.hooks.php
2 files changed, 100 insertions(+), 25 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend
refs/changes/11/313611/1
diff --git a/includes/MobileFrontend.body.php b/includes/MobileFrontend.body.php
index 83745f7..2d08ad5 100644
--- a/includes/MobileFrontend.body.php
+++ b/includes/MobileFrontend.body.php
@@ -23,35 +23,20 @@
}
}
- /**
- * Transforms content to be mobile friendly version.
- * Filters out various elements and runs the MobileFormatter.
- * @param OutputPage $out
- * @param string $mode mobile mode, i.e. stable or beta
- *
- * @return string
- */
- public static function DOMParse( OutputPage $out, $text = null, $isBeta
= false ) {
- $html = $text ? $text : $out->getHTML();
-
+ public static function formatData( OutputPage $out, $html = null,
$isBeta = false ) {
$context = MobileContext::singleton();
$config = $context->getMFConfig();
-
$title = $out->getTitle();
$ns = $title->getNamespace();
+ $html = $html ? $html : $out->getHTML();
+
// Only include the table of contents element if the page is in
the main namespace
// and the MFTOC flag has been set (which means the page
originally had a table of contents)
$includeTOC = $out->getProperty( 'MFTOC' ) && $ns === NS_MAIN;
- $formatter = MobileFormatter::newFromContext( $context, $html );
- $formatter->enableTOCPlaceholder( $includeTOC );
+ $useRest = true;
- Hooks::run( 'MobileFrontendBeforeDOM', [ $context, $formatter ]
);
-
- $isSpecialPage = $title->isSpecialPage();
-
- $formatter->enableExpandableSections(
- // Don't collapse sections e.g. on JS pages
- $out->canUseWikiPage()
+ // Don't collapse sections e.g. on JS pages
+ $collapseSections = $out->canUseWikiPage()
&& $out->getWikiPage()->getContentModel() ==
CONTENT_MODEL_WIKITEXT
// And not in certain namespaces
&& array_search(
@@ -59,13 +44,97 @@
$config->get(
'MFNamespacesWithoutCollapsibleSections' )
) === false
// And not when what's shown is not actually article
text
- && $context->getRequest()->getText( 'action', 'view' )
== 'view'
- );
+ && $context->getRequest()->getText( 'action', 'view' )
== 'view';
+
+ if ( $useRest ) {
+ return self::restFormatData( $title, $context,
$includeTOC, $collapseSections, $html );
+ } else {
+ return self::mobileFormatData( $title, $context,
$includeTOC, $collapseSections, $html );
+ }
+ }
+
+ public static function restFormatData( $title, $context, $includeToc,
$collapseSections, $html ) {
+ $text = '';
+ $url = 'https://trending.wmflabs.org/api/page/en.wikipedia/';
+ $url .= rawurlencode( $title->getPrefixedText() );
+ set_error_handler(function() {}, E_WARNING);
+ $result = file_get_contents( $url, false );
+ restore_error_handler();
+ if ( $result === false ) {
+ return self::mobileFormatData( $title, $context,
$includeToc, $collapseSections, $html );
+ } else {
+ $json = json_decode( $result, true );
+ if ( isset( $json['lead'] ) ) {
+ $lead = $json['lead'];
+ $allSections = array_merge( array_slice(
$lead['sections'], 0, 1 ), $json['remaining']['sections'] );
+
+ if ( isset( $lead['paragraph'] ) ) {
+ $text .= $lead['paragraph'];
+ }
+
+ if ( isset( $lead['infobox'] ) ) {
+ $text .= $lead['infobox'];
+ }
+
+ $topToc = null;
+ foreach ( $allSections as $section ) {
+ if ( isset( $section['line'] ) ) {
+
+ $tocLevel =
$section['toclevel'] + 1;
+ $heading = Html::rawElement(
"h$tocLevel", [], $section['line'] );
+ if ( !$topToc ) {
+ $text .= '</div>'; //
close lead section
+ $text .= $heading;
+ $text .= '<div>';
+ $topToc = $tocLevel;
+ } else if ( $tocLevel ===
$topToc ) {
+ $text .= '</div>';
+ $text .= $heading;
+ $text .= '<div>';
+ $topToc = $tocLevel;
+ }
+ } else {
+ // wrap lead section
+ $text .= '<div class="lead">';
+ }
+ if ( isset( $section['text'] ) ) {
+ $text .= $section['text'];
+ }
+ }
+ // close the last one
+ $text .= '</div>';
+ }
+ return [
+ 'formatted' => [],
+ 'text' => $text,
+ ];
+ }
+ }
+
+ /**
+ * Transforms content to be mobile friendly version.
+ * Filters out various elements and runs the MobileFormatter.
+ * @param Title $title
+ * @param MobileContext $context
+ * @param boolean $includeTOC
+ * @param boolean $collapseSections
+ *
+ * @return array
+ */
+ public static function mobileFormatData( $title, $context, $includeTOC,
$collapseSections, $html ) {
+ $formatter = MobileFormatter::newFromContext( $context, $html );
+ $formatter->enableTOCPlaceholder( $includeTOC );
+
+ // FIXME: We should aim to deprecate this so it's Formatter
agnostic
+ Hooks::run( 'MobileFrontendBeforeDOM', [ $context, $formatter ]
);
+
+ $formatter->enableExpandableSections( $collapseSections );
$removeImages = $context->isLazyLoadImagesEnabled();
$removeReferences = $context->isLazyLoadReferencesEnabled();
if ( $context->getContentTransformations() ) {
+ $isSpecialPage = $title->isSpecialPage();
// Remove images if they're disabled from special
pages, but don't transform otherwise
$formatter->filterContent( /* remove defaults */
!$isSpecialPage,
$removeReferences, $removeImages );
@@ -73,6 +142,7 @@
$contentHtml = $formatter->getText();
+ // FIXME: Let's move this out of here
// If the page is a user page which has not been created, then
let the
// user know about it with pretty graphics and different texts
depending
// on whether the user is the owner of the page or not.
@@ -85,7 +155,10 @@
}
}
- return $contentHtml;
+ return [
+ 'formatted' => [],
+ 'text' => $contentHtml,
+ ];
}
/**
diff --git a/includes/MobileFrontend.hooks.php
b/includes/MobileFrontend.hooks.php
index e62b6a0..b9d6596 100644
--- a/includes/MobileFrontend.hooks.php
+++ b/includes/MobileFrontend.hooks.php
@@ -188,7 +188,9 @@
$context = MobileContext::singleton();
// Perform a few extra changes if we are in mobile mode
if ( $context->shouldDisplayMobileView() ) {
- $text = ExtMobileFrontend::DOMParse( $out, $text,
$context->isBetaGroupMember() );
+ $data = ExtMobileFrontend::formatData( $out, $text,
$context->isBetaGroupMember() );
+ $text = $data['text'];
+ $out->setProperty( 'page-data', $data['formatted'] );
}
// FIXME: remove the following when RelatedArticles are
promoted from beta to stable
--
To view, visit https://gerrit.wikimedia.org/r/313611
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibfd1d728f8e1c155858b9bc657b6d16712619968
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