diff --git a/includes/class-CategoryYear.php b/includes/class-CategoryYear.php index b1e46b9..316cdde 100644 --- a/includes/class-CategoryYear.php +++ b/includes/class-CategoryYear.php @@ -1,108 +1,102 @@ . namespace itwikidelbot; use \wm\WikipediaIt; use \mw\Tokens; /** * Handle a yearly category * * e.g. https://it.wikipedia.org/wiki/Categoria:Cancellazioni_-_2017 */ class CategoryYear { /** * Format of the generic title of a yearly category * * e.g. 'Categoria:Cancellazioni - 2018' */ const GENERIC_TITLE = 'Categoria:Cancellazioni - %d'; /** * Parent category title */ const MAIN_CATEGORY_TITLE = 'Categoria:Pagine in cancellazione per anno'; /** * Create the yearly category if it does not exist * * e.g. 'https://it.wikipedia.org/wiki/Categoria:Cancellazioni_-_2018' * * @param $year int e.g. 2018 */ public static function createIfNotExists( $year ) { $categoryinfo = WikipediaIt::getInstance()->fetch( [ 'action' => 'query', 'prop' => 'categoryinfo', 'titles' => self::title( $year ), ] ); foreach( $categoryinfo->query->pages as $pageid => $page ) { if( $pageid < 0 && isset( $page->missing ) ) { self::create( $year ); } } } /** * Create the yearly category * * e.g. 'https://it.wikipedia.org/wiki/Categoria:Cancellazioni_-_2018' * * @param $year int e.g. 2018 * @param $summary string */ private static function create( $year ) { - $summary = sprintf( - Template::get( 'CATEGORY_YEAR_SUMMARY' ), + $template_args = [ self::MAIN_CATEGORY_TITLE, $year - ); - $text = sprintf( - Template::get( 'CATEGORY_YEAR_CONTENT' ), - self::MAIN_CATEGORY_TITLE, - $year - ); + ]; $wit = WikipediaIt::getInstance(); $args = [ 'action' => 'edit', 'title' => self::title( $year ), - 'text' => $text, - 'summary' => $summary, - 'bot' => 1, + 'text' => Template::get( 'CATEGORY_YEAR_CONTENT', $template_args ), + 'summary' => Template::get( 'CATEGORY_YEAR_SUMMARY', $template_args ), 'token' => $wit->login()->getToken( Tokens::CSRF ), + 'bot' => 1, ]; $wit->post( $args ); } /** * Title of the category in the specified year * * @param $year int e.g. 2018 * @return string Category name e.g. 'Categoria:Cancellazioni - 2018' */ private static function title( $year ) { return sprintf( self::GENERIC_TITLE, $year ); } } diff --git a/includes/class-Template.php b/includes/class-Template.php index 1232063..eda53c9 100644 --- a/includes/class-Template.php +++ b/includes/class-Template.php @@ -1,49 +1,62 @@ . namespace itwikidelbot; /** * Handle templates of text * - * Templates are 'something.template' stuff into the /template directory + * 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 - * @return string + * @param $args array Template arguments + * @return string Template content */ - static function get( $name ) { + static function get( $name, $args ) { + + // ../templates/$name.template $path = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $name . '.template'; if( ! file_exists( $path ) ) { throw new \InvalidArgumentException( 'unexisting template' ); } + // template content with also documentation $content = file_get_contents( $path ); - // stripping header + // 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 ); - return rtrim( $content ); + // text-editors usually put an unuseful newline before the EOF + $content = rtrim( $content ); + + // pass arguments + return vsprintf( $content, $args ); } }