Skip to content

Commit

Permalink
fixes #105
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Aug 21, 2024
1 parent a9902c0 commit ba2680e
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 53 deletions.
1 change: 1 addition & 0 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<name>wikibot</name>
<comment></comment>
<projects>
<project>pyLoDStorage</project>
</projects>
<buildSpec>
<buildCommand>
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dependencies = [
# https://pypi.org/project/wikitextparser/
'wikitextparser>=0.54.0',
# https://pypi.org/project/pylodstorage/
'pylodstorage>=0.11.7',
'pylodstorage>=0.12.1',
# mysql-connector-python 8.3.0
'mysql-connector-python>=8.3.0',
# https://pypi.org/project/bcrypt/
Expand Down Expand Up @@ -69,10 +69,11 @@ test = [
]

[tool.hatch.build.targets.wheel]
only-include = ["wikibot3rd"]
only-include = ["wikibot3rd","wikibot3rd_examples"]

[tool.hatch.build.targets.wheel.sources]
"wikibot3rd" = "wikibot3rd"
"wikibot3rd_examples" = "wikibot3rd_examples"


[project.scripts]
Expand Down
15 changes: 8 additions & 7 deletions tests/test_WikiPush.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def testIssue14(self):
def testIssue12(self):
"""
https://github.com/WolfgangFahl/py-3rdparty-mediawiki/issues/12
add -qf / queryField option to allow to select other property than mainlabel
add -pf / pageField option to allow to select other property than mainlabel
"""
# don't test this in Travis
if self.inPublicCI():
Expand All @@ -146,9 +146,10 @@ def testIssue12(self):
|format=table
|limit=200
}}"""
pages = wikipush.query(ask, queryField="Event")
# self.debug=True
if self.debug:
pages = wikipush.query(ask, pageField="Event")
debug=self.debug
debug=True
if debug:
print(pages)
print(len(pages))
self.assertTrue(len(pages) > 15)
Expand All @@ -161,10 +162,10 @@ def testTransferPagesFromMaster(self):
if self.inPublicCI():
return
debug = self.debug
# debug=True
#debug=True
ask = "{{#ask: [[TransferPage page::+]][[TransferPage wiki::Master]]| mainlabel=-| ?TransferPage page = page| format=table|limit=300}}"
wikipush = WikiPush("master-capri", "test", login=True, debug=debug)
pages = wikipush.query(ask, queryField="page")
wikipush = WikiPush("master", "test", login=True, debug=debug)
pages = wikipush.query(ask, pageField="page")
if debug:
print(pages)
self.assertTrue(len(pages) > 100)
Expand Down
2 changes: 1 addition & 1 deletion wikibot3rd/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.11.8"
__version__ = "0.12.0"
2 changes: 1 addition & 1 deletion wikibot3rd/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Version(object):
name = "py-3rdparty-mediawiki"
version = wikibot3rd.__version__
date = "2020-10-31"
updated = "2024-08-20"
updated = "2024-08-21"

authors = "Wolfgang Fahl, Tim Holzheim"

Expand Down
72 changes: 30 additions & 42 deletions wikibot3rd/wikipush.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from git import Repo
from lodstorage.query import Query
from lodstorage.query_cmd import QueryCmd
from mwclient.image import Image

from wikibot3rd.selector import Selector
Expand Down Expand Up @@ -208,7 +209,7 @@ def query(
self,
askQuery,
wiki=None,
queryField=None,
pageField=None,
limit=None,
showProgress=False,
queryDivision=1,
Expand All @@ -219,7 +220,7 @@ def query(
Args:
askQuery(string): Semantic Media Wiki in line query https://www.semantic-mediawiki.org/wiki/Help:Inline_queries
wiki(wikibot3rd): the wiki to query - use fromWiki if not specified
queryField(string): the field to select the pageTitle from
pageField(string): the field to select the pageTitle from
limit(int): the limit for the query (optional)
showProgress(bool): true if progress of the query retrieval should be indicated (default: one dot per 50 records ...)
Returns:
Expand All @@ -228,13 +229,13 @@ def query(
pageRecords = self.queryPages(
askQuery, wiki, limit, showProgress, queryDivision
)
if queryField is None:
if pageField is None:
return pageRecords.keys()
# use a Dict to remove duplicates
pagesDict = {}
for pageRecord in pageRecords.values():
if queryField in pageRecord:
pagesDict[pageRecord[queryField]] = True
if pageField in pageRecord:
pagesDict[pageRecord[pageField]] = True
return list(pagesDict.keys())

def nuke(self, pageTitles, force=False):
Expand Down Expand Up @@ -1116,31 +1117,17 @@ def main(argv=None, mode="wikipush"): # IGNORE:C0111
"wikiquery",
"wikirestore",
]:
parser.add_argument(
"--limit", dest="limit", type=int, help="limit for query"
)
QueryCmd.add_args(parser=parser)
parser.add_argument(
"--progress",
dest="showProgress",
action="store_true",
help="shows progress for query",
)
parser.add_argument(
"-q",
"--query",
dest="query",
help="select pages with given SMW ask query",
required=False,
)
parser.add_argument(
"--queryFile",
dest="queryFile",
help="file the query should be read from",
)
parser.add_argument(
"-qf",
"--queryField",
dest="queryField",
"-pf",
"--pageField",
dest="pageField",
help="query result field which contains page",
)
parser.add_argument(
Expand Down Expand Up @@ -1209,12 +1196,17 @@ def main(argv=None, mode="wikipush"): # IGNORE:C0111
elif hasattr(args, "stdinp"):
if args.stdinp:
pages = sys.stdin.readlines()
elif args.query or args.queryFile:
if args.query:
query = args.query
else:
with open(args.queryFile, "r") as queryFile:
query = queryFile.read()
if pages is None:
# set the fixed language for the wikpush toolkit
# SMW ask
# see https://www.semantic-mediawiki.org/wiki/Help:Inline_queries
# Parser function #ask
args.language="ask"
query_cmd=QueryCmd(args=args,with_default_queries=False)
handled=query_cmd.handle_args()
if handled:
return 0
query=query_cmd.queryCode
if mode == "wikiquery":
formatedQueryResults = wikipush.formatQueryResult(
query,
Expand All @@ -1234,29 +1226,25 @@ def main(argv=None, mode="wikipush"): # IGNORE:C0111
pages = wikipush.query(
query,
wiki=queryWiki,
queryField=args.queryField,
pageField=args.pageField,
limit=args.limit,
showProgress=args.showProgress,
queryDivision=args.queryDivision,
)
if pages is None:
if mode == "wikiquery":
# we are finished
pass
return 0
elif mode == "wikirestore":
if (
args.pages is None
and args.queryFile is None
and args.query is None
):
wikipush.restore(
pageTitles=None,
backupPath=args.backupPath,
listFile=args.listFile,
)
# full restore
wikipush.restore(
pageTitles=None,
backupPath=args.backupPath,
listFile=args.listFile,
)
else:
raise Exception(
"no pages specified - you might want to use the -p, -q or --queryFile option"
"no pages specified - you might want to use the -p, -q -qn or --queryFile option"
)
else:
if args.ui and len(pages) > 0:
Expand Down
Empty file added wikibot3rd_examples/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions wikibot3rd_examples/queries.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Sample Queries for py-3rdparty-mediawiki library
#
# WF 2024-08-21
'List of Contexts':
ask: |
{{#ask: [[Concept:Context]]
|mainlabel=Context
|?Context since = since
|?Context updated = updated
|?Context demo = demo
|?Context master = master
|limit=200
|sort=Context name
|order=ascending
}}

0 comments on commit ba2680e

Please sign in to comment.