r86347 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86346‎ | r86347 | r86348 >
Date:21:25, 18 April 2011
Author:happy-melon
Status:resolved (Comments)
Tags:
Comment:
Cleanup in SpecialPage.php:
* Enforce private access for member variables suggested since at least 1.4. Didn't do $mName because grepping for "->mName" gave far too many results to check.
* Move the stuff related to redirects (getRedirect(), getRedirectQuery(), $mAllowedRedirectParams and $mAddedRedirectParams) to SpecialRedirectToSpecial and adjust callers
* Document stuff
* Mark getFile() as deprecated
* Group together getListed(), setListed() and listed() to draw attention to the fact that all three have been there since 1.6 and that we need to pick one and deprecate the other(s)
* add isIncludable() getter
* mark as deprecated and evil the mutators added in 1.6 for things which *really* shouldn't be mutating anywhere. AFAICT they're not actually used many places. Didn't deprecate including() as it's in wide use and it's legitimately set in SpecialPageFactory::executePath().
Modified paths:
  • /trunk/extensions/Configure/specials/ConfigurationPage.php (modified) (history)
  • /trunk/extensions/examples/SpecialIncludable.php (modified) (history)
  • /trunk/phase3/includes/SpecialPage.php (modified) (history)
  • /trunk/phase3/includes/SpecialPageFactory.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/SpecialPageFactory.php
@@ -416,19 +416,21 @@
417417
418418 // Check for redirect
419419 if ( !$including ) {
420 - $redirect = $page->getRedirect( $par );
421 - $query = $page->getRedirectQuery();
422 - if ( $redirect instanceof Title ) {
423 - $url = $redirect->getFullUrl( $query );
424 - $context->output->redirect( $url );
425 - wfProfileOut( __METHOD__ );
426 - return $redirect;
427 - } elseif ( $redirect === true ) {
428 - global $wgScript;
429 - $url = $wgScript . '?' . wfArrayToCGI( $query );
430 - $context->output->redirect( $url );
431 - wfProfileOut( __METHOD__ );
432 - return $redirect;
 420+ if( $page instanceof SpecialRedirectToSpecial ){
 421+ $redirect = $page->getRedirect( $par );
 422+ $query = $page->getRedirectQuery();
 423+ if ( $redirect instanceof Title ) {
 424+ $url = $redirect->getFullUrl( $query );
 425+ $context->output->redirect( $url );
 426+ wfProfileOut( __METHOD__ );
 427+ return $redirect;
 428+ } elseif ( $redirect === true ) {
 429+ global $wgScript;
 430+ $url = $wgScript . '?' . wfArrayToCGI( $query );
 431+ $context->output->redirect( $url );
 432+ wfProfileOut( __METHOD__ );
 433+ return $redirect;
 434+ }
433435 }
434436
435437 // Redirect to canonical alias for GET commands
@@ -449,7 +451,7 @@
450452 $context->title = $page->getTitle();
451453 }
452454
453 - } elseif ( !$page->includable() ) {
 455+ } elseif ( !$page->isIncludable() ) {
454456 wfProfileOut( __METHOD__ );
455457 return false;
456458 }
Index: trunk/phase3/includes/SpecialPage.php
@@ -28,52 +28,34 @@
2929 * @ingroup SpecialPage
3030 */
3131 class SpecialPage {
32 - /**#@+
33 - * @access private
34 - */
 32+
 33+ // The canonical name of this special page
 34+ // Also used for the default <h1> heading, @see getDescription()
 35+ /*private*/ var $mName;
 36+
 37+ // The local name of this special page
 38+ private $mLocalName;
 39+
 40+ // Minimum user level required to access this page, or "" for anyone.
 41+ // Also used to categorise the pages in Special:Specialpages
 42+ private $mRestriction;
 43+
 44+ // Listed in Special:Specialpages?
 45+ private $mListed;
 46+
 47+ // Function name called by the default execute()
 48+ private $mFunction;
 49+
 50+ // File which needs to be included before the function above can be called
 51+ private $mFile;
 52+
 53+ // Whether or not this special page is being included from an article
 54+ protected $mIncluding;
 55+
 56+ // Whether the special page can be included in an article
 57+ protected $mIncludable;
 58+
3559 /**
36 - * The canonical name of this special page
37 - * Also used for the default <h1> heading, @see getDescription()
38 - */
39 - var $mName;
40 - /**
41 - * The local name of this special page
42 - */
43 - var $mLocalName;
44 - /**
45 - * Minimum user level required to access this page, or "" for anyone.
46 - * Also used to categorise the pages in Special:Specialpages
47 - */
48 - var $mRestriction;
49 - /**
50 - * Listed in Special:Specialpages?
51 - */
52 - var $mListed;
53 - /**
54 - * Function name called by the default execute()
55 - */
56 - var $mFunction;
57 - /**
58 - * File which needs to be included before the function above can be called
59 - */
60 - var $mFile;
61 - /**
62 - * Whether or not this special page is being included from an article
63 - */
64 - var $mIncluding;
65 - /**
66 - * Whether the special page can be included in an article
67 - */
68 - var $mIncludable;
69 - /**
70 - * Query parameters that can be passed through redirects
71 - */
72 - var $mAllowedRedirectParams = array();
73 - /**
74 - * Query parameteres added by redirects
75 - */
76 - var $mAddedRedirectParams = array();
77 - /**
7860 * Current request context
7961 * @var RequestContext
8062 */
@@ -380,25 +362,87 @@
381363 }
382364 }
383365
384 - function getName() { return $this->mName; }
385 - function getRestriction() { return $this->mRestriction; }
386 - function getFile() { return $this->mFile; }
387 - function isListed() { return $this->mListed; }
 366+ /**
 367+ * Get the name of this Special Page.
 368+ * @return String
 369+ */
 370+ function getName() {
 371+ return $this->mName;
 372+ }
