r34930 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r34929‎ | r34930 | r34931 >
Date:18:52, 16 May 2008
Author:nhuffschmid
Status:old
Tags:
Comment:
redesign of buggy/incomprehensible function: remove_section()
Modified paths:
  • /trunk/wap/hawpedia.php (modified) (history)

Diff [purge]

Index: trunk/wap/hawpedia.php
@@ -12,43 +12,43 @@
1313 {
1414 $sessionDeck = new HAW_deck();
1515 $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
1818
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+ }
2323
24 - session_start();
25 -
26 - determine_settings();
 24+ session_start();
 25+
 26+ determine_settings();
2727 }
2828
2929 function set_deck_properties(&$deck)
3030 {
3131 $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);
3636 }
3737
3838 function hawpedia_error($error_msg)
3939 {
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();
5353 }
5454
5555 function validate_language($lang) {
@@ -126,19 +126,19 @@
127127
128128 function hawtra($text)
129129 {
130 - // translate given text
 130+ // translate given text
131131
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
143143 }
144144
145145 function hawpptra($input)
@@ -160,20 +160,20 @@
161161
162162 function translate_wikipedia_keyword($keyword) {
163163
164 - // translate language-specific wikipedia keyword
 164+ // translate language-specific wikipedia keyword
 165+
 166+ if ($_SESSION['language'] == 'en')
 167+ return $keyword; // no translation needed
165168
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);
168173
169 - $languageFile = "lang/" . $_SESSION['language'] . "/keywords.php";
170 - if (!file_exists($languageFile))
171 - die("file not found: " . $filename);
172 - require($languageFile);
173 -
174174 if (!isset($keywords[$keyword]))
175 - die("unknown keyword: " . $keyword);
176 -
177 - return $keywords[$keyword];
 175+ die("unknown keyword: " . $keyword);
 176+
 177+ return $keywords[$keyword];
178178 }
179179
180180 function export_wikipedia($searchTerm)
@@ -181,9 +181,9 @@
182182 $result = array();
183183
184184 $export_keyword = translate_wikipedia_keyword('Special:Export');
185 -
 185+
186186 $searchTerm = str_replace(" ", "_", $searchTerm); // blanks must become underscores
187 -
 187+
188188 // get wikipedia xml file
189189 $ch = curl_init();
190190 $url = "http://" . $_SESSION['language'] . ".wikipedia.org/wiki/" . $export_keyword . "/" . $searchTerm;
@@ -192,19 +192,19 @@
193193 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
194194 $curlResultString = curl_exec($ch);
195195 if (!is_string($curlResultString))
196 - hawpedia_error(hawtra("Wikipedia currently not available")); // exits internally
 196+ hawpedia_error(hawtra("Wikipedia currently not available")); // exits internally
197197
198198 curl_close($ch);
199199
200200 // determine page title
201201 if (!preg_match("%<title>(.*)</title>%", $curlResultString, $matches))
202 - return false; // search term not found
203 -
 202+ return false; // search term not found
 203+
204204 $result['title'] = $matches[1];
205 -
 205+
206206 // determine wiki text
207207 if (!preg_match("/(<text [^>]*>)/", $curlResultString, $matches))
208 - hawpedia_error(hawtra("wikipedia export error")); // exits internally
 208+ hawpedia_error(hawtra("wikipedia export error")); // exits internally
209209 $textStart = strpos($curlResultString, $matches[1]) + strlen($matches[1]);
210210 $textEnd = strpos($curlResultString, "</text>");
211211 $result['wikitext'] = substr($curlResultString, $textStart, $textEnd - $textStart);
@@ -232,11 +232,11 @@
233233 function search_articles($article)
234234 {
235235 // search related articles (after export has failed)
236 -
 236+
237237 $result = array();
238238
239239 $search_keyword = translate_wikipedia_keyword('Special:Search');
240 -
 240+
241241 $article = str_replace(" ", "_", $article); // blanks must become underscores
242242
243243 // get wikipedia search result (in html format)
@@ -247,76 +247,76 @@
248248 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
249249 $curlResultString = curl_exec($ch);
250250 if (!is_string($curlResultString))
251 - hawpedia_error(hawtra("Wikipedia currently not available")); // exits internally
 251+ hawpedia_error(hawtra("Wikipedia currently not available")); // exits internally
252252
253253 curl_close($ch);
254254
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++) {
259259 // 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+ }
262266
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;
271271 }
272272
273273 function show_related_articles($articles, $searchterm)
274274 {
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);
277281
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);
281296
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();
299299 }
300300
301301 function extract_chapter($wikitext, $chapter)
302302 {
303303 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+
306306 $chapterStart = strpos($wikitext, $matches[1] . $matches[2]);
307307
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+
321321 return($wikitext);
322322 }
323323
@@ -325,57 +325,52 @@
326326 // remove all text within startStr and endStr (incl. limiters)
327327 // preg_replace can cause problems as described here: http://bugs.php.net/bug.php?id=24460
328328
329 - while (true) {
330 - // do for all sections in $wikitext ...
 329+ $index = 0; // current position in wikitext
 330+ $counter = 0; // for nested sections
331331
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;
335338
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++;
348352
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
355374
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 -
380375 return($wikitext);
381376 }
382377
@@ -401,13 +396,13 @@
402397 function links2text($wikitext)
403398 {
404399 // make [[wikilinks]] to wikilinks
405 - $wikitext = preg_replace('/\[\[([^:\]]*\|)?([^:\]]*)\]\]/','${2}', $wikitext);
406 -
 400+ $wikitext = preg_replace('/\[\[([^:\]]*\|)?([^:\]]*)\]\]/','${2}', $wikitext);
 401+
407402 // 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+
412407 //echo $wikitext;
413408 return($wikitext);
414409 }
@@ -416,37 +411,37 @@
417412 {
418413 $result = array(); // init empty array
419414
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
421426
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+ }
423436
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;
451446 }
452447
453448 function save_url()
@@ -461,5 +456,5 @@
462457 fclose($fp);
463458 }
464459 }
465 -
 460+
466461 ?>

Status & tagging log