User:Mooeypoo/articlesandbox.js

var ARTICLE_REPO = 'User:Mooeypoo/test/articlesandbox';

$.when( mw.loader.using( [ 'oojs-ui', 'mediawiki.util', 'mediawiki.api' ] ), $.ready).then( function {	buildArticleStructure		.then( function ( articles ) { console.log( 'final', articles ); } ); } );

/** * Build the article structure from the API */ function buildArticleStructure { var path, flatPath, articles = {};

return ( new mw.Api ).get( {		action: 'query',		format: 'json',		list: 'prefixsearch',		pssearch: ARTICLE_REPO	} ) .then(		// Success		function ( queryData ) {			var articlesArr = queryData.query.prefixsearch;			articlesArr.forEach( function ( articleData ) { var pieces = [], title = articleData.title;

if ( title.indexOf( ARTICLE_REPO ) !== 0 ) { return; }				pieces = title.replace( ARTICLE_REPO, '' ).split( '/' ); pieces = pieces.filter( function ( piece ) {					return !!piece; // Get rid of empty strings				} );

// Build article structure path = articles; flatPath = ''; pieces.forEach( function ( piece ) {					flatPath += '/' + piece;

path[ piece ] = path[ piece ] || { _path: ARTICLE_REPO + flatPath, _articles: {} };					// Shift reference path = path[piece]._articles; } );			} );			return articles; },		// Convert failure to empty success function { return $.Deferred.resolve( {} ); }	); }