Hi pywikibot experts, Just a little update about the 3rd method I found to automatically create new properties: the use of the php script "importProperties.php" ( https://github.com/JeroenDeDauw/Wikibase/blob/master/repo/maintenance/importProperties.php ).
I simply modified the 'createProperty' function ( https://github.com/JeroenDeDauw/Wikibase/blob/master/repo/maintenance/importProperties.php#L147 ), as follows: /** * @param string[] $contents Associative array, mapping language codes to labels. * * @return bool true if the item was created, false otherwise */ private function createProperty( array $contents ) { $property = Property::newFromType( 'string' ); $fingerprint = $property->getFingerprint(); foreach ( $contents as $contents_to_be_split ) { list($propertylabel_en, $propertydescription_en, $propertylabel_it, $propertydescription_it) = explode("|", $contents_to_be_split); $fingerprint->setLabel( "en", $propertylabel_en ); $fingerprint->setDescription( "en", $propertydescription_en ); $fingerprint->setLabel( "it", $propertylabel_it ); $fingerprint->setDescription( "it", $propertydescription_it ); } try { $this->store->saveEntity( $property, 'imported', $this->user, EDIT_NEW ); return true; } catch ( Exception $ex ) { $this->doPrint( 'ERROR: ' . str_replace( "\n", ' ', $ex->getMessage() ) ); } return false; } It works for more complex cases of fingerprints also containing descriptions, and multiple languages (but it doesn't work for the creation of statements into the new property) In any case, I would really prefer a solution based on Pywikibot, as described in my previous mail Thanks for your attention! On Wed, Mar 23, 2016 at 2:57 PM, comfortably numb < [email protected]> wrote: > Hi pywikibot experts, > > How can I create new properties via pywikibot? (I'm trying to do it via > bot, because I'm doing some experiments on a dedicated wikibase > installation with - possibly - hundreds of properties to be created... and > Pywikibot would certainly be my favorite tool!) > > In case I have, instead, to directly wrap the action "wbeditentity" from > mediawiki API ( > https://www.wikidata.org/w/api.php?action=help&modules=wbeditentity ), > are there some Python examples? > > And, in case I have to use the php script "importProperties.php" ( > https://github.com/JeroenDeDauw/Wikibase/blob/master/repo/maintenance/importProperties.php > ), > how can I manage properties more complex than the ones contained in the > example ( > https://github.com/JeroenDeDauw/Wikibase/blob/master/repo/maintenance/en-elements-properties.csv > )? > > > Using pywikibot, I'm able to MODIFY existing properties with instructions > like the following ones (which let me generate the object-content in one > shot via json ... as I need): > > In [1]: import pywikibot ; site = pywikibot.Site() ; repo = > site.data_repository() > In [2]: property_page = pywikibot.PropertyPage(repo, u"P2") > In [3]: myjson = {u'descriptions': {u'en': {u'language': u'en', u'value': > u'invented description'}}, u'labels': {u'en': {u'language': u'en', > u'value': u'test property'}}} > In [4]: property_page.editEntity(myjson) > > ...but I cannot CREATE new properties (instantiating a PropertyPage > object), because pywikibot asks for the identifier of an existing instance: > > In [11]: p_page = pywikibot.PropertyPage(repo) > --------------------------------------------------------------------------- > InvalidTitle Traceback (most recent call last) > <ipython-input-11-e72b812b8cd3> in <module>() > ----> 1 p_page = pywikibot.PropertyPage(repo) > > /home/user/src/pywikibot_repo/pywikibot/page.pyc in __init__(self, source, > title) > 4027 if not title or not self.id.startswith('P'): > 4028 raise pywikibot.InvalidTitle( > -> 4029 u"'%s' is not an property page title" % title) > 4030 Property.__init__(self, source, self.id) > 4031 > > InvalidTitle: '' is not an property page title > > In [12]: > > In fact, as I understand, in the source code of the "WikibasePage" class, > I see that > while for the Item type, a "Special case for empty item" is mentioned ( > https://github.com/wikimedia/pywikibot-core/blob/master/pywikibot/page.py#L3760 > ) > > # Special case for empty item. > if title is None or title == '-1': > super(ItemPage, self).__init__(site, u'-1', ns=ns) > assert self.id == '-1' > return > > ...for the Property type, instead, an empty object is NOT allowed ( > https://github.com/wikimedia/pywikibot-core/blob/master/pywikibot/page.py#L4168 > ) > > if not title or not self.id.startswith('P'): > raise pywikibot.InvalidTitle( > u"'%s' is not an property page title" % title) > Property.__init__(self, source, self.id) > > > Thanks a lot for your attention! >
_______________________________________________ pywikibot mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/pywikibot
