jenkins-bot has submitted this change and it was merged. Change subject: Added API action=zeroconfig (META only) to get all Zero IPs ......................................................................
Added API action=zeroconfig (META only) to get all Zero IPs API module returns a custom mapping of Zero Config IDs to their corresponding IP ranges (CIDRs) as specified by all Zero: pages. The result is specifically designed for the netmapper varnish module that runs on all varnish servers. A cron job will download updated configuration and update local file with this mapping. Change-Id: Ieb573c8ae9bfde726983603a71db72838355f358 --- M ZeroRatedMobileAccess.php A includes/ApiZeroConfig.php 2 files changed, 75 insertions(+), 1 deletion(-) Approvals: Dr0ptp4kt: Looks good to me, approved jenkins-bot: Verified diff --git a/ZeroRatedMobileAccess.php b/ZeroRatedMobileAccess.php index 8917917..bcd6835 100644 --- a/ZeroRatedMobileAccess.php +++ b/ZeroRatedMobileAccess.php @@ -38,6 +38,7 @@ // autoload extension classes $autoloadClasses = array ( + 'ApiZeroConfig' => 'ApiZeroConfig', 'CarrierConfig' => 'CarrierConfig', 'CarrierConfigStore' => 'CarrierConfigStore', 'ConfigPageHooks' => 'ConfigPageHooks', @@ -113,7 +114,7 @@ // Registers hooks which are only required on the configuration wiki. $wgExtensionFunctions[] = function() { - global $wgHooks, $wgContentHandlers, $wgZeroRatedMobileAccessEnableZeroConfigPages; + global $wgHooks, $wgZeroRatedMobileAccessEnableZeroConfigPages, $wgAPIModules; if ( $wgZeroRatedMobileAccessEnableZeroConfigPages === true ) { $hook = 'Extensions\ZeroRatedMobileAccess\ConfigPageHooks::'; @@ -122,6 +123,7 @@ $wgHooks['EditFilterMerged'][] = $hook . 'onEditFilterMerged'; $wgHooks['CodeEditorGetPageLanguage'][] = $hook . 'onCodeEditorGetPageLanguage'; $wgHooks['PageContentSaveComplete'][] = $hook . 'onPageContentSaveComplete'; + $wgAPIModules['zeroconfig'] = 'Extensions\ZeroRatedMobileAccess\ApiZeroConfig'; return true; } diff --git a/includes/ApiZeroConfig.php b/includes/ApiZeroConfig.php new file mode 100644 index 0000000..b5a1010 --- /dev/null +++ b/includes/ApiZeroConfig.php @@ -0,0 +1,72 @@ +<?php + +namespace Extensions\ZeroRatedMobileAccess; + +use ApiBase; +use ApiFormatBase; +use ApiMain; +use FauxRequest; +use FormatJson; + +class ApiZeroConfig extends ApiBase { + /** + * Override built-in handling of format parameter. + * Only JSON is supported. + * @return ApiFormatBase + */ + public function getCustomPrinter() { + return $this->getMain()->createPrinterByName( 'json' ); + } + + public function execute() { + $this->getMain()->setCacheMaxAge( 10*60 ); // seconds + $this->getMain()->setCacheMode( 'public' ); + $result = $this->getResult(); + $result->disableSizeCheck(); + + $reqParamsOriginal = array( + 'action' => 'query', + 'generator' => 'allpages', + 'gaplimit' => 'max', + 'gapnamespace' => '480', + 'prop' => 'revisions', + 'rvprop' => 'content', + 'continue' => '', + ); + $reqParams = $reqParamsOriginal; + + while ( true ) { + // Get needed data + $req = new FauxRequest( $reqParams ); + $api = new ApiMain( $req ); + $api->execute(); + $data = $api->getResultData(); + if ( array_key_exists( 'error', $data ) ) { + $this->dieUsage( "Error getting data", "data", 500, $data['error'] ); + } + foreach ( (array)$data['query']['pages'] as $pageId => $page ) { + $rawText = $page['revisions'][0]['*']; + $json = FormatJson::decode( $rawText, true ) ?: false; + if ( !is_array( $json ) ) { + $this->dieUsage( "Unable to parse json of page $pageId", "json", 500 ); + } + if ( !array_key_exists( 'ips', $json ) ) { + continue; + } + $result->addValue( null, str_replace( 'Zero:', '', $page['title'] ), $json['ips'] ); + } + if ( !array_key_exists( 'continue', $data ) ) { + break; + } + $reqParams = array_merge( $reqParamsOriginal, $data['continue'] ); + } + } + + public function getDescription() { + return 'Return Zero configuration for server varnish mapping'; + } + + public function getExamples() { + return 'api.php?action=zeroconfig'; + } +} -- To view, visit https://gerrit.wikimedia.org/r/83923 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ieb573c8ae9bfde726983603a71db72838355f358 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/ZeroRatedMobileAccess Gerrit-Branch: master Gerrit-Owner: Yurik <yu...@wikimedia.org> Gerrit-Reviewer: Dr0ptp4kt <ab...@wikimedia.org> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits