mediawiki.api.category: Avoid exceptions
[lhc/web/wiklou.git] / resources / src / mediawiki / api / category.js
index 14077e0..85df90e 100644 (file)
                 */
                isCategory: function ( title ) {
                        var apiPromise = this.get( {
+                               formatversion: 2,
                                prop: 'categoryinfo',
-                               titles: String( title )
+                               titles: [ String( title ) ]
                        } );
 
                        return apiPromise
                                .then( function ( data ) {
-                                       var exists = false;
-                                       if ( data.query && data.query.pages ) {
-                                               $.each( data.query.pages, function ( id, page ) {
-                                                       if ( page.categoryinfo ) {
-                                                               exists = true;
-                                                       }
-                                               } );
-                                       }
-                                       return exists;
+                                       return !!(
+                                               data.query && // query is missing on title=""
+                                               data.query.pages && // query.pages is missing on title="#" or title="mw:"
+                                               data.query.pages[ 0 ].categoryinfo
+                                       );
                                } )
                                .promise( { abort: apiPromise.abort } );
                },
@@ -46,6 +43,7 @@
                getCategoriesByPrefix: function ( prefix ) {
                        // Fetch with allpages to only get categories that have a corresponding description page.
                        var apiPromise = this.get( {
+                               formatversion: 2,
                                list: 'allpages',
                                apprefix: prefix,
                                apnamespace: mw.config.get( 'wgNamespaceIds' ).category
 
                        return apiPromise
                                .then( function ( data ) {
-                                       var texts = [];
-                                       if ( data.query && data.query.allpages ) {
-                                               $.each( data.query.allpages, function ( i, category ) {
-                                                       texts.push( new mw.Title( category.title ).getMainText() );
-                                               } );
-                                       }
-                                       return texts;
+                                       return data.query.allpages.map( function ( category ) {
+                                               return new mw.Title( category.title ).getMainText();
+                                       } );
                                } )
                                .promise( { abort: apiPromise.abort } );
                },
                 */
                getCategories: function ( title ) {
                        var apiPromise = this.get( {
+                               formatversion: 2,
                                prop: 'categories',
-                               titles: String( title )
+                               titles: [ String( title ) ]
                        } );
 
                        return apiPromise
                                .then( function ( data ) {
-                                       var titles = false;
-                                       if ( data.query && data.query.pages ) {
-                                               $.each( data.query.pages, function ( id, page ) {
-                                                       if ( page.categories ) {
-                                                               if ( titles === false ) {
-                                                                       titles = [];
-                                                               }
-                                                               $.each( page.categories, function ( i, cat ) {
-                                                                       titles.push( new mw.Title( cat.title ) );
-                                                               } );
-                                                       }
-                                               } );
+                                       var page;
+
+                                       if ( !data.query || !data.query.pages ) {
+                                               return false;
+                                       }
+                                       page = data.query.pages[ 0 ];
+                                       if ( !page.categories ) {
+                                               return false;
                                        }
-                                       return titles;
+                                       return page.categories.map( function ( cat ) {
+                                               return new mw.Title( cat.title );
+                                       } );
                                } )
                                .promise( { abort: apiPromise.abort } );
                }