mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Embeddable: Create core dbs in proper subdirectory
This commit is contained in:
@@ -13,3 +13,170 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.mediawiki.includes.interwiki; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*; import gplx.xowa.mediawiki.includes.*;
|
||||
/**
|
||||
* Value Object for representing interwiki records.
|
||||
*/
|
||||
public class XomwInterwiki {
|
||||
|
||||
/** @var String The interwiki prefix, (e.g. "Meatball", or the language prefix "de") */
|
||||
private byte[] mPrefix;
|
||||
|
||||
/** @var String The URL of the wiki, with "1" as a placeholder for an article name. */
|
||||
private byte[] mURL;
|
||||
|
||||
/** @var String The URL of the file api.php */
|
||||
private byte[] mAPI;
|
||||
|
||||
/** @var String The name of the database (for a connection to be established
|
||||
* with wfGetLB('wikiid'))
|
||||
*/
|
||||
private byte[] mWikiID;
|
||||
|
||||
/** @var boolean Whether the wiki is in this project */
|
||||
private boolean mLocal;
|
||||
|
||||
/** @var boolean Whether interwiki transclusions are allowed */
|
||||
private boolean mTrans;
|
||||
|
||||
public byte[] interwikiId;
|
||||
|
||||
public XomwInterwiki(byte[] prefix, byte[] url, byte[] api, byte[] wikiId, boolean local, boolean trans) {
|
||||
this.mPrefix = prefix;
|
||||
this.mURL = url;
|
||||
this.mAPI = api;
|
||||
this.mWikiID = wikiId;
|
||||
this.mLocal = local;
|
||||
this.mTrans = trans;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether an interwiki prefix exists
|
||||
*
|
||||
* [@]deprecated since 1.28, use InterwikiLookup instead
|
||||
*
|
||||
* @param String prefix Interwiki prefix to use
|
||||
* @return boolean Whether it exists
|
||||
*/
|
||||
public static boolean isValidInterwiki(XomwMediaWikiServices mws, byte[] prefix) {
|
||||
return mws.getInterwikiLookup().isValidInterwiki(prefix);
|
||||
// return MediaWikiServices::getInstance().getInterwikiLookup().isValidInterwiki(prefix);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Fetch an Interwiki Object
|
||||
// *
|
||||
// * @deprecated since 1.28, use InterwikiLookup instead
|
||||
// *
|
||||
// * @param String prefix Interwiki prefix to use
|
||||
// * @return Interwiki|null|boolean
|
||||
// */
|
||||
// public static function fetch(prefix) {
|
||||
// return MediaWikiServices::getInstance().getInterwikiLookup().fetch(prefix);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Purge the cache (local and persistent) for an interwiki prefix.
|
||||
// *
|
||||
// * @param String prefix
|
||||
// * @since 1.26
|
||||
// */
|
||||
// public static function invalidateCache(prefix) {
|
||||
// return MediaWikiServices::getInstance().getInterwikiLookup().invalidateCache(prefix);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Returns all interwiki prefixes
|
||||
// *
|
||||
// * @deprecated since 1.28, unused. Use InterwikiLookup instead.
|
||||
// *
|
||||
// * @param String|null local If set, limits output to local/non-local interwikis
|
||||
// * @return array List of prefixes
|
||||
// * @since 1.19
|
||||
// */
|
||||
// public static function getAllPrefixes(local = null) {
|
||||
// return MediaWikiServices::getInstance().getInterwikiLookup().getAllPrefixes(local);
|
||||
// }
|
||||
|
||||
/**
|
||||
* Get the URL for a particular title (or with 1 if no title given)
|
||||
*
|
||||
* @param String title What text to put for the article name
|
||||
* @return String The URL
|
||||
* @note Prior to 1.19 The getURL with an argument was broken.
|
||||
* If you if you use this arg in an extension that supports MW earlier
|
||||
* than 1.19 please wfUrlencode and substitute 1 on your own.
|
||||
*/
|
||||
// title=null
|
||||
public byte[] getURL(byte[] title) {
|
||||
byte[] url = this.mURL;
|
||||
if (title != null) {
|
||||
url = XophpString.str_replace(ARG_1, XomwGlobalFunctions.wfUrlencode(title), url);
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the API URL for this wiki
|
||||
*
|
||||
* @return String The URL
|
||||
*/
|
||||
public byte[] getAPI() {
|
||||
return this.mAPI;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DB name for this wiki
|
||||
*
|
||||
* @return String The DB name
|
||||
*/
|
||||
public byte[] getWikiID() {
|
||||
return this.mWikiID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this a local link from a sister project, or is
|
||||
* it something outside, like Google
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isLocal() {
|
||||
return this.mLocal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Can pages from this wiki be transcluded?
|
||||
* Still requires wgEnableScaryTransclusion
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isTranscludable() {
|
||||
return this.mTrans;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name for the interwiki site
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public byte[] getName(XomwEnv env) {
|
||||
// XomwMessage msg = XomwGlobalFunctions.wfMessage(env, "interwiki-name-" + this.mPrefix).inContentLanguage();
|
||||
//
|
||||
// return !msg.exists() ? Bry_.Empty : msg.text();
|
||||
Tfds.Write(mPrefix);
|
||||
return null;
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Get a description for this interwiki
|
||||
// *
|
||||
// * @return String
|
||||
// */
|
||||
// public function getDescription() {
|
||||
// msg = wfMessage('interwiki-desc-' . this.mPrefix).inContentLanguage();
|
||||
//
|
||||
// return !msg.exists() ? '' : msg.text();
|
||||
// }
|
||||
private static final byte[] ARG_1 = Bry_.new_a7("$1");
|
||||
}
|
||||
|
||||
@@ -13,3 +13,40 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.mediawiki.includes.interwiki; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*; import gplx.xowa.mediawiki.includes.*;
|
||||
/**
|
||||
* Service interface for looking up Interwiki records.
|
||||
*
|
||||
* @since 1.28
|
||||
*/
|
||||
public interface XomwInterwikiLookup {
|
||||
/**
|
||||
* Check whether an interwiki prefix exists
|
||||
*
|
||||
* @param String $prefix Interwiki prefix to use
|
||||
* @return boolean Whether it exists
|
||||
*/
|
||||
boolean isValidInterwiki(byte[] prefix);
|
||||
|
||||
/**
|
||||
* Fetch an Interwiki Object
|
||||
*
|
||||
* @param String $prefix Interwiki prefix to use
|
||||
* @return Interwiki|null|boolean
|
||||
*/
|
||||
XomwInterwiki fetch(byte[] prefix);
|
||||
|
||||
/**
|
||||
* Returns all interwiki prefixes
|
||||
*
|
||||
* @param String|null $local If set, limits output to local/non-local interwikis
|
||||
* @return String[] List of prefixes
|
||||
*/
|
||||
byte[][] getAllPrefixes(boolean local);
|
||||
|
||||
// /**
|
||||
// * Purge the in-process and persistent Object cache for an interwiki prefix
|
||||
// * @param String $prefix
|
||||
// */
|
||||
// void invalidateCache(byte[] prefix);
|
||||
}
|
||||
|
||||
@@ -13,3 +13,157 @@ The terms of each license can be found in the source code repository:
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.mediawiki.includes.interwiki; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*; import gplx.xowa.mediawiki.includes.*;
|
||||
import gplx.xowa.mediawiki.includes.site.*;
|
||||
public class XomwInterwikiLookupAdapter implements XomwInterwikiLookup {
|
||||
/**
|
||||
* @var SiteLookup
|
||||
*/
|
||||
private final XomwSiteLookup siteLookup;
|
||||
|
||||
/**
|
||||
* @var Interwiki[]|null associative array mapping interwiki prefixes to Interwiki objects
|
||||
*/
|
||||
private Ordered_hash interwikiMap = Ordered_hash_.New_bry();
|
||||
|
||||
public XomwInterwikiLookupAdapter (
|
||||
XomwSiteLookup siteLookup
|
||||
// Ordered_hash interwikiMap
|
||||
) {
|
||||
this.siteLookup = siteLookup;
|
||||
}
|
||||
|
||||
/**
|
||||
* See InterwikiLookup::isValidInterwiki
|
||||
* It loads the whole interwiki map.
|
||||
*
|
||||
* @param String $prefix Interwiki prefix to use
|
||||
* @return boolean Whether it exists
|
||||
*/
|
||||
public boolean isValidInterwiki(byte[] prefix) {
|
||||
return XophpArray.array_key_exists(prefix, this.getInterwikiMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* See InterwikiLookup::fetch
|
||||
* It loads the whole interwiki map.
|
||||
*
|
||||
* @param String $prefix Interwiki prefix to use
|
||||
* @return Interwiki|null|boolean
|
||||
*/
|
||||
public XomwInterwiki fetch(byte[] prefix) {
|
||||
if (prefix == Bry_.Empty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!this.isValidInterwiki(prefix)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (XomwInterwiki)this.interwikiMap.Get_by(prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* See InterwikiLookup::getAllPrefixes
|
||||
*
|
||||
* @param String|null $local If set, limits output to local/non-local interwikis
|
||||
* @return String[] List of prefixes
|
||||
*/
|
||||
public byte[][] getAllPrefixes(boolean local) {
|
||||
if (!local) {
|
||||
XophpArray.array_keys_bry(this.getInterwikiMap());
|
||||
}
|
||||
List_adp res = List_adp_.New();
|
||||
Ordered_hash hash = this.getInterwikiMap();
|
||||
int len = hash.Len();
|
||||
for (int i = 0; i < len; i++) {
|
||||
XomwInterwiki interwiki = (XomwInterwiki)hash.Get_at(i);
|
||||
if (interwiki.isLocal() == local) {
|
||||
res.Add(interwiki.interwikiId);
|
||||
}
|
||||
}
|
||||
return (byte[][])res.To_ary_and_clear(byte[].class);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * See InterwikiLookup::invalidateCache
|
||||
// *
|
||||
// * @param String $prefix
|
||||
// */
|
||||
// public function invalidateCache($prefix) {
|
||||
// if (!isset(this.interwikiMap[$prefix])) {
|
||||
// return;
|
||||
// }
|
||||
// $globalId = this.interwikiMap[$prefix].getWikiID();
|
||||
// unset(this.interwikiMap[$prefix]);
|
||||
//
|
||||
// // Reload the interwiki
|
||||
// site = this.siteLookup.getSites().getSite($globalId);
|
||||
// interwikis = this.getSiteInterwikis(site);
|
||||
// this.interwikiMap = array_merge(this.interwikiMap, [ interwikis[$prefix] ]);
|
||||
// }
|
||||
|
||||
/**
|
||||
* Load interwiki map to use as cache
|
||||
*/
|
||||
private Ordered_hash loadInterwikiMap() {
|
||||
Ordered_hash interwikiMap = Ordered_hash_.New();
|
||||
XomwSiteList siteList = this.siteLookup.getSites();
|
||||
int len = siteList.Len();
|
||||
for (int i = 0; i < len; i++) {
|
||||
XomwSite site = siteList.GetAt(i);
|
||||
XomwInterwiki[] interwikis = this.getSiteInterwikis(site);
|
||||
// interwikiMap = array_merge(interwikiMap, interwikis);
|
||||
for (XomwInterwiki interwiki : interwikis) {
|
||||
interwikiMap.Add(interwiki.interwikiId, interwiki);
|
||||
}
|
||||
}
|
||||
this.interwikiMap = interwikiMap;
|
||||
return interwikiMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get interwikiMap attribute, load if needed.
|
||||
*
|
||||
* @return Interwiki[]
|
||||
*/
|
||||
private Ordered_hash getInterwikiMap() {
|
||||
if (this.interwikiMap == null) {
|
||||
this.loadInterwikiMap();
|
||||
}
|
||||
return this.interwikiMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load interwikis for the given site
|
||||
*
|
||||
* @param Site site
|
||||
* @return Interwiki[]
|
||||
*/
|
||||
private XomwInterwiki[] getSiteInterwikis(XomwSite site) {
|
||||
Ordered_hash interwikis = Ordered_hash_.New();
|
||||
Ordered_hash hash = site.getInterwikiIds();
|
||||
int len = hash.Len();
|
||||
for (int i = 0; i < len; i++) {
|
||||
String interwiki = (String)hash.Get_at(i);
|
||||
String url = site.getPageUrl();
|
||||
String path = null;
|
||||
if (Type_.Eq_by_obj(site, XomwMediaWikiSite.class)) {
|
||||
path = ((XomwMediaWikiSite)site).getFileUrl("api.php");
|
||||
} else {
|
||||
path = "";
|
||||
}
|
||||
boolean local = String_.Eq(site.getSource(), "local");
|
||||
// TODO: How to adapt trans?
|
||||
interwikis.Add(interwiki, new XomwInterwiki(
|
||||
Bry_.new_u8(interwiki),
|
||||
Bry_.new_u8(url),
|
||||
Bry_.new_u8(path),
|
||||
Bry_.new_u8(site.getGlobalId()),
|
||||
local
|
||||
, false
|
||||
));
|
||||
}
|
||||
return (XomwInterwiki[])interwikis.To_ary_and_clear(XomwInterwiki.class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user