r91980 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91979‎ | r91980 | r91981 >
Date:18:01, 12 July 2011
Author:aaron
Status:resolved (Comments)
Tags:
Comment:
Added WikiPageFactory class with newFromTitle() function
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/WikiPageFactory.php (added) (history)
  • /trunk/phase3/tests/phpunit/includes/ArticleTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/ArticleTest.php
@@ -63,4 +63,17 @@
6464 "Article static functions" );
6565 }
6666
 67+ function testWikiPageFactory() {
 68+ $title = Title::makeTitle( NS_FILE, 'Someimage.png' );
 69+ $page = WikiPageFactory::newFromTitle( $title );
 70+ $this->assertEquals( 'WikiFilePage', get_class( $page ) );
 71+
 72+ $title = Title::makeTitle( NS_CATEGORY, 'SomeCategory' );
 73+ $page = WikiPageFactory::newFromTitle( $title );
 74+ $this->assertEquals( 'WikiCategoryPage', get_class( $page ) );
 75+
 76+ $title = Title::makeTitle( NS_MAIN, 'SomePage' );
 77+ $page = WikiPageFactory::newFromTitle( $title );
 78+ $this->assertEquals( 'WikiPage', get_class( $page ) );
 79+ }
6780 }
Index: trunk/phase3/includes/WikiPageFactory.php
@@ -0,0 +1,28 @@
 2+<?php
 3+/**
 4+ * Factory class for classes representing a MediaWiki article and history.
 5+ */
 6+class WikiPageFactory {
 7+ /**
 8+ * Create an WikiPage object of the appropriate class for the given title.
 9+ *
 10+ * @param $title Title
 11+ * @return WikiPage object
 12+ */
 13+ public static function newFromTitle( Title $title ) {
 14+ switch( $title->getNamespace() ) {
 15+ case NS_MEDIA:
 16+ throw new MWException( "NS_MEDIA is a virtual namespace" );
 17+ case NS_FILE:
 18+ $page = new WikiFilePage( $title );
 19+ break;
 20+ case NS_CATEGORY:
 21+ $page = new WikiCategoryPage( $title );
 22+ break;
 23+ default:
 24+ $page = new WikiPage( $title );
 25+ }
 26+
 27+ return $page;
 28+ }
 29+}
Property changes on: trunk/phase3/includes/WikiPageFactory.php
___________________________________________________________________
Added: svn:eol-style
130 + native
Index: trunk/phase3/includes/AutoLoader.php
@@ -243,6 +243,7 @@
244244 'WikiFilePage' => 'includes/WikiFilePage.php',
245245 'WikiImporter' => 'includes/Import.php',
246246 'WikiPage' => 'includes/WikiPage.php',
 247+ 'WikiPageFactory' => 'includes/WikiPageFactory.php',
247248 'WikiRevision' => 'includes/Import.php',
248249 'WikiMap' => 'includes/WikiMap.php',
249250 'WikiReference' => 'includes/WikiMap.php',

Follow-up revisions

RevisionCommit summaryAuthorDate
r92002Follow-up r91997, moved newFromTitle() function to WikiPage.php and renamed t...aaron20:32, 12 July 2011

Comments

#Comment by Nikerabbit (talk | contribs)   20:18, 12 July 2011

Weren't we supposed to use FooClass::factory pattern? Why do we need a new class for this?

#Comment by Aaron Schulz (talk | contribs)   20:20, 12 July 2011

Are we? Is this on mw.org? So you'd have a WikiPage::factory() method?

#Comment by Nikerabbit (talk | contribs)   20:30, 12 July 2011
  1. Yes I've been under that impression
  2. I have no idea.
  3. Yes

I've understood it as that Foo::newFromBar() always returns object of type Foo, where as Baz::factory() returns an object which is a subclass of Baz. Compare Title::newFromRow and Language::factory( 'de' ).

#Comment by Duplicatebug (talk | contribs)   17:08, 14 July 2011

What about NS_SPECIAL? Throw Exception (just in case) or create the special page?

#Comment by Duplicatebug (talk | contribs)   17:56, 14 July 2011

Handled with r92169

Status & tagging log