r101627 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101626‎ | r101627 | r101628 >
Date:15:02, 2 November 2011
Author:catrope
Status:deferred (Comments)
Tags:
Comment:
[RL2] Add ForeignAPIGadgetRepo. Tested and working
Modified paths:
  • /branches/RL2/extensions/Gadgets/Gadgets.php (modified) (history)
  • /branches/RL2/extensions/Gadgets/backend/ForeignAPIGadgetRepo.php (added) (history)
  • /branches/RL2/extensions/Gadgets/backend/ForeignDBGadgetRepo.php (modified) (history)

Diff [purge]

Index: branches/RL2/extensions/Gadgets/Gadgets.php
@@ -53,7 +53,7 @@
5454 * For foreign API-based gadget repositories, use:
5555 * $wgGadgetRepositories[] = array(
5656 * 'class' => 'ForeignAPIGadgetRepo',
57 - * // TODO
 57+ * 'source' => 'mediawikiwiki',
5858 * );
5959 */
6060 $wgGadgetRepositories = array();
@@ -127,6 +127,7 @@
128128 $wgAutoloadClasses['ApiGetGadgetPrefs'] = $dir . 'api/ApiGetGadgetPrefs.php';
129129 $wgAutoloadClasses['ApiSetGadgetPrefs'] = $dir . 'api/ApiSetGadgetPrefs.php';
130130 $wgAutoloadClasses['CachedGadgetRepo'] = $dir . 'backend/CachedGadgetRepo.php';
 131+$wgAutoloadClasses['ForeignAPIGadgetRepo'] = $dir . 'backend/ForeignAPIGadgetRepo.php';
