r23311 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23310‎ | r23311 | r23312 >
Date:04:15, 24 June 2007
Author:simetrical
Status:old
Tags:
Comment:
Initial checkin, quite incomplete.
Modified paths:
  • /branches/simetrical/ChangesPage/includes/ChangesPage.php (added) (history)
  • /branches/simetrical/ChangesPage/includes/WebRequest.php (modified) (history)

Diff [purge]

Index: branches/simetrical/ChangesPage/includes/WebRequest.php
@@ -311,6 +311,22 @@
312312 }
313313
314314 /**
 315+ * Fetch a boolean value from the input or return null if not set.
 316+ * Has normal PHP semantics for boolean interpretation of strings.
 317+ *
 318+ * @param string $name
 319+ * @return mixed bool or null
 320+ */
 321+ function getBoolOrNull( $name ) {
 322+ $ret = $this->getVal( $name );
 323+ if( $ret === null ) {
 324+ return null;
 325+ } else {
 326+ return (bool)$ret;
 327+ }
 328+ }
 329+
 330+ /**
315331 * Return true if the named value is set in the input, whatever that
316332 * value is (even "0"). Return false if the named value is not set.
317333 * Example use is checking for the presence of check boxes in forms.
Index: branches/simetrical/ChangesPage/includes/ChangesPage.php
@@ -0,0 +1,378 @@
 2+<?php
 3+
 4+/**
 5+ * Abstract class allowing access to a list of changes to pages, drawn from the
 6+ * recentchanges table.
 7+ *
 8+ * @addtogroup SpecialPage
 9+ * @author Simetrical
 10+ */
 11+
 12+abstract class ChangesPage extends SpecialPage {
 13+ /**
 14+ * $mTimespan takes the form array( begintime, endtime ), where each of
 15+ * begintime and endtime is either a standard MW timestamp or null (since
 16+ * beginning/to present: i.e., no limit).
 17+ */
 18+ protected $mTimespan = array();
 19+
 20+ /**
 21+ * The maximum number of results to show per page, to be passed to the
 22+ * Pager.
 23+ */
 24+ protected $mPerPage = 50;
 25+
 26+ /**
 27+ * An integer namespace number or array of namespace numbers to return
 28+ * results from. null for all namespaces. Derived classes may wish to
 29+ * restrict this variable's values for efficiency by overriding
 30+ * setNamespaces().
 31+ */
 32+ private $mNamespaces = null;
 33+
 34+ /**
 35+ * An array of boolean options to control what edits get displayed.
 36+ * Derived classes should change these defaults if appropriate.
 37+ */
 38+ protected $mShow = array(
 39+ 'own' => true,
 40+ 'bot' => true,
 41+ 'anon' => true,
 42+ 'loggedin' => true,
 43+ 'patrolled' => true,
 44+ 'minor' => true
 45+ );
 46+
 47+ /**
 48+ * Is this an RSS/Atom/whatever feed?
 49+ */
 50+ protected $mFeed = false;
 51+
 52+ /**
 53+ * Derived classes should call this constructor to initialize stuff.
 54+ *
 55+ * @param $restriction What permission, if any, is needed to access
 56+ * @param $listed Whether to list the page on Special:Specialpages.
 57+ * @param $includable Whether the page can be included in non-special
 58+ * pages
 59+ */
 60+ protected __construct( $restriction='', $listed=true, $includable=false ) {
 61+ wfProfileIn( __METHOD__ );
 62+ parent::__construct( '', $restriction, $listed, false, 'default', $includable );
 63+
 64+ // Calculating all these variants is confusing and error-prone to do by
 65+ // hand, so this is split off into a function.
 66+ $this->calcShow( 'own', array( 'hidemyself', 'hideown' );
 67+ $this->calcShow( 'bots', array( 'hidebots', 'hideBots' ) );
 68+ $this->calcShow( 'anon', 'hideanons' );
 69+ $this->calcShow( 'loggedin', 'hideliu' );
 70+ $this->calcShow( 'patrolled', 'hidepatrolled' );
 71+ $this->calcShow( 'minor', 'hideminor' );
 72+
 73+ // It makes no sense to hide both anons and logged-in users. Show
 74+ // anons if this occurs.
 75+ if( $this->mShow['anon'] and $this->mShow['loggedin'] ) {
 76+ $this->mShow['anon'] = true;
 77+ }
 78+
 79+ wfProfileOut( __METHOD__ );
 80+ }
 81+
 82+ /**
 83+ * Based on the query string, decide whether a particular class of edits
 84+ * should be hidden or shown and set member variables appropriately. The
 85+ * preferred URL variant is the first element of the $positive array;
 86+ * others are kept for compatibility. If different parameters conflict,
 87+ * the edits in question are shown.
 88+ *
 89+ * @param $positive Array of strings to check for in the URL, that indicate
 90+ * the class of edits should be shown. The first is the index into
 91+ * $this->mShow and the preferred form; the others are deprecated.
 92+ * @param $negative Array of strings to check for in the URL, that indicate
 93+ * the class of edits should not be shown. All are deprecated.
 94+ * @return nothing
 95+ */
 96+ private static function calcShow( $positive, $negative ) {
 97+ global $wgRequest;
 98+
 99+ if( !is_array( $positive ) ) {
 100+ $positive = array( $positive );
 101+ }
 102+ if( !is_array( $negative ) ) {
 103+ $negative = array( $negative );
 104+ }
 105+ $key = $positive[0];
 106+ foreach( array( $positive, $negative ) as $arr ) {
 107+ foreach( $arr as $param ) {
 108+ $val = $wgRequest->getBoolOrNull( $param );
 109+ if( $val !== null and $arr == $negative ) {
 110+ $val = !$val;
 111+ }
 112+ if( $val === null ) {
 113+ continue;
 114+ } else {
 115+ $this->mShow[$key] = $val;
 116+ if( $val ) {
 117+ // Short-circuit on true values
 118+ return;
 119+ }
 120+ }
 121+ }
 122+ }
 123+ }
 124+
 125+ /**
 126+ * Show the special page
 127+ */
 128+ public function execute() {
 129+ wfProfileIn( __METHOD__ );
 130+
 131+
 132+
 133+ wfProfileOut( __METHOD__ );
 134+ }
 135+
 136+ /**
 137+ * @return an object belonging to a subclass of ChangesPager to do the
 138+ * paging.
 139+ */
 140+ abstract protected function getPager();
 141+}
 142+
 143+/**
 144+ * @addtogroup Pager
 145+ */
 146+abstract class ChangesPager extends ReverseChronologicalPager {
 147+ /** The instance of the ChangesPage class that this corresponds to. */
 148+ protected $mChangesPage = null;
 149+
 150+ /** The user's skin object. */
 151+ protected $mSkin = null;
 152+
 153+ public function __construct( ChangesPage &$changesPage, Skin &$skin ) {
 154+ parent::__construct();
 155+ $this->mChangesPage = &$changesPage;
 156+ $this->mSkin = &$skin;
 157+ }
 158+
 159+ public function formatRow( $row ) {
 160+ wfProfileIn( __METHOD__ );
 161+
 162+ if( !$title->userCan( 'read' ) ) {
 163+ // This should be dealt with on the query level, ideally, but that
 164+ // might not always be possible? This will result in an incorrect
 165+ // number of rows being returned if we just blank it, so return
 166+ // a message instead . . .
 167+ wfProfileOut(__METHOD__);
 168+ return '<li class="mw-'.$this->getRowClass()' mw-hidden">' .
 169+ wfMsgExt( 'hidden-changes-result', array( 'parseinline' ) ) .
 170+ '</li>';
 171+ }
 172+ $ret = '<li class="mw-'.$this->getRowClass()'">';
 173+ if( $this->hasOldId( $row ) ) {
 174+ $ret .= '<a href="' .
 175+ $this->getDiffLink( $row ) .
 176+ '" title="' .
 177+ $this->getPageName( $row ) .
 178+ '" tabindex=';
 179+ }
 180+
 181+ wfProfileOut( __METHOD__ );
 182+ }
 183+
 184+ /**
 185+ * Provide the class that should be used for rows (minus 'mw-' prefix).
 186+ */
 187+ abstract private function getRowClass();
 188+}
 189+
 190+?>
 191+<?php
 192+
 193+/**
 194+ * Abstract class allowing access to a list of changes to pages, drawn from the
 195+ * recentchanges table.
 196+ *
 197+ * @addtogroup SpecialPage
 198+ * @author Simetrical
 199+ */
 200+
 201+abstract class ChangesPage extends SpecialPage {
 202+ /**
 203+ * $mTimespan takes the form array( begintime, endtime ), where each of
 204+ * begintime and endtime is either a standard MW timestamp or null (since
 205+ * beginning/to present: i.e., no limit).
 206+ */
 207+ protected $mTimespan = array();
 208+
 209+ /**
 210+ * The maximum number of results to show per page, to be passed to the
 211+ * Pager.
 212+ */
 213+ protected $mPerPage = 50;
 214+
 215+ /**
 216+ * An integer namespace number or array of namespace numbers to return
 217+ * results from. null for all namespaces. Derived classes may wish to
 218+ * restrict this variable's values for efficiency by overriding
 219+ * setNamespaces().
 220+ */
 221+ private $mNamespaces = null;
 222+
 223+ /**
 224+ * An array of boolean options to control what edits get displayed.
 225+ * Derived classes should change these defaults if appropriate.
 226+ */
 227+ protected $mShow = array(
 228+ 'own' => true,
 229+ 'bot' => true,
 230+ 'anon' => true,
 231+ 'loggedin' => true,
 232+ 'patrolled' => true,
 233+ 'minor' => true
 234+ );
 235+
 236+ /**
 237+ * Is this an RSS/Atom/whatever feed?
 238+ */
 239+ protected $mFeed = false;
 240+
 241+ /**
 242+ * Derived classes should call this constructor to initialize stuff.
 243+ *
 244+ * @param $restriction What permission, if any, is needed to access
 245+ * @param $listed Whether to list the page on Special:Specialpages.
 246+ * @param $includable Whether the page can be included in non-special
 247+ * pages
 248+ */
 249+ protected __construct( $restriction='', $listed=true, $includable=false ) {
 250+ wfProfileIn( __METHOD__ );
 251+ parent::__construct( '', $restriction, $listed, false, 'default', $includable );
 252+
 253+ // Calculating all these variants is confusing and error-prone to do by
 254+ // hand, so this is split off into a function.
 255+ $this->calcShow( 'own', array( 'hidemyself', 'hideown' );
 256+ $this->calcShow( 'bots', array( 'hidebots', 'hideBots' ) );
 257+ $this->calcShow( 'anon', 'hideanons' );
 258+ $this->calcShow( 'loggedin', 'hideliu' );
 259+ $this->calcShow( 'patrolled', 'hidepatrolled' );
 260+ $this->calcShow( 'minor', 'hideminor' );
 261+
 262+ // It makes no sense to hide both anons and logged-in users. Show
 263+ // anons if this occurs.
 264+ if( $this->mShow['anon'] and $this->mShow['loggedin'] ) {
 265+ $this->mShow['anon'] = true;
 266+ }
 267+
 268+ wfProfileOut( __METHOD__ );
 269+ }
 270+
 271+ /**
 272+ * Based on the query string, decide whether a particular class of edits
 273+ * should be hidden or shown and set member variables appropriately. The
 274+ * preferred URL variant is the first element of the $positive array;
 275+ * others are kept for compatibility. If different parameters conflict,
 276+ * the edits in question are shown.
 277+ *
 278+ * @param $positive Array of strings to check for in the URL, that indicate
 279+ * the class of edits should be shown. The first is the index into
 280+ * $this->mShow and the preferred form; the others are deprecated.
 281+ * @param $negative Array of strings to check for in the URL, that indicate
 282+ * the class of edits should not be shown. All are deprecated.
 283+ * @return nothing
 284+ */
 285+ private static function calcShow( $positive, $negative ) {
 286+ global $wgRequest;
 287+
 288+ if( !is_array( $positive ) ) {
 289+ $positive = array( $positive );
 290+ }
 291+ if( !is_array( $negative ) ) {
 292+ $negative = array( $negative );
 293+ }
 294+ $key = $positive[0];
 295+ foreach( array( $positive, $negative ) as $arr ) {
 296+ foreach( $arr as $param ) {
 297+ $val = $wgRequest->getBoolOrNull( $param );
 298+ if( $val !== null and $arr == $negative ) {
 299+ $val = !$val;
 300+ }
 301+ if( $val === null ) {
 302+ continue;
 303+ } else {
 304+ $this->mShow[$key] = $val;
 305+ if( $val ) {
 306+ // Short-circuit on true values
 307+ return;
 308+ }
 309+ }
 310+ }
 311+ }
 312+ }
 313+
 314+ /**
 315+ * Show the special page
 316+ */
 317+ public function execute() {
 318+ wfProfileIn( __METHOD__ );
 319+
 320+
 321+
 322+ wfProfileOut( __METHOD__ );
 323+ }
 324+
 325+ /**
 326+ * @return an object belonging to a subclass of ChangesPager to do the
 327+ * paging.
 328+ */
 329+ abstract protected function getPager();
 330+}
 331+
 332+/**
 333+ * @addtogroup Pager
 334+ */
 335+abstract class ChangesPager extends ReverseChronologicalPager {
 336+ /** The instance of the ChangesPage class that this corresponds to. */
 337+ protected $mChangesPage = null;
 338+
 339+ /** The user's skin object. */
 340+ protected $mSkin = null;
 341+
 342+ public function __construct( ChangesPage &$changesPage, Skin &$skin ) {
 343+ parent::__construct();
 344+ $this->mChangesPage = &$changesPage;
 345+ $this->mSkin = &$skin;
 346+ }
 347+
 348+ public function formatRow( $row ) {
 349+ wfProfileIn( __METHOD__ );
 350+
 351+ if( !$title->userCan( 'read' ) ) {
 352+ // This should be dealt with on the query level, ideally, but that
 353+ // might not always be possible? This will result in an incorrect
 354+ // number of rows being returned if we just blank it, so return
 355+ // a message instead . . .
 356+ wfProfileOut(__METHOD__);
 357+ return '<li class="mw-'.$this->getRowClass()' mw-hidden">' .
 358+ wfMsgExt( 'hidden-changes-result', array( 'parseinline' ) ) .
 359+ '</li>';
 360+ }
 361+ $ret = '<li class="mw-'.$this->getRowClass()'">';
 362+ if( $this->hasOldId( $row ) ) {
 363+ $ret .= '<a href="' .
 364+ $this->getDiffLink( $row ) .
 365+ '" title="' .
 366+ $this->getPageName( $row ) .
 367+ '" tabindex=';
 368+ }
 369+
 370+ wfProfileOut( __METHOD__ );
 371+ }
 372+
 373+ /**
 374+ * Provide the class that should be used for rows (minus 'mw-' prefix).
 375+ */
 376+ abstract private function getRowClass();
 377+}
 378+
 379+?>

Status & tagging log