jenkins-bot has submitted this change and it was merged. ( 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, 28 insertions(+), 5 deletions(-)
Approvals: jenkins-bot: Verified Xqt: Looks good to me, approved
diff --git a/pywikibot/login.py b/pywikibot/login.py index dafcb84..d3b4f93 100644 --- a/pywikibot/login.py +++ b/pywikibot/login.py @@ -283,19 +283,24 @@ else: warn('Invalid password format', _PasswordFileWarning)
- def login(self, retry=False): + def login(self, retry=False, autocreate=False): """ Attempt to log into the server.
@param retry: infinitely retry if the API returns an unknown error @type retry: bool
+ @param autocreate: if true, allow auto-creation of the account + using unified login + @type autocreate: bool + @raises NoUsername: Username is not recognised by the site. """ 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..9794b6e 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -2038,10 +2038,18 @@ 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.
+ @param sysop: if true, log in with the sysop account. + @type sysop: bool + + @param autocreate: if true, allow auto-creation of the account + using unified login + @type autocreate: bool + + @raises NoUsername: Username is not recognised by the site. U{https://www.mediawiki.org/wiki/API:Login%7D """ # TODO: this should include an assert that loginstatus @@ -2075,6 +2083,10 @@ # May occur if you are not logged in (no API read permissions). except api.APIError: pass + except NoUsername as e: + if not autocreate: + raise e + if self.is_oauth_token_available(): if sysop: raise NoUsername('No sysop is permitted with OAuth') @@ -2088,7 +2100,7 @@ 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..0868cce 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 unified login when necessary. + Note: the global account must exist already before using this. + 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(