forked from WikiToLearn/Wiki2Latex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
w2lLaTeXCompiler.php
executable file
·195 lines (159 loc) · 4.65 KB
/
w2lLaTeXCompiler.php
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
<?php
/*
* File: w2lLaTeXCompiler.php
*
* Purpose:
* Provides the commandLine-interface to LaTeX
*
* License:
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
if ( !defined('MEDIAWIKI') ) {
$msg = 'To install Wiki2LaTeX, put the following line in LocalSettings.php:<br/>';
$msg .= '<tt>require_once( $IP."/extensions/path_to_Wiki2LaTeX_files/wiki2latex.php" );</tt>';
echo $msg;
exit( 1 );
}
class Wiki2LaTeXCompiler {
function __construct($piece, $mkdir) {
global $w2lConfig;
$this->files = array();
$this->command = $w2lConfig['ltx_command'];
$this->sort = $w2lConfig['ltx_sort'];
$this->bibtex = $w2lConfig['ltx_bibtex'];
$this->repeat = $w2lConfig['ltx_repeat'];
$this->piece = $piece;//name of the working (temporary) directory
$this->path = ''; //path of the working directory
$this->mkdir = $mkdir;
$this->debug = false;
$this->log = '';
return true;
}
function addFiles($files) {
$this->files = $files;
}
function generateFiles($tpl_vars) {
global $wgOut;
$msg = '';
$tempdir = wfTempDir(); // temporary dir
$tempdir .= DIRECTORY_SEPARATOR.'w2ltmp-'.$this->piece;
$this->path = $tempdir;
//check whatever needed to work in the temp directory
if ( true == $this->mkdir ) {
if ( !@mkdir($this->path) ) {
$wgOut->addHTML( wfMsg('w2l_temp_dir_missing') );
return false;
}
if ( !file_exists($this->path) OR !is_dir($this->path) OR !is_writable($this->path) ) {
$wgOut->addHTML( wfMsg('w2l_temp_dir_missing') );
return false;
}
$chmod = chmod($this->path, 0777);
}
$cur_dir = getcwd();
chdir($tempdir);
/**
* @todo Add multilanguage support for the next strings => DONE
*/
foreach( $this->files as $file_name => $file_content) {
$file_content = str_replace(array_keys($tpl_vars), array_values($tpl_vars), $file_content);
$failure = file_put_contents($file_name, $file_content);
if ( $failure !== false ) {
$msg .= wfMsg('w2l_creating_file_success', $file_name,$failure);
//$msg .= 'Creating file '.$file_name.' with '..' Bytes'."\n"; //migliorare supporto linguistico qui
} else {
$msg .= wfMsg('w2l_creating_file_error', $file_name,$failure);
//$msg .= 'Error creating file '.$file_name."\n";
}
chmod($file_name, 0666);
}
chdir($cur_dir);
$this->msg = $msg;
return true;
}
function runLaTeX( $file = 'Main', $sort = false, $bibtex = false ) {
//complete the shell command
$command = str_replace('%file%', $file, $this->command);
$cur_dir = getcwd();
chdir($this->path);
$go = true;
$i = 1;
//message with operation returning status
$msg = $this->msg;
$msg .= wfMsg('w2l_compile_command', $command )."\n";
$msg .= wfMsg('w2l_temppath', $this->path )."\n";
if ( $this->debug == true ) {
$msg .= "User: ".wfShellExec("whoami");
}
while ( (true == $go ) OR ( $i > 5 ) ) {
$msg .= "\n".wfMsg('w2l_compile_run', $i)."\n";
$msg .= wfShellExec($command);
if ( !file_exists( $file.'.pdf' ) ) {
$msg .= wfMsg('w2l_pdf_not_created')."\n";
$compile_error = true;
$go = false;
} else {
$compile_error = false;
if ( true == $sort ) {
// sort it, baby
$msg .= '===Sort-Result==='."\n";
$msg .= $this->sortIndexFile($file);
$msg .= "\n";
}
/*
if ( true == $bibtex ) {
// run bibtex
$msg .= '===BibTeX-Result==='."\n";
$msg .= $this->doBibTex($file);
$msg .= "\n";
}
*/
if ( $this->repeat == $i ) {
$go = false;
}
++$i;
}
}
// Now chmod-ing some files
$mod = 0666;
if ( is_dir($this->path) ) {
$directory = dir($this->path);
while ( false !== ($tmp_file = $directory->read()) ) {
if ( is_file($tmp_file) ) {
$res = chmod($tmp_file, $mod);
}
}
$directory->close();
}
$this->log = file_get_contents($file.'.log');
chdir($cur_dir);
$this->msg = $msg;
return $compile_error;
}
function sortIndexFile($file = 'Main') {
$command = str_replace('%file%', $file, $this->sort);
$msg = 'Command: '.$command;
$msg .= wfShellExec($command);
return $msg;
}
function doBibTex($file = 'Main') {
// Disabling this for now.
return 'unsupported';
$command = str_replace('%file%', $file, $this->bibtex);
$msg = 'Command: '.$command;
$msg .= wfShellExec($command);
return $msg;
}
function getLog() {
return $this->getCompileLog();
}
function getCompileLog() {
return $this->msg;
}
function getLogFile() {
return $this->log;
}
}