131132 $wgAutoloadClasses['ForeignDBGadgetRepo'] = $dir . 'backend/ForeignDBGadgetRepo.php';
132133 $wgAutoloadClasses['Gadget'] = $dir . 'backend/Gadget.php';
133134 $wgAutoloadClasses['GadgetsHooks'] = $dir . 'Gadgets.hooks.php';
Index: branches/RL2/extensions/Gadgets/backend/ForeignDBGadgetRepo.php
@@ -81,6 +81,8 @@
8282 }
8383 } else {
8484 // No access to the foreign wiki's memc, so cache locally
 85+ // This uses the same cache keys as ForeignDBGadgetRepo but that's fine,
 86+ // source names should be unique.
8587 if ( $id === null ) {
8688 return wfMemcKey( 'gadgets', 'foreignrepoids', $this->source );
8789 } else {
Index: branches/RL2/extensions/Gadgets/backend/ForeignAPIGadgetRepo.php
@@ -0,0 +1,146 @@
 2+<?php
 3+/**
 4+ * Gadget repository that gets its gadgets from a foreign wiki using api.php
 5+ *
 6+ * Options (all of these are MANDATORY):
 7+ * 'source': Name of the source these gadgets are loaded from, as defined in ResourceLoader
 8+ */
 9+class ForeignAPIGadgetRepo extends CachedGadgetRepo {
 10+ /**
 11+ * This version string is used in the user agent for requests and will help
 12+ * server maintainers in identify ForeignAPIGadgetRepo usage.
 13+ * Update the version every time you make breaking or significant changes.
 14+ */
 15+ const VERSION = '1.0';
 16+
 17+ protected $source, $apiURL;
 18+
 19+ /**
 20+ * Constructor.
 21+ * @param $options array See class documentation comment for option details
 22+ */
 23+ public function __construct( array $options ) {
 24+ global $wgResourceLoaderSources;
 25+ parent::__construct( $options );
 26+
 27+ $this->source = $options['source'];
 28+ $this->apiURL = $wgResourceLoaderSources[$this->source]['apiScript'];
 29+ }
 30+
 31+ /*** Protected methods inherited from CachedGadgetRepo ***/
 32+
 33+ protected function loadAllData() {
 34+ $apiResult = $this->doAPIRequest( null );
 35+ if ( is_array( $apiResult ) ) {
 36+ return $this->reformatAPIResult( $apiResult );
 37+ } else {
 38+ // TODO what do we do with the error mesage? loadAllData() doesn't have a facility for this
 39+ return array();
 40+ }
 41+ }
 42+
 43+ protected function loadDataFor( $id ) {
 44+ $apiResult = $this->doAPIRequest( $id );
 45+ if ( is_array( $apiResult ) ) {
 46+ $formatted = $this->reformatAPIResult( $apiResult );
 47+ return $formatted[0];
 48+ } else {
 49+ // TODO what do we do with the error mesage? loadAllData() doesn't have a facility for this
 50+ return array();
 51+ }
 52+ }
 53+
 54+ protected function getCacheKey( $id ) {
 55+ // No access to the foreign wiki's memc, so cache locally
 56+ // This uses the same cache keys as ForeignDBGadgetRepo but that's fine,
 57+ // source names should be unique.
 58+ if ( $id === null ) {
 59+ return wfMemcKey( 'gadgets', 'foreignrepoids', $this->source );
 60+ } else {
 61+ return wfMemcKey( 'gadgets', 'foreignrepodata', $this->source, $id );
 62+ }
 63+ }
 64+
 65+ /*** Protected methods ***/
 66+
 67+ /**
 68+ * Obtain a gadget list using HTTP to call api.php
 69+ * @param $id string ID of the gadget to obtain data for, or null to obtain data for all gadgets
 70+ * @return array|Status The decoded JSON response from the API (array), or a Status object on failure
 71+ */
 72+ protected function doAPIRequest( $id ) {
 73+ $query = array(
 74+ 'format' => 'json',
 75+ 'action' => 'query',
 76+ 'list' => 'gadgets',
 77+ 'gaprop' => 'id|metadata|definitiontimestamp',
 78+ );
 79+ if ( $id !== null ) {
 80+ $query['gaids'] = $id;
 81+ }
 82+ $url = wfAppendQuery( $this->apiURL, $query );
 83+
 84+ $options = array( 'timeout' => 'default', 'method' => 'GET' );
 85+ $req = MWHttpRequest::factory( $url, $options );
 86+ $req->setUserAgent( Http::userAgent() . " ForeignAPIGadgetRepo/" . self::VERSION );
 87+ $status = $req->execute();
 88+
 89+ if ( $status->isOK() ) {
 90+ return FormatJson::decode( $req->getContent(), true );
 91+ } else {
 92+ return $status;
 93+ }
 94+ }
 95+
 96+ /**
 97+ * Reformat a response from the API into the format that loadAllData()
 98+ * is supposed to return.
 99+ * @param $apiResponse array
 100+ * @return array|false
 101+ */
 102+ protected function reformatAPIResult( $apiResult ) {
 103+ if ( !isset( $apiResult['query']['gadgets'] ) ) {
 104+ return false;
 105+ }
 106+ $gadgets = $apiResult['query']['gadgets'];
 107+ if ( !is_array( $gadgets ) ) {
 108+ return false;
 109+ }
 110+
 111+ $retval = array();
 112+ foreach ( $gadgets as $gadget ) {
 113+ $retval[$gadget['id']] = array(
 114+ 'json' => FormatJson::encode( $gadget['metadata'] ),
 115+ 'timestamp' => $gadget['definitiontimestamp']
 116+ );
 117+ }
 118+ return $retval;
 119+ }
 120+
 121+ /*** Public methods inherited from GadgetRepo ***/
 122+
 123+ public function getSource() {
 124+ return $this->source;
 125+ }
 126+
 127+ public function isWriteable() {
 128+ return false;
 129+ }
 130+
 131+ public function getDB() {
 132+ return null;
 133+ }
 134+
 135+ public function modifyGadget( Gadget $gadget, $timestamp = null ) {
 136+ return Status::newFatal( 'gadget-manager-readonly-repository' );
 137+ }
 138+
 139+ public function deleteGadget( $id ) {
 140+ return Status::newFatal( 'gadget-manager-readonly-repository' );
 141+ }
 142+
 143+ public function isLocal() {
 144+ return false;
 145+ }
 146+
 147+}
Property changes on: branches/RL2/extensions/Gadgets/backend/ForeignAPIGadgetRepo.php
___________________________________________________________________
Added: svn:eol-style
1148 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r101628Allow overrides of ResourceLoaderWikiModule::getDB() (introduced in r93760) t...catrope15:04, 2 November 2011
r102916[RL2] fix wrong comment from r101627...krinkle22:10, 13 November 2011
r107879Fix typo in r101627catrope11:35, 3 January 2012

Comments

#Comment by Nikerabbit (talk | contribs)   15:09, 24 December 2011

Typo: mesage

Status & tagging log