📄 cmsrelationsystemvalidator.java
字号:
org.opencms.workplace.commons.Messages.ERR_PROGRESS_INTERRUPTED_0));
}
thread.setProgress((index * 20 / resources.size()) + 20);
}
CmsResource resource = (CmsResource)validatableResources.get(index);
String resourceName = resource.getRootPath();
if (report != null) {
report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_SUCCESSION_2,
new Integer(index + 1),
new Integer(size)), I_CmsReport.FORMAT_NOTE);
report.print(Messages.get().container(Messages.RPT_HTMLLINK_VALIDATING_0), I_CmsReport.FORMAT_NOTE);
report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
dbc.removeSiteRoot(resourceName)));
report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
}
List brokenLinks = validateLinks(dbc, resource, offlineFilesLookup, project, report);
if (brokenLinks.size() > 0) {
// the resource contains broken links
invalidResources.put(resourceName, brokenLinks);
foundBrokenLinks = true;
} else {
// the resource contains *NO* broken links
if (report != null) {
report.println(
org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
I_CmsReport.FORMAT_OK);
}
}
}
if (foundBrokenLinks) {
// print a summary if we found broken links in the validated resources
if (report != null) {
report.println(
Messages.get().container(Messages.RPT_HTMLLINK_VALIDATOR_ERROR_0),
I_CmsReport.FORMAT_ERROR);
}
}
if (report != null) {
report.println(Messages.get().container(Messages.RPT_HTMLLINK_VALIDATOR_END_0), I_CmsReport.FORMAT_HEADLINE);
}
return invalidResources;
}
/**
* Validates the links for the specified resource.<p>
*
* @param dbc the database context
* @param resource the resource that will be validated
* @param fileLookup a map for faster lookup with all resources keyed by their rootpath
* @param project the project to validate
* @param report the report to write to
*
* @return a list with the broken links as {@link CmsRelation} objects for the specified resource,
* or an empty list if no broken links were found
*/
protected List validateLinks(
CmsDbContext dbc,
CmsResource resource,
Map fileLookup,
CmsProject project,
I_CmsReport report) {
List brokenRelations = new ArrayList();
Map validatedLinks = new HashMap();
// get the relations
List relations = null;
try {
if (!resource.getState().isDeleted()) {
// search the target of links in the current (offline) project
relations = m_driverManager.getRelationsForResource(dbc, resource, CmsRelationFilter.TARGETS);
} else {
// search the source of links in the online project
CmsProject currentProject = dbc.currentProject();
dbc.getRequestContext().setCurrentProject(project);
try {
relations = m_driverManager.getRelationsForResource(dbc, resource, CmsRelationFilter.SOURCES);
} finally {
dbc.getRequestContext().setCurrentProject(currentProject);
}
}
} catch (CmsException e) {
LOG.error(Messages.get().getBundle().key(Messages.LOG_LINK_SEARCH_1, resource), e);
if (report != null) {
report.println(Messages.get().container(
Messages.LOG_LINK_SEARCH_1,
dbc.removeSiteRoot(resource.getRootPath())), I_CmsReport.FORMAT_ERROR);
}
return brokenRelations;
}
// check the relations
boolean first = true;
Iterator itRelations = relations.iterator();
while (itRelations.hasNext()) {
CmsRelation relation = (CmsRelation)itRelations.next();
String link;
if (!resource.getState().isDeleted()) {
link = relation.getTargetPath();
} else {
link = relation.getSourcePath();
}
boolean isValidLink = true;
if (CmsStringUtil.isEmptyOrWhitespaceOnly(link)) {
// skip empty links
continue;
}
if (validatedLinks.keySet().contains(link)) {
// skip already validated links
if (((Boolean)validatedLinks.get(link)).booleanValue()) {
// add broken relation of different type
brokenRelations.add(relation);
}
continue;
}
// the link is valid...
try {
// ... if the linked resource exists in the online project
if (!resource.getState().isDeleted()) {
// search the target of link in the online project
try {
link = m_driverManager.getVfsDriver().readResource(
new CmsDbContext(),
project.getUuid(),
relation.getTargetId(),
true).getRootPath();
} catch (CmsVfsResourceNotFoundException e) {
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_LINK_VALIDATION_READBYID_FAILED_2,
relation.getTargetId().toString(),
project.getName()), e);
}
m_driverManager.getVfsDriver().readResource(
new CmsDbContext(),
project.getUuid(),
relation.getTargetPath(),
true);
}
} else {
// since we are going to delete the resource
// check if the linked resource is also to be deleted
isValidLink = false;
if (fileLookup.containsKey(link)) {
CmsResource offlineResource = (CmsResource)fileLookup.get(link);
if (offlineResource.getState().isDeleted()) {
isValidLink = true;
}
}
}
} catch (CmsException e) {
// ... or if the linked resource is a resource that gets actually published
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_LINK_VALIDATION_READBYPATH_FAILED_2,
relation.getTargetPath(),
project.getName()), e);
}
if (!fileLookup.containsKey(link)) {
isValidLink = false;
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_LINK_VALIDATION_RESOURCENOTINLOOKUP_1,
link));
}
}
} finally {
// ... and if the linked resource to be published get deleted
if (!resource.getState().isDeleted() && fileLookup.containsKey(link)) {
CmsResource offlineResource = (CmsResource)fileLookup.get(link);
if (offlineResource.getState().isDeleted()) {
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_LINK_VALIDATION_RESOURCEDELETED_1,
link));
}
isValidLink = false;
}
}
}
if (!isValidLink) {
if (first) {
if (report != null) {
report.println(
Messages.get().container(Messages.RPT_HTMLLINK_FOUND_BROKEN_LINKS_0),
I_CmsReport.FORMAT_WARNING);
}
first = false;
}
brokenRelations.add(relation);
if (report != null) {
if (!resource.getState().isDeleted()) {
report.println(Messages.get().container(
Messages.RPT_HTMLLINK_BROKEN_TARGET_2,
relation.getSourcePath(),
dbc.removeSiteRoot(link)), I_CmsReport.FORMAT_WARNING);
} else {
report.println(Messages.get().container(
Messages.RPT_HTMLLINK_BROKEN_SOURCE_2,
dbc.removeSiteRoot(link),
relation.getTargetPath()), I_CmsReport.FORMAT_WARNING);
}
}
}
validatedLinks.put(link, Boolean.valueOf(!isValidLink));
}
return brokenRelations;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -