| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.wiki.template.internal.migration; |
| 21 |
|
|
| 22 |
|
import java.util.List; |
| 23 |
|
|
| 24 |
|
import javax.inject.Inject; |
| 25 |
|
import javax.inject.Named; |
| 26 |
|
import javax.inject.Singleton; |
| 27 |
|
|
| 28 |
|
import org.xwiki.component.annotation.Component; |
| 29 |
|
import org.xwiki.model.reference.DocumentReference; |
| 30 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
| 31 |
|
import org.xwiki.query.Query; |
| 32 |
|
import org.xwiki.query.QueryException; |
| 33 |
|
import org.xwiki.query.QueryManager; |
| 34 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
| 35 |
|
import org.xwiki.wiki.template.internal.WikiTemplateClassDocumentInitializer; |
| 36 |
|
|
| 37 |
|
import com.xpn.xwiki.XWiki; |
| 38 |
|
import com.xpn.xwiki.XWikiContext; |
| 39 |
|
import com.xpn.xwiki.XWikiException; |
| 40 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 41 |
|
import com.xpn.xwiki.objects.BaseObject; |
| 42 |
|
import com.xpn.xwiki.store.migration.DataMigrationException; |
| 43 |
|
import com.xpn.xwiki.store.migration.XWikiDBVersion; |
| 44 |
|
import com.xpn.xwiki.store.migration.hibernate.AbstractHibernateDataMigration; |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
@since |
| 50 |
|
@version |
| 51 |
|
|
| 52 |
|
@Component |
| 53 |
|
@Named("R54000WikiTemplateMigration") |
| 54 |
|
@Singleton |
| |
|
| 85.7% |
Uncovered Elements: 4 (28) |
Complexity: 6 |
Complexity Density: 0.25 |
|
| 55 |
|
public class WikiTemplateMigration extends AbstractHibernateDataMigration |
| 56 |
|
{ |
| 57 |
|
private static final String OLD_TEMPLATE_PROPERTY = "iswikitemplate"; |
| 58 |
|
|
| 59 |
|
@Inject |
| 60 |
|
private WikiDescriptorManager wikiDescriptorManager; |
| 61 |
|
|
| 62 |
|
@Inject |
| 63 |
|
private QueryManager queryManager; |
| 64 |
|
|
| 65 |
|
@Inject |
| 66 |
|
private DocumentReferenceResolver<String> documentReferenceResolver; |
| 67 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 68 |
0 |
@Override... |
| 69 |
|
public String getDescription() |
| 70 |
|
{ |
| 71 |
0 |
return "http://jira.xwiki.org/browse/XWIKI-9934"; |
| 72 |
|
} |
| 73 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 74 |
3 |
@Override... |
| 75 |
|
public XWikiDBVersion getVersion() |
| 76 |
|
{ |
| 77 |
3 |
return new XWikiDBVersion(54000); |
| 78 |
|
} |
| 79 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 80 |
2 |
@Override... |
| 81 |
|
public boolean shouldExecute(XWikiDBVersion startupVersion) |
| 82 |
|
{ |
| 83 |
|
|
| 84 |
2 |
return wikiDescriptorManager.getCurrentWikiId().equals(wikiDescriptorManager.getMainWikiId()); |
| 85 |
|
} |
| 86 |
|
|
| |
|
| 90.5% |
Uncovered Elements: 2 (21) |
Complexity: 3 |
Complexity Density: 0.14 |
|
| 87 |
1 |
@Override... |
| 88 |
|
protected void hibernateMigrate() throws DataMigrationException, XWikiException |
| 89 |
|
{ |
| 90 |
|
|
| 91 |
1 |
XWikiContext context = getXWikiContext(); |
| 92 |
1 |
XWiki xwiki = context.getWiki(); |
| 93 |
|
|
| 94 |
|
|
| 95 |
1 |
DocumentReference templateClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), |
| 96 |
|
WikiTemplateClassDocumentInitializer.DOCUMENT_SPACE, |
| 97 |
|
WikiTemplateClassDocumentInitializer.DOCUMENT_NAME); |
| 98 |
|
|
| 99 |
|
|
| 100 |
1 |
DocumentReference descriptorClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), |
| 101 |
|
XWiki.SYSTEM_SPACE, "XWikiServerClass"); |
| 102 |
|
|
| 103 |
|
|
| 104 |
1 |
DocumentReference superAdmin = new DocumentReference(wikiDescriptorManager.getMainWikiId(), |
| 105 |
|
XWiki.SYSTEM_SPACE, "superadmin"); |
| 106 |
|
|
| 107 |
1 |
try { |
| 108 |
|
|
| 109 |
1 |
String statement = "select distinct doc.fullName " |
| 110 |
|
+ "from Document doc, doc.object(XWiki.XWikiServerClass) as obj"; |
| 111 |
1 |
Query query = queryManager.createQuery(statement, Query.XWQL); |
| 112 |
1 |
List<String> results = query.execute(); |
| 113 |
1 |
for (String wikiPage : results) { |
| 114 |
4 |
XWikiDocument document = xwiki.getDocument(documentReferenceResolver.resolve(wikiPage), context); |
| 115 |
|
|
| 116 |
4 |
BaseObject descriptorObject = document.getXObject(descriptorClassReference); |
| 117 |
4 |
int isTemplate = descriptorObject.getIntValue(OLD_TEMPLATE_PROPERTY, 0); |
| 118 |
|
|
| 119 |
4 |
descriptorObject.removeField(OLD_TEMPLATE_PROPERTY); |
| 120 |
|
|
| 121 |
4 |
BaseObject object = document.getXObject(templateClassReference, true, context); |
| 122 |
|
|
| 123 |
4 |
isTemplate = object.getIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate); |
| 124 |
|
|
| 125 |
4 |
object.setIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate); |
| 126 |
|
|
| 127 |
4 |
document.setAuthorReference(superAdmin); |
| 128 |
|
|
| 129 |
4 |
xwiki.saveDocument(document, "[UPGRADE] Upgrade the template section.", context); |
| 130 |
|
} |
| 131 |
|
} catch (QueryException e) { |
| 132 |
0 |
throw new DataMigrationException("Failed to get the list of all existing descriptors.", e); |
| 133 |
|
} catch (XWikiException e) { |
| 134 |
0 |
throw new DataMigrationException("Failed to upgrade a wiki descriptor.", e); |
| 135 |
|
} |
| 136 |
|
} |
| 137 |
|
} |