jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/368287 )
Change subject: Allow content to be sourced from Parsoid ...................................................................... Allow content to be sourced from Parsoid This helps with testing Apps related bugs and Parsoid related bugs e.g. T171727 To use: $wgMFContentProviderClass = 'MobileFrontend\ContentProviders\McsContentProvider'; $wgMFMcsContentProviderBaseUri = "https://en.wikipedia.org/api/rest_v1"; Change-Id: I648ed26e1826aaca4d6616097fdfc3019ca7a848 --- M extension.json M includes/content-providers/ContentProviderFactory.php A includes/content-providers/McsContentProvider.php 3 files changed, 86 insertions(+), 0 deletions(-) Approvals: Pmiazga: Looks good to me, but someone else must approve jenkins-bot: Verified Jdlrobson: Looks good to me, approved diff --git a/extension.json b/extension.json index ef0f5c1..53f7d4d 100644 --- a/extension.json +++ b/extension.json @@ -86,6 +86,7 @@ "MobileFrontend\\ContentProviders\\ContentProviderFactory": "includes/content-providers/ContentProviderFactory.php", "MobileFrontend\\ContentProviders\\DefaultContentProvider": "includes/content-providers/DefaultContentProvider.php", "MobileFrontend\\ContentProviders\\IContentProvider": "includes/content-providers/IContentProvider.php", + "MobileFrontend\\ContentProviders\\McsContentProvider": "includes/content-providers/McsContentProvider.php", "MobileFrontend\\ContentProviders\\MwApiContentProvider": "includes/content-providers/MwApiContentProvider.php" }, "ResourceModules": { @@ -1242,6 +1243,7 @@ "config": { "MFContentProviderClass": "MobileFrontend\\ContentProviders\\DefaultContentProvider", "MFMwApiContentProviderBaseUri": "https://en.wikipedia.org/w/api.php", + "MFMcsContentProviderBaseUri": "https://en.wikipedia.org/api/rest_v1", "MFEnableXAnalyticsLogging": false, "MFAppPackageId": false, "MFAppScheme": "http", diff --git a/includes/content-providers/ContentProviderFactory.php b/includes/content-providers/ContentProviderFactory.php index 43f36f0..8017fea 100644 --- a/includes/content-providers/ContentProviderFactory.php +++ b/includes/content-providers/ContentProviderFactory.php @@ -8,6 +8,7 @@ class ContentProviderFactory { const MW_API = 'MobileFrontend\\ContentProviders\\MwApiContentProvider'; const PHP_PARSER = 'MobileFrontend\\ContentProviders\\DefaultContentProvider'; + const MCS_API = 'MobileFrontend\\ContentProviders\\McsContentProvider'; /** * Create an instance of IContentProvider @@ -28,6 +29,9 @@ } switch ( $contentProviderClass ) { + case self::MCS_API: + $baseUrl = $config->get( 'MFMcsContentProviderBaseUri' ); + return new $contentProviderClass( $baseUrl, $out ); case self::MW_API: $baseUrl = $config->get( 'MFMwApiContentProviderBaseUri' ); return new $contentProviderClass( $baseUrl, $out ); diff --git a/includes/content-providers/McsContentProvider.php b/includes/content-providers/McsContentProvider.php new file mode 100644 index 0000000..130f2fd --- /dev/null +++ b/includes/content-providers/McsContentProvider.php @@ -0,0 +1,80 @@ +<?php + +namespace MobileFrontend\ContentProviders; + +use MobileFrontend\ContentProviders\IContentProvider; +use OutputPage; + +/** + * Sources content from the Mobile-Content-Service + * This requires allow_url_fopen to be set. + * @since + */ +class McsContentProvider implements IContentProvider { + /** @var OutputPage */ + private $out; + /** @var string */ + private $baseUrl; + + /** + * Constructor + * + * @param string $baseUrl for the MediaWiki API to be used minus query string e.g. /w/api.php + * @param OutputPage $out so that the ResourceLoader modules specific to the page can be added + */ + public function __construct( $baseUrl, OutputPage $out ) { + $this->baseUrl = $baseUrl; + $this->out = $out; + } + + /** + * @param array $json response + * @return string + */ + protected function buildHtmlFromResponse( array $json ) { + $lead = $json['lead']; + $html = isset( $lead['sections'][0]['text'] ) ? $lead['sections'][0]['text'] : ''; + + $remaining = $json['remaining']; + + foreach ( $remaining['sections'] as $section ) { + if ( isset( $section['line'] ) ) { + $toc = $section['toclevel'] + 1; + $line = $section['line']; + $html .= "<h$toc>$line</h$toc>"; + } + if ( isset( $section['text'] ) ) { + $html .= $section['text']; + } + } + return $html; + } + + /** + * @inheritdoc + */ + public function getHTML() { + $out = $this->out; + $title = $out->getTitle(); + if ( !$title ) { + return ''; + } + $url = $this->baseUrl . '/page/mobile-sections/'; + $url .= urlencode( $title->getPrefixedDBKey() ); + // file_get_contents() will throw php warning when server returns 404. MCS will return 404 + // when article is not available, we're handling that case so we can safely ignore error + // MediaWiki PHPCS forbids silencing errors but there is no other way to ignore 404 warning + // @codingStandardsIgnoreLine + $resp = @file_get_contents( $url, false ); + if ( $resp ) { + $json = json_decode( $resp, true ); + if ( is_array( $json ) ) { + return $this->buildHtmlFromResponse( $json ); + } else { + return ''; + } + } else { + return ''; + } + } +} -- To view, visit https://gerrit.wikimedia.org/r/368287 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I648ed26e1826aaca4d6616097fdfc3019ca7a848 Gerrit-PatchSet: 6 Gerrit-Project: mediawiki/extensions/MobileFrontend Gerrit-Branch: master Gerrit-Owner: Jdlrobson <jrob...@wikimedia.org> Gerrit-Reviewer: Jdlrobson <jrob...@wikimedia.org> Gerrit-Reviewer: Niedzielski <sniedziel...@wikimedia.org> Gerrit-Reviewer: Pmiazga <pmia...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits