Whym has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/399625 )

Change subject: Add option to auto-create accounts when logging in
......................................................................

Add option to auto-create accounts when logging in

Bug: T183466

Change-Id: I781e57dfb4d04e7900686b90b17eee502f599094
---
M pywikibot/login.py
M pywikibot/site.py
M scripts/login.py
3 files changed, 17 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/25/399625/1

diff --git a/pywikibot/login.py b/pywikibot/login.py
index dafcb84..b9086cd 100644
--- a/pywikibot/login.py
+++ b/pywikibot/login.py
@@ -283,7 +283,7 @@
                 else:
                     warn('Invalid password format', _PasswordFileWarning)
 
-    def login(self, retry=False):
+    def login(self, retry=False, autocreate=False):
         """
         Attempt to log into the server.
 
@@ -295,7 +295,8 @@
         if not self.password:
             # First check that the username exists,
             # to avoid asking for a password that will not work.
-            self.check_user_exists()
+            if not autocreate:
+                self.check_user_exists()
 
             # As we don't want the password to appear on the screen, we set
             # password = True
diff --git a/pywikibot/site.py b/pywikibot/site.py
index 9b82484..fb08680 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -2038,7 +2038,7 @@
         auth_token = get_authentication(self.base_url(''))
         return auth_token is not None and len(auth_token) == 4
 
-    def login(self, sysop=False):
+    def login(self, sysop=False, autocreate=False):
         """
         Log the user in if not already logged in.
 
@@ -2075,6 +2075,9 @@
         # May occur if you are not logged in (no API read permissions).
         except api.APIError:
             pass
+        except NoUsername:
+            pass
+
         if self.is_oauth_token_available():
             if sysop:
                 raise NoUsername('No sysop is permitted with OAuth')
@@ -2082,13 +2085,13 @@
                 raise NoUsername('Logged in on %(site)s via OAuth as '
                                  '%(wrong)s, but expect as %(right)s'
                                  % {'site': self,
-                                    'wrong': self.userinfo['name'],
-                                    'right': self._username[sysop]})
+                                        'wrong': self.userinfo['name'],
+                                        'right': self._username[sysop]})
             else:
                 raise NoUsername('Logging in on %s via OAuth failed' % self)
         loginMan = api.LoginManager(site=self, sysop=sysop,
                                     user=self._username[sysop])
-        if loginMan.login(retry=True):
+        if loginMan.login(retry=True, autocreate=autocreate):
             self._username[sysop] = loginMan.username
             self.getuserinfo(force=True)
             self._loginstatus = (LoginStatus.AS_SYSOP
diff --git a/scripts/login.py b/scripts/login.py
index 2adaf33..e593524 100755
--- a/scripts/login.py
+++ b/scripts/login.py
@@ -40,6 +40,9 @@
                 manually. -logout, -pass, -force, -pass:XXXX and -sysop are not
                 compatible with -oauth.
 
+   -autocreate  Auto-create an account using the unified login system.
+                Note that auto-creation works for unified accounts only.
+
 If not given as parameter, the script will ask for your username and
 password (password entry will be hidden), log in to your home wiki using
 this combination, and store the resulting cookies (containing your password
@@ -120,6 +123,7 @@
     logall = False
     logout = False
     oauth = False
+    autocreate = False
     unknown_args = []
     for arg in pywikibot.handle_args(args):
         if arg.startswith("-pass"):
@@ -141,6 +145,8 @@
             logout = True
         elif arg == '-oauth':
             oauth = True
+        elif arg == '-autocreate':
+            autocreate = True
         else:
             unknown_args += [arg]
 
@@ -170,7 +176,7 @@
                 if logout:
                     site.logout()
                 else:
-                    site.login(sysop)
+                    site.login(sysop, autocreate=autocreate)
                 user = site.user()
                 if user:
                     pywikibot.output(

-- 
To view, visit https://gerrit.wikimedia.org/r/399625
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I781e57dfb4d04e7900686b90b17eee502f599094
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Whym <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to