{ "cells": [ { "cell_type": "markdown", "id": "a0eee7d3", "metadata": {}, "source": [ "* this [Notebook](https://github.com/salgo60/spa2Commons/blob/main/Notebook/SDC%20set%20SPA%20property.ipynb)\n", "\n", "Work in progress see also working solution in [PAWS](https://public.paws.wmcloud.org/User:Salgo60/Traverse%20category%20to%20find%20SPA%20id%20.ipynb)\n", "### Set SDC SPA\n", "#### Theory\n", "* read all imagefiles in category [Category:Uploaded_with_spa2Commons](https://commons.wikimedia.org/wiki/Category:Uploaded_with_spa2Commons) missing SPA set\n", "* extact template and param Source" ] }, { "cell_type": "code", "execution_count": 1, "id": "88e96d9f", "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "module 'pywikibot.pagegenerators' has no attribute 'ReferringPageGenerator'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 87\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 88\u001b[0m \u001b[0mtemplate_name\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Constraint:Units\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 89\u001b[0;31m \u001b[0mtmpl_gen\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlist_template_usage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msite\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtemplate_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 90\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 91\u001b[0m \u001b[0mheader\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m\"Property\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Constraint:Units\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mlist_template_usage\u001b[0;34m(site_obj, tmpl_name)\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0mname\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"{}:{}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msite\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnamespace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtmpl_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0mtmpl_page\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpywikibot\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mPage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msite\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0mref_gen\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mReferringPageGenerator\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtmpl_page\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0monlyTemplateInclusion\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 17\u001b[0m \u001b[0mfilter_gen\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mNamespaceFilterPageGenerator\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mref_gen\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnamespaces\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m121\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0mgenerator\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msite\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpreloadpages\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilter_gen\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpageprops\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAttributeError\u001b[0m: module 'pywikibot.pagegenerators' has no attribute 'ReferringPageGenerator'" ] } ], "source": [ "import pywikibot\n", "from pywikibot import pagegenerators as pg\n", "\n", "def list_template_usage(site_obj, tmpl_name):\n", " \"\"\"\n", " Takes Site object and template name and returns a generator.\n", "\n", " The function expects a Site object (pywikibot.Site()) and\n", " a template name (String). It creates a list of all\n", " pages using that template and returns them as a generator.\n", " It only returns pages in the 121-namespace (property-talk-pages).\n", " The generator will load 50 pages at a time for iteration.\n", " \"\"\"\n", " name = \"{}:{}\".format(site.namespace(10), tmpl_name)\n", " tmpl_page = pywikibot.Page(site, name)\n", " ref_gen = pg.ReferringPageGenerator(tmpl_page, onlyTemplateInclusion=True)\n", " filter_gen = pg.NamespaceFilterPageGenerator(ref_gen, namespaces=[121])\n", " generator = site.preloadpages(filter_gen, pageprops=True)\n", " return generator\n", "\n", "def gather_template_usage(generator, template_name, header=None):\n", " \"\"\"\n", " Takes a generator and a template name and returns usage list.\n", "\n", " The function can also take a header (list of strings) that will be\n", " the headers of the table (Needs to be the same dimension as the table).\n", " The first column needs to be a link to property (It will be made into\n", " a link. In this example the second column is a list of links to Q-items.\n", " \"\"\"\n", " tmpl_usage = []\n", " if header != None:\n", " tmpl_usage.append(header)\n", "\n", " for page in generator:\n", " page_str = page.get()\n", " tmpl_list = pywikibot.textlib.extract_templates_and_params(page_str)\n", "\n", " for tmpl in tmpl_list:\n", " if template_name in tmpl:\n", " page_title = page.title().split(\":\")[1]\n", " property_link = \"{{{{P|{}}}}}\".format(page_title)\n", " tmpl_usage.append([property_link, tmpl[1][\"list\"]])\n", " return tmpl_usage\n", "\n", "def create_table_string(data, highlight=(True, False, False, False),\n", " table_class='wikitable', style=''):\n", " \"\"\"\n", " Takes a list and returns a wikitable.\n", "\n", " @param data: The list that is converted to a wikitable.\n", " @type data: List (Nested)\n", " @param highlight: Tuple of rows and columns that should be highlighted.\n", " (first row, last row, left column, right column)\n", " @type highlight: Tuple\n", " @param table_class: A string containing the class description.\n", " See wikitable help.\n", " @type table_class: String\n", " @param style: A string containing the style description.\n", " See wikitable help.\n", " @type style: String\n", " \"\"\"\n", " last_row = len(data) - 1\n", " last_cell = len(data[0]) - 1\n", "\n", " table = '{{| class=\"{}\" style=\"{}\"\\n'.format(table_class, style)\n", " for key, row in enumerate(data):\n", " if key == 0 and highlight[0] or key == last_row and highlight[1]:\n", " row_string = '|-\\n! ' + '\\n! '.join(cell for cell in row)\n", " else:\n", " row_string = '|-'\n", " cells = ''\n", " for ckey, cell in enumerate(row):\n", " if ckey == 0 and highlight[2]:\n", " cells += '\\n! ' + cell\n", " elif ckey == last_cell and highlight[3]:\n", " cells += '\\n! ' + cell\n", " else:\n", " cells += '\\n| ' + cell\n", " row_string += cells\n", "\n", " table += row_string + '\\n'\n", " table += '|}'\n", " return table\n", "\n", "\n", "site = pywikibot.Site(\"wikidata\", 'wikidata')\n", "\n", "template_name = \"Constraint:Units\"\n", "tmpl_gen = list_template_usage(site, template_name)\n", "\n", "header = [\"Property\", \"Constraint:Units\"]\n", "tmpl_usage = gather_template_usage(tmpl_gen, template_name, header)\n", "\n", "table = create_table_string(tmpl_usage)\n", "\n", "write_page = pywikibot.Page(site, \"Wikidata:Database reports/List of properties with Constraint:Unit templates and their values\")\n", "write_page.save(summary=\"Updating Table\")" ] }, { "cell_type": "code", "execution_count": null, "id": "c0c2257b", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.4" } }, "nbformat": 4, "nbformat_minor": 5 }