Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | 15x 15x 15x 15x 15x 15x 15x 15x 6x 6x 6x 6x 6x 6x 15x 6x 6x 6x 6x 6x 6x 15x 9x 9x 9x 9x 15x 84x 84x 84x 78x 6x 6x 6x 6x 6x 3x 6x 6x 15x 1x 1x 1x 1x 1x 6x 1x 1x 1x 15x 3x 3x 3x 3x 3x 3x 3x 15x | module.exports = function(crowi) { const debug = require('debug')('crowi:models:backlink') const mongoose = require('mongoose') const ObjectId = mongoose.Schema.Types.ObjectId var backlinkSchema backlinkSchema = new mongoose.Schema({ page: { type: ObjectId, ref: 'Page', index: true }, fromPage: { type: ObjectId, ref: 'Page' }, fromRevision: { type: ObjectId, ref: 'Revision' }, updatedAt: { type: Date, default: Date.now, index: true }, }) backlinkSchema.statics.findByPageId = function(pageId, limit, offset) { var Backlink = this limit = limit || 10 offset = offset || 0 limit = parseInt(limit, 10) offset = parseInt(offset, 10) return new Promise((resolve, reject) => { var conditions = { page: pageId, } var projection = { fromPage: 1, fromRevision: 1, updatedAt: 1, } var options = { limit: limit, skip: offset, sort: { updatedAt: -1 }, } Backlink.find(conditions, projection, options) .populate('fromPage') .populate('fromRevision') .exec((err, backlinks) => { if (err) { return reject(err) } // populate author var options = { path: 'fromRevision.author', model: 'User', select: { username: 1, name: 1, image: 1, }, } Backlink.populate(backlinks, options, (err, backlinks) => { if (err) { return reject(err) } return resolve(backlinks) }) }) }) } backlinkSchema.statics.removeByPageId = function(pageId) { const Backlink = this return Backlink.remove({ fromPage: pageId }) } backlinkSchema.statics.removeBySavedPage = function(savedPage) { var Backlink = this return new Promise((resolve, reject) => { var conditions = { fromPage: savedPage._id, } Backlink.remove(conditions, (err, data) => { Iif (err) { return reject(err) } return resolve(data) }) }) } backlinkSchema.statics.createByParameters = function(parameters) { var Backlink = this return new Promise((resolve, reject) => { var data = { page: parameters.page, fromPage: parameters.fromPage, fromRevision: parameters.fromRevision, updatedAt: Date.now(), } Backlink.create(data, (err, savedBacklink) => { Iif (err) { return reject(err) } return resolve(savedBacklink) }) }) } const convertLinksToPageIds = async (page, { paths, objectIds }) => { const Page = crowi.model('Page') let ids = await Promise.all([...paths.map(path => Page.isExistByPath(path)), ...objectIds.map(id => Page.isExistById(id))]) // Make unique and remove own page ids = ids.filter((id, index, array) => array.indexOf(id) === index && id.toString() !== page._id.toString() && id !== false) return ids } backlinkSchema.statics.createBySavedPage = async function(savedPage) { const Backlink = this const linkDetector = require('../util/linkDetector')(crowi) if (!(savedPage.revision && savedPage.revision.body)) { throw new Error('no revision/body in savedPage') } const body = savedPage.revision.body await Backlink.removeBySavedPage(savedPage) const links = linkDetector.search(body) const ids = await convertLinksToPageIds(savedPage, links) return Promise.all( ids.map(id => Backlink.createByParameters({ page: id, fromPage: savedPage._id, fromRevision: savedPage.revision._id, }), ), ) .then(backlinks => { debug('All backlinks saved') return backlinks }) .catch(err => { throw err }) } backlinkSchema.statics.createByAllPages = async function() { const Backlink = this const Page = crowi.model('Page') const Revision = crowi.model('Revision') const linkDetector = require('../util/linkDetector')(crowi) const pages = await Page.find({}).select('_id revision') const latestRevisionIds = pages.map(({ revision }) => revision) const revisions = await Revision.find({ _id: { $in: latestRevisionIds } }).and({ $or: [{ body: linkDetector.getLinkRegexp() }, { body: linkDetector.getPathRegexps()[0] }, { body: linkDetector.getPathRegexps()[1] }], }) await Backlink.remove({}) return Promise.all( revisions.map(async ({ _id: revisionId, body }) => { const page = pages.find(({ revision }) => revision.toString() === revisionId.toString()) const pageId = page._id const links = linkDetector.search(body) const ids = await convertLinksToPageIds(page, links) return Promise.all( ids.map(id => Backlink.createByParameters({ page: id, fromPage: pageId, fromRevision: revisionId, }), ), ) .then(backlinks => { debug('All backlinks saved') return backlinks }) .catch(err => { throw err }) }), ) } return mongoose.model('Backlink', backlinkSchema) } |