1
|
1 |
<?php
|
|
2 |
// vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
|
|
3 |
/**
|
|
4 |
* Mediawiki: Parses for links to (inter)wiki pages or images.
|
|
5 |
*
|
|
6 |
* Text_Wiki rule parser to find links, it groups the 3 rules:
|
|
7 |
* # Wikilink: links to internal Wiki pages
|
|
8 |
* # Interwiki: links to external Wiki pages (sister projects, interlangage)
|
|
9 |
* # Image: Images
|
|
10 |
* as defined by text surrounded by double brackets [[]]
|
|
11 |
* Translated are the link itself, the section (anchor) and alternate text
|
|
12 |
*
|
|
13 |
* PHP versions 4 and 5
|
|
14 |
*
|
|
15 |
* @category Text
|
|
16 |
* @package Text_Wiki
|
|
17 |
* @author Bertrand Gugger <bertrand@toggg.com>
|
|
18 |
* @author Paul M. Jones <pmjones@php.net>
|
|
19 |
* @copyright 2005 bertrand Gugger
|
|
20 |
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
|
21 |
* @version CVS: $Id: Wikilink.php,v 1.7 2006/02/25 13:34:50 toggg Exp $
|
|
22 |
* @link http://pear.php.net/package/Text_Wiki
|
|
23 |
*/
|
|
24 |
|
|
25 |
/**
|
|
26 |
* Wikilink, Interwiki and Image rules parser class for Mediawiki.
|
|
27 |
* This class implements a Text_Wiki_Parse to find links marked
|
|
28 |
* in source by text surrounded by 2 opening/closing brackets as
|
|
29 |
* [[Wiki page name#Section|Alternate text]]
|
|
30 |
* On parsing, the link is replaced with a token.
|
|
31 |
*
|
|
32 |
* @category Text
|
|
33 |
* @package Text_Wiki
|
|
34 |
* @author Bertrand Gugger <bertrand@toggg.com>
|
|
35 |
* @copyright 2005 bertrand Gugger
|
|
36 |
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
|
37 |
* @version Release: @package_version@
|
|
38 |
* @link http://pear.php.net/package/Text_Wiki
|
|
39 |
* @see Text_Wiki_Parse::Text_Wiki_Parse()
|
|
40 |
*/
|
|
41 |
class Text_Wiki_Parse_Wikilink extends Text_Wiki_Parse {
|
|
42 |
|
|
43 |
/**
|
|
44 |
* Configuration for this rule (Wikilink)
|
|
45 |
*
|
|
46 |
* @access public
|
|
47 |
* @var array
|
|
48 |
*/
|
|
49 |
var $conf = array(
|
|
50 |
'spaceUnderscore' => true,
|
|
51 |
'project' => array('demo', 'd'),
|
|
52 |
'url' => 'http://example.com/en/page=%s',
|
|
53 |
'langage' => 'en'
|
|
54 |
);
|
|
55 |
|
|
56 |
/**
|
|
57 |
* Configuration for the Image rule
|
|
58 |
*
|
|
59 |
* @access public
|
|
60 |
* @var array
|
|
61 |
*/
|
|
62 |
var $imageConf = array(
|
|
63 |
'prefix' => array('Image', 'image')
|
|
64 |
);
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Configuration for the Interwiki rule
|
|
68 |
*
|
|
69 |
* @access public
|
|
70 |
* @var array
|
|
71 |
*/
|
|
72 |
var $interwikiConf = array(
|
|
73 |
'sites' => array(
|
|
74 |
'manual' => 'http://www.php.net/manual/en/%s',
|
|
75 |
'pear' => 'http://pear.php.net/package/%s',
|
|
76 |
'bugs' => 'http://pear.php.net/package/%s/bugs'
|
|
77 |
),
|
|
78 |
'interlangage' => array('en', 'de', 'fr')
|
|
79 |
);
|
|
80 |
|
|
81 |
/**
|
|
82 |
* The regular expression used to parse the source text and find
|
|
83 |
* matches conforming to this rule. Used by the parse() method.
|
|
84 |
*
|
|
85 |
* @access public
|
|
86 |
* @var string
|
|
87 |
* @see Text_Wiki_Parse::parse()
|
|
88 |
*/
|
|
89 |
var $regex = '/(?<!\[)\[\[(?!\[)\s*(:?)((?:[^:]+:)+)?([^:]+)(?:#(.*))?\s*(?:\|(((?R))|.*))?]]/msU';
|
|
90 |
|
|
91 |
/**
|
|
92 |
* Constructor.
|
|
93 |
* We override the constructor to get Image and Interwiki config
|
|
94 |
*
|
|
95 |
* @param object &$obj the base conversion handler
|
|
96 |
* @return The parser object
|
|
97 |
* @access public
|
|
98 |
*/
|
|
99 |
function Text_Wiki_Parse_Wikilink(&$obj)
|
|
100 |
{
|
|
101 |
$default = $this->conf;
|
|
102 |
parent::Text_Wiki_Parse($obj);
|
35
|
103 |
|
|
104 |
global $paths;
|
|
105 |
$this->imageConf = array(
|
|
106 |
'prefix' => array(':' . $paths->nslist['File'])
|
|
107 |
);
|
1
|
108 |
|
|
109 |
// override config options for image if specified
|
|
110 |
if (in_array('Image', $this->wiki->disable)) {
|
|
111 |
$this->imageConf['prefix'] = array();
|
|
112 |
} else {
|
|
113 |
if (isset($this->wiki->parseConf['Image']) &&
|
|
114 |
is_array($this->wiki->parseConf['Image'])) {
|
|
115 |
$this->imageConf = array_merge(
|
|
116 |
$this->imageConf,
|
|
117 |
$this->wiki->parseConf['Image']
|
|
118 |
);
|
|
119 |
}
|
|
120 |
}
|
|
121 |
|
|
122 |
// override config options for interwiki if specified
|
|
123 |
if (in_array('Interwiki', $this->wiki->disable)) {
|
|
124 |
$this->interwikiConf['sites'] = array();
|
|
125 |
$this->interwikiConf['interlangage'] = array();
|
|
126 |
} else {
|
|
127 |
if (isset($this->wiki->parseConf['Interwiki']) &&
|
|
128 |
is_array($this->wiki->parseConf['Interwiki'])) {
|
|
129 |
$this->interwikiConf = array_merge(
|
|
130 |
$this->interwikiConf,
|
|
131 |
$this->wiki->parseConf['Interwiki']
|
|
132 |
);
|
|
133 |
}
|
|
134 |
if (empty($this->conf['langage'])) {
|
|
135 |
$this->interwikiConf['interlangage'] = array();
|
|
136 |
}
|
|
137 |
}
|
|
138 |
// convert the list of recognized schemes to a regex OR,
|
|
139 |
/* $schemes = $this->getConf('schemes', $default['schemes']);
|
|
140 |
$this->url = str_replace( '#delim#', $this->wiki->delim,
|
|
141 |
'#(?:' . (is_array($schemes) ? implode('|', $schemes) : $schemes) . ')://'
|
|
142 |
. $this->getConf('host_regexp', $default['host_regexp'])
|
|
143 |
. $this->getConf('path_regexp', $default['path_regexp']) .'#'); */
|
|
144 |
}
|
|
145 |
|
|
146 |
/**
|
|
147 |
* Generates a replacement for the matched text. Token options are:
|
|
148 |
* - 'page' => the name of the target wiki page
|
|
149 |
* -'anchor' => the optional section in it
|
|
150 |
* - 'text' => the optional alternate link text
|
|
151 |
*
|
|
152 |
* @access public
|
|
153 |
* @param array &$matches The array of matches from parse().
|
|
154 |
* @return string token to be used as replacement
|
|
155 |
*/
|
|
156 |
function process(&$matches)
|
|
157 |
{
|
|
158 |
// Starting colon ?
|
|
159 |
$colon = !empty($matches[1]);
|
|
160 |
$auto = $interlang = $interwiki = $image = $site = '';
|
|
161 |
// Prefix ?
|
|
162 |
if (!empty($matches[2])) {
|
|
163 |
$prefix = explode(':', substr($matches[2], 0, -1));
|
|
164 |
$count = count($prefix);
|
|
165 |
$i = -1;
|
|
166 |
// Autolink
|
|
167 |
if (isset($this->conf['project']) &&
|
|
168 |
in_array(trim($prefix[0]), $this->conf['project'])) {
|
|
169 |
$auto = trim($prefix[0]);
|
|
170 |
unset($prefix[0]);
|
|
171 |
$i = 0;
|
|
172 |
}
|
|
173 |
while (++$i < $count) {
|
|
174 |
$prefix[$i] = trim($prefix[$i]);
|
|
175 |
// interlangage
|
|
176 |
if (!$interlang &&
|
|
177 |
in_array($prefix[$i], $this->interwikiConf['interlangage'])) {
|
|
178 |
$interlang = $prefix[$i];
|
|
179 |
unset($prefix[$i]);
|
|
180 |
continue;
|
|
181 |
}
|
|
182 |
// image
|
|
183 |
if (!$image && in_array($prefix[$i], $this->imageConf['prefix'])) {
|
|
184 |
$image = $prefix[$i];
|
|
185 |
unset($prefix[$i]);
|
|
186 |
break;
|
|
187 |
}
|
|
188 |
// interwiki
|
|
189 |
if (isset($this->interwikiConf['sites'][$prefix[$i]])) {
|
|
190 |
$interwiki = $this->interwikiConf['sites'][$prefix[$i]];
|
|
191 |
$site = $prefix[$i];
|
|
192 |
unset($prefix[$i]);
|
|
193 |
}
|
|
194 |
break;
|
|
195 |
}
|
|
196 |
if ($prefix) {
|
|
197 |
$matches[3] = implode(':', $prefix) . ':' . $matches[3];
|
|
198 |
}
|
|
199 |
}
|
|
200 |
$text = empty($matches[5]) ? $matches[3] : $matches[5];
|
|
201 |
$matches[3] = trim($matches[3]);
|
|
202 |
$matches[4] = empty($matches[4]) ? '' : trim($matches[4]);
|
|
203 |
if ($this->conf['spaceUnderscore']) {
|
|
204 |
$matches[3] = preg_replace('/\s+/', '_', $matches[3]);
|
|
205 |
$matches[4] = preg_replace('/\s+/', '_', $matches[4]);
|
|
206 |
}
|
|
207 |
if ($image) {
|
|
208 |
return $this->image($matches[3] . (empty($matches[4]) ? '' : '#' . $matches[4]),
|
|
209 |
$text, $interlang, $colon);
|
|
210 |
}
|
|
211 |
if (!$interwiki && $interlang && isset($this->conf['url'])) {
|
|
212 |
if ($interlang == $this->conf['langage']) {
|
|
213 |
$interlang = '';
|
|
214 |
} else {
|
|
215 |
$interwiki = $this->conf['url'];
|
|
216 |
$site = isset($this->conf['project']) ? $this->conf['project'][0] : '';
|
|
217 |
}
|
|
218 |
}
|
|
219 |
if ($interwiki) {
|
|
220 |
return $this->interwiki($site, $interwiki,
|
|
221 |
$matches[3] . (empty($matches[4]) ? '' : '#' . $matches[4]),
|
|
222 |
$text, $interlang, $colon);
|
|
223 |
}
|
|
224 |
if ($interlang) {
|
|
225 |
$matches[3] = $interlang . ':' . $matches[3];
|
|
226 |
$text = (empty($matches[5]) ? $interlang . ':' : '') . $text;
|
|
227 |
}
|
|
228 |
// set the options
|
|
229 |
$options = array(
|
|
230 |
'page' => $matches[3],
|
|
231 |
'anchor' => (empty($matches[4]) ? '' : $matches[4]),
|
|
232 |
'text' => $text
|
|
233 |
);
|
|
234 |
|
|
235 |
// create and return the replacement token
|
|
236 |
return $this->wiki->addToken($this->rule, $options);
|
|
237 |
}
|
|
238 |
|
|
239 |
/**
|
|
240 |
* Generates an image token. Token options are:
|
|
241 |
* - 'src' => the name of the image file
|
|
242 |
* - 'attr' => an array of attributes for the image:
|
|
243 |
* | - 'alt' => the optional alternate image text
|
|
244 |
* | - 'align => 'left', 'center' or 'right'
|
|
245 |
*
|
|
246 |
* @access public
|
|
247 |
* @param array &$matches The array of matches from parse().
|
|
248 |
* @return string token to be used as replacement
|
|
249 |
*/
|
|
250 |
function image($name, $text, $interlang, $colon)
|
|
251 |
{
|
|
252 |
$attr = array('alt' => '');
|
|
253 |
// scan text for supplementary attibutes
|
|
254 |
if (strpos($text, '|') !== false) {
|
|
255 |
$splits = explode('|', $text);
|
|
256 |
$sep = '';
|
|
257 |
foreach ($splits as $split) {
|
|
258 |
switch (strtolower($split)) {
|
|
259 |
case 'left': case 'center': case 'right':
|
|
260 |
$attr['align'] = strtolower($split);
|
|
261 |
break;
|
|
262 |
default:
|
|
263 |
$attr['alt'] .= $sep . $split;
|
|
264 |
$sep = '|';
|
|
265 |
}
|
|
266 |
}
|
|
267 |
} else {
|
|
268 |
$attr['alt'] = $text;
|
|
269 |
}
|
|
270 |
$options = array(
|
|
271 |
'src' => ($interlang ? $interlang . ':' : '') . $name,
|
|
272 |
'attr' => $attr);
|
|
273 |
|
|
274 |
// create and return the replacement token
|
|
275 |
return $this->wiki->addToken('Image', $options);
|
|
276 |
}
|
|
277 |
|
|
278 |
/**
|
|
279 |
* Generates an interwiki token. Token options are:
|
|
280 |
* - 'page' => the name of the target wiki page
|
|
281 |
* - 'site' => the key for external site
|
|
282 |
* - 'url' => the full target url
|
|
283 |
* - 'text' => the optional alternate link text
|
|
284 |
*
|
|
285 |
* @access public
|
|
286 |
* @param array &$matches The array of matches from parse().
|
|
287 |
* @return string token to be used as replacement
|
|
288 |
*/
|
|
289 |
function interwiki($site, $interwiki, $page, $text, $interlang, $colon)
|
|
290 |
{
|
|
291 |
if ($interlang) {
|
|
292 |
$interwiki = preg_replace('/\b' . $this->conf['langage'] . '\b/i',
|
|
293 |
$interlang, $interwiki);
|
|
294 |
}
|
|
295 |
// set the options
|
|
296 |
$options = array(
|
|
297 |
'page' => $page,
|
|
298 |
'site' => $site,
|
|
299 |
'url' => sprintf($interwiki, $page),
|
|
300 |
'text' => $text
|
|
301 |
);
|
|
302 |
|
|
303 |
// create and return the replacement token
|
|
304 |
return $this->wiki->addToken('Interwiki', $options);
|
|
305 |
}
|
|
306 |
}
|
|
307 |
?>
|