From version < 4.1 >
edited by Guido Kracke
on 2019/10/29 17:33
To version < 5.1
edited by Guido Kracke
on 2019/11/28 11:42
<
Change comment: There is no comment for this version

Summary

Details

ExtensionCode.ExtensionClass[0]
Description
... ... @@ -52,7 +52,7 @@
52 52   LaTeX Templates
53 53   </th>
54 54   <td style="border: 1px solid #ddd;padding: 3px">
55 - <select id="page" name="page" onchange="onTemplates(this.value)">
55 + <select id="page" name="page" onchange="onTemplates(this.value)" onload="onTemplates(this.value)">
56 56  """
57 57  if (LATEX != null) {
58 58   out << "<option"
... ... @@ -79,7 +79,7 @@
79 79   Upload from Attachment
80 80   </th>
81 81   <td style="border: 1px solid #ddd;padding: 3px">
82 - <select id="file" name="file" size=1 onchange="onAttachment(this.value)">
82 + <select id="file" name="file" size=1 onchange="onAttachment(this.value)" onload="onAttachment(this.value)">
83 83  """
84 84  if (file == null || file == "") {
85 85   out << "<option selected></option>"
... ... @@ -191,24 +191,27 @@
191 191   ZipInputStream zip = new ZipInputStream(att.getContentInputStream(), CHARSET)
192 192   ZipEntry entry = zip.getNextEntry()
193 193   while (entry != null) {
194 - Object template = templates.get(entry.getName())
195 - if (template == null) {
196 - template = skin.newObject(XCLASS)
197 - template.set("path", entry.getName())
194 + if (!entry.isDirectory()) {
195 + Object template = templates.get(entry.getName())
196 + if (template == null) {
197 + template = skin.newObject(XCLASS)
198 + template.set("path", entry.getName())
199 + }
200 + ByteArrayOutputStream bos = new ByteArrayOutputStream()
201 + byte[] buffer = new byte[2048]
202 + int len = zip.read(buffer)
203 + while (len > 0) {
204 + bos.write(buffer, 0, len)
205 + len = zip.read(buffer)
206 + }
207 + bos.close()
208 + template.set("content", bos.toString(CHARSET_NAME))
198 198   }
199 - ByteArrayOutputStream bos = new ByteArrayOutputStream()
200 - byte[] buffer = new byte[2048]
201 - int len = zip.read(buffer)
202 - while (len > 0) {
203 - bos.write(buffer, 0, len)
204 - len = zip.read(buffer)
205 - }
206 - bos.close()
207 - template.set("content", bos.toString(CHARSET_NAME))
208 208   entry = zip.getNextEntry()
209 209   }
210 210   zip.close()
211 211   skin.save()
214 + response.sendRedirect(xwiki.getURL(doc.getFullName()))
212 212   }
213 213  }
214 214  {{/groovy}}
cogDownload default LaTeX templates; Upload/download LaTeX templates to/from a skin page.
Developed by

Guido Kracke

Rating
Rate!
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

Usage:

  • Paste the following code to an empty page
  • Select LaTeX as LaTeX Templates and press Download LaTeX Templates to download a zip file with the default templates
  • Extract the template files and make you changes
  • Zip the changed files preserving the directory structure
  • Attach the zip file to the page
  • Select, e.g., XWiki.DefaultSkin as skin and the attachment as Upload from Attachment and press Upload LaTeX Templates to upload the changed templates to the skin

If there is an easy way to do the upload without attaching the zip file, I'd like to know.

{{groovy}}
import com.xpn.xwiki.api.Attachment
import com.xpn.xwiki.api.Document
import com.xpn.xwiki.api.Object
import com.xpn.xwiki.web.XWikiServletResponse
import java.nio.charset.Charset
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream
import org.xwiki.extension.InstalledExtension
import org.xwiki.query.Query

String CHARSET_NAME = "UTF-8"
Charset CHARSET = Charset.forName(CHARSET_NAME)
String XCLASS = "XWiki.XWikiSkinFileOverrideClass"
String LATEX = null
InstalledExtension latex = services.extension.installed.getInstalledExtension("org.xwiki.contrib.latex:latex-export", "wiki:xwiki")
if (latex != null) {
    LATEX = "LaTeX " + latex.getId().getVersion().getValue()
}

