diff --git a/includes/class-PageTemplated.php b/includes/class-PageTemplated.php index 7c9ff59..6c6b6d0 100644 --- a/includes/class-PageTemplated.php +++ b/includes/class-PageTemplated.php @@ -1,125 +1,145 @@ . namespace itwikidelbot; /** * Handle a generic page associated to a template */ abstract class PageTemplated extends Page { /** * Template name of this page * * To be overrided */ const TEMPLATE_NAME = 'EXAMPLE_NAME'; /** * Constructor * * @param $args Template arguments */ public function __construct() { parent::__construct( $this->getTemplatedTitle() ); } /** * Get the name of the template * * @return string */ public static function getTemplateName() { return static::TEMPLATE_NAME; } /** * Get the name of the template for the page title * * @return string */ public static function getTemplateTitleName() { return static::getTemplateName() . '.title'; } /** * Get the name of the template for the summary * * @return string */ public static function getTemplateSummaryName() { return static::getTemplateName() . '.summary'; } /** * Get the name of the template for the content */ public static function getTemplateContentName() { return static::getTemplateName() . '.content'; } /** * Get the template arguments * * @return array */ public abstract function getTemplateArguments(); + /** + * Get the template arguments, plus some more arguments + * + * @param array $args Array of arguments + * @param mixed $more More arguments (one string or an array) + * @return array + */ + public static function sumArgs( $args, $more ) { + + if( is_array( $more ) ) { + // append all the fields + $args = array_merge( $args, $more ); + } else { + // append just one field + $args[] = $more; + } + + return $args; + } + /** * Get the edit summary for this page from its template * * @return string */ public function getTemplatedSummary() { return Template::get( static::getTemplateSummaryName(), $this->getTemplateArguments() ); } /** * Get the page title from its template * * @return string */ public function getTemplatedTitle() { return Template::get( static::getTemplateTitleName(), $this->getTemplateArguments() ); } /** * Get the page content for this page from its template * * @return string */ public function getTemplatedContent() { return Template::get( static::getTemplateContentName(), $this->getTemplateArguments() ); } /** * Save this page from the content of its template * * @return mixed */ public function save() { return $this->saveByContentSummary( $this->getTemplatedContent(), $this->getTemplatedSummary() ); } /** * Save this page from the content of its template if it does not exist * * @return mixed */ public function saveIfNotExists() { return $this->saveByContentSummaryIfNotExists( $this->getTemplatedContent(), $this->getTemplatedSummary() ); } } diff --git a/includes/class-PageYear.php b/includes/class-PageYear.php index bc0a82e..aa2eb55 100644 --- a/includes/class-PageYear.php +++ b/includes/class-PageYear.php @@ -1,60 +1,60 @@ . namespace itwikidelbot; /** * Abstraction of a page related to a year */ abstract class PageYear extends PageTemplated { /** * Year * * @var int */ private $year; /** * Constructor * * @param $year int * @see PageTemplated::__construct() */ public function __construct( $year ) { $this->year = (int) $year; parent::__construct(); } /** * Get the year * * @return int */ public function getYear() { return $this->year; } /** * Template arguments * - * @override CategoryTemplated::getTemplateArguments() + * @override PageTemplated::getTemplateArguments() */ public function getTemplateArguments() { return [ $this->getYear() ]; } } diff --git a/includes/class-PageYearMonthDayPDCsCount.php b/includes/class-PageYearMonthDayPDCsCount.php index d1a33ba..07de9e6 100644 --- a/includes/class-PageYearMonthDayPDCsCount.php +++ b/includes/class-PageYearMonthDayPDCsCount.php @@ -1,157 +1,180 @@ . namespace itwikidelbot; /** * Abstraction of a page containing PDCs' log of the day */ class PageYearMonthDayPDCsCount extends PageYearMonthDayPDCs { /** * Template name of this page * * @override CategoryTemplated::TEMPLATE_NAME */ const TEMPLATE_NAME = 'PAGE_COUNT'; /** * Get the template arguments: * 1: page title * 2: temperature * 3: '1' if it's multiple * 4: color associated to the PDC e.g. '#fff' * 5: number of the row * 6: PDC type e.g. 'consensuale' * 7: duration e.g. 'un giorno' * 8: title of the log page * 9: human date 'year monthname day' * * @override PageTemplated::getTemplateArguments() * @return array */ public function getTemplateArguments() { + $parent_args = parent::getTemplateArguments(); + $sections = []; // runnings $i = 0; $entries = []; foreach( $this->getRunningPDCsByType() as $type => $pdcs ) { foreach( $pdcs as $pdc ) { $entries[] = $this->createPDCEntryContent( ++$i, $pdc ); } } + if( $entries ) { - $sections[] = Template::get( self::TEMPLATE_NAME . '.RUNNING.section', [ - implode( "\n", $entries ) - ] ); + $sections[] = Template::get( + // template name + 'PAGE_COUNT.RUNNING.section', + + // template arguments + self::sumArgs( $parent_args, implode( "\n", $entries ) ) + ); } // endeds $i = 0; $entries = []; foreach( $this->getEndedPDCsByType() as $type => $pdcs ) { foreach( $pdcs as $pdc ) { $entries[] = $this->createPDCEntryContent( ++$i, $pdc ); } } if( $entries ) { - $sections[] = Template::get( self::TEMPLATE_NAME . '.ENDED.section', [ - implode( "\n", $entries ) - ] ); + $sections[] = Template::get( + // template name + 'PAGE_COUNT.ENDED.section', + + // template arguments + self::sumArgs( $parent_args, [ implode( "\n", $entries ) ] ) + ); } // with some fixable errors $erroneous = []; foreach( $this->getPDCs() as $pdc ) { - if( ! $pdc->isTitleSubjectConsistent() ) { - $erroneous[] = self::createPDCErrorMessage( $pdc, - Template::get( self::TEMPLATE_NAME . '.ERRONEOUS.msg.sortkey', [ - $pdc->getTitleSubject() - ] ) + if( !$pdc->isTitleSubjectConsistent() ) { + $erroneous[] = self::createPDCErrorMessage( + $pdc, + Template::get( + // template name + 'PAGE_COUNT.ERRONEOUS.msg.sortkey', + + // template arguments + self::sumArgs( $parent_args, $pdc->getTitleSubject() ) + ) ); } } if( $erroneous ) { - $sections[] = Template::get( self::TEMPLATE_NAME . '.ERRONEOUS.section', [ - implode( "\n", $erroneous ) - ] ); - } + $sections[] = Template::get( + // template name + 'PAGE_COUNT.ERRONEOUS.section', - $args = parent::getTemplateArguments(); + // template arguments + self::sumArgs( $parent_args, implode( "\n", $erroneous ) ) + ); + } - // Does it have content? + // no content no party if( $sections ) { - $args[] = implode( "\n", $sections ); - } else { - $args[] = Template::get( self::TEMPLATE_NAME . '.empty' ); + + // the latest argument contains the section + $parent_args[] = implode( "\n", $sections ); + } else { + + // the latest arguments contains an empty section + $parent_args[] = Template::get( 'PAGE_COUNT.empty', $parent_args ); } - return $args; + + return $parent_args; } /** * Get the PDC entry content * * @param $pdc PDC * @param $i Ordinal number passed from the count page * @return string */ public function createPDCEntryContent( $i, PDC $pdc ) { $template_name = self::TEMPLATE_NAME; $template_name .= $pdc->isRunning() ? '.RUNNING.entry' : '.ENDED.entry'; $turnover = $pdc->getTurnover(); return Template::get( $template_name, [ $pdc->getTitleSubject(), $pdc->getTemperature(), $pdc->isMultiple(), '', // TODO: Ex color. Now unuseful. $i, $pdc->getHumanType(), $pdc->getHumanDuration(), $this->getTitle(), sprintf( '%d %s %d', $this->getYear(), $this->getMonthName(), $this->getDay() ), $turnover ? $turnover : '', implode( ', ', $pdc->getSubjectThemes() ) ] ); } /** * Create a PDC error message * * @param $pdc object * @param $message string * @return string */ public static function createPDCErrorMessage( $pdc, $message ) { - return Template::get( self::TEMPLATE_NAME . '.ERRONEOUS.entry', [ + return Template::get( 'PAGE_COUNT.ERRONEOUS.entry', [ $pdc->getTitle(), $message, ] ); } } diff --git a/includes/class-Template.php b/includes/class-Template.php index d00e0c0..f7dc39e 100644 --- a/includes/class-Template.php +++ b/includes/class-Template.php @@ -1,71 +1,75 @@ . namespace itwikidelbot; /** * Handle templates of text * * Template files are 'something.template' into the /template directory */ class Template { /** * Placeholder used to mark where the template content starts */ const START_PLACEHOLDER = "\n"; /** * Get the content of a template passing its arguments * * @param $name string Template name * @param $args array Template arguments * @return string Template content */ static function get( $name, $args = [] ) { // remove spaces $name = str_replace( ' ', '_', $name ); // ../templates/$name.template $path = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $name . '.tpl'; if( ! file_exists( $path ) ) { throw new \InvalidArgumentException( "unexisting template $name" ); } // template content with also documentation $content = file_get_contents( $path ); // stripping documentation etc. $pos = strpos( $content, self::START_PLACEHOLDER ); if( false === $pos ) { throw new \Exception( 'missing header in template' ); } $pos += strlen( self::START_PLACEHOLDER ); $content = substr( $content, $pos ); // text-editors usually put an unuseful newline before the EOF $content = rtrim( $content ); // pass arguments $s = vsprintf( $content, $args ); if( false === $s ) { + + echo "CONTENT\n"; + echo $content . "\n"; + var_dump( "ASD", $args ); throw new \Exception( "wrong number of arguments for template $name" ); } return $s; } } diff --git a/templates/PAGE_COUNT.ENDED.section.tpl b/templates/PAGE_COUNT.ENDED.section.tpl index 01b5fe1..26b3035 100644 --- a/templates/PAGE_COUNT.ENDED.section.tpl +++ b/templates/PAGE_COUNT.ENDED.section.tpl @@ -1,13 +1,18 @@ This is the template for a section of a daily counting page. This is the section for ended PDCs. Placeholders: - %1$s: PDC rows - %%: a literal '%' + %1$d: year + %2$d: month 1-12 + %2$02d: month 01-12 + %3$s: month name + %4$d: day 1-31 + %4$02d: day 01-31 + %5$s: PDC rows -{{Conteggio cancellazioni/Concluse/Start|~~~~~}} -%1$s +{{Conteggio cancellazioni/Concluse/Start|data=%1$d %3$s %4$d}} +%5$s {{Conteggio cancellazioni/Concluse/Stop}} diff --git a/templates/PAGE_COUNT.ERRONEOUS.section.tpl b/templates/PAGE_COUNT.ERRONEOUS.section.tpl index 8b7b186..0640f8f 100644 --- a/templates/PAGE_COUNT.ERRONEOUS.section.tpl +++ b/templates/PAGE_COUNT.ERRONEOUS.section.tpl @@ -1,10 +1,18 @@ +This is the template for a section of a daily counting page. + This is the template for a section of erroneous PDCs. Placeholders: - %1$s: errors + %1$d: year + %2$d: month 1-12 + %2$02d: month 01-12 + %3$s: month name + %4$d: day 1-31 + %4$02d: day 01-31 + %5$s: errors '''Attenzione:''' Sono state rilevate alcune PDC con errori: -%1$s +%5$s diff --git a/templates/PAGE_COUNT.RUNNING.section.tpl b/templates/PAGE_COUNT.RUNNING.section.tpl index 503f97a..cdf2bd6 100644 --- a/templates/PAGE_COUNT.RUNNING.section.tpl +++ b/templates/PAGE_COUNT.RUNNING.section.tpl @@ -1,12 +1,18 @@ This is the template for a section of a daily counting page. This is the section for running PDCs. Placeholders: - %1$s: PDC rows + %1$d: year + %2$d: month 1-12 + %2$02d: month 01-12 + %3$s: month name + %4$d: day 1-31 + %4$02d: day 01-31 + %5$s: PDC rows -{{Conteggio cancellazioni/In corso/Start|~~~~~}} -%1$s +{{Conteggio cancellazioni/In corso/Start|data=%1$d %3$s %4$d}} +%5$s {{Conteggio cancellazioni/In corso/Stop}} diff --git a/templates/PAGE_COUNT.empty.tpl b/templates/PAGE_COUNT.empty.tpl index e43fa00..b35d6de 100644 --- a/templates/PAGE_COUNT.empty.tpl +++ b/templates/PAGE_COUNT.empty.tpl @@ -1,5 +1,13 @@ This is the template for an empty daily counting page. +Placeholders: + %1$d: year + %2$d: month 1-12 + %2$02d: month 01-12 + %3$s: month name + %4$d: day 1-31 + %4$02d: day 01-31 + -{{Conteggio cancellazioni/Vuoto|~~~~~}} +{{Conteggio cancellazioni/Vuoto|%1$d %3$s %4$d}}