diff --git a/2020/cli/import.php b/2020/cli/import.php new file mode 100755 index 0000000..9609e93 --- /dev/null +++ b/2020/cli/import.php @@ -0,0 +1,305 @@ +#!/usr/bin/php +. + +/** + * This script import contents from Meta-wiki + */ + +require 'load.php'; + +// query this Conference +$conference = ( new QueryConference() ) + ->whereConferenceUID( THIS_CONFERENCE_UID ) + ->queryRow(); + +// no Conference no party +if( !$conference ) { + echo "missing conference\n"; + exit; +} + +// current Confererence ID +$conference_ID = $conference->getConferenceID(); + +// template arguments +TemplateArg::add( 'titolo' ); +TemplateArg::add( 'abstract' ); +TemplateArg::add( 'descrizione' ); +TemplateArg::add( 'note' ); +TemplateArg::add( 'chi' ); +TemplateArg::add( 'giorno' ); +TemplateArg::add( 'ora inizio' ); +TemplateArg::add( 'ora fine' ); + +// wikimedia meta +$meta = \wm\MetaWiki::instance(); + +// months and related number +$MONTHS = [ + 'gennaio' => '01', + 'febbraio' => '02', + 'marzo' => '03', + 'aprile' => '04', + 'maggio' => '05', + 'giugno' => '06', + 'luglio' => '07', + 'agosto' => '08', + 'settembre' => '09', + 'ottobre' => '10', + 'novembre' => '11', + 'dicembre' => '12', +]; + +// API query category members +$queries = + $meta->createQuery( [ + // generator of category members + 'action' => 'query', + 'generator' => 'categorymembers', + 'gcmtitle' => 'Category:ItWikiCon 2020 - Programma', + + // for each page retrieved from the generator ask the revision + 'prop' => 'revisions', + 'rvslots' => 'main', + 'rvprop' => [ + 'ids', + 'timestamp', + + // we can ask the content and manually parse it with regexes, or just get the id and call API parse + // 'content', + ], + ] ); + +// for each API query request +foreach( $queries as $query ) { + + // for each page found during this request + foreach( $query->query->pages as $page ) { + + // page ID + $pageid = $page->pageid; + + // revisions is an array of just one element + $revisions = $page->revisions ?? []; + foreach( $revisions as $revision ) { + + // revision ID + $revid = $revision->revid; + + // parse the templates + // https://www.mediawiki.org/w/api.php?action=help&modules=parse + $parse_request = + $meta->fetch( [ + 'action' => 'parse', + 'oldid' => $revid, + 'prop' => 'parsetree', + ] ); + + // DOM tree as XML + $tree = $parse_request->parse->parsetree->{'*'} ?? null; + if( $tree ) { + $reader = simplexml_load_string( $tree ); + + $template = $reader->template; + $template_title = trim( $template->title ); + if( $template_title === 'ItWikiCon/2020/Session' ) { + + $template_values = []; + + // template arguments + foreach( $template->part as $template_part ) { + + // argument title and its value ( | name = value ) + $part_name = trim( $template_part->name ); + $part_value = trim( $template_part->value ); + + // find the template value + $template_value = TemplateArg::createValue( $part_name, $part_value ); + if( $template_value ) { + $template_values[] = $template_value; + } + } + + // finally extract the template parameters + // $pageid + $event_title = TemplateArgValue::findAndGetValue( $template_values, 'titolo' ); + $abstract = TemplateArgValue::findAndGetValue( $template_values, 'abstract' ); + $note = TemplateArgValue::findAndGetValue( $template_values, 'note' ); + $giorno = TemplateArgValue::findAndGetValue( $template_values, 'giorno' ); + $ora_inizio = TemplateArgValue::findAndGetValue( $template_values, 'ora inizio' ); + $ora_fine = TemplateArgValue::findAndGetValue( $template_values, 'ora fine' ); + $event_description = TemplateArgValue::findAndGetValue( $template_values, 'descrizione' ); + $lingua = TemplateArgValue::findAndGetValue( $template_values, 'lingua', 'it' ); + + // 24 ottobre + $giorno_dd_mm = explode( ' ', $giorno ); + $giorno_dd_mm[1] = $MONTHS[ $giorno_dd_mm[1] ]; // ottobre -> 10 + $event_start = sprintf( + '%s-%s-%s %s:00', + date( 'Y' ), + $giorno_dd_mm[1], + $giorno_dd_mm[0], + $ora_inizio + ); + $event_end = sprintf( + '%s-%s-%s %s:00', + date( 'Y' ), + $giorno_dd_mm[1], + $giorno_dd_mm[0], + $ora_fine + ); + + $event_uid = generate_slug( $event_title ); + + // option used to remember the Meta pageid -> Event ID + $option_name = "event-from-meta-pageid-$pageid"; + + // check if this page is new + $event_ID = get_option( $option_name ); + if( $event_ID ) { + + // update + + } else { + + echo "Inserting $event_title\n"; + + query( 'START TRANSACTION' ); + + // insert + ( new QueryEvent() ) + ->insertRow( [ + 'conference_ID' => $conference_ID, + 'event_title' => $event_title, + 'event_uid' => $event_uid, + "event_description_$lingua" => $event_description, + "event_abstract_$lingua" => $abstract, + "event_note_$lingua" => $note, + 'event_language' => $lingua, + 'event_start' => $event_start, + 'event_end' => $event_end, + ] ); + + $event_ID = last_inserted_ID(); + + set_option( $option_name, $event_ID, false ); + + query( 'COMMIT' ); + } + } + } + } + } +} + +class TemplateArg { + + private $name; + + public static $args = []; + + public function __construct( $name ) { + $this->name = $name; + } + + public function getName() { + return $this->name; + } + + /** + * Add a template argument in the known list of arguments + */ + public static function add( $name ) { + self::$args[] = new TemplateArg( $name ); + } + + public static function createValue( $name, $value ) { + + $arg = self::find( $name ); + if( $arg ) { + return new TemplateArgValue( $arg, $value ); + } + + return false; + } + + public static function find( $name ) { + + foreach( self::$args as $arg ) { + if( $arg->getName() === $name ) { + return $arg; + } + } + + return false; + } + +} + +class TemplateArgValue { + + private $arg; + + private $value; + + public function __construct( TemplateArg $arg, $value ) { + $this->arg = $arg; + $this->value = $value; + } + + public function getValue( $default_value = null ) { + if( $default_value ) { + var_dump( "asd", $default_value ); + } + + $value = $this->value; + if( !$value ) { + $value = $default_value; + } + return $value; + } + + public function getArg() { + return $this->arg; + } + + public function getName() { + return $this->getArg()->getName(); + } + + public static function find( $all, $name ) { + foreach( $all as $one ) { + if( $one->getName() === $name ) { + return $one; + } + } + return false; + } + + public static function findAndGetValue( $all, $name, $default_value = null ) { + + $value = $default_value; + + $one = self::find( $all, $name ); + if( $one ) { + $value = $one->getValue( $default_value ); + } + + return $value; + } +} diff --git a/2020/cli/load.php b/2020/cli/load.php new file mode 100644 index 0000000..a4ad86e --- /dev/null +++ b/2020/cli/load.php @@ -0,0 +1,5 @@ +