String page = request.getParameter("page")
String file = request.getParameter("file")
Query query = services.query.hql("""
    select d.fullName
    from XWikiDocument as d, BaseObject as o
    where d.fullName = o.name
    and o.className = :class
"""
)
ArrayList<String> pages = query.bindValue("class", "XWiki.XWikiSkins").execute()
StringBuffer out = new StringBuffer()
out << """
{{html}}

  

    
      
        
          
          
        
        
          
          
        
      
    

            LaTeX Templates
          

            
          

"""
if (file == null || file == "") {
    out << """"""
} else {
    out << """"""
}
out << """
  




{{/html}}

"""

print out.toString()

if (page != null) {
   if (file == "") {
        XWikiServletResponse response = xcontext.getResponse()
        response.setContentType("application/zip")
        response.setHeader("Content-disposition", "attachment; filename=latex.zip")
        ZipOutputStream zip = new ZipOutputStream(response.getOutputStream(), CHARSET)
       if (page == LATEX) {
           for (extension in services.extension.installed.getInstalledExtensions()) {
               if (extension.getId().getId().startsWith("org.xwiki.contrib.latex:")) {
                    ZipInputStream jar = new ZipInputStream(extension.getLocalExtension().getFile().openStream(), CHARSET)
                    ZipEntry entry = jar.getNextEntry()
                   while (entry != null) {
                        String path = entry.getName()
                       if (path.startsWith("templates/latex/default/") && !path.endsWith("/")) {
                            ByteArrayOutputStream bos = new ByteArrayOutputStream()
                           byte[] buffer = new byte[2048]
                           int len = jar.read(buffer)
                           while (len > 0) {
                                bos.write(buffer, 0, len)
                                len = jar.read(buffer)
                           }
                            bos.close()
                            zip.putNextEntry(new ZipEntry(path.replace("templates/latex/default/", "latex/")))
                            buffer = bos.toByteArray()
                            zip.write(buffer, 0, buffer.length)
                            zip.closeEntry()
                       }
                        entry = jar.getNextEntry()
                   }
                    jar.close()
               }
           }
       }
       else {
            Document skin = xwiki.getDocument("xwiki:" + page)
           for (Object o : skin.getObjects(XCLASS)) {
                String path = o.getValue("path")
                String content = o.getValue("content")
               byte[] bytes = content.getBytes(CHARSET_NAME)
                zip.putNextEntry(new ZipEntry(path))
                zip.write(bytes, 0, bytes.length)
                zip.closeEntry()
           }
       }
        zip.close()
        response.flushBuffer()
   }
   else {
        Map<String, Object> templates = new HashMap<>()
        Document skin = xwiki.getDocument("xwiki:" + page)
       for (Object o : skin.getObjects(XCLASS)) {
            templates.put(o.getValue("path"), o)
       }
        Attachment att = doc.getAttachment(file)
        ZipInputStream zip = new ZipInputStream(att.getContentInputStream(), CHARSET)
        ZipEntry entry = zip.getNextEntry()
       while (entry != null) {
            Object template = templates.get(entry.getName())
           if (template == null) {
                template = skin.newObject(XCLASS)
                template.set("path", entry.getName())
           }
            ByteArrayOutputStream bos = new ByteArrayOutputStream()
           byte[] buffer = new byte[2048]
           int len = zip.read(buffer)
           while (len > 0) {
                bos.write(buffer, 0, len)
                len = zip.read(buffer)
           }
            bos.close()
            template.set("content", bos.toString(CHARSET_NAME))
            entry = zip.getNextEntry()
       }
        zip.close()
        skin.save()
   }
}
{{/groovy}}
{{groovy}}
import com.xpn.xwiki.api.Attachment
import com.xpn.xwiki.api.Document
import com.xpn.xwiki.api.Object
import com.xpn.xwiki.web.XWikiServletResponse
import java.nio.charset.Charset
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream
import org.xwiki.extension.InstalledExtension
import org.xwiki.query.Query

