Skip to content

Commit

Permalink
Support saving Wikidata credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
Futur3r committed Mar 26, 2023
1 parent 5c88864 commit 3f279b1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
22 changes: 22 additions & 0 deletions src/dialogs/preferences/PreferenceDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { debug } from '../../wikicite';
const PreferenceDialog = (props) => {
const [storage, setStorage] = useState(props.Prefs.get('storage'));

const [username, setUsername] = useState(Zotero.Prefs.get('wikidata-username'));
const [password, setPassword] = useState(Zotero.Prefs.get('wikidata-password'));

async function migrateStorageLocation(from, to) {
const progress = new Progress(
'loading',
Expand Down Expand Up @@ -68,6 +71,15 @@ const PreferenceDialog = (props) => {
}
});
}

// save credentials if there are defined (the conditions are there to avoid bug with Zotero.Prefs.set();)
if (username !== undefined) {
Zotero.Prefs.set('wikidata-username', username);
}
if (password !== undefined) {
Zotero.Prefs.set('wikidata-password', password);
}

window.close();
}

Expand All @@ -76,6 +88,16 @@ const PreferenceDialog = (props) => {
}

return <div orient="vertical">
<fieldset>
<legend>{props.getString('wikicite.prefs.wikidata-credentials')}</legend>
<span>{props.getString('wikicite.prefs.wikidata-credentials-desc')}</span>
<br />
<label htmlFor="wikidata-credentials-username">{props.getString('wikicite.prefs.wikidata-credentials-username')}</label>
<input id="text-credentials-username" type="text" value={username} onChange={(event) => setUsername(event.target.value)} />
<br />
<label htmlFor="wikidata-credentials-password">{props.getString('wikicite.prefs.wikidata-credentials-password')}</label>
<input id="text-credentials-password" type="password" value={password} onChange={(event) => setPassword(event.target.value)} />
</fieldset>
<fieldset>
<legend>{props.getString('wikicite.prefs.citation-storage')}</legend>
<span>{props.getString('wikicite.prefs.citation-storage-desc')}</span>
Expand Down
9 changes: 4 additions & 5 deletions src/wikidata.js
Original file line number Diff line number Diff line change
Expand Up @@ -934,10 +934,7 @@ SELECT ?item ?itemLabel ?doi ?isbn WHERE {
const actionType = getActionType(citesWorkClaims[id]);

do {
if (
!login.cancelled &&
(!login.anonymous || login.error)
) {
if (!login.cancelled && (!login.anonymous || login.error)) {
login.prompt();
}
if (login.cancelled) {
Expand Down Expand Up @@ -999,6 +996,8 @@ SELECT ?item ?itemLabel ?doi ?isbn WHERE {
class Login {
constructor() {
this.error = false;
this.username = Zotero.Prefs.get('wikidata-username');
this.password = Zotero.Prefs.get('wikidata-password');
}

get credentials() {
Expand Down Expand Up @@ -1070,7 +1069,7 @@ class Login {
);

const username = {value: this.username};
const password = {value: undefined};
const password = {value: this.password};
const save = {value: false};
let loginPrompt;
do {
Expand Down
4 changes: 4 additions & 0 deletions static/chrome/locale/en-US/wikicite.properties
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ wikicite.prefs.citation-storage.progress.done=Successfully migrated citation sto
wikicite.prefs.citation-storage.progress.failed=Failed to migrate citation storage
wikicite.prefs.citation-storage.alert.failed=Migration failed
wikicite.prefs.citation-storage.alert.failed-explanation=The migration of citation storage has failed. The storage preference has not been updated. Migration failed for the following items, please check their citation data and try again:
wikicite.prefs.wikidata-credentials=Wikidata credentials
wikicite.prefs.wikidata-credentials-desc=Enter Wikidata username and password. Please use a Bot Password as these credentials are stored locally in clear ! (see https://www.mediawiki.org/wiki/Special:BotPasswords)
wikicite.prefs.wikidata-credentials-username=Username:
wikicite.prefs.wikidata-credentials-password=Password:
wikicite.source-item.add-identifier.progress.done = %s citation(s) added
wikicite.source-item.add-identifier.progress.error = Adding citation(s) by identifier failed
wikicite.source-item.add-identifier.progress.invalid = No valid identifiers provided
Expand Down

0 comments on commit 3f279b1

Please sign in to comment.