Skip to content

Commit 26a5a01

Browse files
committed
[repo] Add bulk importer
1 parent f3b039a commit 26a5a01

File tree

5 files changed

+401
-162
lines changed

5 files changed

+401
-162
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"lint-staged": "^12.3.7",
8585
"markdownlint-cli2": "^0.4.0",
8686
"markdownlint-rule-helpers": "^0.16.0",
87+
"node-fetch": "^3.2.4",
8788
"nodemw": "^0.17.0",
8889
"stylelint": "^14.6.1",
8990
"stylelint-config-standard": "^25.0.0",

scripts/migration/phases/05-wikilinks/rule.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ const {
2323
getLineMetadata,
2424
} = require('markdownlint-rule-helpers');
2525

26+
const path = require('path');
27+
28+
const getFileName = (matches) => {
29+
const [link] = matches.groups.link.split('|');
30+
if (link.startsWith('File:')) {
31+
return link.replace(/^File:/, '');
32+
}
33+
34+
return null;
35+
};
36+
2637
const getWikilinkLink = (matches) => {
2738
const [link] = matches.groups.link.split('|');
2839

@@ -52,18 +63,30 @@ const getWikilinkDescription = (matches) => {
5263
.replaceAll('_', ' ');
5364
};
5465

55-
const replaceWikiLinks = (line, lineNumber, onError) => {
66+
const getFixInfo = (matches, filename) => {
67+
const imageFilename = getFileName(matches);
68+
if (imageFilename) {
69+
const description = matches.groups.link.split('|').pop().replace('File:', '');
70+
const imageDirName = `./_${path.basename(filename.replace(/\.md.?/, '').replaceAll(' ', '_'))}`;
71+
return `![${description}](${imageDirName}/${imageFilename.replaceAll(' ', '_')})`;
72+
}
73+
74+
const description = getWikilinkDescription(matches);
75+
const link = getWikilinkLink(matches);
76+
return `[${description}](${link})`;
77+
};
78+
79+
const replaceWikiLinks = (line, lineNumber, onError, filename) => {
5680
const linkSearch = /(?<wikilink>\[\[(?<link>[^\]]*)\]\])/g;
5781
let matches = linkSearch.exec(line);
5882
if (!matches) {
5983
return;
6084
}
6185
do {
6286
const foundWikilink = matches.groups.wikilink;
63-
const link = getWikilinkLink(matches);
64-
const description = getWikilinkDescription(matches);
87+
const replacement = getFixInfo(matches, filename);
6588
const column = matches.index + 1;
66-
const replacement = `[${description}](${link})`;
89+
6790
const fixInfo = {
6891
editColumn: column,
6992
deleteCount: matches.groups.link.length + 4,
@@ -96,7 +119,7 @@ module.exports = {
96119
}
97120

98121
const lineNumber = lineIndex + 1;
99-
replaceWikiLinks(line, lineNumber, onError);
122+
replaceWikiLinks(line, lineNumber, onError, params.name);
100123
});
101124
},
102125
};

scripts/utils.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18+
/* eslint-disable import/no-extraneous-dependencies */
1819
const Bot = require('nodemw'); // cspell:ignore nodemw
1920
const path = require('path');
2021
const winston = require('winston');
@@ -43,6 +44,17 @@ const getFetchDoc = (logger) => (client) => (pageTitle) => new Promise((resolve,
4344
});
4445
});
4546

47+
const getGetPagesByPrefix = (logger) => (client) => (prefix) => new Promise((resolve, reject) => {
48+
logger.debug(`Fetching all articles with prefix ${prefix}`);
49+
client.getPagesByPrefix(prefix, (err, data) => {
50+
if (err) {
51+
reject(err);
52+
}
53+
54+
resolve(data);
55+
});
56+
});
57+
4658
const getGetMigratedPageIds = (logger) => (client) => async (pageTitle) => {
4759
const articleData = await getFetchDoc(logger)(client)(pageTitle);
4860

@@ -124,6 +136,26 @@ const getGetPagesTranscluding = (logger) => (client) => (templateName) => new Pr
124136
});
125137
});
126138

139+
const getGetImageInfo = (logger) => (client) => (filename) => new Promise((resolve, reject) => {
140+
logger.info(`Fetching image info for ${filename}`);
141+
client.getImageInfo(filename, (err, data) => {
142+
if (err) {
143+
reject(new Error(err));
144+
}
145+
resolve(data);
146+
});
147+
});
148+
149+
const getGetImagesFromArticle = (logger) => (client) => (title) => new Promise((resolve, reject) => {
150+
logger.info(`Fetching images forpage ${title}`);
151+
client.getImagesFromArticle(title, (err, data) => {
152+
if (err) {
153+
reject(new Error(err));
154+
}
155+
resolve(data);
156+
});
157+
});
158+
127159
const getLogIn = (logger) => (client) => async () => new Promise((resolve, reject) => {
128160
if (!process.env.WIKIMEDIA_SECRET) {
129161
reject(new Error('No credentials to log in with'));
@@ -350,12 +382,15 @@ module.exports = {
350382
getClient,
351383
getFetchDoc,
352384
getGetMigratedPageIds,
385+
getGetImagesFromArticle,
386+
getGetImageInfo,
353387
getGetPagesTranscluding,
354388
getLogIn,
355389
getLogger,
356390
getMigrationPagePath,
357391
getObsoletePagePath,
358392
getNormalizedPath,
393+
getGetPagesByPrefix,
359394
getUpdateMigratedPages,
360395
getUpdateMigratedPagesProtection,
361396
guessSlug,

0 commit comments

Comments
 (0)