String CHARSET_NAME = "UTF-8"
Charset CHARSET = Charset.forName(CHARSET_NAME)
String XCLASS = "XWiki.XWikiSkinFileOverrideClass"
String LATEX = null
InstalledExtension latex = services.extension.installed.getInstalledExtension("org.xwiki.contrib.latex:latex-export", "wiki:xwiki")
if (latex != null) {
    LATEX = "LaTeX " + latex.getId().getVersion().getValue()
}

String page = request.getParameter("page")
String file = request.getParameter("file")
Query query = services.query.hql("""
    select d.fullName
    from XWikiDocument as d, BaseObject as o
    where d.fullName = o.name
    and o.className = :class
"""
)
ArrayList<String> pages = query.bindValue("class", "XWiki.XWikiSkins").execute()
StringBuffer out = new StringBuffer()
out << """
{{html}}

  

    
      
        
          
          
        
        
          
          
        
      
    

            LaTeX Templates
          

            
          

"""
if (file == null || file == "") {
    out << """"""
} else {
    out << """"""
}
out << """
  




{{/html}}

"""

print out.toString()

if (page != null) {
   if (file == "") {
        XWikiServletResponse response = xcontext.getResponse()
        response.setContentType("application/zip")
        response.setHeader("Content-disposition", "attachment; filename=latex.zip")
        ZipOutputStream zip = new ZipOutputStream(response.getOutputStream(), CHARSET)
       if (page == LATEX) {
           for (extension in services.extension.installed.getInstalledExtensions()) {
               if (extension.getId().getId().startsWith("org.xwiki.contrib.latex:")) {
                    ZipInputStream jar = new ZipInputStream(extension.getLocalExtension().getFile().openStream(), CHARSET)
                    ZipEntry entry = jar.getNextEntry()
                   while (entry != null) {
                        String path = entry.getName()
                       if (path.startsWith("templates/latex/default/") && !path.endsWith("/")) {
                            ByteArrayOutputStream bos = new ByteArrayOutputStream()
                           byte[] buffer = new byte[2048]
                           int len = jar.read(buffer)
                           while (len > 0) {
                                bos.write(buffer, 0, len)
                                len = jar.read(buffer)
                           }
                            bos.close()
                            zip.putNextEntry(new ZipEntry(path.replace("templates/latex/default/", "latex/")))
                            buffer = bos.toByteArray()
                            zip.write(buffer, 0, buffer.length)
                            zip.closeEntry()
                       }
                        entry = jar.getNextEntry()
                   }
                    jar.close()
               }
           }
       }
       else {
            Document skin = xwiki.getDocument("xwiki:" + page)
           for (Object o : skin.getObjects(XCLASS)) {
                String path = o.getValue("path")
                String content = o.getValue("content")
               byte[] bytes = content.getBytes(CHARSET_NAME)
                zip.putNextEntry(new ZipEntry(path))
                zip.write(bytes, 0, bytes.length)
                zip.closeEntry()
           }
       }
        zip.close()
        response.flushBuffer()
   }
   else {
        Map<String, Object> templates = new HashMap<>()
        Document skin = xwiki.getDocument("xwiki:" + page)
       for (Object o : skin.getObjects(XCLASS)) {
            templates.put(o.getValue("path"), o)
       }
        Attachment att = doc.getAttachment(file)
        ZipInputStream zip = new ZipInputStream(att.getContentInputStream(), CHARSET)
        ZipEntry entry = zip.getNextEntry()
       while (entry != null) {
           if (!entry.isDirectory()) {
                Object template = templates.get(entry.getName())
               if (template == null) {
                    template = skin.newObject(XCLASS)
                    template.set("path", entry.getName())
               }
                ByteArrayOutputStream bos = new ByteArrayOutputStream()
               byte[] buffer = new byte[2048]
               int len = zip.read(buffer)
               while (len > 0) {
                    bos.write(buffer, 0, len)
                    len = zip.read(buffer)
               }
                bos.close()
                template.set("content", bos.toString(CHARSET_NAME))
           }
            entry = zip.getNextEntry()
       }
        zip.close()
        skin.save()
        response.sendRedirect(xwiki.getURL(doc.getFullName()))
   }
}
{{/groovy}}

Prerequisites & Installation Instructions

Copy the code snippet to a page and save it.

Get Connected