1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2025-05-30 14:04:56 +00:00

Xomw: Add getNamespaceIds, getNamespaceAliases

This commit is contained in:
gnosygnu 2017-02-26 17:05:08 -05:00
parent 5a6ac3a335
commit 1cab009d3c
15 changed files with 182 additions and 143 deletions

View File

@ -19,9 +19,9 @@ public class XophpUtility {
public static boolean empty(boolean v) {return v == false;}
public static boolean empty(int v) {return v == 0;}
public static boolean isset(byte[] v) {return v != null;}
public static boolean isset(int v) {return v != Null_int;}
public static boolean isset(double v) {return v != Null_double;}
public static boolean istrue(int v) {return v != Null_int;}
public static boolean isset(int v) {return v != NULL_INT;}
public static boolean isset(double v) {return v != NULL_DOUBLE;}
public static boolean istrue(int v) {return v != NULL_INT;}
public static boolean isnumeric(byte[] src) {
if (src == null) return false;
int len = src.length;
@ -38,7 +38,7 @@ public class XophpUtility {
return true;
}
public static final int Null_int = Int_.Max_value;
public static final double Null_double = Double_.MinValue;
public static final byte[] Null_bry = null;
public static final int NULL_INT = Int_.Max_value;
public static final double NULL_DOUBLE = Double_.MinValue;
public static final byte[] NULL_BRY = null;
}

View File

@ -377,7 +377,7 @@ public class XomwLinker {
|| XophpUtility.isset(frameParams.frameless)
|| !XophpUtility.istrue(handlerParams.width)
) {
if (widthOption == XophpUtility.Null_int) { // XO.MW: MW does extra validation that widthOption is in array; ("!isset( $wgThumbLimits[$widthOption] )")
if (widthOption == XophpUtility.NULL_INT) { // XO.MW: MW does extra validation that widthOption is in array; ("!isset( $wgThumbLimits[$widthOption] )")
widthOption = env.User__default__thumbsize;
}
@ -395,7 +395,7 @@ public class XomwLinker {
// Use width which is smaller: real image width or user preference width
// Unless image is scalable vector.
if (handlerParams.height == XophpUtility.Null_int && handlerParams.width <= 0 ||
if (handlerParams.height == XophpUtility.NULL_INT && handlerParams.width <= 0 ||
prefWidth < handlerParams.width || file.isVectorized()) {
handlerParams.width = prefWidth;
}

View File

@ -188,9 +188,9 @@ public class XomwNamespace {
* @return array
* @since 1.17
*/
private static XomwNamespaceHash namespaces = null;
public static XomwNamespaceHash getCanonicalNamespaces() {return getCanonicalNamespaces(false);}
public static XomwNamespaceHash getCanonicalNamespaces(boolean rebuild) {
private static XomwNamespacesById namespaces = null;
public static XomwNamespacesById getCanonicalNamespaces() {return getCanonicalNamespaces(false);}
public static XomwNamespacesById getCanonicalNamespaces(boolean rebuild) {
if (namespaces == null || rebuild) {
// global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
namespaces = XomwSetup.wgCanonicalNamespaceNames.Clone();
@ -234,8 +234,8 @@ public class XomwNamespace {
xNamespaces = Hash_adp_bry.cs();
int len = namespaces.Len();
for (int i = 0; i < len; i++) {
XomwNamespaceItem item = (XomwNamespaceItem)namespaces.GetItemOrNull(i);
xNamespaces.Add(Bry_.Lcase__all(item.text), item); // NOTE: MW does "strtolower($text)"; canonical namespaces are always ascii
XomwNamespaceItem item = (XomwNamespaceItem)namespaces.GetAtOrNull(i);
xNamespaces.Add(Bry_.Lcase__all(item.name), item); // NOTE: MW does "strtolower($text)"; canonical namespaces are always ascii
}
}
// if (array_key_exists($name, $xNamespaces)) {
@ -498,5 +498,5 @@ public class XomwNamespace {
//
// return $usableLevels;
// }
public static final int NULL_NS_ID = XophpUtility.Null_int;
public static final int NULL_NS_ID = XophpUtility.NULL_INT;
}

View File

@ -16,9 +16,9 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
package gplx.xowa.mediawiki.includes; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*;
public class XomwNamespaceItem {
public final int id;
public final byte[] text;
public XomwNamespaceItem(int id, byte[] text) {
public final byte[] name;
public XomwNamespaceItem(int id, byte[] name) {
this.id = id;
this.text = text;
this.name = name;
}
}

View File

@ -15,22 +15,22 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.xowa.mediawiki.includes; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*;
import gplx.core.lists.*;
public class XomwNamespaceHash {
public class XomwNamespacesById {
private HashByInt hash = new HashByInt();
public int Len() {return hash.Len();}
public byte[] GetTextOrNull(int id) {
public byte[] GetNameOrNull(int id) {
XomwNamespaceItem item = (XomwNamespaceItem)hash.Get_by_or_null(id);
return item == null ? null : item.text;
return item == null ? null : item.name;
}
public XomwNamespaceItem GetItemOrNull(int idx) {
public XomwNamespaceItem GetAtOrNull(int idx) {
return (XomwNamespaceItem)hash.Get_at_or_null(idx);
}
public XomwNamespaceHash Add(int id, String text) {
public XomwNamespacesById Add(int id, String text) {
hash.Add(id, new XomwNamespaceItem(id, Bry_.new_u8(text)));
return this;
}
public XomwNamespaceHash Clone() {
XomwNamespaceHash rv = new XomwNamespaceHash();
public XomwNamespacesById Clone() {
XomwNamespacesById rv = new XomwNamespacesById();
rv.hash = hash.Clone();
return rv;
}

View File

@ -0,0 +1,35 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis.
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; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*;
public class XomwNamespacesByName {
private final Ordered_hash hash = Ordered_hash_.New_bry();
public int Len() {return hash.Len();}
public int GetAsIdOrNullInt(byte[] name) {
XomwNamespaceItem item = (XomwNamespaceItem)hash.Get_by(name);
return item == null ? XophpUtility.NULL_INT : item.id;
}
public XomwNamespaceItem GetAtOrNull(int idx) {
return (XomwNamespaceItem)hash.Get_at(idx);
}
public void Add(byte[] name, XomwNamespaceItem item) {
hash.Add(name, item);
}
public XomwNamespacesByName Add(String name, int id) {
byte[] nameBry = Bry_.new_u8(name);
hash.Add(nameBry, new XomwNamespaceItem(id, nameBry));
return this;
}
}

View File

@ -169,15 +169,17 @@ public class XomwSetup {
// * all users if desired.
// */
// $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
//
// /**
// * The canonical names of namespaces 6 and 7 are, as of v1.14, "File"
// * and "File_talk". The old names "Image" and "Image_talk" are
// * retained as aliases for backwards compatibility.
// */
// $wgNamespaceAliases['Image'] = NS_FILE;
// $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
//
/**
* The canonical names of namespaces 6 and 7 are, as of v1.14, "File"
* and "File_talk". The old names "Image" and "Image_talk" are
* retained as aliases for backwards compatibility.
*/
public static final XomwNamespacesByName wgNamespaceAliases = new XomwNamespacesByName()
.Add("Image", XomwDefines.NS_FILE)
.Add("Image_talk", XomwDefines.NS_FILE_TALK)
;
// /**
// * Initialise $wgLockManagers to include basic FS version
// */
@ -392,7 +394,7 @@ public class XomwSetup {
* Definitions of the NS_ constants are in Defines.php
* @private
*/
public static XomwNamespaceHash wgCanonicalNamespaceNames = new XomwNamespaceHash()
public static XomwNamespacesById wgCanonicalNamespaceNames = new XomwNamespacesById()
.Add(XomwDefines.NS_MEDIA , "Media")
.Add(XomwDefines.NS_SPECIAL , "Special")
.Add(XomwDefines.NS_TALK , "Talk")

View File

@ -76,7 +76,7 @@ public abstract class XomwImageHandler extends XomwMediaHandler { private final
// pos = Bry_find_.Find_fwd_while_num(src, 1, len); // skip numeric
// if (Bry_.Match(src, pos, len, Xomw_lnki_wkr.Bry__px)) { // matches "px"
// Xomw_params_handler rv = new Xomw_params_handler();
// rv.width = Bry_.To_int_or(src, 0, pos, XophpUtility.Null_int);
// rv.width = Bry_.To_int_or(src, 0, pos, XophpUtility.NULL_INT);
// return rv;
// }
// }
@ -130,7 +130,7 @@ public abstract class XomwImageHandler extends XomwMediaHandler { private final
handlerParams.physicalWidth = handlerParams.width;
} else {
// Height was crap, unset it so that it will be calculated later
handlerParams.height = XophpUtility.Null_int;
handlerParams.height = XophpUtility.NULL_INT;
}
}

View File

@ -36,7 +36,7 @@ class XomwImageHandler_fxt {
public XomwImageHandler_fxt() {
this.handler = new XomwTransformationalImageHandler(Bry_.new_a7("test_handler"));
}
public Xomw_params_handler Make__handlerParams(int w) {return Make__handlerParams(w, XophpUtility.Null_int, XophpUtility.Null_int, XophpUtility.Null_int);}
public Xomw_params_handler Make__handlerParams(int w) {return Make__handlerParams(w, XophpUtility.NULL_INT, XophpUtility.NULL_INT, XophpUtility.NULL_INT);}
public Xomw_params_handler Make__handlerParams(int w, int h, int phys_w, int phys_h) {
Xomw_params_handler rv = new Xomw_params_handler();
rv.width = w;

View File

@ -724,7 +724,7 @@ public class Xomw_lnki_wkr {// THREAD.UNSAFE: caching for repeated calls
// Parsed a width param of imagelink like 300px or 200x300px
// XO.MW.NOTE: for MW, "" -> null, null while "AxB" -> 0x0
public void parseWidthParam(int[] img_size, byte[] src) {
img_size[0] = img_size[1] = XophpUtility.Null_int;
img_size[0] = img_size[1] = XophpUtility.NULL_INT;
if (src == Bry_.Empty) {
return;
}

View File

@ -44,12 +44,12 @@ public class Xomw_params_frame {
}
public Xomw_params_frame Clear() {
desc_link = false;
upright = XophpUtility.Null_double;
upright = XophpUtility.NULL_DOUBLE;
align = valign = caption = frame = framed = frameless
= thumbnail = manualthumb = alt = title = cls = img_cls
= link_title = link_url = link_target = no_link
= custom_url_link = custom_target_link = desc_query
= XophpUtility.Null_bry;
= XophpUtility.NULL_BRY;
return this;
}
public void Copy_to(Xomw_params_frame src) {

View File

@ -22,7 +22,7 @@ public class Xomw_params_handler {
public int physicalHeight;
public Xomw_params_handler Clear() {
width = height = page
= physicalWidth = physicalHeight = XophpUtility.Null_int;
= physicalWidth = physicalHeight = XophpUtility.NULL_INT;
return this;
}
public void Copy_to(Xomw_params_handler src) {

View File

@ -28,6 +28,6 @@ public class Xomw_params_scalar {
public byte[] dstUrl;
public byte[] interlace;
public Xomw_params_scalar() {
physicalWidth = physicalHeight = clientWidth = clientHeight = srcWidth = srcHeight = XophpUtility.Null_int;
physicalWidth = physicalHeight = clientWidth = clientHeight = srcWidth = srcHeight = XophpUtility.NULL_INT;
}
}

View File

@ -285,65 +285,60 @@ public class XomwMediaWikiTitleCodec implements XomwTitleFormatter {
// if (XomwRegexTitlePrefix.preg_match(m, dbkey)) {
// byte[] p = m[0];
// int ns = this.language.getNsIndex(p);
// }
// } while (true);
// do {
// $m = [];
// if (preg_match($prefixRegexp, dbkey, $m)) {
// $p = $m[1];
// $ns = $this->language->getNsIndex($p);
// if ($ns !== false) {
// if (ns != XophpUtility.NULL_INT) {
// // Ordinary namespace
// dbkey = $m[2];
// parts['namespace'] = $ns;
// dbkey = m[1];
// parts.ns = ns;
// // For Talk:X pages, check if X has a "namespace" prefix
// if ($ns == NS_TALK && preg_match($prefixRegexp, dbkey, $x)) {
// if ($this->language->getNsIndex($x[1])) {
// if (ns == XomwDefines.NS_TALK && XomwRegexTitlePrefix.preg_match(m, dbkey)) {
// if (this.language.getNsIndex(m[0]) != XophpUtility.NULL_INT) {
// // Disallow Talk:File:x type titles...
// throw new XomwMalformedTitleException('title-invalid-talk-namespace', text);
// } elseif (Interwiki::isValidInterwiki($x[1])) {
// // TODO: get rid of global state!
// // Disallow Talk:Interwiki:x type titles...
// throw new XomwMalformedTitleException('title-invalid-talk-namespace', text);
// throw new XomwMalformedTitleException("title-invalid-talk-namespace", text);
// }
// }
// } elseif (Interwiki::isValidInterwiki($p)) {
// // Interwiki link
// dbkey = $m[2];
// parts['interwiki'] = $this->language->lc($p);
//
// // Redundant interwiki prefix to the local wiki
// foreach ($this->localInterwikis as $localIW) {
// if (0 == strcasecmp(parts['interwiki'], $localIW)) {
// if (dbkey == '') {
// // Empty self-links should point to the Main Page, to ensure
// // compatibility with cross-wiki transclusions and the like.
// $mainPage = Title::newMainPage();
// return [
// 'interwiki' => $mainPage->getInterwiki(),
// 'local_interwiki' => true,
// 'fragment' => $mainPage->getFragment(),
// 'namespace' => $mainPage->getNamespace(),
// 'dbkey' => $mainPage->getDBkey(),
// 'user_case_dbkey' => $mainPage->getUserCaseDBKey()
// ];
// }
// parts['interwiki'] = '';
// // local interwikis should behave like initial-colon links
// parts['local_interwiki'] = true;
//
// // Do another namespace split...
// continue 2;
// }
// }
//
// // If there's an initial colon after the interwiki, that also
// // resets the default namespace
// if (dbkey !== '' && dbkey[0] == ':') {
// parts['namespace'] = NS_MAIN;
// dbkey = substr(dbkey, 1);
//// else if (Interwiki::isValidInterwiki($x[1])) {
//// // TODO: get rid of global state!
//// // Disallow Talk:Interwiki:x type titles...
//// throw new XomwMalformedTitleException('title-invalid-talk-namespace', text);
//// }
// }
// }
//// else if (Interwiki::isValidInterwiki($p)) {
//// // Interwiki link
//// dbkey = $m[2];
//// parts['interwiki'] = this.language->lc($p);
////
//// // Redundant interwiki prefix to the local wiki
//// foreach (this.localInterwikis as $localIW) {
//// if (0 == strcasecmp(parts['interwiki'], $localIW)) {
//// if (dbkey == '') {
//// // Empty self-links should point to the Main Page, to ensure
//// // compatibility with cross-wiki transclusions and the like.
//// $mainPage = Title::newMainPage();
//// return [
//// 'interwiki' => $mainPage->getInterwiki(),
//// 'local_interwiki' => true,
//// 'fragment' => $mainPage->getFragment(),
//// 'namespace' => $mainPage->getNamespace(),
//// 'dbkey' => $mainPage->getDBkey(),
//// 'user_case_dbkey' => $mainPage->getUserCaseDBKey()
//// ];
//// }
//// parts['interwiki'] = '';
//// // local interwikis should behave like initial-colon links
//// parts['local_interwiki'] = true;
////
//// // Do another namespace split...
//// continue 2;
//// }
//// }
////
//// // If there's an initial colon after the interwiki, that also
//// // resets the default namespace
//// if (dbkey !== '' && dbkey[0] == ':') {
//// parts['namespace'] = NS_MAIN;
//// dbkey = substr(dbkey, 1);
//// }
//// }
// // If there's no recognized interwiki or namespace,
// // then let the colon expression be part of the title.
// }

View File

@ -30,9 +30,9 @@ public class XomwLanguage {
// public $mExtendedSpecialPageAliases;
//
// /** @var array|null */
private XomwNamespaceHash namespaceNames;
// protected $mNamespaceIds, $namespaceAliases;
//
private XomwNamespacesById namespaceNames;
private XomwNamespacesByName mNamespaceIds, namespaceAliases;
// /**
// * ReplacementArray Object caches
// */
@ -485,11 +485,11 @@ public class XomwLanguage {
*
* @return array
*/
public XomwNamespaceHash getNamespaces() {
public XomwNamespacesById getNamespaces() {
if (this.namespaceNames == null) {
// global $wgMetaNamespace, $wgMetaNamespaceTalk, $wgExtraNamespaces;
//
XomwNamespaceHash validNamespaces = XomwNamespace.getCanonicalNamespaces();
XomwNamespacesById validNamespaces = XomwNamespace.getCanonicalNamespaces();
//
// this.namespaceNames = $wgExtraNamespaces +
// self::$dataCache->getItem(this.mCode, 'namespaceNames');
@ -566,8 +566,8 @@ public class XomwLanguage {
* @return String|boolean String if the namespace value exists, otherwise false
*/
public byte[] getNsText(int index) {
XomwNamespaceHash nsHash = this.getNamespaces();
return nsHash.GetTextOrNull(index);
XomwNamespacesById nsHash = this.getNamespaces();
return nsHash.GetNameOrNull(index);
}
// /**
@ -640,12 +640,14 @@ public class XomwLanguage {
// $ids = this.getNamespaceIds();
// return isset($ids[$lctext]) ? $ids[$lctext] : false;
// }
//
// /**
// * @return array
// */
// public function getNamespaceAliases() {
// if (is_null(this.namespaceAliases)) {
/**
* @return array
*/
public XomwNamespacesByName getNamespaceAliases() {
if (this.namespaceAliases == null) {
// XO.MW: MW uses two sets: "aliases" + "convertedNames" and then combines them; XO just uses one
this.namespaceAliases = new XomwNamespacesByName();
// $aliases = self::$dataCache->getItem(this.mCode, 'namespaceAliases');
// if (!$aliases) {
// $aliases = [];
@ -667,8 +669,8 @@ public class XomwLanguage {
// $aliases[$alias] = $index;
// }
// }
//
// # Also add converted namespace names as aliases, to avoid confusion.
// Also add converted namespace names as aliases, to avoid confusion.
// $convertedNames = [];
// foreach (this.getVariants() as $variant) {
// if ($variant === this.mCode) {
@ -680,36 +682,42 @@ public class XomwLanguage {
// }
//
// this.namespaceAliases = $aliases + $convertedNames;
// }
//
// return this.namespaceAliases;
// }
//
// /**
// * @return array
// */
// public function getNamespaceIds() {
// if (is_null(this.mNamespaceIds)) {
// global $wgNamespaceAliases;
// # Put namespace names and aliases into a hashtable.
// # If this is too slow, then we should arrange it so that it is done
// # before caching. The catch is that at pre-cache time, the above
// # class-specific fixup hasn't been done.
// this.mNamespaceIds = [];
// foreach (this.getNamespaces() as $index => $name) {
// this.mNamespaceIds[this.lc($name)] = $index;
// }
// foreach (this.getNamespaceAliases() as $name => $index) {
// this.mNamespaceIds[this.lc($name)] = $index;
// }
// if ($wgNamespaceAliases) {
// foreach ($wgNamespaceAliases as $name => $index) {
// this.mNamespaceIds[this.lc($name)] = $index;
// }
// }
// }
// return this.mNamespaceIds;
// }
}
return this.namespaceAliases;
}
/**
* @return array
*/
public XomwNamespacesByName getNamespaceIds() {
if (this.mNamespaceIds == null) {
// Put namespace names and aliases into a hashtable.
// If this is too slow, then we should arrange it so that it is done
// before caching. The catch is that at pre-cache time, the above
// class-specific fixup hasn't been done.
this.mNamespaceIds = new XomwNamespacesByName();
XomwNamespacesById getNamespacesHash = this.getNamespaces();
int len = getNamespacesHash.Len();
for (int i = 0; i < len; i++) {
XomwNamespaceItem item = (XomwNamespaceItem)getNamespacesHash.GetAtOrNull(i);
this.mNamespaceIds.Add(this.lc(item.name), item);
}
XomwNamespacesByName getNamespaceAliasesHash = this.getNamespaceAliases();
len = getNamespaceAliasesHash.Len();
for (int i = 0; i < len; i++) {
XomwNamespaceItem item = (XomwNamespaceItem)getNamespaceAliasesHash.GetAtOrNull(i);
this.mNamespaceIds.Add(this.lc(item.name), item);
}
XomwNamespacesByName wgNamespaceAliases = XomwSetup.wgNamespaceAliases;
len = wgNamespaceAliases.Len();
for (int i = 0; i < len; i++) {
XomwNamespaceItem item = (XomwNamespaceItem)wgNamespaceAliases.GetAtOrNull(i);
this.mNamespaceIds.Add(this.lc(item.name), item);
}
}
return this.mNamespaceIds;
}
/**
* Get a namespace key by value, case insensitive. Canonical namespace
@ -724,9 +732,8 @@ public class XomwLanguage {
if (ns != XomwNamespace.NULL_NS_ID) {
return ns;
}
// $ids = this.getNamespaceIds();
// return isset($ids[$lctext]) ? $ids[$lctext] : false;
return XophpUtility.Null_int;
XomwNamespacesByName ids = this.getNamespaceIds();
return ids.GetAsIdOrNullInt(lctext);
}
// /**