388373
 374+ /**
 375+ * Get the permission that a user must have to execute this page
 376+ * @return String
 377+ */
 378+ function getRestriction() {
 379+ return $this->mRestriction;
 380+ }
 381+
 382+ /**
 383+ * Get the file which will be included by SpecialPage::execute() if your extension is
 384+ * still stuck in the past and hasn't overridden the execute() method. No modern code
 385+ * should want or need to know this.
 386+ * @return String
 387+ * @deprecated since 1.18
 388+ */
 389+ function getFile() {
 390+ return $this->mFile;
 391+ }
 392+
 393+ // FIXME: decide which syntax to use for this, and stick to it
 394+ /**
 395+ * Whether this special page is listed in Special:SpecialPages
 396+ * @since r3583 (v1.3)
 397+ * @return Bool
 398+ */
 399+ function isListed() {
 400+ return $this->mListed;
 401+ }
 402+ /**
 403+ * Set whether this page is listed in Special:Specialpages, at run-time
 404+ * @since r3583 (v1.3)
 405+ * @return Bool
 406+ */
 407+ function setListed( $listed ) {
 408+ return wfSetVar( $this->mListed, $listed );
 409+ }
 410+ /**
 411+ * Get or set whether this special page is listed in Special:SpecialPages
 412+ * @since r11308 (v1.6)
 413+ * @return Bool
 414+ */
 415+ function listed( $x = null) {
 416+ return wfSetVar( $this->mListed, $x );
 417+ }
 418+
 419+ /**
 420+ * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
 421+ * @return Bool
 422+ */
 423+ public function isIncludable(){
 424+ return $this->mIncludable;
 425+ }
 426+
 427+ /**
 428+ * These mutators are very evil, as the relevant variables should not mutate. So
 429+ * don't use them.
 430+ * @deprecated since 1.18
 431+ */
