r75621 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75620‎ | r75621 | r75622 >
Date:19:20, 28 October 2010
Author:btongminh
Status:resolved (Comments)
Tags:
Comment:
(bug 25648) API discovery information has been added as RSD link in page <head> and by providing an API module action=rsd. Added hook ApiRsdServiceApis for extensions to add their own service to the services list.

Patch by Brion Vibber and Bryan Tong Minh.
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/docs/hooks.txt (modified) (history)
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/Skin.php (modified) (history)
  • /trunk/phase3/includes/api/ApiMain.php (modified) (history)
  • /trunk/phase3/includes/api/ApiRsd.php (added) (history)

Diff [purge]

Index: trunk/phase3/docs/hooks.txt
@@ -362,6 +362,13 @@
363363 $tokenFunctions array and return true (returning false makes no sense)
364364 $tokenFunctions: array(action => callback)
365365
 366+'ApiRsdServiceApis': Add or remove APIs from the RSD services list.
 367+Each service should have its own entry in the $apis array and have a
 368+unique name, passed as key for the array that represents the service data.
 369+In this data array, the key-value-pair identified by the apiLink key is
 370+required.
 371+&$apis: array of services
 372+
366373 'ArticleAfterFetchContent': after fetching content of an article from
367374 the database
368375 $article: the article (object) being loaded from the database
Index: trunk/phase3/includes/api/ApiRsd.php
@@ -0,0 +1,183 @@
 2+<?php
 3+
 4+/**
 5+ * API for MediaWiki 1.17+
 6+ *
 7+ * Created on October 26, 2010
 8+ *
 9+ * Copyright © 2010 Bryan Tong Minh and Brion Vibber
 10+ *
 11+ * This program is free software; you can redistribute it and/or modify
 12+ * it under the terms of the GNU General Public License as published by
 13+ * the Free Software Foundation; either version 2 of the License, or
 14+ * (at your option) any later version.
 15+ *
 16+ * This program is distributed in the hope that it will be useful,
 17+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 19+ * GNU General Public License for more details.
 20+ *
 21+ * You should have received a copy of the GNU General Public License along
 22+ * with this program; if not, write to the Free Software Foundation, Inc.,
 23+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 24+ * http://www.gnu.org/copyleft/gpl.html
 25+ *
 26+ * @file
 27+ */
 28+
 29+if ( !defined( 'MEDIAWIKI' ) ) {
 30+ require_once( 'ApiBase.php' );
 31+}
 32+
 33+/**
 34+ * API interface for page purging
 35+ * @ingroup API
 36+ */
 37+class ApiRsd extends ApiBase {
 38+
 39+ public function __construct( $main, $action ) {
 40+ parent::__construct( $main, $action );
 41+ }
 42+
 43+ public function execute() {
 44+ $result = $this->getResult();
 45+
 46+
 47+ $result->addValue( null, 'version', '1.0' );
 48+ $result->addValue( null, 'xmlns', 'http://archipelago.phrasewise.com/rsd' );
 49+
 50+ $service = array(
 51+ 'engineName' => array(
 52+ '*' => 'MediaWiki'
 53+ ),
 54+ 'engineLink' => array(
 55+ '*' => 'http://www.mediawiki.org/'
 56+ ),
 57+ 'apis' => $this->formatRsdApiList()
 58+ );
 59+
 60+ $result->setIndexedTagName( $service['apis'], 'api' );
 61+
 62+ $result->addValue( null, 'service', $service );
 63+ }
 64+
 65+ public function getCustomPrinter() {
 66+ return new ApiFormatXmlRsd( $this->getMain(), 'xml' );
 67+ }
 68+
 69+ public function getAllowedParams() {
 70+ return array();
 71+ }
 72+
 73+ public function getParamDescription() {
 74+ return array();
 75+ }
 76+
 77+ public function getDescription() {
 78+ return 'Export an RSD schema';
 79+ }
 80+
 81+ protected function getExamples() {
 82+ return array(
 83+ 'api.php?action=rsd'
 84+ );
 85+ }
 86+
 87+ /**
 88+ * Builds an internal list of APIs to expose information about.
 89+ * Normally this only lists the MediaWiki API, with its base URL,
 90+ * link to documentation, and a marker as to available authentication
 91+ * (to aid in OAuth client apps switching to support in the future).
 92+ *
 93+ * Extensions can expose other APIs, such as WordPress or Twitter-
 94+ * compatible APIs, by hooking 'ApiRsdServiceApis' and adding more
 95+ * elements to the array.
 96+ *
 97+ * See http://cyber.law.harvard.edu/blogs/gems/tech/rsd.html for
 98+ * the base RSD spec, and check WordPress and StatusNet sites for
 99+ * in-production examples listing several blogging and micrblogging
 100+ * APIs.
 101+ *
 102+ * @return array
 103+ */
 104+ protected function getRsdApiList() {
 105+ $apis = array(
 106+ 'MediaWiki' => array(
 107+ // The API link is required for all RSD API entries.
 108+ 'apiLink' => wfExpandUrl( wfScript( 'api' ) ),
 109+
 110+ // Docs link is optional, but recommended.
 111+ 'docs' => 'http://mediawiki.org/wiki/API',
 112+
 113+ // Some APIs may need a blog ID, but it may be left blank.
 114+ 'blogID' => '',
 115+
 116+ // Additional settings are optional.
 117+ 'settings' => array(
 118+ // Change this to true in the future as an aid to
 119+ // machine discovery of OAuth for API access.
 120+ 'OAuth' => false,
 121+ )
 122+ ),
 123+ );
 124+ wfRunHooks( 'ApiRsdServiceApis', array( &$apis ) );
 125+ return $apis;
 126+ }
 127+
 128+ /**
 129+ * Formats the internal list of exposed APIs into an array suitable
 130+ * to pass to the API's XML formatter.
 131+ *
 132+ * @return array
 133+ */
 134+ protected function formatRsdApiList() {
 135+ $apis = $this->getRsdApiList();
 136+
 137+ $outputData = array();
 138+ foreach ( $apis as $name => $info ) {
 139+ $data = array(
 140+ 'name' => $name,
 141+ 'preferred' => wfBoolToStr( $name == 'MediaWiki' ),
 142+ 'apiLink' => $info['apiLink'],
 143+ 'blogID' => isset( $info['blogID'] ) ? $info['blogID'] : ''
 144+ );
 145+ if ( isset( $info['docs'] ) ) {
 146+ $data['settings']['docs'] = array(
 147+ '*' => $info['docs'],
 148+ );
 149+ }
 150+ if ( isset( $info['settings'] ) ) {
 151+ foreach ( $info['settings'] as $setting => $val ) {
 152+ if ( is_bool( $val ) ) {
 153+ $xmlVal = wfBoolToStr( $val );
 154+ } else {
 155+ $xmlVal = $val;
 156+ }
 157+ $data['settings'][] = array(
 158+ 'name' => $setting,
 159+ '*' => $xmlVal,
 160+ );
 161+ }
 162+ }
 163+ if ( isset( $data['settings'] ) ) {
 164+ $data['settings']['_element'] = 'setting';
 165+ }
 166+ $outputData[] = $data;
 167+ }
 168+ return $outputData;
 169+ }
 170+ public function getVersion() {
 171+ return __CLASS__ . ': $Id$';
 172+ }
 173+}
 174+
 175+class ApiFormatXmlRsd extends ApiFormatXml {
 176+ public function __construct( $main, $format ) {
 177+ parent::__construct( $main, $format );
 178+ $this->setRootElement( 'rsd' );
 179+ }
 180+
 181+ public function getMimeType() {
 182+ return 'application/rsd+xml';
 183+ }
 184+}
