Index: trunk/wap/hawpedia.php |
— | — | @@ -12,43 +12,43 @@ |
13 | 13 | { |
14 | 14 | $sessionDeck = new HAW_deck(); |
15 | 15 | $sessionDeck->enable_session(); |
16 | | - ini_set('session.use_cookies', 0); // do not use cookies |
17 | | - ini_set('session.use_trans_sid', 1); // use transient sid support |
| 16 | + ini_set('session.use_cookies', 0); // do not use cookies |
| 17 | + ini_set('session.use_trans_sid', 1); // use transient sid support |
18 | 18 | |
19 | | - if ($sessionDeck->ml == HAW_HTML) { |
20 | | - // remove form entry - see http://bugs.php.net/bug.php?id=13472 |
21 | | - ini_set('url_rewriter.tags', 'a=href'); |
22 | | - } |
| 19 | + if ($sessionDeck->ml == HAW_HTML) { |
| 20 | + // remove form entry - see http://bugs.php.net/bug.php?id=13472 |
| 21 | + ini_set('url_rewriter.tags', 'a=href'); |
| 22 | + } |
23 | 23 | |
24 | | - session_start(); |
25 | | - |
26 | | - determine_settings(); |
| 24 | + session_start(); |
| 25 | + |
| 26 | + determine_settings(); |
27 | 27 | } |
28 | 28 | |
29 | 29 | function set_deck_properties(&$deck) |
30 | 30 | { |
31 | 31 | $deck->set_charset("UTF-8"); |
32 | | - $deck->set_width(HAWIKI_DISP_WIDTH); |
33 | | - $deck->set_height(HAWIKI_DISP_HEIGHT); |
34 | | - $deck->set_disp_bgcolor(HAWIKI_DISP_BGCOLOR); |
35 | | - $deck->use_simulator(HAWIKI_SKIN); |
| 32 | + $deck->set_width(HAWIKI_DISP_WIDTH); |
| 33 | + $deck->set_height(HAWIKI_DISP_HEIGHT); |
| 34 | + $deck->set_disp_bgcolor(HAWIKI_DISP_BGCOLOR); |
| 35 | + $deck->use_simulator(HAWIKI_SKIN); |
36 | 36 | } |
37 | 37 | |
38 | 38 | function hawpedia_error($error_msg) |
39 | 39 | { |
40 | | - $error_deck = new HAW_deck(HAWIKI_TITLE); |
41 | | - set_deck_properties($error_deck); |
42 | | - $error_text = new HAW_text($error_msg); |
43 | | - $error_deck->add_text($error_text); |
44 | | - |
45 | | - $rule = new HAW_rule(); |
46 | | - $error_deck->add_rule($rule); |
47 | | - |
48 | | - $homelink = new HAW_link(hawtra("Home"), "index.php"); |
49 | | - $error_deck->add_link($homelink); |
50 | | - |
51 | | - $error_deck->create_page(); |
52 | | - exit(); |
| 40 | + $error_deck = new HAW_deck(HAWIKI_TITLE); |
| 41 | + set_deck_properties($error_deck); |
| 42 | + $error_text = new HAW_text($error_msg); |
| 43 | + $error_deck->add_text($error_text); |
| 44 | + |
| 45 | + $rule = new HAW_rule(); |
| 46 | + $error_deck->add_rule($rule); |
| 47 | + |
| 48 | + $homelink = new HAW_link(hawtra("Home"), "index.php"); |
| 49 | + $error_deck->add_link($homelink); |
| 50 | + |
| 51 | + $error_deck->create_page(); |
| 52 | + exit(); |
53 | 53 | } |
54 | 54 | |
55 | 55 | function validate_language($lang) { |
— | — | @@ -126,19 +126,19 @@ |
127 | 127 | |
128 | 128 | function hawtra($text) |
129 | 129 | { |
130 | | - // translate given text |
| 130 | + // translate given text |
131 | 131 | |
132 | | - $translationFile = "lang/" . $_SESSION['language'] . "/translations.php"; |
133 | | - |
134 | | - if (!file_exists($translationFile)) |
135 | | - return($text); // no translation possible |
136 | | - |
137 | | - require($translationFile); |
138 | | - |
139 | | - if (isset($translation[$text])) |
140 | | - return $translation[$text]; |
141 | | - else |
142 | | - return $text; // no translation available |
| 132 | + $translationFile = "lang/" . $_SESSION['language'] . "/translations.php"; |
| 133 | + |
| 134 | + if (!file_exists($translationFile)) |
| 135 | + return($text); // no translation possible |
| 136 | + |
| 137 | + require($translationFile); |
| 138 | + |
| 139 | + if (isset($translation[$text])) |
| 140 | + return $translation[$text]; |
| 141 | + else |
| 142 | + return $text; // no translation available |
143 | 143 | } |
144 | 144 | |
145 | 145 | function hawpptra($input) |
— | — | @@ -160,20 +160,20 @@ |
161 | 161 | |
162 | 162 | function translate_wikipedia_keyword($keyword) { |
163 | 163 | |
164 | | - // translate language-specific wikipedia keyword |
| 164 | + // translate language-specific wikipedia keyword |
| 165 | + |
| 166 | + if ($_SESSION['language'] == 'en') |
| 167 | + return $keyword; // no translation needed |
165 | 168 | |
166 | | - if ($_SESSION['language'] == 'en') |
167 | | - return $keyword; // no translation needed |
| 169 | + $languageFile = "lang/" . $_SESSION['language'] . "/keywords.php"; |
| 170 | + if (!file_exists($languageFile)) |
| 171 | + die("file not found: " . $filename); |
| 172 | + require($languageFile); |
168 | 173 | |
169 | | - $languageFile = "lang/" . $_SESSION['language'] . "/keywords.php"; |
170 | | - if (!file_exists($languageFile)) |
171 | | - die("file not found: " . $filename); |
172 | | - require($languageFile); |
173 | | - |
174 | 174 | if (!isset($keywords[$keyword])) |
175 | | - die("unknown keyword: " . $keyword); |
176 | | - |
177 | | - return $keywords[$keyword]; |
| 175 | + die("unknown keyword: " . $keyword); |
| 176 | + |
| 177 | + return $keywords[$keyword]; |
178 | 178 | } |
179 | 179 | |
180 | 180 | function export_wikipedia($searchTerm) |
— | — | @@ -181,9 +181,9 @@ |
182 | 182 | $result = array(); |
183 | 183 | |
184 | 184 | $export_keyword = translate_wikipedia_keyword('Special:Export'); |
185 | | - |
| 185 | + |
186 | 186 | $searchTerm = str_replace(" ", "_", $searchTerm); // blanks must become underscores |
187 | | - |
| 187 | + |
188 | 188 | // get wikipedia xml file |
189 | 189 | $ch = curl_init(); |
190 | 190 | $url = "http://" . $_SESSION['language'] . ".wikipedia.org/wiki/" . $export_keyword . "/" . $searchTerm; |
— | — | @@ -192,19 +192,19 @@ |
193 | 193 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
194 | 194 | $curlResultString = curl_exec($ch); |
195 | 195 | if (!is_string($curlResultString)) |
196 | | - hawpedia_error(hawtra("Wikipedia currently not available")); // exits internally |
| 196 | + hawpedia_error(hawtra("Wikipedia currently not available")); // exits internally |
197 | 197 | |
198 | 198 | curl_close($ch); |
199 | 199 | |
200 | 200 | // determine page title |
201 | 201 | if (!preg_match("%<title>(.*)</title>%", $curlResultString, $matches)) |
202 | | - return false; // search term not found |
203 | | - |
| 202 | + return false; // search term not found |
| 203 | + |
204 | 204 | $result['title'] = $matches[1]; |
205 | | - |
| 205 | + |
206 | 206 | // determine wiki text |
207 | 207 | if (!preg_match("/(<text [^>]*>)/", $curlResultString, $matches)) |
208 | | - hawpedia_error(hawtra("wikipedia export error")); // exits internally |
| 208 | + hawpedia_error(hawtra("wikipedia export error")); // exits internally |
209 | 209 | $textStart = strpos($curlResultString, $matches[1]) + strlen($matches[1]); |
210 | 210 | $textEnd = strpos($curlResultString, "</text>"); |
211 | 211 | $result['wikitext'] = substr($curlResultString, $textStart, $textEnd - $textStart); |
— | — | @@ -232,11 +232,11 @@ |
233 | 233 | function search_articles($article) |
234 | 234 | { |
235 | 235 | // search related articles (after export has failed) |
236 | | - |
| 236 | + |
237 | 237 | $result = array(); |
238 | 238 | |
239 | 239 | $search_keyword = translate_wikipedia_keyword('Special:Search'); |
240 | | - |
| 240 | + |
241 | 241 | $article = str_replace(" ", "_", $article); // blanks must become underscores |
242 | 242 | |
243 | 243 | // get wikipedia search result (in html format) |
— | — | @@ -247,76 +247,76 @@ |
248 | 248 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
249 | 249 | $curlResultString = curl_exec($ch); |
250 | 250 | if (!is_string($curlResultString)) |
251 | | - hawpedia_error(hawtra("Wikipedia currently not available")); // exits internally |
| 251 | + hawpedia_error(hawtra("Wikipedia currently not available")); // exits internally |
252 | 252 | |
253 | 253 | curl_close($ch); |
254 | 254 | |
255 | | - // extract article links from html |
256 | | - preg_match_all("%1em;\"><a href=\"/wiki/([^?\"]*)%", $curlResultString, $matches); |
257 | | - |
258 | | - for ($i=0; $i < count($matches[1]); $i++) { |
| 255 | + // extract article links from html |
| 256 | + preg_match_all("%1em;\"><a href=\"/wiki/([^?\"]*)%", $curlResultString, $matches); |
| 257 | + |
| 258 | + for ($i=0; $i < count($matches[1]); $i++) { |
259 | 259 | // iterate over found articles (no category links!) |
260 | | - if (!strstr($matches[1][$i], ":")) |
261 | | - $result[] = $matches[1][$i]; |
| 260 | + if (!strstr($matches[1][$i], ":")) |
| 261 | + $result[] = $matches[1][$i]; |
| 262 | + |
| 263 | + if (count($result) >= 10) |
| 264 | + break; // consider not more than 10 links |
| 265 | + } |
262 | 266 | |
263 | | - if (count($result) >= 10) |
264 | | - break; // consider not more than 10 links |
265 | | - } |
266 | | - |
267 | | - if (count($result) == 0) |
268 | | - return 0; // nothing found |
269 | | - else |
270 | | - return $result; |
| 267 | + if (count($result) == 0) |
| 268 | + return 0; // nothing found |
| 269 | + else |
| 270 | + return $result; |
271 | 271 | } |
272 | 272 | |
273 | 273 | function show_related_articles($articles, $searchterm) |
274 | 274 | { |
275 | | - $search_deck = new HAW_deck(HAWIKI_TITLE); |
276 | | - set_deck_properties($search_deck); |
| 275 | + $search_deck = new HAW_deck(HAWIKI_TITLE); |
| 276 | + set_deck_properties($search_deck); |
| 277 | + |
| 278 | + // tell what this deck is about |
| 279 | + $intro = new HAW_text(hawtra("Found articles for:") . " " . $searchterm); |
| 280 | + $search_deck->add_text($intro); |
277 | 281 | |
278 | | - // tell what this deck is about |
279 | | - $intro = new HAW_text(hawtra("Found articles for:") . " " . $searchterm); |
280 | | - $search_deck->add_text($intro); |
| 282 | + // separate intro from link section |
| 283 | + $rule = new HAW_rule(); |
| 284 | + $search_deck->add_rule($rule); |
| 285 | + |
| 286 | + // create one link for each article |
| 287 | + foreach ($articles as $article) { |
| 288 | + $article_link = new HAW_link(urldecode($article), "transcode.php?go=" . $article); |
| 289 | + $search_deck->add_link($article_link); |
| 290 | + } |
| 291 | + |
| 292 | + // add home link |
| 293 | + $search_deck->add_rule($rule); |
| 294 | + $homelink = new HAW_link(hawtra("Home"), "index.php"); |
| 295 | + $search_deck->add_link($homelink); |
281 | 296 | |
282 | | - // separate intro from link section |
283 | | - $rule = new HAW_rule(); |
284 | | - $search_deck->add_rule($rule); |
285 | | - |
286 | | - // create one link for each article |
287 | | - foreach ($articles as $article) { |
288 | | - $article_link = new HAW_link(urldecode($article), "transcode.php?go=" . $article); |
289 | | - $search_deck->add_link($article_link); |
290 | | - } |
291 | | - |
292 | | - // add home link |
293 | | - $search_deck->add_rule($rule); |
294 | | - $homelink = new HAW_link(hawtra("Home"), "index.php"); |
295 | | - $search_deck->add_link($homelink); |
296 | | - |
297 | | - $search_deck->create_page(); |
298 | | - exit(); |
| 297 | + $search_deck->create_page(); |
| 298 | + exit(); |
299 | 299 | } |
300 | 300 | |
301 | 301 | function extract_chapter($wikitext, $chapter) |
302 | 302 | { |
303 | 303 | if (!preg_match("/\n(==+)(\s?" . $chapter . "\s?==+)/", $wikitext, $matches)) |
304 | | - return("invalid chapter"); // should never happen |
305 | | - |
| 304 | + return("invalid chapter"); // should never happen |
| 305 | + |
306 | 306 | $chapterStart = strpos($wikitext, $matches[1] . $matches[2]); |
307 | 307 | |
308 | | - // search end of chapter |
309 | | - $chapterEnd = $chapterStart + strlen($chapter); |
310 | | - do { |
311 | | - // number of '=' characters must match exactly |
312 | | - $chapterEnd = strpos($wikitext, "\n" . $matches[1], $chapterEnd + 1); |
313 | | - } |
314 | | - while (($chapterEnd !== false) && (substr($wikitext, $chapterEnd + 1 + strlen($matches[1]), 1) == "=")); |
315 | | - |
316 | | - if ($chapterEnd !== false) |
317 | | - $wikitext = substr($wikitext, $chapterStart, $chapterEnd - $chapterStart); |
318 | | - else |
319 | | - $wikitext = substr($wikitext, $chapterStart); |
320 | | - |
| 308 | + // search end of chapter |
| 309 | + $chapterEnd = $chapterStart + strlen($chapter); |
| 310 | + do { |
| 311 | + // number of '=' characters must match exactly |
| 312 | + $chapterEnd = strpos($wikitext, "\n" . $matches[1], $chapterEnd + 1); |
| 313 | + } |
| 314 | + while (($chapterEnd !== false) && (substr($wikitext, $chapterEnd + 1 + strlen($matches[1]), 1) == "=")); |
| 315 | + |
| 316 | + if ($chapterEnd !== false) |
| 317 | + $wikitext = substr($wikitext, $chapterStart, $chapterEnd - $chapterStart); |
| 318 | + else |
| 319 | + $wikitext = substr($wikitext, $chapterStart); |
| 320 | + |
321 | 321 | return($wikitext); |
322 | 322 | } |
323 | 323 | |
— | — | @@ -325,57 +325,52 @@ |
326 | 326 | // remove all text within startStr and endStr (incl. limiters) |
327 | 327 | // preg_replace can cause problems as described here: http://bugs.php.net/bug.php?id=24460 |
328 | 328 | |
329 | | - while (true) { |
330 | | - // do for all sections in $wikitext ... |
| 329 | + $index = 0; // current position in wikitext |
| 330 | + $counter = 0; // for nested sections |
331 | 331 | |
332 | | - $secStart = strpos($wikitext, $startStr); |
333 | | - if ($secStart === false) |
334 | | - break; |
| 332 | + while (true) { |
| 333 | + |
| 334 | + // Guard against PHP whingeing with strpos start position out of range |
| 335 | + if ((strlen($wikitext) < ($index + strlen($startStr))) || |
| 336 | + (strlen($wikitext) < ($index + strlen($startStr)))) |
| 337 | + break; |
335 | 338 | |
336 | | - // consider nested limiters |
337 | | - $nest_count = 1; |
338 | | - $search_pos = $secStart + strlen($startStr); |
339 | | - $secEnd = false; |
340 | | - while (true) { |
341 | | - // Guard against PHP whinging with strpos start position out of range |
342 | | - if( $search_pos > strlen( $wikitext ) ) |
343 | | - break; |
344 | | - if( $search_pos + strlen( $startStr ) > strlen( $wikitext ) ) |
345 | | - break; |
346 | | - $start_pos = strpos($wikitext, $startStr, $search_pos); |
347 | | - $end_pos = strpos($wikitext, $endStr, $search_pos + strlen($startStr)); |
| 339 | + $startIndex = strpos($wikitext, $startStr, $index); |
| 340 | + $endIndex = strpos($wikitext, $endStr, $index); |
| 341 | + |
| 342 | + if (($startIndex === false) && ($counter == 0)) |
| 343 | + break; // we're done |
| 344 | + |
| 345 | + if ($endIndex === false) |
| 346 | + break; // missing end - invalid syntax! |
| 347 | + |
| 348 | + if (($startIndex !== false) && ($startIndex < $endIndex)) { |
| 349 | + // (sub-)section starts here |
| 350 | + $index = $startIndex + strlen($startStr); |
| 351 | + $counter++; |
348 | 352 | |
349 | | - if (($start_pos !== false) && ($start_pos < $end_pos)) { |
350 | | - // nested section found |
351 | | - $search_pos = $start_pos + strlen($startStr); |
352 | | - $nest_count++; |
353 | | - continue; |
354 | | - } |
| 353 | + if ($counter == 1) { |
| 354 | + // section starts here |
| 355 | + $secStart = $startIndex; |
| 356 | + } |
| 357 | + } |
| 358 | + else { |
| 359 | + // (sub-)section ends here |
| 360 | + $index = $endIndex + strlen($endStr); |
| 361 | + $counter--; |
| 362 | + |
| 363 | + if ($counter < 0) |
| 364 | + break; // endStr without startStr - invalid syntax! |
| 365 | + |
| 366 | + if ($counter == 0) { |
| 367 | + // remove section |
| 368 | + $wikitext = substr($wikitext, 0, $secStart) . |
| 369 | + substr($wikitext, $endIndex + strlen($endStr)); |
| 370 | + $index = $secStart; |
| 371 | + } |
| 372 | + } |
| 373 | + }; // end of while loop |
355 | 374 | |
356 | | - if ($end_pos !== false) { |
357 | | - $nest_count--; |
358 | | - if ($nest_count == 0) { |
359 | | - // end of section found |
360 | | - $secEnd = $end_pos; |
361 | | - break; |
362 | | - } |
363 | | - else { |
364 | | - // continue after end of nested section |
365 | | - $search_pos = $end_pos + strlen($endStr); |
366 | | - } |
367 | | - } |
368 | | - else |
369 | | - break; // invalid section syntax! |
370 | | - }; |
371 | | - |
372 | | - //remove section |
373 | | - if (($secStart !== false) && ($secEnd !== false)) { |
374 | | - $wikitext = substr($wikitext, 0, $secStart) . substr($wikitext, $secEnd + strlen($endStr)); |
375 | | - } |
376 | | - else |
377 | | - break; |
378 | | - }; |
379 | | - |
380 | 375 | return($wikitext); |
381 | 376 | } |
382 | 377 | |
— | — | @@ -401,13 +396,13 @@ |
402 | 397 | function links2text($wikitext) |
403 | 398 | { |
404 | 399 | // make [[wikilinks]] to wikilinks |
405 | | - $wikitext = preg_replace('/\[\[([^:\]]*\|)?([^:\]]*)\]\]/','${2}', $wikitext); |
406 | | - |
| 400 | + $wikitext = preg_replace('/\[\[([^:\]]*\|)?([^:\]]*)\]\]/','${2}', $wikitext); |
| 401 | + |
407 | 402 | // disable detection of http links |
408 | | - $wikitext = preg_replace('/http/','h t t p ', $wikitext); |
409 | | - //$wikitext = preg_replace('@\[?http://\S*(.*?)\]?@','${1}', $wikitext); |
410 | | - //$wikitext = preg_replace('@\[?http://(\S*)\]?@','${1}', $wikitext); |
411 | | - |
| 403 | + $wikitext = preg_replace('/http/','h t t p ', $wikitext); |
| 404 | + //$wikitext = preg_replace('@\[?http://\S*(.*?)\]?@','${1}', $wikitext); |
| 405 | + //$wikitext = preg_replace('@\[?http://(\S*)\]?@','${1}', $wikitext); |
| 406 | + |
412 | 407 | //echo $wikitext; |
413 | 408 | return($wikitext); |
414 | 409 | } |
— | — | @@ -416,37 +411,37 @@ |
417 | 412 | { |
418 | 413 | $result = array(); // init empty array |
419 | 414 | |
420 | | - while(true) { |
| 415 | + while(true) { |
| 416 | + |
| 417 | + $seg = substr($wikitext, 0, $segLength); // determine maximum segment |
| 418 | + |
| 419 | + if (strlen($seg) < $segLength) { |
| 420 | + // end of text |
| 421 | + $result[] = $seg; // add last array element |
| 422 | + break; |
| 423 | + } |
| 424 | + |
| 425 | + $crPos = strrpos($seg, "\n"); // find previous new line |
421 | 426 | |
422 | | - $seg = substr($wikitext, 0, $segLength); // determine maximum segment |
| 427 | + if ($crPos === false) { |
| 428 | + // no newline found in segment, find next new line |
| 429 | + $crPos = strpos($wikitext, "\n", $segLength); |
| 430 | + } |
| 431 | + |
| 432 | + if (($crPos === false) || ($crPos == 0)) { |
| 433 | + // no newline in whole text |
| 434 | + $crPos = strlen($wikitext); // consider whole text |
| 435 | + } |
423 | 436 | |
424 | | - if (strlen($seg) < $segLength) { |
425 | | - // end of text |
426 | | - $result[] = $seg; // add last array element |
427 | | - break; |
428 | | - } |
429 | | - |
430 | | - $crPos = strrpos($seg, "\n"); // find previous new line |
431 | | - |
432 | | - if ($crPos === false) { |
433 | | - // no newline found in segment, find next new line |
434 | | - $crPos = strpos($wikitext, "\n", $segLength); |
435 | | - } |
436 | | - |
437 | | - if (($crPos === false) || ($crPos == 0)) { |
438 | | - // no newline in whole text |
439 | | - $crPos = strlen($wikitext); // consider whole text |
440 | | - } |
441 | | - |
442 | | - $seg = substr($wikitext, 0, $crPos + 1); // determine segment |
443 | | - $result[] = $seg; // add array element |
444 | | - $wikitext = substr($wikitext, $crPos + 1); // continue with new line |
445 | | - |
446 | | - if ($crPos == 0) |
447 | | - exit; |
448 | | - } |
449 | | - |
450 | | - return $result; |
| 437 | + $seg = substr($wikitext, 0, $crPos + 1); // determine segment |
| 438 | + $result[] = $seg; // add array element |
| 439 | + $wikitext = substr($wikitext, $crPos + 1); // continue with new line |
| 440 | + |
| 441 | + if ($crPos == 0) |
| 442 | + exit; |
| 443 | + } |
| 444 | + |
| 445 | + return $result; |
451 | 446 | } |
452 | 447 | |
453 | 448 | function save_url() |
— | — | @@ -461,5 +456,5 @@ |
462 | 457 | fclose($fp); |
463 | 458 | } |
464 | 459 | } |
465 | | - |
| 460 | + |
466 | 461 | ?> |