Add legend and tooltips to explain RC flags
[lhc/web/wiklou.git] / includes / specials / SpecialRecentchanges.php
index 16904b9..04dfb40 100644 (file)
@@ -1,27 +1,33 @@
 <?php
+
 /**
- * @file
+ * Implements Special:Recentchanges
  * @ingroup SpecialPage
  */
-
 class SpecialRecentChanges extends SpecialPage {
        public function __construct() {
-       SpecialPage::SpecialPage( 'Recentchanges' );
+               parent::__construct( 'Recentchanges' );
                $this->includable( true );
        }
 
+       /**
+        * Get a FormOptions object containing the default options
+        *
+        * @return FormOptions
+        */
        public function getDefaultOptions() {
+               global $wgUser;
                $opts = new FormOptions();
 
-               $opts->add( 'days',  (int)User::getDefaultOption( 'rcdays' ) );
-               $opts->add( 'limit', (int)User::getDefaultOption( 'rclimit' ) );
+               $opts->add( 'days',  (int)$wgUser->getOption( 'rcdays' ) );
+               $opts->add( 'limit', (int)$wgUser->getOption( 'rclimit' ) );
                $opts->add( 'from', '' );
 
-               $opts->add( 'hideminor',     false );
+               $opts->add( 'hideminor',     $wgUser->getBoolOption( 'hideminor' ) );
                $opts->add( 'hidebots',      true  );
                $opts->add( 'hideanons',     false );
                $opts->add( 'hideliu',       false );
-               $opts->add( 'hidepatrolled', false );
+               $opts->add( 'hidepatrolled', $wgUser->getBoolOption( 'hidepatrolled' ) );
                $opts->add( 'hidemyself',    false );
 
                $opts->add( 'namespace', '', FormOptions::INTNULL );
@@ -29,110 +35,151 @@ class SpecialRecentChanges extends SpecialPage {
 
                $opts->add( 'categories', '' );
                $opts->add( 'categories_any', false );
-
+               $opts->add( 'tagfilter', '' );
                return $opts;
-}
+       }
 
+       /**
+        * Get a FormOptions object with options as specified by the user
+        *
+        * @return FormOptions
+        */
        public function setup( $parameters ) {
-               global $wgUser, $wgRequest;
+               global $wgRequest;
 
                $opts = $this->getDefaultOptions();
-               $opts['days'] = (int)$wgUser->getOption( 'rcdays', $opts['days'] );
-               $opts['limit'] = (int)$wgUser->getOption( 'rclimit', $opts['limit'] );
-               $opts['hideminor'] = $wgUser->getOption( 'hideminor', $opts['hideminor'] );
                $opts->fetchValuesFromRequest( $wgRequest );
 
                // Give precedence to subpage syntax
-               if ( $parameters !== null ) {
+               if( $parameters !== null ) {
                        $this->parseParameters( $parameters, $opts );
                }
 
-               $opts->validateIntBounds( 'limit', 0, 5000 );
+               $opts->validateIntBounds( 'limit', 0, 500 );
                return $opts;
        }
 
+       /**
+        * Get a FormOptions object sepcific for feed requests
+        *
+        * @return FormOptions
+        */
        public function feedSetup() {
                global $wgFeedLimit, $wgRequest;
                $opts = $this->getDefaultOptions();
-               $opts->fetchValuesFromRequest( $wgRequest, array( 'days', 'limit', 'hideminor' ) );
+               # Feed is cached on limit,hideminor; other params would randomly not work
+               $opts->fetchValuesFromRequest( $wgRequest, array( 'limit', 'hideminor' ) );
                $opts->validateIntBounds( 'limit', 0, $wgFeedLimit );
                return $opts;
        }
 
+       /**
+        * Main execution point
+        *
+        * @param $parameters string
+        */
        public function execute( $parameters ) {
                global $wgRequest, $wgOut;
                $feedFormat = $wgRequest->getVal( 'feed' );
 
                # 10 seconds server-side caching max
                $wgOut->setSquidMaxage( 10 );
-
+               # Check if the client has a cached version
                $lastmod = $this->checkLastModified( $feedFormat );
-               if( $lastmod === false ){
+               if( $lastmod === false ) {
                        return;
                }
 
                $opts = $feedFormat ? $this->feedSetup() : $this->setup( $parameters );
                $this->setHeaders();
+               $this->outputHeader();
 
                // Fetch results, prepare a batch link existence check query
                $rows = array();
-               $batch = new LinkBatch;
                $conds = $this->buildMainQueryConds( $opts );
-               $res = $this->doMainQuery( $conds, $opts );
-               $dbr = wfGetDB( DB_SLAVE );
-               while( $row = $dbr->fetchObject( $res ) ){
-                       $rows[] = $row;
-                       if ( !$feedFormat ) {
-                               // User page and talk links
+               $rows = $this->doMainQuery( $conds, $opts );
+               if( $rows === false ){
+                       if( !$this->including() ) {
+                               $this->doHeader( $opts );
+                       }
+                       return;
+               }
+
+               if( !$feedFormat ) {
+                       $batch = new LinkBatch;
+                       foreach( $rows as $row ) {
                                $batch->add( NS_USER, $row->rc_user_text  );
                                $batch->add( NS_USER_TALK, $row->rc_user_text  );
+                               $batch->add( $row->rc_namespace, $row->rc_title );
                        }
-
+                       $batch->execute();
                }
-               $dbr->freeResult( $res );
-
-               if ( $feedFormat ) {
-                       $feed = new ChangesFeed( $feedFormat, 'rcfeed' );
-                       $feedObj = $feed->getFeedObject(
-                               wfMsgForContent( 'recentchanges' ),
-                               wfMsgForContent( 'recentchanges-feed-description' )
-                       );
-                       $feed->execute( $feedObj, $rows, $opts['limit'], $opts['hideminor'], $lastmod );
+               $target = isset($opts['target']) ? $opts['target'] : ''; // RCL has targets
+               if( $feedFormat ) {
+                       list( $feed, $feedObj ) = $this->getFeedObject( $feedFormat );
+                       $feed->execute( $feedObj, $rows, $opts['limit'], $opts['hideminor'], $lastmod, $target );
                } else {
-                       $batch->execute();
                        $this->webOutput( $rows, $opts );
                }
-       
+
+               $rows->free();
        }
 
+       /**
+        * Return an array with a ChangesFeed object and ChannelFeed object
+        *
+        * @return array
+        */
+       public function getFeedObject( $feedFormat ){
+               $feed = new ChangesFeed( $feedFormat, 'rcfeed' );
+               $feedObj = $feed->getFeedObject(
+                       wfMsgForContent( 'recentchanges' ),
+                       wfMsgForContent( 'recentchanges-feed-description' )
+               );
+               return array( $feed, $feedObj );
+       }
+
+       /**
+        * Process $par and put options found if $opts
+        * Mainly used when including the page
+        *
+        * @param $par String
+        * @param $opts FormOptions
+        */
        public function parseParameters( $par, FormOptions $opts ) {
                $bits = preg_split( '/\s*,\s*/', trim( $par ) );
-               foreach ( $bits as $bit ) {
-                       if ( 'hidebots' === $bit ) $opts['hidebots'] = true;
-                       if ( 'bots' === $bit ) $opts['hidebots'] = false;
-                       if ( 'hideminor' === $bit ) $opts['hideminor'] = true;
-                       if ( 'minor' === $bit ) $opts['hideminor'] = false;
-                       if ( 'hideliu' === $bit ) $opts['hideliu'] = true;
-                       if ( 'hidepatrolled' === $bit ) $opts['hidepatrolled'] = true;
-                       if ( 'hideanons' === $bit ) $opts['hideanons'] = true;
-                       if ( 'hidemyself' === $bit ) $opts['hidemyself'] = true;
-
-                       if ( is_numeric( $bit ) ) $opts['limit'] =  $bit;
+               foreach( $bits as $bit ) {
+                       if( 'hidebots' === $bit ) $opts['hidebots'] = true;
+                       if( 'bots' === $bit ) $opts['hidebots'] = false;
+                       if( 'hideminor' === $bit ) $opts['hideminor'] = true;
+                       if( 'minor' === $bit ) $opts['hideminor'] = false;
+                       if( 'hideliu' === $bit ) $opts['hideliu'] = true;
+                       if( 'hidepatrolled' === $bit ) $opts['hidepatrolled'] = true;
+                       if( 'hideanons' === $bit ) $opts['hideanons'] = true;
+                       if( 'hidemyself' === $bit ) $opts['hidemyself'] = true;
+
+                       if( is_numeric( $bit ) ) $opts['limit'] =  $bit;
 
                        $m = array();
-                       if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) $opts['limit'] = $m[1];
-                       if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) $opts['days'] = $m[1];
+                       if( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) $opts['limit'] = $m[1];
+                       if( preg_match( '/^days=(\d+)$/', $bit, $m ) ) $opts['days'] = $m[1];
                }
        }
 
-       # Get last modified date, for client caching
-       # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
+       /**
+        * Get last modified date, for client caching
+        * Don't use this if we are using the patrol feature, patrol changes don't
+        * update the timestamp
+        *
+        * @param $feedFormat String
+        * @return string or false
+        */
        public function checkLastModified( $feedFormat ) {
                global $wgUseRCPatrol, $wgOut;
                $dbr = wfGetDB( DB_SLAVE );
-               $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __FUNCTION__ );
-               if ( $feedFormat || !$wgUseRCPatrol ) {
-                       if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
+               $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__ );
+               if( $feedFormat || !$wgUseRCPatrol ) {
+                       if( $lastmod && $wgOut->checkLastModified( $lastmod ) ) {
                                # Client cache fresh and headers sent, nothing more to do.
                                return false;
                        }
@@ -140,6 +187,12 @@ class SpecialRecentChanges extends SpecialPage {
                return $lastmod;
        }
 
+       /**
+        * Return an array of conditions depending of options set in $opts
+        *
+        * @param $opts FormOptions
+        * @return array
+        */
        public function buildMainQueryConds( FormOptions $opts ) {
                global $wgUser;
 
@@ -178,12 +231,12 @@ class SpecialRecentChanges extends SpecialPage {
                $hideLoggedInUsers = $opts['hideliu'] && !$forcebot;
                $hideAnonymousUsers = $opts['hideanons'] && !$forcebot;
 
-               if ( $opts['hideminor'] )  $conds['rc_minor'] = 0;
-               if ( $opts['hidebots'] )   $conds['rc_bot'] = 0;
-               if ( $hidePatrol )         $conds['rc_patrolled'] = 0;
-               if ( $forcebot )           $conds['rc_bot'] = 1;
-               if ( $hideLoggedInUsers )  $conds[] = 'rc_user = 0';
-               if ( $hideAnonymousUsers ) $conds[] = 'rc_user != 0';
+               if( $opts['hideminor'] )  $conds['rc_minor'] = 0;
+               if( $opts['hidebots'] )   $conds['rc_bot'] = 0;
+               if( $hidePatrol )         $conds['rc_patrolled'] = 0;
+               if( $forcebot )           $conds['rc_bot'] = 1;
+               if( $hideLoggedInUsers )  $conds[] = 'rc_user = 0';
+               if( $hideAnonymousUsers ) $conds[] = 'rc_user != 0';
 
                if( $opts['hidemyself'] ) {
                        if( $wgUser->getId() ) {
@@ -192,10 +245,10 @@ class SpecialRecentChanges extends SpecialPage {
                                $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $wgUser->getName() );
                        }
                }
-               
+
                # Namespace filtering
-               if ( $opts['namespace'] !== '' ) {
-                       if ( !$opts['invert'] ) {
+               if( $opts['namespace'] !== '' ) {
+                       if( !$opts['invert'] ) {
                                $conds[] = 'rc_namespace = ' . $dbr->addQuotes( $opts['namespace'] );
                        } else {
                                $conds[] = 'rc_namespace != ' . $dbr->addQuotes( $opts['namespace'] );
@@ -205,11 +258,19 @@ class SpecialRecentChanges extends SpecialPage {
                return $conds;
        }
 
+       /**
+        * Process the query
+        *
+        * @param $conds array
+        * @param $opts FormOptions
+        * @return database result or false (for Recentchangeslinked only)
+        */
        public function doMainQuery( $conds, $opts ) {
                global $wgUser;
 
                $tables = array( 'recentchanges' );
                $join_conds = array();
+               $query_options = array( 'USE INDEX' => array('recentchanges' => 'rc_timestamp') );
 
                $uid = $wgUser->getId();
                $dbr = wfGetDB( DB_SLAVE );
@@ -217,20 +278,38 @@ class SpecialRecentChanges extends SpecialPage {
                $namespace = $opts['namespace'];
                $invert = $opts['invert'];
 
+               $join_conds = array();
+
                // JOIN on watchlist for users
-               if( $wgUser->getId() ) {
+               if( $uid ) {
                        $tables[] = 'watchlist';
-                       $join_conds = array( 'watchlist' => array('LEFT JOIN',"wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace") );
+                       $join_conds['watchlist'] = array('LEFT JOIN',
+                               "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace");
+               }
+               if ($wgUser->isAllowed("rollback")) {
+                       $tables[] = 'page';
+                       $join_conds['page'] = array('LEFT JOIN', 'rc_cur_id=page_id');
                }
+               // Tag stuff.
+               $fields = array();
+               // Fields are * in this case, so let the function modify an empty array to keep it happy.
+               ChangeTags::modifyDisplayQuery( $tables,
+                                                                               $fields,
+                                                                               $conds,
+                                                                               $join_conds,
+                                                                               $query_options,
+                                                                               $opts['tagfilter']
+                                                                       );
 
                wfRunHooks('SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts ) );
 
                // Is there either one namespace selected or excluded?
+               // Tag filtering also has a better index.
                // Also, if this is "all" or main namespace, just use timestamp index.
-               if( is_null($namespace) || $invert || $namespace == NS_MAIN ) {
+               if( is_null($namespace) || $invert || $opts['tagfilter'] ) {
                        $res = $dbr->select( $tables, '*', $conds, __METHOD__,
-                               array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit
-                                       'USE INDEX' => array('recentchanges' => 'rc_timestamp') ),
+                               array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ) +
+                               $query_options,
                                $join_conds );
                // We have a new_namespace_time index! UNION over new=(0,1) and sort result set!
                } else {
@@ -238,31 +317,38 @@ class SpecialRecentChanges extends SpecialPage {
                        $sqlNew = $dbr->selectSQLText( $tables, '*',
                                array( 'rc_new' => 1 ) + $conds,
                                __METHOD__,
-                               array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit, 
-                                       'USE INDEX' =>  array('recentchanges' => 'new_name_timestamp') ),
+                               array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
+                                       'USE INDEX' =>  array('recentchanges' => 'rc_timestamp') ),
                                $join_conds );
                        // Old pages
                        $sqlOld = $dbr->selectSQLText( $tables, '*',
                                array( 'rc_new' => 0 ) + $conds,
                                __METHOD__,
-                               array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit, 
-                                       'USE INDEX' =>  array('recentchanges' => 'new_name_timestamp') ),
+                               array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
+                                       'USE INDEX' =>  array('recentchanges' => 'rc_timestamp') ),
                                $join_conds );
                        # Join the two fast queries, and sort the result set
-                       $sql = "($sqlNew) UNION ($sqlOld) ORDER BY rc_timestamp DESC LIMIT $limit";
+                       $sql = $dbr->unionQueries(array($sqlNew, $sqlOld), false).' ORDER BY rc_timestamp DESC';
+                       $sql = $dbr->limitResult($sql, $limit, false);
                        $res = $dbr->query( $sql, __METHOD__ );
                }
 
                return $res;
        }
 
+       /**
+        * Send output to $wgOut, only called if not used feeds
+        *
+        * @param $rows array of database rows
+        * @param $opts FormOptions
+        */
        public function webOutput( $rows, $opts ) {
                global $wgOut, $wgUser, $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
                global $wgAllowCategorizedRecentChanges;
 
                $limit = $opts['limit'];
 
-               if ( !$this->including() ) {
+               if( !$this->including() ) {
                        // Output options box
                        $this->doHeader( $opts );
                }
@@ -270,98 +356,94 @@ class SpecialRecentChanges extends SpecialPage {
                // And now for the content
                $wgOut->setSyndicated( true );
 
-               $list = ChangesList::newFromUser( $wgUser );
-
-               if ( $wgAllowCategorizedRecentChanges ) {
-                       rcFilterByCategories( $rows, $opts );
+               if( $wgAllowCategorizedRecentChanges ) {
+                       $this->filterByCategories( $rows, $opts );
                }
 
-               $s = $list->beginRecentChangesList();
-               $counter = 1;
-
                $showWatcherCount = $wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' );
                $watcherCache = array();
 
                $dbr = wfGetDB( DB_SLAVE );
 
-               foreach( $rows as $obj ){
-                       if( $limit == 0) {
-                               break;
-                       }
-
-                       if ( ! ( $opts['hideminor']     && $obj->rc_minor     ) &&
-                            ! ( $opts['hidepatrolled'] && $obj->rc_patrolled ) ) {
-                               $rc = RecentChange::newFromRow( $obj );
-                               $rc->counter = $counter++;
-
-                               if ($wgShowUpdatedMarker
-                                       && !empty( $obj->wl_notificationtimestamp )
-                                       && ($obj->rc_timestamp >= $obj->wl_notificationtimestamp)) {
-                                               $rc->notificationtimestamp = true;
-                               } else {
-                                       $rc->notificationtimestamp = false;
-                               }
+               $counter = 1;
+               $list = ChangesList::newFromUser( $wgUser );
 
-                               $rc->numberofWatchingusers = 0; // Default
-                               if ($showWatcherCount && $obj->rc_namespace >= 0) {
-                                       if (!isset($watcherCache[$obj->rc_namespace][$obj->rc_title])) {
-                                               $watcherCache[$obj->rc_namespace][$obj->rc_title] =
-                                                       $dbr->selectField( 'watchlist',
-                                                               'COUNT(*)',
-                                                               array(
-                                                                       'wl_namespace' => $obj->rc_namespace,
-                                                                       'wl_title' => $obj->rc_title,
-                                                               ),
-                                                               __METHOD__ . '-watchers' );
-                                       }
-                                       $rc->numberofWatchingusers = $watcherCache[$obj->rc_namespace][$obj->rc_title];
+               $s = $list->beginRecentChangesList();
+               foreach( $rows as $obj ) {
+                       if( $limit == 0 ) break;
+                       $rc = RecentChange::newFromRow( $obj );
+                       $rc->counter = $counter++;
+                       # Check if the page has been updated since the last visit
+                       if( $wgShowUpdatedMarker && !empty($obj->wl_notificationtimestamp) ) {
+                               $rc->notificationtimestamp = ($obj->rc_timestamp >= $obj->wl_notificationtimestamp);
+                       } else {
+                               $rc->notificationtimestamp = false; // Default
+                       }
+                       # Check the number of users watching the page
+                       $rc->numberofWatchingusers = 0; // Default
+                       if( $showWatcherCount && $obj->rc_namespace >= 0 ) {
+                               if( !isset($watcherCache[$obj->rc_namespace][$obj->rc_title]) ) {
+                                       $watcherCache[$obj->rc_namespace][$obj->rc_title] =
+                                                $dbr->selectField( 'watchlist',
+                                                       'COUNT(*)',
+                                                       array(
+                                                               'wl_namespace' => $obj->rc_namespace,
+                                                               'wl_title' => $obj->rc_title,
+                                                       ),
+                                                       __METHOD__ . '-watchers' );
                                }
-                               $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ) );
-                               --$limit;
+                               $rc->numberofWatchingusers = $watcherCache[$obj->rc_namespace][$obj->rc_title];
                        }
+                       $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ), $counter );
+                       --$limit;
                }
                $s .= $list->endRecentChangesList();
                $wgOut->addHTML( $s );
        }
 
+       /**
+        * Return the text to be displayed above the changes
+        *
+        * @param $opts FormOptions
+        * @return String: XHTML
+        */
        public function doHeader( $opts ) {
                global $wgScript, $wgOut;
-               $wgOut->addWikiText( wfMsgForContentNoTrans( 'recentchangestext' ) );
+
+               $this->setTopText( $wgOut, $opts );
 
                $defaults = $opts->getAllValues();
                $nondefaults = $opts->getChangedValues();
-               $opts->consumeValues( array( 'namespace', 'invert' ) );
+               $opts->consumeValues( array( 'namespace', 'invert', 'tagfilter' ) );
 
                $panel = array();
-               $panel[] = rcOptionsPanel( $defaults, $nondefaults );
+               $panel[] = $this->optionsPanel( $defaults, $nondefaults );
                $panel[] = '<hr />';
 
-               $extraOpts = array();
-               $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
+               $extraOpts = $this->getExtraOptions( $opts );
+               $extraOptsCount = count( $extraOpts );
+               $count = 0;
+               $submit = ' ' . Xml::submitbutton( wfMsg( 'allpagessubmit' ) );
 
-               global $wgAllowCategorizedRecentChanges;
-               if ( $wgAllowCategorizedRecentChanges ) {
-                       $extraOpts['category'] = $this->categoryFilterForm( $opts );
-               }
+               $out = Xml::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
+               foreach( $extraOpts as $optionRow ) {
+                       # Add submit button to the last row only
+                       ++$count;
+                       $addSubmit = $count === $extraOptsCount ? $submit : '';
 
-               wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
-               $extraOpts['submit'] = Xml::submitbutton( wfMsg('allpagessubmit') );
-
-               $out = Xml::openElement( 'table' );
-               foreach ( $extraOpts as $optionRow ) {
                        $out .= Xml::openElement( 'tr' );
-                       if ( is_array($optionRow) ) {
-                               $out .= Xml::tags( 'td', null, $optionRow[0] );
-                               $out .= Xml::tags( 'td', null, $optionRow[1] );
+                       if( is_array( $optionRow ) ) {
+                               $out .= Xml::tags( 'td', array( 'class' => 'mw-label' ), $optionRow[0] );
+                               $out .= Xml::tags( 'td', array( 'class' => 'mw-input' ), $optionRow[1] . $addSubmit );
                        } else {
-                               $out .= Xml::tags( 'td', array( 'colspan' => 2 ), $optionRow );
+                               $out .= Xml::tags( 'td', array( 'class' => 'mw-input', 'colspan' => 2 ), $optionRow . $addSubmit );
                        }
                        $out .= Xml::closeElement( 'tr' );
                }
                $out .= Xml::closeElement( 'table' );
 
                $unconsumed = $opts->getUnconsumedValues();
-               foreach ( $unconsumed as $key => $value ) {
+               foreach( $unconsumed as $key => $value ) {
                        $out .= Xml::hidden( $key, $value );
                }
 
@@ -372,22 +454,80 @@ class SpecialRecentChanges extends SpecialPage {
                $panelString = implode( "\n", $panel );
 
                $wgOut->addHTML(
-                       Xml::fieldset( wfMsg( 'recentchanges' ), $panelString, array( 'class' => 'rcoptions' ) )
+                       Xml::fieldset( wfMsg( 'recentchanges-legend' ), $panelString, array( 'class' => 'rcoptions' ) )
                );
+
+               # TODO: This is probably a bad format for the message.  If anyone
+               # customizes it and we add a new flag, it won't show up in the
+               # customized message unless it's changed.
+               $wgOut->addWikiMsg( 'recentchanges-label-legend',
+                       ChangesList::flag( 'newpage' ), ChangesList::flag( 'minor' ),
+                       ChangesList::flag( 'bot' ), ChangesList::flag( 'unpatrolled' ) );
+
+               $this->setBottomText( $wgOut, $opts );
        }
 
        /**
-       * Creates the choose namespace selection
-       *
-       * @return string
-       */
+        * Get options to be displayed in a form
+        *
+        * @param $opts FormOptions
+        * @return array
+        */
+       function getExtraOptions( $opts ){
+               $extraOpts = array();
+               $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
+
+               global $wgAllowCategorizedRecentChanges;
+               if( $wgAllowCategorizedRecentChanges ) {
+                       $extraOpts['category'] = $this->categoryFilterForm( $opts );
+               }
+
+               $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
+               if ( count($tagFilter) )
+                       $extraOpts['tagfilter'] = $tagFilter;
+
+               wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
+               return $extraOpts;
+       }
+
+       /**
+        * Send the text to be displayed above the options
+        *
+        * @param $out OutputPage
+        * @param $opts FormOptions
+        */
+       function setTopText( OutputPage $out, FormOptions $opts ){
+               $out->addWikiText( wfMsgForContentNoTrans( 'recentchangestext' ) );
+       }
+
+       /**
+        * Send the text to be displayed after the options, for use in
+        * Recentchangeslinked
+        *
+        * @param $out OutputPage
+        * @param $opts FormOptions
+        */
+       function setBottomText( OutputPage $out, FormOptions $opts ){}
+
+       /**
+        * Creates the choose namespace selection
+        *
+        * @param $opts FormOptions
+        * @return string
+        */
        protected function namespaceFilterForm( FormOptions $opts ) {
-               $nsSelect = HTMLnamespaceselector( $opts['namespace'], '' );
+               $nsSelect = Xml::namespaceSelector( $opts['namespace'], '' );
                $nsLabel = Xml::label( wfMsg('namespace'), 'namespace' );
                $invert = Xml::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $opts['invert'] );
                return array( $nsLabel, "$nsSelect $invert" );
        }
 
+       /**
+        * Create a input to filter changes by categories
+        *
+        * @param $opts FormOptions
+        * @return array
+        */
        protected function categoryFilterForm( FormOptions $opts ) {
                list( $label, $input ) = Xml::inputLabelSep( wfMsg('rc_categories'),
                        'categories', 'mw-categories', false, $opts['categories'] );
@@ -398,216 +538,157 @@ class SpecialRecentChanges extends SpecialPage {
                return array( $label, $input );
        }
 
-}
-
-function rcFilterByCategories ( &$rows, FormOptions $opts ) {
-       $categories = array_map( 'trim', explode( "|" , $categories ) );
-
-       if( empty($categories) ) {
-               return;
-       }
+       /**
+        * Filter $rows by categories set in $opts
+        *
+        * @param $rows array of database rows
+        * @param $opts FormOptions
+        */
+       function filterByCategories( &$rows, FormOptions $opts ) {
+               $categories = array_map( 'trim', explode( "|" , $opts['categories'] ) );
+
+               if( empty($categories) ) {
+                       return;
+               }
 
-       # Filter categories
-       $cats = array();
-       foreach ( $opts['categories'] AS $cat ) {
-               $cat = trim( $cat );
-               if ( $cat == "" ) continue;
-               $cats[] = $cat;
-       }
+               # Filter categories
+               $cats = array();
+               foreach( $categories as $cat ) {
+                       $cat = trim( $cat );
+                       if( $cat == "" ) continue;
+                       $cats[] = $cat;
+               }
 
-       # Filter articles
-       $articles = array();
-       $a2r = array();
-       foreach ( $rows AS $k => $r ) {
-               $nt = Title::makeTitle( $r->rc_namespace, $r->rc_title );
-               $id = $nt->getArticleID();
-               if ( $id == 0 ) continue; # Page might have been deleted...
-               if ( !in_array($id, $articles) ) {
-                       $articles[] = $id;
+               # Filter articles
+               $articles = array();
+               $a2r = array();
+               foreach( $rows AS $k => $r ) {
+                       $nt = Title::makeTitle( $r->rc_namespace, $r->rc_title );
+                       $id = $nt->getArticleID();
+                       if( $id == 0 ) continue; # Page might have been deleted...
+                       if( !in_array($id, $articles) ) {
+                               $articles[] = $id;
+                       }
+                       if( !isset($a2r[$id]) ) {
+                               $a2r[$id] = array();
+                       }
+                       $a2r[$id][] = $k;
                }
-               if ( !isset($a2r[$id]) ) {
-                       $a2r[$id] = array();
+
+               # Shortcut?
+               if( !count($articles) || !count($cats) )
+                       return ;
+
+               # Look up
+               $c = new Categoryfinder ;
+               $c->seed( $articles, $cats, $opts['categories_any'] ? "OR" : "AND" ) ;
+               $match = $c->run();
+
+               # Filter
+               $newrows = array();
+               foreach( $match AS $id ) {
+                       foreach( $a2r[$id] AS $rev ) {
+                               $k = $rev;
+                               $newrows[$k] = $rows[$k];
+                       }
                }
-               $a2r[$id][] = $k;
+               $rows = $newrows;
        }
 
-       # Shortcut?
-       if ( !count($articles) || !count($cats) )
-               return ;
-
-       # Look up
-       $c = new Categoryfinder ;
-       $c->seed( $articles, $cats, $opts['categories_any'] ? "OR" : "AND" ) ;
-       $match = $c->run();
-
-       # Filter
-       $newrows = array();
-       foreach ( $match AS $id ) {
-               foreach ( $a2r[$id] AS $rev ) {
-                       $k = $rev;
-                       $newrows[$k] = $rows[$k];
+       /**
+        * Makes change an option link which carries all the other options
+        * @param $title see Title
+        * @param $override
+        * @param $options
+        */
+       function makeOptionsLink( $title, $override, $options, $active = false ) {
+               global $wgUser;
+               $sk = $wgUser->getSkin();
+               $params = $override + $options;
+               if ( $active ) {
+                       return $sk->link( $this->getTitle(), '<strong>' . htmlspecialchars( $title ) . '</strong>',
+                                                         array(), $params, array( 'known' ) );
+               } else {
+                       return $sk->link( $this->getTitle(), htmlspecialchars( $title ), array() , $params, array( 'known' ) );
                }
        }
-       $rows = $newrows;
-}
 
-/**
- *
- */
-function rcCountLink( $lim, $d, $page='Recentchanges', $more='', $active = false ) {
-       global $wgUser, $wgLang, $wgContLang;
-       $sk = $wgUser->getSkin();
-       $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
-         ($lim ? $wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
-         ($d ? "days={$d}&" : '') . 'limit='.$lim, '', '',
-         $active ? 'style="font-weight: bold;"' : '' );
-       return $s;
-}
+       /**
+        * Creates the options panel.
+        * @param $defaults array
+        * @param $nondefaults array
+        */
+       function optionsPanel( $defaults, $nondefaults ) {
+               global $wgLang, $wgUser, $wgRCLinkLimits, $wgRCLinkDays;
+
+               $options = $nondefaults + $defaults;
+
+               $note = '';
+               if( !wfEmptyMsg( 'rclegend', wfMsg('rclegend') ) ) {
+                       $note .= '<div class="mw-rclegend">' . wfMsgExt( 'rclegend', array('parseinline') ) . "</div>\n";
+               }
+               if( $options['from'] ) {
+                       $note .= wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
+                               $wgLang->formatNum( $options['limit'] ),
+                               $wgLang->timeanddate( $options['from'], true ),
+                               $wgLang->date( $options['from'], true ),
+                               $wgLang->time( $options['from'], true ) ) . '<br />';
+               }
 
-/**
- *
- */
-function rcDaysLink( $lim, $d, $page='Recentchanges', $more='', $active = false ) {
-       global $wgUser, $wgLang, $wgContLang;
-       $sk = $wgUser->getSkin();
-       $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
-         ($d ? $wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
-         ($lim ? '&limit='.$lim : ''), '', '',
-         $active ? 'style="font-weight: bold;"' : '' );
-       return $s;
-}
+               # Sort data for display and make sure it's unique after we've added user data.
+               $wgRCLinkLimits[] = $options['limit'];
+               $wgRCLinkDays[] = $options['days'];
+               sort( $wgRCLinkLimits );
+               sort( $wgRCLinkDays );
+               $wgRCLinkLimits = array_unique( $wgRCLinkLimits );
+               $wgRCLinkDays = array_unique( $wgRCLinkDays );
+
+               // limit links
+               foreach( $wgRCLinkLimits as $value ) {
+                       $cl[] = $this->makeOptionsLink( $wgLang->formatNum( $value ),
+                               array( 'limit' => $value ), $nondefaults, $value == $options['limit'] ) ;
+               }
+               $cl = $wgLang->pipeList( $cl );
 
-/**
- * Used by Recentchangeslinked
- */
-function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
-       $botLink = '', $liuLink = '', $patrLink = '', $myselfLink = '' ) {
-       global $wgRCLinkLimits, $wgRCLinkDays;
-       if ($more != '') $more .= '&';
-       
-       # Sort data for display and make sure it's unique after we've added user data.
-       # FIXME: why does this piss around with globals like this? Why is $limit added on globally?
-       $wgRCLinkLimits[] = $limit;
-       $wgRCLinkDays[] = $days;
-       sort($wgRCLinkLimits);
-       sort($wgRCLinkDays);
-       $wgRCLinkLimits = array_unique($wgRCLinkLimits);
-       $wgRCLinkDays = array_unique($wgRCLinkDays);
-       
-       $cl = array();
-       foreach( $wgRCLinkLimits as $countLink ) {
-               $cl[] = rcCountLink( $countLink, $days, $page, $more, $countLink == $limit );
-       }
-       if( $doall ) $cl[] = rcCountLink( 0, $days, $page, $more );
-       $cl = implode( ' | ', $cl);
-       
-       $dl = array();
-       foreach( $wgRCLinkDays as $daysLink ) {
-               $dl[] = rcDaysLink( $limit, $daysLink, $page, $more, $daysLink == $days );
-       }
-       if( $doall ) $dl[] = rcDaysLink( $limit, 0, $page, $more );
-       $dl = implode( ' | ', $dl);
-       
-       $linkParts = array( 'minorLink' => 'minor', 'botLink' => 'bots', 'liuLink' => 'liu', 'patrLink' => 'patr', 'myselfLink' => 'mine' );
-       foreach( $linkParts as $linkVar => $linkMsg ) {
-               if( $$linkVar != '' )
-                       $links[] = wfMsgHtml( 'rcshowhide' . $linkMsg, $$linkVar );
+               // day links, reset 'from' to none
+               foreach( $wgRCLinkDays as $value ) {
+                       $dl[] = $this->makeOptionsLink( $wgLang->formatNum( $value ),
+                               array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] ) ;
+               }
+               $dl = $wgLang->pipeList( $dl );
+
+
+               // show/hide links
+               $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
+               $minorLink = $this->makeOptionsLink( $showhide[1-$options['hideminor']],
+                       array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
+               $botLink = $this->makeOptionsLink( $showhide[1-$options['hidebots']],
+                       array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
+               $anonsLink = $this->makeOptionsLink( $showhide[ 1 - $options['hideanons'] ],
+                       array( 'hideanons' => 1 - $options['hideanons'] ), $nondefaults );
+               $liuLink   = $this->makeOptionsLink( $showhide[1-$options['hideliu']],
+                       array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
+               $patrLink  = $this->makeOptionsLink( $showhide[1-$options['hidepatrolled']],
+                       array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
+               $myselfLink = $this->makeOptionsLink( $showhide[1-$options['hidemyself']],
+                       array( 'hidemyself' => 1-$options['hidemyself'] ), $nondefaults);
+
+               $links[] = wfMsgHtml( 'rcshowhideminor', $minorLink );
+               $links[] = wfMsgHtml( 'rcshowhidebots', $botLink );
+               $links[] = wfMsgHtml( 'rcshowhideanons', $anonsLink );
+               $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
+               if( $wgUser->useRCPatrol() )
+                       $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
+               $links[] = wfMsgHtml( 'rcshowhidemine', $myselfLink );
+               $hl = $wgLang->pipeList( $links );
+
+               // show from this onward link
+               $now = $wgLang->timeanddate( wfTimestampNow(), true );
+               $tl =  $this->makeOptionsLink( $now, array( 'from' => wfTimestampNow() ), $nondefaults );
+
+               $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter' ),
+                       $cl, $dl, $hl );
+               $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter' ), $tl );
+               return "{$note}$rclinks<br />$rclistfrom";
        }
-
-       $shm = implode( ' | ', $links );
-       $note = wfMsg( 'rclinks', $cl, $dl, $shm );
-       return $note;
-}
-
-
-/**
- * Makes change an option link which carries all the other options
- * @param $title see Title
- * @param $override
- * @param $options
- */
-function makeOptionsLink( $title, $override, $options, $active = false ) {
-       global $wgUser, $wgContLang;
-       $sk = $wgUser->getSkin();
-       return $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
-               htmlspecialchars( $title ), wfArrayToCGI( $override, $options ), '', '',
-               $active ? 'style="font-weight: bold;"' : '' );
 }
-
-/**
- * Creates the options panel.
- * @param $defaults
- * @param $nondefaults
- */
-function rcOptionsPanel( $defaults, $nondefaults ) {
-       global $wgLang, $wgUser, $wgRCLinkLimits, $wgRCLinkDays;
-
-       $options = $nondefaults + $defaults;
-
-       if( $options['from'] )
-               $note = wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
-                       $wgLang->formatNum( $options['limit'] ),
-                       $wgLang->timeanddate( $options['from'], true ) );
-       else
-               $note = wfMsgExt( 'rcnote', array( 'parseinline' ),
-                       $wgLang->formatNum( $options['limit'] ),
-                       $wgLang->formatNum( $options['days'] ),
-                       $wgLang->timeAndDate( wfTimestampNow(), true ) );
-
-       # Sort data for display and make sure it's unique after we've added user data.
-       $wgRCLinkLimits[] = $options['limit'];
-       $wgRCLinkDays[] = $options['days'];
-       sort($wgRCLinkLimits);
-       sort($wgRCLinkDays);
-       $wgRCLinkLimits = array_unique($wgRCLinkLimits);
-       $wgRCLinkDays = array_unique($wgRCLinkDays);
-       
-       // limit links
-       foreach( $wgRCLinkLimits as $value ) {
-               $cl[] = makeOptionsLink( $wgLang->formatNum( $value ),
-                       array( 'limit' => $value ), $nondefaults, $value == $options['limit'] ) ;
-       }
-       $cl = implode( ' | ', $cl);
-
-       // day links, reset 'from' to none
-       foreach( $wgRCLinkDays as $value ) {
-               $dl[] = makeOptionsLink( $wgLang->formatNum( $value ),
-                       array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] ) ;
-       }
-       $dl = implode( ' | ', $dl);
-
-
-       // show/hide links
-       $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
-       $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
-               array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
-       $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
-               array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
-       $anonsLink = makeOptionsLink( $showhide[ 1 - $options['hideanons'] ],
-               array( 'hideanons' => 1 - $options['hideanons'] ), $nondefaults );
-       $liuLink   = makeOptionsLink( $showhide[1-$options['hideliu']],
-               array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
-       $patrLink  = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
-               array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
-       $myselfLink = makeOptionsLink( $showhide[1-$options['hidemyself']],
-               array( 'hidemyself' => 1-$options['hidemyself'] ), $nondefaults);
-
-       $links[] = wfMsgHtml( 'rcshowhideminor', $minorLink );
-       $links[] = wfMsgHtml( 'rcshowhidebots', $botLink );
-       $links[] = wfMsgHtml( 'rcshowhideanons', $anonsLink );
-       $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
-       if( $wgUser->useRCPatrol() )
-               $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
-       $links[] = wfMsgHtml( 'rcshowhidemine', $myselfLink );
-       $hl = implode( ' | ', $links );
-
-       // show from this onward link
-       $now = $wgLang->timeanddate( wfTimestampNow(), true );
-       $tl =  makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
-
-       $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter'),
-               $cl, $dl, $hl );
-       $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter'), $tl );
-       return "$note<br />$rclinks<br />$rclistfrom";
-
-}
\ No newline at end of file