Kategorien
FreewarWiki:Bot/Skripts/maplist.php
< FreewarWiki:Bot | Skripts
Das Skript ist semantisch identisch zu FreewarWiki:Bot/Skripts/maplist.pl mit dem Unterschied, dass es die Informationen aus Kategorie:Felder holt und daher keine Angaben über Passagen machen kann.
<?php header('Content-Type: text/plain; charset=utf-8;'); error_reporting(E_ALL ^ E_NOTICE); function is_obsolete($wiki_text) { // Veraltetes Feld=none liefert true! return (bool)preg_match('/Veraltetes Feld=[^}]+/', $wiki_text); } function get_areas($url, $prefix) { preg_match_all("/>$prefix:([^<]+)</", file_get_contents($url), $areas); return $areas[1]; } function parse_field_article($area, $host) { $fields = array(); // Artikel fetchen $field_url = "$host/index.php/Felder:" . str_replace(' ', '%20', $area). "?action=edit"; $html = file_get_contents($field_url); if (is_obsolete($html) === true) { // veraltet return array(); } else { // Layout Vorlagen matchen $pattern_template = "/\{\{(Vorlage:)?Feldzusammenfassung\/Layout([^}]+)\}\}/"; preg_match_all($pattern_template, $html, $matches); // Layout Vorlage durchlaufen foreach ($matches[2] as $field_template) { // init und parsen $field = array_merge(array('area' => $area), parse_field_template($field_template)); // push $fields[] = $field; } return $fields; } } function parse_field_template($wiki_text) { // Vorlagen als Array: Parameter => Wert $template = array(); // Standard-Werte $field = array( 'accessible' => 1, 'pos_x' => -10, 'pos_y' => -9, 'npc' => array(), 'url' => '', 'passages' => array() ); // Key-Value Paare spliten $lines = array_filter(explode('|', $wiki_text)); foreach ($lines as $line) { // Key/Value trennen $keyval = explode('=', $line, 2); // und entsprechend ins Array eintragen $template[$keyval[0]] = trim($keyval[1]); // 'Parameter=' wirft undefined offset 1 } // Vorlagewerte maschinenlesbar machen $field['pos_x'] = (int)$template['X']; $field['pos_y'] = (int)$template['Y']; $field['url'] = $template['Bild']; // npcs lesen preg_match_all('/\[\[([^]]+)\]\]/', $template['NPC'], $npc_matches); $field['npc'] = $npc_matches[1]; return $field; } $host = 'http://fwwiki.de'; $prefix = 'Felder'; // Wiki-Namespace $parser_function = 'parse_field_article'; // Parser Funktion des Skripts $category_url = "$host/index.php/Kategorie:Felder"; // Gebietskategorie // init $fields = array(); // Gebiete fetchen $areas = get_areas($category_url, $prefix); // durchlaufen foreach ($areas as $area) { $fields = array_merge($fields, $parser_function($area, $host)); } // und ausgeben $delimiter = ';'; // head #echo implode($delimiter, array_keys($fields[0])) . "\n"; // body foreach ($fields as $field) { // output wie maplist.pl $field['npc'] = implode('/', $field['npc']); $field['passages'] = implode('/', $field['passages']); echo implode($delimiter, $field) . "\n"; }