389432 function name( $x = null ) { return wfSetVar( $this->mName, $x ); }
390 - function restrictions( $x = null) {
391 - # Use the one below this
392 - wfDeprecated( __METHOD__ );
393 - return wfSetVar( $this->mRestriction, $x );
394 - }
395433 function restriction( $x = null) { return wfSetVar( $this->mRestriction, $x ); }
396 - function listed( $x = null) { return wfSetVar( $this->mListed, $x ); }
397434 function func( $x = null) { return wfSetVar( $this->mFunction, $x ); }
398435 function file( $x = null) { return wfSetVar( $this->mFile, $x ); }
399436 function includable( $x = null ) { return wfSetVar( $this->mIncludable, $x ); }
400 - function including( $x = null ) { return wfSetVar( $this->mIncluding, $x ); }
401437
402438 /**
 439+ * Whether the special page is being evaluated via transclusion
 440+ * @return Bool
 441+ */
 442+ function including( $x = null ) {
 443+ return wfSetVar( $this->mIncluding, $x );
 444+ }
 445+
 446+ /**
403447 * Get the localised name of the special page
404448 */
405449 function getLocalName() {
@@ -528,49 +572,6 @@
529573 function getTitle( $subpage = false ) {
530574 return self::getTitleFor( $this->mName, $subpage );
531575 }
532 -
533 - /**
534 - * Set whether this page is listed in Special:Specialpages, at run-time
535 - *
536 - * @return Bool
537 - */
538 - function setListed( $listed ) {
539 - return wfSetVar( $this->mListed, $listed );
540 - }
541 -
542 - /**
543 - * If the special page is a redirect, then get the Title object it redirects to.
544 - * False otherwise.
545 - *
546 - * @return Title|false
547 - */
548 - function getRedirect( $subpage ) {
549 - return false;
550 - }
551 -
552 - /**
553 - * Return part of the request string for a special redirect page
554 - * This allows passing, e.g. action=history to Special:Mypage, etc.
555 - *
556 - * @return String
557 - */
558 - function getRedirectQuery() {
559 - $params = array();
560 -
561 - foreach( $this->mAllowedRedirectParams as $arg ) {
562 - if( $this->getContext()->request->getVal( $arg, null ) !== null ){
563 - $params[$arg] = $this->getContext()->request->getVal( $arg );
564 - }
565 - }
566 -
567 - foreach( $this->mAddedRedirectParams as $arg => $val ) {
568 - $params[$arg] = $val;
569 - }
570 -
571 - return count( $params )
572 - ? $params
573 - : false;
574 - }
575576
576577 /**
577578 * Sets the context this SpecialPage is executed in
@@ -667,6 +668,10 @@
668669 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
669670 parent::__construct( $name, $restriction, false, $function, $file );
670671 }
 672+
 673+ public function isListed(){
 674+ return false;
 675+ }
671676 }
672677
673678 /**
@@ -678,6 +683,10 @@
679684 function __construct( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
680685 parent::__construct( $name, $restriction, $listed, $function, $file, true );
681686 }
 687+
 688+ public function isIncludable(){
 689+ return true;
 690+ }
682691 }
683692
684693 /**
@@ -685,6 +694,13 @@
686695 * @ingroup SpecialPage
687696 */
688697 abstract class SpecialRedirectToSpecial extends UnlistedSpecialPage {
 698+
 699+ // Query parameters that can be passed through redirects
 700+ private $mAllowedRedirectParams = array();
 701+
 702+ // Query parameteres added by redirects
 703+ private $mAddedRedirectParams = array();
 704+
689705 var $redirName, $redirSubpage;
690706
691707 function __construct( $name, $redirName, $redirSubpage = false, $allowedRedirectParams = array(), $addedRedirectParams = array() ) {
@@ -695,13 +711,43 @@
696712 $this->mAddedRedirectParams = $addedRedirectParams;
697713 }
698714
699 - function getRedirect( $subpage ) {
 715+ /**
 716+ * If the special page is a redirect, then get the Title object it redirects to.
 717+ * False otherwise.
 718+ *
 719+ * @return Title|false
 720+ */
 721+ public function getRedirect( $subpage ) {
700722 if ( $this->redirSubpage === false ) {
701723 return SpecialPage::getTitleFor( $this->redirName, $subpage );
702724 } else {
703725 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
704726 }
705727 }
 728+
 729+ /**
 730+ * Return part of the request string for a special redirect page
 731+ * This allows passing, e.g. action=history to Special:Mypage, etc.
 732+ *
 733+ * @return String
 734+ */
 735+ public function getRedirectQuery() {
 736+ $params = array();
 737+
 738+ foreach( $this->mAllowedRedirectParams as $arg ) {
 739+ if( $this->getContext()->request->getVal( $arg, null ) !== null ){
 740+ $params[$arg] = $this->getContext()->request->getVal( $arg );
 741+ }
 742+ }
 743+
 744+ foreach( $this->mAddedRedirectParams as $arg => $val ) {
 745+ $params[$arg] = $val;
 746+ }
 747+
 748+ return count( $params )
 749+ ? $params
 750+ : false;
 751+ }
706752 }
707753
708754 /**
@@ -794,7 +840,7 @@
795841
796842 function getRedirect( $subpage ) {
797843 global $wgUser;
798 - return SpecialPageFactory::getTitleFor( 'Contributions', $wgUser->getName() );
 844+ return SpecialPage::getTitleFor( 'Contributions', $wgUser->getName() );
799845 }
800846 }
801847
@@ -809,7 +855,7 @@
810856
811857 function getRedirect( $subpage ) {
812858 global $wgUser;
813 - return SpecialPageFactory::getTitleFor( 'Listfiles', $wgUser->getName() );
 859+ return SpecialPage::getTitleFor( 'Listfiles', $wgUser->getName() );
814860 }
815861 }
816862
Index: trunk/extensions/Configure/specials/ConfigurationPage.php
@@ -140,7 +140,7 @@
141141 static $allowed = null;
142142 if ( $allowed === null ) {
143143 global $wgUser;
144 - $allowed = $wgUser->isAllowed( $this->mRestriction . '-all' );
 144+ $allowed = $wgUser->isAllowed( $this->getRestriction() . '-all' );
145145 }
146146 return $allowed;
147147 }
@@ -153,7 +153,7 @@
154154 static $allowed = null;
155155 if ( $allowed === null ) {
156156 global $wgUser;
157 - $allowed = $wgUser->isAllowed( $this->mRestriction . '-interwiki' );
 157+ $allowed = $wgUser->isAllowed( $this->getRestriction() . '-interwiki' );
158158 }
159159 return $allowed;
160160 }
Index: trunk/extensions/examples/SpecialIncludable.php
@@ -25,18 +25,17 @@
2626 // See FourFileTemplate how to do i18n
2727 //$wgExtensionMessagesFiles['Includable'] = dirname( __FILE__ ) . '/Includable.i18n.php';
2828
29 -class SpecialIncludable extends SpecialPage {
 29+class SpecialIncludable extends IncludableSpecialPage {
3030 /**
3131 * Constructor
3232 */
3333 function __construct() {
3434 parent::__construct( 'Includable' );
35 - $this->includable( true );
3635 }
3736
3837 /**
39 - * main()
40 - */
 38+ * main()
 39+ */
4140 function execute( $par = null ) {
4241 global $wgOut;
4342

Follow-up revisions

RevisionCommit summaryAuthorDate
r86383Follow-up r86347: fix undefined variable by actually returning the type sugge...happy-melon10:11, 19 April 2011
r86797Follow-up r86347: the $mFile written to in SpecialUndelete is not actually th...happy-melon21:26, 23 April 2011
r90093Followup to r86347, r86797: change SpecialUndelete->mFile to mFilename to rem...brion21:47, 14 June 2011
r90861Comment shuffling, also fixing r86347 CR.happy-melon23:25, 26 June 2011
r106079Followup r106070, r86347: copy some doc comments that apply to multiple funct...brion20:20, 13 December 2011

Comments

#Comment by Nikerabbit (talk | contribs)   05:14, 19 April 2011

Doxygen doesn't catch //, you need /// or the /** */ which were used previously.

#Comment by Raymond (talk | contribs)   08:12, 19 April 2011

Not sure, if this revision is the culprit, but you shuffled a lot in the last days:

Seen on translatewiki:

PHP Notice: Undefined variable: redirect in /www/w/includes/SpecialPageFactory.php on line 449
#Comment by Happy-melon (talk | contribs)   15:42, 19 April 2011

Fixed in r86383

#Comment by Aaron Schulz (talk | contribs)   19:02, 21 June 2011

getGroup( &$page ) docs are wrong.

Status & tagging log