Index: branches/RL2/extensions/Gadgets/Gadgets.php |
— | — | @@ -53,7 +53,7 @@ |
54 | 54 | * For foreign API-based gadget repositories, use: |
55 | 55 | * $wgGadgetRepositories[] = array( |
56 | 56 | * 'class' => 'ForeignAPIGadgetRepo', |
57 | | - * // TODO |
| 57 | + * 'source' => 'mediawikiwiki', |
58 | 58 | * ); |
59 | 59 | */ |
60 | 60 | $wgGadgetRepositories = array(); |
— | — | @@ -127,6 +127,7 @@ |
128 | 128 | $wgAutoloadClasses['ApiGetGadgetPrefs'] = $dir . 'api/ApiGetGadgetPrefs.php'; |
129 | 129 | $wgAutoloadClasses['ApiSetGadgetPrefs'] = $dir . 'api/ApiSetGadgetPrefs.php'; |
130 | 130 | $wgAutoloadClasses['CachedGadgetRepo'] = $dir . 'backend/CachedGadgetRepo.php'; |
| 131 | +$wgAutoloadClasses['ForeignAPIGadgetRepo'] = $dir . 'backend/ForeignAPIGadgetRepo.php'; |
131 | 132 | $wgAutoloadClasses['ForeignDBGadgetRepo'] = $dir . 'backend/ForeignDBGadgetRepo.php'; |
132 | 133 | $wgAutoloadClasses['Gadget'] = $dir . 'backend/Gadget.php'; |
133 | 134 | $wgAutoloadClasses['GadgetsHooks'] = $dir . 'Gadgets.hooks.php'; |
Index: branches/RL2/extensions/Gadgets/backend/ForeignDBGadgetRepo.php |
— | — | @@ -81,6 +81,8 @@ |
82 | 82 | } |
83 | 83 | } else { |
84 | 84 | // 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. |
85 | 87 | if ( $id === null ) { |
86 | 88 | return wfMemcKey( 'gadgets', 'foreignrepoids', $this->source ); |
87 | 89 | } 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 |
1 | 148 | + native |