Wiki Tools¶
Collection of helper functions for pages, queries and wikitext.
CONDITION_PATTERN = re.compile('\\[\\[.*?\\]\\]', re.DOTALL)
module-attribute
¶
matches an SMW query condition, which may contain '|' as the disjunction operator and must therefore be removed before the parameters are split
LIMIT_PARAM_PATTERN = re.compile('^limit\\s*=\\s*(\\d+)$', re.IGNORECASE)
module-attribute
¶
matches a limit parameter of an SMW query, e.g. 'limit=100'
SearchParam
¶
Bases: OswBaseModel
Search parameters for semantic and prefix search
Source code in src/osw/wiki_tools.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
limit = 1000
class-attribute
instance-attribute
¶
the result limit to apply, None to apply none and leave it to the wiki. Ignored by semantic_search for a query that sets 'limit=' itself, since SMW honours the last limit in the query string
return_meta = False
class-attribute
instance-attribute
¶
If True, semantic_search returns one SemanticSearchResult per query instead of a flat list of titles, so that a caller can report a truncated result set. Ignored when return_json is True, since the raw wiki response already carries the truncation signal
SemanticSearchResult
¶
Bases: OswBaseModel
Result of a single semantic query, including whether the wiki truncated it
Source code in src/osw/wiki_tools.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
count
instance-attribute
¶
the number of results this response carried, before dropping non-existing pages, so it can be larger than len(titles). It is not the total number of matching pages on the wiki, which SMW does not report
next_offset = None
class-attribute
instance-attribute
¶
the absolute offset at which the remaining results start, to be passed back as '|offset='. None for a complete result set. It says where to continue, not how many results remain
query
instance-attribute
¶
the query as sent to the wiki, including any limit appended by semantic_search
titles
instance-attribute
¶
the page-title fulltext strings of the results that exist
truncated
instance-attribute
¶
True if the wiki reported further results beyond those returned
content_search(site, text)
¶
Searches the content (wikitext) of pages. Equivalent to the following mediawiki API call api.php?action=query&list=search&srsearch=Star Wars.
See https://www.mediawiki.org/wiki/API:Search for details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
site
|
Site
|
Site object from mwclient lib |
required |
text
|
Union[str, List[str], SearchParam]
|
Query text or instance of SearchParam |
required |
Returns:
| Name | Type | Description |
|---|---|---|
result |
Union[List[str], List[dict]]
|
With |
Source code in src/osw/wiki_tools.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
copy_list_of_wiki_pages(title_list, site0, site1, overwrite, callback=None)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title_list
|
list
|
|
required |
site0
|
Site
|
Source site object from mwclient lib |
required |
site1
|
Site
|
Target site object from mwclient lib |
required |
overwrite
|
bool
|
Whether to overwrite existing pages at target site |
required |
callback
|
NoneType or function
|
Function passed over, to perform operation on the titles of the source pages and to be passed as title of the target pages. See examples below. Example functions: capitalize = lambda x: x.capitalize() def change_namespace(title, namespace): if ":" in namespace: namespace = namespace.split(":")[0] if ":" in title: splits = title.split(":") old_name = splits[1].capitalize() new_title = namespace + ":" + old_name else: new_title = namespace + ":" + title.capitalize() return new_title Examples of passing a function as parameter: callback = capitalize callback = lambda x: x.lower() |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
results_dict |
dict
|
Dictionary, containing the results of the copying operations |
Source code in src/osw/wiki_tools.py
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 | |
copy_wiki_page(title0, title1, site0, site1, overwrite=True)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title0
|
str
|
Title of the source page |
required |
title1
|
str
|
Title of the target page |
required |
site0
|
Site
|
Source site object from mwclient lib |
required |
site1
|
Site
|
Target site object from mwclient lib |
required |
overwrite
|
bool
|
Whether to overwrite existing pages at target site |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
success |
bool
|
|
Source code in src/osw/wiki_tools.py
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 | |
create_or_overwrite_wiki_page(title, content, site)
¶
Creates a page with the passed title and content. If the page already exists, the prior content is replaced with the passed content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Title of the wiki page, e.g., User:Someone1234 |
required |
content
|
str
|
|
required |
site
|
Site
|
Site object from mwclient lib |
required |
Returns:
| Name | Type | Description |
|---|---|---|
success |
bool
|
|
Source code in src/osw/wiki_tools.py
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 | |
create_or_update_wiki_page_with_template(title, content, site, overwrite_with_empty=False)
¶
Creates a wiki page with a template included in the content. If the page does already exist, the parameters within the template are update
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Title of the wiki page, e.g., User:Someone1234 |
required |
content
|
str
|
|
required |
site
|
Site
|
Site object from mwclient lib |
required |
overwrite_with_empty
|
bool
|
Decided whether a template parameter's value in an preexisting page is overwritten with an empty value |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
success |
bool
|
|
Source code in src/osw/wiki_tools.py
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
create_site_object(domain, password_file='', credentials=None)
¶
Parameters
Parameters
domain :
Domain of the OSW instance, as specifed in the yaml file
password_file :
path to file with <username>
Returns
Returns
site : mwclient.client.Site
Site object from mwclient lib
Source code in src/osw/wiki_tools.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
delete_wiki_page(title, site, reason)
¶
Deletes the wiki page with the passed title, if it was found (exact match!), otherwise returns False
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Title of the wiki page, e.g., User:Someone1234 |
required |
site
|
Site
|
Site object from mwclient lib |
required |
reason
|
str
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
success |
bool
|
|
Source code in src/osw/wiki_tools.py
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 | |
edit_wiki_page_with_content_merge(title, new_content, site, template_name)
¶
Edits an existing wiki page, while merging the passed new content with the content of the existing page
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Title of the wiki page, e.g., User:Someone1234 |
required |
new_content
|
str
|
|
required |
site
|
Site
|
Site object from mwclient lib |
required |
template_name
|
str
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
success |
bool
|
|
Source code in src/osw/wiki_tools.py
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 | |
get_file_info_and_usage(site, title)
¶
(For 'File' pages only) Get information about the file and its usage
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
site
|
Site
|
Site object from mwclient lib. |
required |
title
|
Union[str, List[str], SearchParam]
|
Title(s) of the wiki page(s) or instance of SearchParam. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
result |
List[Dict[str, Union[Dict[str, str], List[str]]]]
|
Dictionary with page titles as keys and nested dictionary with keys 'info' and 'usage'. |
Notes
Query to reproduce: action=query format=json prop=imageinfo|fileusage titles=File%3AOSW857d85031d85425aa94db8b4720e84b7.png &iiprop=timestamp%7Cuser&fulimit=5000"
Resources
Use the sandbox to design and test the queries: https://demo.open-semantic-lab.org/wiki/Special:ApiSandbox
Source code in src/osw/wiki_tools.py
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 | |
get_query_limit(query)
¶
Returns the limit an SMW ask query sets itself, None if it sets none
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
an SMW ask query string, e.g. '[[Category:Item]]|?Name|limit=2' |
required |
Returns:
| Name | Type | Description |
|---|---|---|
result |
Optional[int]
|
the limit the query asks for, or None if the query does not set one or sets one that is not a number |
Source code in src/osw/wiki_tools.py
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
prefix_search(site, text)
¶
Standard query. Equivalent to the following mediawiki API call api.php?action=query&list=prefixsearch&pssearch=Star Wars.
See https://www.mediawiki.org/wiki/API:Prefixsearch for details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
site
|
Site
|
Site object from mwclient lib |
required |
text
|
Union[str, SearchParam]
|
Query text or instance of SearchParam |
required |
Returns:
| Name | Type | Description |
|---|---|---|
result |
Union[List[str], List[dict]]
|
With |
Source code in src/osw/wiki_tools.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
read_credentials_from_yaml(password_file, domain=None)
¶
Reads credentials from a yaml file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
password_file
|
Union[str, FilePath]
|
Path to the yaml file with the credentials. |
required |
domain
|
str
|
Domain of the OSW instance, as specifed in the yaml file. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
credentials |
dict
|
Dictionary with the credentials, expected to contain keys 'username' and 'password'. |
Source code in src/osw/wiki_tools.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
read_domains_from_credentials_file(cred_filepath)
¶
Reads domains and credentials from a yaml file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cred_filepath
|
Union[str, FilePath]
|
Path to the yaml file with the credentials |
required |
Source code in src/osw/wiki_tools.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
search_redirection_sources(site, target_title, debug=False)
¶
Returns a list of pages redirecting to the page with target_title per #REDIRECT [[target]] syntax
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
site
|
Site
|
Site object from mwclient lib |
required |
target_title
|
str
|
Title of the target wiki page |
required |
debug
|
bool
|
Whether to log debugging messages |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
page_list |
list of pages redirecting to the page with target_title
|
|
Source code in src/osw/wiki_tools.py
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 | |
search_wiki_page(title, site)
¶
Page search wrapper that adds exact match functionality with ignore-case on top of the prefix_search()'s functionality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Title of the wiki page, e.g., User:Someone1234 |
required |
site
|
Site
|
Site object from mwclient lib |
required |
Returns:
| Name | Type | Description |
|---|---|---|
result_dict |
dict
|
|
Source code in src/osw/wiki_tools.py
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | |
semantic_search(site, query)
¶
Semantic query
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
site
|
Site
|
Site object from mwclient lib |
required |
query
|
Union[str, List[str], SearchParam]
|
(List of) query text(s) or instance of SearchParam. A query that sets
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
result |
Union[List[str], List[dict], List[SemanticSearchResult]]
|
With |
Source code in src/osw/wiki_tools.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |