r65953 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65952‎ | r65953 | r65954 >
Date:16:02, 5 May 2010
Author:svip
Status:deferred
Tags:
Comment:
Follow up to r65951; a premature structure of how the system will look in the future, as well as the upstart implementation of CitationCore.
Modified paths:
  • /trunk/extensions/TemplateAdventures/TemplateAdventures.i18n.magic.php (modified) (history)
  • /trunk/extensions/TemplateAdventures/TemplateAdventures.php (modified) (history)
  • /trunk/extensions/TemplateAdventures/Templates (added) (history)
  • /trunk/extensions/TemplateAdventures/Templates/CitationCore.php (added) (history)

Diff [purge]

Index: trunk/extensions/TemplateAdventures/TemplateAdventures.i18n.magic.php
@@ -0,0 +1,8 @@
 2+<?php
 3+
 4+$magicWords = array();
 5+
 6+$magicWords['en'] = array(
 7+ 'citationcore' => array( 1, 'citationcore' ),
 8+ 'ta_cc_author' => array( 0, 'author' ),
 9+);
Index: trunk/extensions/TemplateAdventures/TemplateAdventures.php
@@ -0,0 +1,198 @@
 2+<?php
 3+/**
 4+ * TemplateAdventures is for recreation of popular but demanding templates
 5+ * in PHP. Wikicode is powerful, but slow. Templates such as cite core
 6+ * suffers greatly from this.
 7+ *
 8+ *
 9+ * Copyright (C) 2010 'Svip', 'MZMcBride' and others.
 10+ *
 11+ * This program is free software; you can redistribute it and/or modify
 12+ * it under the terms of the GNU General Public License as published by
 13+ * the Free Software Foundation, either version 2 of the License, or
 14+ * (at your option) any later version; or the DWTFYWWI License version 1,
 15+ * as detailed below.
 16+ *
 17+ * This program is distributed in the hope that it will be useful,
 18+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 19+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 20+ * GNU General Public License for more details.
 21+ *
 22+ * You should have received a copy of the GNU General Public License along
 23+ * with this program; if not, write to the Free Software Foundation, Inc.,
 24+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 25+ * http://www.gnu.org/copyleft/gpl.html
 26+ *
 27+ * -----------------------------------------------------------------
 28+ * DWTFYWWI LICENSE
 29+ * Version 1, January 2006
 30+ *
 31+ * Copyright (C) 2006 Ævar Arnfjörð Bjarmason
 32+ *
 33+ * DWTFYWWI LICENSE
 34+ * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
 35+ * 0. The author grants everyone permission to do whatever the fuck they
 36+ * want with the software, whatever the fuck that may be.
 37+ * -----------------------------------------------------------------
 38+ */
 39+
 40+$wgExtensionCredits['parserhook'][] = array(
 41+ 'path' => __FILE__,
 42+ 'name' => 'Template Adventures',
 43+ 'author' => array( 'Svip' ),
 44+ 'url' => 'http://www.mediawiki.org/wiki/Extension:TemplateAdventures',
 45+ 'description' => 'Recreation of popular templates in PHP.',
 46+ 'descriptionmsg' => 'ta-desc',
 47+ 'version' => '0.1'
 48+);
 49+
 50+
 51+$dir = dirname(__FILE__);
 52+$wgExtensionMessagesFiles['TemplateAdventures'] = "$dir/TemplateAdventures.i18n.php";
 53+$wgExtensionMessagesFiles['TemplateAdventuresMagic'] = "$dir/TemplateAdventures.i18n.magic.php";
 54+
 55+$wgAutoloadClasses['CitationCore'] = $dir . '/Templates/CitationCore.php';
 56+
 57+$wgHooks['ParserFirstCallInit'][] = 'TemplateAdventures::onParserFirstCallInit';
 58+
 59+$wgParserTestFiles[] = dirname( __FILE__ ) . "/taParserTests.txt";
 60+
 61+class TemplateAdventures {
 62+
 63+ public static function onParserFirstCallInit( $parser ) {
 64+ $parser->setFunctionHook(
 65+ 'citationcore',
 66+ array( __CLASS__, 'citationCore' ),
 67+ SFH_OBJECT_ARGS
 68+ );
 69+ return true;
 70+ }
 71+
 72+ /**
 73+ * Render {{#citation core:}}
 74+ *
 75+ * @param $parser Parser
 76+ * @param $frame PPFrame_DOM
 77+ * @param $args Array
 78+ * @return wikicode parsed
 79+ */
 80+ public static function citationCore( $parser, $frame, $args ) {
 81+ if ( count( $args ) == 0 )
 82+ return '';
 83+ $obj = new CitationCore( $parser, $frame, $args );
 84+ $obj->parse();
 85+
 86+ return $obj->output();
 87+ }
 88+}
 89+
 90+class TemplateAdventureBasic {
 91+
 92+ protected $mParser;
 93+ protected $mFrame;
 94+ public $mArgs;
 95+ protected $mOutput;
 96+
 97+ /**
 98+ * Constructor
 99+ * @param $parser Parser
 100+ * @param $frame PPFrame_DOM
 101+ * @param $args Array
 102+ */
 103+ public function __construct( &$parser, &$frame, &$args ){
 104+ $this->mParser = $parser;
 105+ $this->mFrame = $frame;
 106+ $this->mArgs = $args;
 107+ }
 108+
 109+ /**
 110+ * Outputter
 111+ */
 112+ public function output() {
 113+ return $this->mOutput;
 114+ }
 115+
 116+ /**
 117+ * Do stuff.
 118+ */
 119+ public function parse() {
 120+ return;
 121+ }
 122+
 123+ /**
 124+ * Read options from $this->mArgs. Let the children handle the options.
 125+ */
 126+ protected function readOptions ( ) {
 127+
 128+ $args = $this->mArgs;
 129+
 130+ # an array of items not options
 131+ $this->mReaditems = array();
 132+
 133+ # first input is a bit different than the rest,
 134+ # so we'll treat that differently
 135+ $primary = trim( $this->mFrame->expand( array_shift( $args ) ) );
 136+ $primary = $this->handleInputItem( $primary );
 137+
 138+ # check the rest for options
 139+ foreach( $args as $arg ) {
 140+ $item = $this->handleInputItem( $arg );
 141+ }
 142+ }
 143+
 144+ /**
 145+ * This functions handles individual items found in the arguments,
 146+ * and decides whether it is an option or not.
 147+ * If it is, then it handles the option (and applies it).
 148+ * If it isn't, then it just returns the string it found.
 149+ *
 150+ * @param $arg String Argument
 151+ * @return String if element, else return false
 152+ */
 153+ protected function handleInputItem( $arg ) {
 154+ if ( $arg instanceof PPNode_DOM ) {
 155+ $bits = $arg->splitArg();
 156+ $index = $bits['index'];
 157+ if ( $index === '' ) { # Found
 158+ $var = trim( $this->mFrame->expand( $bits['name'] ) );
 159+ $value = trim( $this->mFrame->expand( $bits['value'] ) );
 160+ } else { # Not found
 161+ return trim( $this->mFrame->expand( $arg ) );
 162+ }
 163+ } else {
 164+ $parts = array_map( 'trim', explode( '=', $arg, 2 ) );
 165+ if ( count( $parts ) == 2 ) { # Found "="
 166+ $var = $parts[0];
 167+ $value = $parts[1];
 168+ } else { # Not found
 169+ return $arg;
 170+ }
 171+ }
 172+ # Still here? Then it must be an option
 173+ return $this->optionParse( $var, $value );
 174+ }
 175+
 176+ /**
 177+ * Parse the option.
 178+ * This should be rewritten in classes inheriting this class.
 179+ *
 180+ * @param $var
 181+ * @param $value
 182+ * @return False if option else element
 183+ */
 184+ protected function optionParse( $var, $value ) {
 185+ return $arg instanceof PPNode_DOM
 186+ ? trim( $this->mFrame->expand( $arg ) )
 187+ : $arg;
 188+ }
 189+
 190+ /**
 191+ * Using magic to store all known names for each option
 192+ *
 193+ * @param $input String
 194+ * @return The option found; otherwise false
 195+ */
 196+ protected function parseOptionName( $value ) {
 197+ return false;
 198+ }
 199+}
Index: trunk/extensions/TemplateAdventures/Templates/CitationCore.php
@@ -0,0 +1,42 @@
 2+<?php
 3+
 4+class CitationCore extends TemplateAdventureBasic {
 5+
 6+ private $dAuthors = array();
 7+
 8+ public function parse() {
 9+ $this->readOptions( );
 10+ $this->mOutput = "''{$this->dAuthors[0]}''";
 11+ }
 12+
 13+ protected function optionParse( $var, $value ) {
 14+ switch ( $name = self::parseOptionName( $var ) ) {
 15+ case 'author':
 16+ $this->dAuthors[] = $value;
 17+ break;
 18+ default:
 19+ # Wasn't an option after all
 20+ return $arg instanceof PPNode_DOM
 21+ ? trim( $this->mFrame->expand( $arg ) )
 22+ : $arg;
 23+ }
 24+ return false;
 25+ }
 26+
 27+ protected function parseOptionName( $value ) {
 28+
 29+ static $magicWords = null;
 30+ if ( $magicWords === null ) {
 31+ $magicWords = new MagicWordArray( array(
 32+ 'ta_cc_author'
 33+ ) );
 34+ }
 35+
 36+ if ( $name = $magicWords->matchStartToEnd( trim($value) ) ) {
 37+ return str_replace( 'ta_cc_', '', $name );
 38+ }
 39+
 40+ # blimey, so not an option!?
 41+ return false;
 42+ }
 43+}

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r65951TemplateAdventures init commit.svip15:05, 5 May 2010

Status & tagging log