r112450 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112449‎ | r112450 | r112451 >
Date:22:25, 26 February 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
some refactoring
Modified paths:
  • /trunk/extensions/EducationProgram/EducationProgram.hooks.php (modified) (history)
  • /trunk/extensions/EducationProgram/EducationProgram.php (modified) (history)
  • /trunk/extensions/EducationProgram/actions/EPDeleteAction.php (modified) (history)
  • /trunk/extensions/EducationProgram/actions/EPEditAction.php (modified) (history)
  • /trunk/extensions/EducationProgram/actions/EPHistoryAction.php (modified) (history)
  • /trunk/extensions/EducationProgram/actions/EPViewAction.php (modified) (history)
  • /trunk/extensions/EducationProgram/actions/EditCourseAction.php (modified) (history)
  • /trunk/extensions/EducationProgram/actions/ViewCourseAction.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPCoursePager.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPCourses.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPOrgPager.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPOrgs.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPPageObject.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPPageTable.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPRevisionPager.php (modified) (history)
  • /trunk/extensions/EducationProgram/pages/CoursePage.php (modified) (history)
  • /trunk/extensions/EducationProgram/pages/EPPage.php (modified) (history)
  • /trunk/extensions/EducationProgram/pages/OrgPage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/EducationProgram.php
@@ -60,8 +60,6 @@
6161 $wgAutoloadClasses['EPSettings'] = dirname( __FILE__ ) . '/EducationProgram.settings.php';
6262
6363 $wgAutoloadClasses['CourseHistoryAction'] = dirname( __FILE__ ) . '/actions/CourseHistoryAction.php';
64 -//$wgAutoloadClasses['DeleteCourseAction'] = dirname( __FILE__ ) . '/actions/DeleteCourseAction.php';
65 -//$wgAutoloadClasses['DeleteOrgAction'] = dirname( __FILE__ ) . '/actions/DeleteOrgAction.php';
6664 $wgAutoloadClasses['EditCourseAction'] = dirname( __FILE__ ) . '/actions/EditCourseAction.php';
6765 $wgAutoloadClasses['EditOrgAction'] = dirname( __FILE__ ) . '/actions/EditOrgAction.php';
6866 $wgAutoloadClasses['EPAddArticleAction'] = dirname( __FILE__ ) . '/actions/EPAddArticleAction.php';
@@ -78,10 +76,6 @@
7977 $wgAutoloadClasses['OrgHistoryAction'] = dirname( __FILE__ ) . '/actions/OrgHistoryAction.php';
8078 $wgAutoloadClasses['ViewCourseAction'] = dirname( __FILE__ ) . '/actions/ViewCourseAction.php';
8179 $wgAutoloadClasses['ViewOrgAction'] = dirname( __FILE__ ) . '/actions/ViewOrgAction.php';
82 -//$wgAutoloadClasses['CourseUndoAction'] = dirname( __FILE__ ) . '/actions/CourseUndoAction.php';
83 -//$wgAutoloadClasses['OrgUndoAction'] = dirname( __FILE__ ) . '/actions/OrgUndoAction.php';
84 -//$wgAutoloadClasses['CourseRestoreAction'] = dirname( __FILE__ ) . '/actions/CourseRestoreAction.php';
85 -//$wgAutoloadClasses['OrgRestoreAction'] = dirname( __FILE__ ) . '/actions/OrgRestoreAction.php';
8680
8781 $wgAutoloadClasses['ApiDeleteEducation'] = dirname( __FILE__ ) . '/api/ApiDeleteEducation.php';
8882 $wgAutoloadClasses['ApiEnlist'] = dirname( __FILE__ ) . '/api/ApiEnlist.php';
@@ -199,7 +193,6 @@
200194 $wgHooks['ArticleFromTitle'][] = 'EPHooks::onArticleFromTitle';
201195 $wgHooks['CanonicalNamespaces'][] = 'EPHooks::onCanonicalNamespaces';
202196 $wgHooks['TitleIsAlwaysKnown'][] = 'EPHooks::onTitleIsAlwaysKnown';
203 -//$wgHooks['UnknownAction'][] = 'EPHooks::onUnknownAction';
204197
205198 // Actions
206199 $wgActions['epremarticle'] = 'EPRemoveArticleAction';
Index: trunk/extensions/EducationProgram/pages/CoursePage.php
@@ -14,6 +14,20 @@
1515 */
1616 class CoursePage extends EPPage {
1717
 18+ protected static $info = array(
 19+ 'ns' => EP_NS_COURSE,
 20+ 'actions' => array(
 21+ 'view' => false,
 22+ 'edit' => 'ep-course',
 23+ 'history' => false,
 24+ 'enroll' => 'ep-enroll',
 25+ ),
 26+ 'edit-right' => 'ep-course',
 27+ 'identifier' => 'name',
 28+ 'list' => 'Courses',
 29+ 'log-type' => 'course',
 30+ );
 31+
1832 /**
1933 * (non-PHPdoc)
2034 * @see EPPage::getActions()
Index: trunk/extensions/EducationProgram/pages/EPPage.php
@@ -2,6 +2,8 @@
33
44 /**
55 * Abstract Page for interacting with a EPPageObject.
 6+ *
 7+ * Forced to implement a bunch of stuff that better should be in Page... :/
68 *
79 * @since 0.1
810 *
@@ -49,8 +51,32 @@
5052 $this->page = new WikiPage( $title );
5153 }
5254
 55+ /**
 56+ * Returns a new instance based on the namespace of the provided title,
 57+ * or throws an exception if the namespace is not handled.
 58+ *
 59+ * @since 0.1
 60+ *
 61+ * @param Title $title
 62+ *
 63+ * @return EPPage
 64+ * @throws MWException
 65+ */
 66+ public static function factory( Title $title ) {
 67+ switch ( $title->getNamespace() ) {
 68+ case EP_NS_COURSE:
 69+ return new CoursePage( $title );
 70+ break;
 71+ case EP_NS_INSTITUTION:
 72+ return new OrgPage( $title );
 73+ break;
 74+ default:
 75+ throw new MWException( 'Namespace not handled by EPPage' );
 76+ }
 77+ }
 78+
5379 public function view() {
54 - // MediaWiki::articleFromTitle($title, $context)
 80+
5581 }
5682
5783 public function setContext( IContextSource $context ) {
@@ -131,4 +157,29 @@
132158 return $this->getLanguage();
133159 }
134160
 161+ public function getEditRight() {
 162+ return static::$info['edit-right'];
 163+ }
 164+
 165+ public function getListPage() {
 166+ return static::$info['list'];
 167+ }
 168+
 169+ public static function displayDeletionLog( IContextSource $context, $messageKey ) {
 170+ $out = $context->getOutput();
 171+
 172+ LogEventsList::showLogExtract(
 173+ $out,
 174+ array( static::$info['log-type'] ),
 175+ $context->getTitle(),
 176+ '',
 177+ array(
 178+ 'lim' => 10,
 179+ 'conds' => array( 'log_action' => 'remove' ),
 180+ 'showIfEmpty' => false,
 181+ 'msgKey' => array( $messageKey )
 182+ )
 183+ );
 184+ }
 185+
135186 }
Index: trunk/extensions/EducationProgram/pages/OrgPage.php
@@ -14,6 +14,19 @@
1515 */
1616 class OrgPage extends EPPage {
1717
 18+ protected static $info = array(
 19+ 'ns' => EP_NS_INSTITUTION,
 20+ 'actions' => array(
 21+ 'view' => false,
 22+ 'edit' => 'ep-org',
 23+ 'history' => false,
 24+ ),
 25+ 'edit-right' => 'ep-org',
 26+ 'identifier' => 'name',
 27+ 'list' => 'Institutions',
 28+ 'log-type' => 'institution',
 29+ );
 30+
1831 /**
1932 * (non-PHPdoc)
2033 * @see EPPage::getActions()
Index: trunk/extensions/EducationProgram/actions/ViewCourseAction.php
@@ -83,7 +83,7 @@
8484 $stats = array();
8585
8686 $orgName = EPOrgs::singleton()->selectFieldsRow( 'name', array( 'id' => $course->getField( 'org_id' ) ) );
87 - $stats['org'] = EPOrgs::singleton()->getLinkFor( $orgName );
 87+ $stats['org'] = OrgPage::getLinkFor( $orgName );
8888
8989 $lang = $this->getLanguage();
9090
Index: trunk/extensions/EducationProgram/actions/EPViewAction.php
@@ -70,13 +70,13 @@
7171 if ( $object === false ) {
7272 $this->displayNavigation();
7373
74 - if ( $this->getUser()->isAllowed( $this->table->getEditRight() ) ) {
 74+ if ( $this->getUser()->isAllowed( $this->page->getEditRight() ) ) {
7575 $out->redirect( $this->getTitle()->getLocalURL( array( 'action' => 'edit' ) ) );
7676 }
7777 else {
7878 $out->addWikiMsg( strtolower( get_called_class() ) . '-none', $name );
7979
80 - $this->table->displayDeletionLog(
 80+ $this->page->displayDeletionLog(
8181 $this->getContext(),
8282 'ep-' . strtolower( $this->getName() ) . '-deleted'
8383 );
Index: trunk/extensions/EducationProgram/actions/EditCourseAction.php
@@ -58,7 +58,7 @@
5959 $this->getOutput()->addModules( array( 'ep.datepicker', 'ep.combobox' ) );
6060
6161 if ( !$this->isNewPost() && !$this->table->hasIdentifier( $this->getTitle()->getText() ) ) {
62 - $this->table->displayDeletionLog(
 62+ $this->page->displayDeletionLog(
6363 $this->getContext(),
6464 'ep-' . strtolower( $this->getName() ) . '-deleted'
6565 );
Index: trunk/extensions/EducationProgram/actions/EPEditAction.php
@@ -100,7 +100,7 @@
101101 }
102102 else {
103103 if ( $object === false ) {
104 - $this->table->displayDeletionLog(
 104+ $this->page->displayDeletionLog(
105105 $this->getContext(),
106106 'ep-' . strtolower( $this->getName() ) . '-deleted'
107107 );
Index: trunk/extensions/EducationProgram/actions/EPHistoryAction.php
@@ -56,7 +56,7 @@
5757 protected function displayNoRevisions() {
5858 $this->getOutput()->addWikiMsg( 'ep-' . strtolower( $this->getName() ) . '-norevs' );
5959
60 - $this->table->displayDeletionLog(
 60+ $this->page->displayDeletionLog(
6161 $this->getContext(),
6262 'ep-' . strtolower( $this->getName() ) . '-deleted'
6363 );
Index: trunk/extensions/EducationProgram/actions/EPDeleteAction.php
@@ -35,7 +35,7 @@
3636 * @see Action::getRestriction()
3737 */
3838 public function getRestriction() {
39 - $this->page->getTable()->getEditRight();
 39+ $this->page->getEditRight();
4040 }
4141
4242 /**
Index: trunk/extensions/EducationProgram/includes/EPOrgPager.php
@@ -62,7 +62,7 @@
6363 public function getFormattedValue( $name, $value ) {
6464 switch ( $name ) {
6565 case 'name':
66 - $value = EPOrgs::getLinkFor( $value );
 66+ $value = OrgPage::getLinkFor( $value );
6767 break;
6868 case 'country':
6969 $countries = array_flip( EPUtils::getCountryOptions( $this->getLanguage()->getCode() ) );
Index: trunk/extensions/EducationProgram/includes/EPOrgs.php
@@ -13,19 +13,6 @@
1414 */
1515 class EPOrgs extends EPPageTable {
1616
17 - protected static $info = array(
18 - 'ns' => EP_NS_INSTITUTION,
19 - 'actions' => array(
20 - 'view' => false,
21 - 'edit' => 'ep-org',
22 - 'history' => false,
23 - ),
24 - 'edit-right' => 'ep-org',
25 - 'identifier' => 'name',
26 - 'list' => 'Institutions',
27 - 'log-type' => 'institution',
28 - );
29 -
3017 /**
3118 * (non-PHPdoc)
3219 * @see DBTable::getDBTable()
@@ -141,4 +128,20 @@
142129 return $options;
143130 }
144131
 132+ /**
 133+ * (non-PHPdoc)
 134+ * @see EPPageObject::getIdentifierField()
 135+ */
 136+ public function getIdentifierField() {
 137+ return 'name';
 138+ }
 139+
 140+ /**
 141+ * (non-PHPdoc)
 142+ * @see EPPageObject::getNamespace()
 143+ */
 144+ public function getNamespace() {
 145+ return EP_NS_INSTITUTION;
 146+ }
 147+
145148 }
Index: trunk/extensions/EducationProgram/includes/EPPageTable.php
@@ -13,46 +13,32 @@
1414 */
1515 abstract class EPPageTable extends DBTable {
1616
17 - public function getIdentifierField() {
18 - return static::$info['identifier'];
19 - }
20 -
21 - public function getEditRight() {
22 - return static::$info['edit-right'];
23 - }
24 -
25 - public static function getTitleFor( $identifierValue ) {
26 - return Title::newFromText(
27 - $identifierValue,
28 - static::$info['ns']
29 - );
30 - }
31 -
32 - public static function getLinkFor( $identifierValue, $action = 'view', $html = null, $customAttribs = array(), $query = array() ) {
33 - if ( $action !== 'view' ) {
34 - $query['action'] = $action;
35 - }
36 -
37 - return Linker::linkKnown( // Linker has no hook that allows us to figure out if the page actually exists :(
38 - self::getTitleFor( $identifierValue, $action ),
39 - is_null( $html ) ? htmlspecialchars( $identifierValue ) : $html,
40 - $customAttribs,
41 - $query
42 - );
43 - }
44 -
 17+ /**
 18+ * Returns the field use to identify this object, ie the part used as page title.
 19+ *
 20+ * @since 0.1
 21+ *
 22+ * @return string
 23+ */
 24+ public abstract function getIdentifierField();
 25+
 26+ /**
 27+ * Returns the namespace in which objects of this type reside.
 28+ *
 29+ * @since 0.1
 30+ *
 31+ * @return integer
 32+ */
 33+ public abstract function getNamespace();
 34+
4535 public function hasIdentifier( $identifier ) {
4636 return $this->has( array( $this->getIdentifierField() => $identifier ) );
4737 }
4838
4939 public function get( $identifier, $fields = null ) {
50 - return static::selectRow( $fields, array( $this->getIdentifierField() => $identifier ) );
 40+ return $this->selectRow( $fields, array( $this->getIdentifierField() => $identifier ) );
5141 }
5242
53 - public function getListPage() {
54 - return static::$info['list'];
55 - }
56 -
5743 /**
5844 * Delete all objects matching the provided condirions.
5945 *
@@ -92,40 +78,48 @@
9379 'title' => $title,
9480 );
9581 }
96 -
97 - public static function getTypeForNS( $ns ) {
98 - foreach ( static::$info as $type => $info ) {
99 - if ( $info['ns'] === $ns ) {
100 - return $type;
101 - }
102 - }
103 -
104 - throw new MWException( 'Unknown EPPageObject ns' );
105 - }
106 -
107 - public static function getLatestRevForTitle( Title $title, $conditions = array() ) {
108 - $conds = array(
109 - 'type' => self::getTypeForNS( $title->getNamespace() ),
110 - 'object_identifier' => $title->getText(),
 82+
 83+ /**
 84+ * Construct a new title for an object of this type with the provided identifier value.
 85+ *
 86+ * @since 0.1
 87+ *
 88+ * @param string $identifierValue
 89+ *
 90+ * @return Title
 91+ */
 92+ public function getTitleFor( $identifierValue ) {
 93+ return Title::newFromText(
 94+ $identifierValue,
 95+ $this->getNamespace()
11196 );
112 -
113 - return EPRevision::getLastRevision( array_merge( $conds, $conditions ) );
11497 }
11598
116 - public static function displayDeletionLog( IContextSource $context, $messageKey ) {
117 - $out = $context->getOutput();
 99+ /**
 100+ * Returns a link to the page representing the object of this type with the provided identifier value.
 101+ *
 102+ * @since 0.1
 103+ *
 104+ * @param string $identifierValue
 105+ * @param string $action
 106+ * @param string $html
 107+ * @param array $customAttribs
 108+ * @param array $query
 109+ *
 110+ * @return string
 111+ */
 112+ public function getLinkFor( $identifierValue, $action = 'view', $html = null, array $customAttribs = array(), array $query = array() ) {
 113+ if ( $action !== 'view' ) {
 114+ $query['action'] = $action;
 115+ }
118116
119 - LogEventsList::showLogExtract(
120 - $out,
121 - array( static::$info['log-type'] ),
122 - $context->getTitle(),
123 - '',
124 - array(
125 - 'lim' => 10,
126 - 'conds' => array( 'log_action' => 'remove' ),
127 - 'showIfEmpty' => false,
128 - 'msgKey' => array( $messageKey )
129 - )
 117+ // Linker has no hook that allows us to figure out if the page actually exists :(
 118+ // FIXME: now it does
 119+ return Linker::linkKnown(
 120+ $this->getTitleFor( $identifierValue, $action ),
 121+ is_null( $html ) ? htmlspecialchars( $identifierValue ) : $html,
 122+ $customAttribs,
 123+ $query
130124 );
131125 }
132126
Index: trunk/extensions/EducationProgram/includes/EPPageObject.php
@@ -13,13 +13,17 @@
1414 */
1515 abstract class EPPageObject extends EPRevisionedObject {
1616
 17+ /**
 18+ * (non-PHPdoc)
 19+ * @see EPRevisionedObject::getIdentifier()
 20+ */
1721 public function getIdentifier() {
1822 return $this->getField( $this->table->getIdentifierField() );
1923 }
20 -
 24+
2125 /**
 26+ * Returns the title of the page representing the object.
2227 *
23 - *
2428 * @since 0.1
2529 *
2630 * @return Title
@@ -29,27 +33,39 @@
3034 }
3135
3236 /**
 37+ * Gets a link to the page representing the object.
 38+ *
 39+ * @since 0.1
 40+ *
 41+ * @param string $action
 42+ * @param string $html
 43+ * @param array $customAttribs
 44+ * @param array $query
 45+ *
 46+ * @return string
 47+ */
 48+ public function getLink( $action = 'view', $html = null, array $customAttribs = array(), array $query = array() ) {
 49+ return $this->table->getLinkFor(
 50+ $this->getIdentifier(),
 51+ $action,
 52+ $html,
 53+ $customAttribs,
 54+ $query
 55+ );
 56+ }
 57+
 58+ /**
3359 * (non-PHPdoc)
3460 * @see DBDataObject::save()
3561 */
3662 public function save() {
37 - if ( $this->hasField( $this->table->getIdentifierField() ) && is_null( $this->getTitle() ) ) {
 63+ if ( $this->hasField( $this->getIdentifierField() ) && is_null( $this->getTitle() ) ) {
3864 throw new MWException( 'The title for a EPPageObject needs to be valid when saving.' );
3965 return false;
4066 }
4167
4268 return parent::save();
4369 }
44 -
45 - public function getLink( $action = 'view', $html = null, $customAttribs = array(), $query = array() ) {
46 - return $this->table->getLinkFor(
47 - $this->getIdentifier(),
48 - $action,
49 - $html,
50 - $customAttribs,
51 - $query
52 - );
53 - }
5470
5571 /**
5672 * (non-PHPdoc)
Index: trunk/extensions/EducationProgram/includes/EPRevisionPager.php
@@ -107,7 +107,8 @@
108108 );
109109 }
110110
111 - if ( $this->getUser()->isAllowed( $this->table->getEditRight() ) ) {
 111+ // TODO: $this->getUser()->isAllowed( $this->table->getEditRight() )
 112+ if ( false ) {
112113 $actionLinks = array();
113114
114115 if ( $this->mOffset !== '' || $this->rowNr < $this->mResult->numRows() - 1 ) {
Index: trunk/extensions/EducationProgram/includes/EPCoursePager.php
@@ -72,7 +72,7 @@
7373 public function getFormattedValue( $name, $value ) {
7474 switch ( $name ) {
7575 case 'name':
76 - $value = EPCourses::getLinkFor( $value );
 76+ $value = CoursePage::getLinkFor( $value );
7777 break;
7878 case 'org_id':
7979 $org = EPOrgs::singleton()->selectRow( 'name', array( 'id' => $value ) );
Index: trunk/extensions/EducationProgram/includes/EPCourses.php
@@ -13,20 +13,6 @@
1414 */
1515 class EPCourses extends EPPageTable {
1616
17 - protected static $info = array(
18 - 'ns' => EP_NS_COURSE,
19 - 'actions' => array(
20 - 'view' => false,
21 - 'edit' => 'ep-course',
22 - 'history' => false,
23 - 'enroll' => 'ep-enroll',
24 - ),
25 - 'edit-right' => 'ep-course',
26 - 'identifier' => 'name',
27 - 'list' => 'Courses',
28 - 'log-type' => 'course',
29 - );
30 -
3117 /**
3218 * (non-PHPdoc)
3319 * @see DBTable::getDBTable()
@@ -145,4 +131,20 @@
146132 ) );
147133 }
148134
 135+ /**
 136+ * (non-PHPdoc)
 137+ * @see EPPageObject::getIdentifierField()
 138+ */
 139+ public function getIdentifierField() {
 140+ return 'name';
 141+ }
 142+
 143+ /**
 144+ * (non-PHPdoc)
 145+ * @see EPPageObject::getNamespace()
 146+ */
 147+ public function getNamespace() {
 148+ return EP_NS_COURSE;
 149+ }
 150+
149151 }
Index: trunk/extensions/EducationProgram/EducationProgram.hooks.php
@@ -255,8 +255,7 @@
256256 $links['actions'] = array();
257257
258258 $user = $sktemplate->getUser();
259 - $class = $classes[$title->getNamespace()];
260 - $exists = $class::singleton()->hasIdentifier( $title->getText() );
 259+ $exists = $classes[$title->getNamespace()]::singleton()->hasIdentifier( $title->getText() );
261260 $type = $sktemplate->getRequest()->getText( 'action' );
262261 $isSpecial = $sktemplate->getTitle()->isSpecialPage();
263262
@@ -268,7 +267,7 @@
269268 );
270269 }
271270
272 - if ( $user->isAllowed( $class::singleton()->getEditRight() ) ) {
 271+ if ( $user->isAllowed( EPPage::factory( $title )->getEditRight() ) ) {
273272 $links['views']['edit'] = array(
274273 'class' => $type === 'edit' ? 'selected' : false,
275274 'text' => wfMsg( $exists ? 'ep-tab-edit' : 'ep-tab-create' ),
@@ -341,30 +340,4 @@
342341 return true;
343342 }
344343
345 - /**
346 - * Used to add new query-string actions.
347 - * @see https://www.mediawiki.org/wiki/Manual:Hooks/UnknownAction
348 - *
349 - * @since 0.1
350 - *
351 - * @param string $action
352 - * @param Page $page
353 - *
354 - * @return true
355 - */
356 - public static function onUnknownAction( $action, Page $page ) {
357 - // Action does not allow us to associate actions that are not known to core
358 - // with a single page, hence the less ideal handling in this hook.
359 - if ( method_exists( $page, 'getActions' ) ) {
360 - $actions = $page->getActions();
361 -
362 - if ( array_key_exists( $action, $actions ) ) {
363 - $action = new $actions[$action]( $page, $page->getContext() );
364 - $action->show();
365 - }
366 - }
367 -
368 - return true;
369 - }
370 -
371344 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r112455Follow up to r112450;jeroendedauw23:12, 26 February 2012

Status & tagging log