Property changes on: trunk/phase3/includes/api/ApiRsd.php
___________________________________________________________________
Added: svn:eol-style
1185 + native
Index: trunk/phase3/includes/api/ApiMain.php
@@ -63,6 +63,7 @@
6464 'feedwatchlist' => 'ApiFeedWatchlist',
6565 'help' => 'ApiHelp',
6666 'paraminfo' => 'ApiParamInfo',
 67+ 'rsd' => 'ApiRsd',
6768
6869 // Write modules
6970 'purge' => 'ApiPurge',
Index: trunk/phase3/includes/Skin.php
@@ -216,6 +216,16 @@
217217 'title' => wfMsgForContent( 'opensearch-desc' ),
218218 ) );
219219
 220+ # Real Simple Discovery link, provides auto-discovery information
 221+ # for the MediaWiki API (and potentially additional custom API
 222+ # support such as WordPress or Twitter-compatible APIs for a
 223+ # blogging extension, etc)
 224+ $out->addLink( array(
 225+ 'rel' => 'EditURI',
 226+ 'type' => 'application/rsd+xml',
 227+ 'href' => wfExpandUrl( wfAppendQuery( wfScript( 'api' ), array( 'action' => 'rsd' ) ) ),
 228+ ) );
 229+
220230 $this->addMetadataLinks( $out );
221231
222232 $this->mRevisionId = $out->mRevisionId;
Index: trunk/phase3/includes/AutoLoader.php
@@ -297,6 +297,7 @@
298298 'ApiPatrol' => 'includes/api/ApiPatrol.php',
299299 'ApiProtect' => 'includes/api/ApiProtect.php',
300300 'ApiPurge' => 'includes/api/ApiPurge.php',
 301+ 'ApiRsd' => 'includes/api/ApiRsd.php',
301302 'ApiQuery' => 'includes/api/ApiQuery.php',
302303 'ApiQueryAllCategories' => 'includes/api/ApiQueryAllCategories.php',
303304 'ApiQueryAllimages' => 'includes/api/ApiQueryAllimages.php',
Index: trunk/phase3/RELEASE-NOTES
@@ -455,6 +455,10 @@
456456 id, try the parser cache, and save it to it if necessary
457457 * (bug 25463) Export header should not be shown if no pages were requested, to
458458 reduce confusion
 459+* (bug 25648) API discovery information has been added as RSD link in page
 460+ <head> and by providing an API module action=rsd. Added hook
 461+ ApiRsdServiceApis for extensions to add their own service to the services
 462+ list.
459463
460464 === Languages updated in 1.17 ===
461465

Follow-up revisions

RevisionCommit summaryAuthorDate
r75631Followup r75621, svn:keywords Idreedy22:02, 28 October 2010
r75632Minor followup to r75621, add whitespace...reedy22:56, 28 October 2010
r75648Follow-up r75621: Check for $wgEnableAPI before adding the RSD link to the headbtongminh16:19, 29 October 2010
r75669r75621 copy-paste failbtongminh20:33, 29 October 2010
r76195Follow up r75621: Use setContent and setIndexedTagName methodsbtongminh15:57, 6 November 2010

Comments

#Comment by Reedy (talk | contribs)   10:44, 29 October 2010

Just a thought... Should we take notice of Manual:$wgEnableAPI?

Obviously if it's false, the API isn't runnable, so we don't want to be giving false discovery information, or at least, information that is of no use...

#Comment by Bryan (talk | contribs)   12:49, 29 October 2010

Yes

#Comment by Reedy (talk | contribs)   13:34, 29 October 2010

Marking fixme. Are you going to do it..?

#Comment by Bryan (talk | contribs)   13:36, 29 October 2010

Maybe later today or tomorrow. You can if you want to.

#Comment by Bryan (talk | contribs)   16:20, 29 October 2010
#Comment by Reedy (talk | contribs)   17:42, 29 October 2010

Cheers :)

#Comment by UV (talk | contribs)   19:31, 29 October 2010

Inaccurate/misleading class documentation comment in file /trunk/phase3/includes/api/ApiRsd.php: "API interface for page purging"

#Comment by UV (talk | contribs)   20:56, 29 October 2010

Thank you for r75669, which fixes this

#Comment by Catrope (talk | contribs)   20:18, 31 October 2010
+			 'engineName' => array(
+				 '*' => 'MediaWiki'
+			 ),
[...]
+				$data['settings']['_element'] = 'setting';

We avoid hardcoding '*' and '_element', use ApiResult::setContent() and setIndexedTagName().

Status & tagging log