private Locale getLocale(EntityReference reference) { if (reference instanceof DocumentReference) { return ((DocumentReference) reference).getLocale(); } else if (reference instanceof LocalDocumentReference) { return ((LocalDocumentReference) reference).getLocale(); } return null; }
protected File getDocumentDir(final DocumentReference docRef) { final File path = new File(getPre11StoreRootDirectory(), this.pathSerializer.serialize(docRef, false)); File docDir = new File(path, THIS_DIR_NAME); // Add the locale Locale docLocale = docRef.getLocale(); if (docLocale != null) { final File docLocalesDir = new File(docDir, FilesystemStoreTools.DOCUMENT_LOCALES_DIR_NAME); final File docLocaleDir = new File(docLocalesDir, docLocale.equals(Locale.ROOT) ? DOCUMENT_LOCALE_ROOT_NAME : docLocale.toString()); docDir = new File(docLocaleDir, THIS_DIR_NAME); } return docDir; }
@Override public boolean copy(DocumentReference source, DocumentReference destination) { XWikiContext xcontext = this.xcontextProvider.get(); try { String language = source.getLocale() != null ? source.getLocale().toString() : null; boolean result = xcontext.getWiki().copyDocument(source, destination, language, false, true, true, xcontext); if (result) { this.logger.info("Document [{}] has been copied to [{}].", source, destination); } else { this.logger.warn( "Cannot fully copy [{}] to [{}] because an orphan translation" + " exists at the destination.", source, destination); } return result; } catch (Exception e) { this.logger.error("Failed to copy [{}] to [{}].", source, destination, e); return false; } }
protected File getDocumentContentDir(final DocumentReference documentReference) { File documentDir = new File(getPre11StoreRootDirectory(), this.fileEntitySerializer.serialize(documentReference, true)); File documentContentDir = new File(documentDir, THIS_DIR_NAME); // Add the locale Locale documentLocale = documentReference.getLocale(); if (documentLocale != null) { final File documentLocalesDir = new File(documentContentDir, FilesystemStoreTools.DOCUMENT_LOCALES_DIR_NAME); final File documentLocaleDir = new File(documentLocalesDir, documentLocale.equals(Locale.ROOT) ? DOCUMENT_LOCALE_ROOT_NAME : documentLocale.toString()); documentContentDir = new File(documentLocaleDir, THIS_DIR_NAME); } return documentContentDir; }
@Override protected void serializeEntityReference(EntityReference currentReference, StringBuilder representation, boolean isLastReference, Object... parameters) { super.serializeEntityReference(currentReference, representation, isLastReference, parameters); // Append parameters for DocumentReference (if any) if (currentReference instanceof DocumentReference) { DocumentReference documentReference = (DocumentReference) currentReference; if (documentReference.getLocale() != null) { representation.append('(').append(documentReference.getLocale()).append(')'); } } else if (currentReference instanceof LocalDocumentReference) { LocalDocumentReference documentReference = (LocalDocumentReference) currentReference; if (documentReference.getLocale() != null) { representation.append('(').append(documentReference.getLocale()).append(')'); } } } }
@Override public int compare(DocumentReference alice, DocumentReference bob) { Iterator<EntityReference> aliceIterator = alice.getReversedReferenceChain().iterator(); Iterator<EntityReference> bobIterator = bob.getReversedReferenceChain().iterator(); // The number of components in a document reference can vary (e.g. for nested pages). while (aliceIterator.hasNext() && bobIterator.hasNext()) { int diff = aliceIterator.next().getName().compareTo(bobIterator.next().getName()); if (diff != 0) { return diff; } } if (aliceIterator.hasNext()) { // Alice's path is longer. return 1; } else if (bobIterator.hasNext()) { // Bob's path is longer. return -1; } // Both references have the same number of components and they all match. Use the locale as a tie-breaker. String aliceLocale = alice.getLocale() != null ? alice.getLocale().toString() : ""; String bobLocale = bob.getLocale() != null ? bob.getLocale().toString() : ""; return aliceLocale.compareTo(bobLocale); } }
/** * @param documentReference reference to the document. * @return the locale code of the referenced document. * @throws SolrIndexerException if problems occur. */ protected Locale getLocale(DocumentReference documentReference) throws SolrIndexerException { Locale locale = null; try { if (documentReference.getLocale() != null && !documentReference.getLocale().equals(Locale.ROOT)) { locale = documentReference.getLocale(); } else { XWikiContext xcontext = this.xcontextProvider.get(); locale = xcontext.getWiki().getDocument(documentReference, xcontext).getRealLocale(); } } catch (Exception e) { throw new SolrIndexerException( String.format("Exception while fetching the locale of the document '%s'", documentReference), e); } return locale; }
/** * Mark an entity as containing an used XClass and specify which objects is using it. * * @param entitySelection The entity containing an used XClass * @param documentObjectName The name of the page containing the objects for this XClass */ public void markImpactedObject(EntitySelection entitySelection, EntityReference documentObjectName) { DocumentReference xclassDocReference = (DocumentReference) entitySelection.getEntityReference(); // we don't want the locale for the XClass reference if (xclassDocReference.getLocale() != null) { xclassDocReference = new DocumentReference(xclassDocReference, (Locale) null); } if (!this.impactedObjects.containsKey(xclassDocReference)) { this.impactedObjects.put(xclassDocReference, new LinkedHashSet<>()); this.xclassPages.add(entitySelection); } this.impactedObjects.get(xclassDocReference).add(documentObjectName); }
/** * @param documentReference reference to the document. * @return the locale code of the referenced document. * @throws SolrIndexerException if problems occur. */ protected Locale getLocale(DocumentReference documentReference) throws SolrIndexerException { Locale locale = null; try { if (documentReference.getLocale() != null && !documentReference.getLocale().equals(Locale.ROOT)) { locale = documentReference.getLocale(); } else { XWikiContext xcontext = this.xcontextProvider.get(); locale = xcontext.getWiki().getDocument(documentReference, xcontext).getRealLocale(); } } catch (Exception e) { throw new SolrIndexerException(String.format("Exception while fetching the locale of the document '%s'", documentReference), e); } return locale; }
/** * Get the directory associated with this document. * * @param documentReference the DocumentReference for the document to get the directory for. * @return a file path corresponding to the attachment location; each segment in the path is URL-encoded in order to * be safe. * @since 11.0 */ public File getDocumentContentDir(final DocumentReference documentReference) { File wikiDir = getWikiDir(documentReference.getWikiReference().getName()); String localKey = LocalUidStringEntityReferenceSerializer.INSTANCE.serialize(documentReference.getLocale() != null ? new DocumentReference(documentReference, (Locale) null) : documentReference); String md5 = DigestUtils.md5Hex(localKey); // Avoid having too many files in one folder because some filesystems don't perform well with large numbers of // files in one folder File documentDir1 = new File(wikiDir, String.valueOf(md5.charAt(0))); File documentDir2 = new File(documentDir1, String.valueOf(md5.charAt(1))); File documentDirFinal = new File(documentDir2, String.valueOf(md5.substring(2))); // Add the locale (if any) Locale documentLocale = documentReference.getLocale(); if (documentLocale != null && !documentLocale.equals(Locale.ROOT)) { File documentLocalesDir = new File(documentDirFinal, DOCUMENT_LOCALES_DIR_NAME); documentDirFinal = new File(documentLocalesDir, documentLocale.toString()); } return documentDirFinal; }
Locale locale = ((DocumentReference) currentReference).getLocale(); if (locale != null) { String localeString = locale.toString();
/** * Utility method to retrieve the default translation of a document using its document reference. * * @param documentReference reference to a document * @return the original {@link XWikiDocument} instance referenced (the default translation) * @throws XWikiException if problems occur */ protected XWikiDocument getDocument(DocumentReference documentReference) throws XWikiException { XWikiContext xcontext = this.xcontextProvider.get(); DocumentReference documentReferenceWithoutLocale = documentReference.getLocale() == null ? documentReference : new DocumentReference(documentReference, (Locale) null); XWikiDocument document = xcontext.getWiki().getDocument(documentReferenceWithoutLocale, xcontext); return document; }
/** * Fetch translated document. * * @param documentReference reference to the document to be translated. * @return translated document. * @throws SolrIndexerException if problems occur. */ protected XWikiDocument getTranslatedDocument(DocumentReference documentReference) throws SolrIndexerException { try { XWikiDocument originalDocument = getDocument(documentReference); Locale locale = documentReference.getLocale(); if (locale == null || locale.equals(Locale.ROOT)) { return originalDocument; } XWikiDocument translatedDocument = originalDocument.getTranslatedDocument(locale, this.xcontextProvider.get()); // XWikiDocument#getTranslatedDocument returns the default document when the locale does not exist if (translatedDocument.getRealLocale().equals(locale)) { return translatedDocument; } } catch (Exception e) { throw new SolrIndexerException( String.format("Failed to get translated document for '%s'", documentReference), e); } return null; }