r93069 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r93068‎ | r93069 | r93070 >
Date:17:13, 25 July 2011
Author:neilk
Status:ok
Tags:
Comment:
helper module for querying categories via API
Modified paths:
  • /trunk/extensions/UploadWizard/resources/mw.Api.category.js (added) (history)

Diff [purge]

Index: trunk/extensions/UploadWizard/resources/mw.Api.category.js
@@ -0,0 +1,71 @@
 2+// library to assist with API calls on categories
 3+
 4+( function( mw, $ ) {
 5+
 6+ // cached token so we don't have to keep fetching new ones for every single post
 7+ var cachedToken = null;
 8+
 9+ $.extend( mw.Api.prototype, {
 10+ /**
 11+ * @param {mw.Title}
 12+ * @param {Function} callback to pass boolean of category's existence
 13+ * @param {Function} optional callback to run if api error
 14+ * @return ajax call object
 15+ */
 16+ isCategory: function( title, callback, error ) {
 17+ var params = {
 18+ 'prop': 'categoryinfo',
 19+ 'titles': title.toString()
 20+ };
 21+
 22+ var ok = function( data ) {
 23+ var exists = false;
 24+ if ( data.query && data.query.pages ) {
 25+ $.each( data.query.pages, function( id, page ) {
 26+ if ( page.categoryinfo ) {
 27+ exists = true;
 28+ }
 29+ } );
 30+ }
 31+ callback( exists );
 32+ };
 33+
 34+ var err = mw.isDefined( error ) ? error : undefined;
 35+
 36+ return this.get( params, ok, err );
 37+
 38+ },
 39+
 40+ /**
 41+ * @param {String} prefix to match
 42+ * @param {Function} callback to pass matched categories to
 43+ * @param {Function} optional callback to run if api error
 44+ * @return ajax call object
 45+ */
 46+ getCategoriesByPrefix: function( prefix, callback, error ) {
 47+
 48+ var params = {
 49+ 'list': 'allcategories',
 50+ 'acprefix': prefix
 51+ };
 52+
 53+ var ok = function( data ) {
 54+ var texts = [];
 55+ if ( data.query && data.query.allcategories ) {
 56+ // API returns an array of objects like
 57+ // allcategories: [ {'*':'foo'}, {'*':'bar'} ]
 58+ $.each( data.query.allcategories, function( i, category ) {
 59+ texts.push( category['*'] );
 60+ } );
 61+ }
 62+ callback( texts );
 63+ };
 64+
 65+ var err = mw.isDefined( error ) ? error : undefined;
 66+
 67+ return this.get( params, ok, err );
 68+
 69+ }
 70+
 71+ } );
 72+} )( window.mediaWiki, jQuery );
Property changes on: trunk/extensions/UploadWizard/resources/mw.Api.category.js
___________________________________________________________________
Added: svn:eol-style
173 + native

Status & tagging log