Merge "Add PreferencesFormPreSave hook"
[lhc/web/wiklou.git] / includes / specials / SpecialRecentchanges.php
index 8a119f5..7352de7 100644 (file)
  *
  * @ingroup SpecialPage
  */
-class SpecialRecentChanges extends SpecialPage {
-       var $rcOptions, $rcSubpage;
-       protected $customFilters;
-
-       /**
-        * The feed format to output as (either 'rss' or 'atom'), or null if no
-        * feed output was requested
-        *
-        * @var string $feedFormat
-        */
-       protected $feedFormat;
+class SpecialRecentChanges extends ChangesListSpecialPage {
 
        public function __construct( $name = 'Recentchanges', $restriction = '' ) {
                parent::__construct( $name, $restriction );
        }
 
-       public function isIncludable() {
-               return true;
+       /**
+        * Main execution point
+        *
+        * @param string $subpage
+        */
+       public function execute( $subpage ) {
+               // 10 seconds server-side caching max
+               $this->getOutput()->setSquidMaxage( 10 );
+               // Check if the client has a cached version
+               $lastmod = $this->checkLastModified( $this->feedFormat );
+               if ( $lastmod === false ) {
+                       return;
+               }
+
+               parent::execute( $subpage );
        }
 
        /**
@@ -52,7 +55,7 @@ class SpecialRecentChanges extends SpecialPage {
         * @return FormOptions
         */
        public function getDefaultOptions() {
-               $opts = new FormOptions();
+               $opts = parent::getDefaultOptions();
                $user = $this->getUser();
 
                $opts->add( 'days', $user->getIntOption( 'rcdays' ) );
@@ -66,10 +69,6 @@ class SpecialRecentChanges extends SpecialPage {
                $opts->add( 'hidepatrolled', $user->getBoolOption( 'hidepatrolled' ) );
                $opts->add( 'hidemyself', false );
 
-               $opts->add( 'namespace', '', FormOptions::INTNULL );
-               $opts->add( 'invert', false );
-               $opts->add( 'associated', false );
-
                $opts->add( 'categories', '' );
                $opts->add( 'categories_any', false );
                $opts->add( 'tagfilter', '' );
@@ -77,33 +76,6 @@ class SpecialRecentChanges extends SpecialPage {
                return $opts;
        }
 
-       /**
-        * Create a FormOptions object with options as specified by the user
-        *
-        * @param array $parameters
-        * @return FormOptions
-        */
-       public function setup( $parameters ) {
-               global $wgFeedLimit;
-
-               $opts = $this->getDefaultOptions();
-
-               foreach ( $this->getCustomFilters() as $key => $params ) {
-                       $opts->add( $key, $params['default'] );
-               }
-
-               $opts->fetchValuesFromRequest( $this->getRequest() );
-
-               // Give precedence to subpage syntax
-               if ( $parameters !== null ) {
-                       $this->parseParameters( $parameters, $opts );
-               }
-
-               $opts->validateIntBounds( 'limit', 0, $this->feedFormat ? $wgFeedLimit : 5000 );
-
-               return $opts;
-       }
-
        /**
         * Get custom show/hide filters
         *
@@ -119,89 +91,7 @@ class SpecialRecentChanges extends SpecialPage {
        }
 
        /**
-        * Get the current FormOptions for this request
-        */
-       public function getOptions() {
-               if ( $this->rcOptions === null ) {
-                       $this->rcOptions = $this->setup( $this->rcSubpage );
-               }
-
-               return $this->rcOptions;
-       }
-
-       /**
-        * Main execution point
-        *
-        * @param string $subpage
-        */
-       public function execute( $subpage ) {
-               $this->rcSubpage = $subpage;
-               $this->feedFormat = $this->including() ? null : $this->getRequest()->getVal( 'feed' );
-
-               # 10 seconds server-side caching max
-               $this->getOutput()->setSquidMaxage( 10 );
-               # Check if the client has a cached version
-               $lastmod = $this->checkLastModified( $this->feedFormat );
-               if ( $lastmod === false ) {
-                       return;
-               }
-
-               $opts = $this->getOptions();
-               $this->setHeaders();
-               $this->outputHeader();
-               $this->addModules();
-
-               // Fetch results, prepare a batch link existence check query
-               $conds = $this->buildMainQueryConds( $opts );
-               $rows = $this->doMainQuery( $conds, $opts );
-               if ( $rows === false ) {
-                       if ( !$this->including() ) {
-                               $this->doHeader( $opts );
-                       }
-
-                       return;
-               }
-
-               if ( !$this->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();
-               }
-               if ( $this->feedFormat ) {
-                       list( $changesFeed, $formatter ) = $this->getFeedObject( $this->feedFormat );
-                       /** @var ChangesFeed $changesFeed */
-                       $changesFeed->execute( $formatter, $rows, $lastmod, $opts );
-               } else {
-                       $this->webOutput( $rows, $opts );
-               }
-
-               $rows->free();
-       }
-
-       /**
-        * Return an array with a ChangesFeed object and ChannelFeed object
-        *
-        * @param string $feedFormat Feed's format (either 'rss' or 'atom')
-        * @return array
-        */
-       public function getFeedObject( $feedFormat ) {
-               $changesFeed = new ChangesFeed( $feedFormat, 'rcfeed' );
-               $formatter = $changesFeed->getFeedObject(
-                       $this->msg( 'recentchanges' )->inContentLanguage()->text(),
-                       $this->msg( 'recentchanges-feed-description' )->inContentLanguage()->text(),
-                       $this->getPageTitle()->getFullURL()
-               );
-
-               return array( $changesFeed, $formatter );
-       }
-
-       /**
-        * Process $par and put options found if $opts
-        * Mainly used when including the page
+        * Process $par and put options found in $opts. Used when including the page.
         *
         * @param string $par
         * @param FormOptions $opts
@@ -251,25 +141,10 @@ class SpecialRecentChanges extends SpecialPage {
                }
        }
 
-       /**
-        * 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 string $feedFormat
-        * @return string|bool
-        */
-       public function checkLastModified( $feedFormat ) {
-               $dbr = wfGetDB( DB_SLAVE );
-               $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__ );
-               if ( $feedFormat || !$this->getUser()->useRCPatrol() ) {
-                       if ( $lastmod && $this->getOutput()->checkLastModified( $lastmod ) ) {
-                               # Client cache fresh and headers sent, nothing more to do.
-                               return false;
-                       }
-               }
-
-               return $lastmod;
+       public function validateOptions( FormOptions $opts ) {
+               global $wgFeedLimit;
+               $opts->validateIntBounds( 'limit', 0, $this->feedFormat ? $wgFeedLimit : 5000 );
+               parent::validateOptions( $opts );
        }
 
        /**
@@ -279,21 +154,8 @@ class SpecialRecentChanges extends SpecialPage {
         * @return array
         */
        public function buildMainQueryConds( FormOptions $opts ) {
-               $dbr = wfGetDB( DB_SLAVE );
-               $conds = array();
-
-               # It makes no sense to hide both anons and logged-in users
-               # Where this occurs, force anons to be shown
-               $forcebot = false;
-               if ( $opts['hideanons'] && $opts['hideliu'] ) {
-                       # Check if the user wants to show bots only
-                       if ( $opts['hidebots'] ) {
-                               $opts['hideanons'] = false;
-                       } else {
-                               $forcebot = true;
-                               $opts['hidebots'] = false;
-                       }
-               }
+               $dbr = $this->getDB();
+               $conds = parent::buildMainQueryConds( $opts );
 
                // Calculate cutoff
                $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
@@ -309,59 +171,6 @@ class SpecialRecentChanges extends SpecialPage {
 
                $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
 
-               $hidePatrol = $this->getUser()->useRCPatrol() && $opts['hidepatrolled'];
-               $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['hidemyself'] ) {
-                       if ( $this->getUser()->getId() ) {
-                               $conds[] = 'rc_user != ' . $dbr->addQuotes( $this->getUser()->getId() );
-                       } else {
-                               $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $this->getUser()->getName() );
-                       }
-               }
-
-               # Namespace filtering
-               if ( $opts['namespace'] !== '' ) {
-                       $selectedNS = $dbr->addQuotes( $opts['namespace'] );
-                       $operator = $opts['invert'] ? '!=' : '=';
-                       $boolean = $opts['invert'] ? 'AND' : 'OR';
-
-                       # namespace association (bug 2429)
-                       if ( !$opts['associated'] ) {
-                               $condition = "rc_namespace $operator $selectedNS";
-                       } else {
-                               # Also add the associated namespace
-                               $associatedNS = $dbr->addQuotes(
-                                       MWNamespace::getAssociated( $opts['namespace'] )
-                               );
-                               $condition = "(rc_namespace $operator $selectedNS "
-                                       . $boolean
-                                       . " rc_namespace $operator $associatedNS)";
-                       }
-
-                       $conds[] = $condition;
-               }
-
                return $conds;
        }
 
@@ -373,35 +182,34 @@ class SpecialRecentChanges extends SpecialPage {
         * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
         */
        public function doMainQuery( $conds, $opts ) {
-               $tables = array( 'recentchanges' );
-               $join_conds = array();
-               $query_options = array();
+               global $wgAllowCategorizedRecentChanges;
 
-               $uid = $this->getUser()->getId();
-               $dbr = wfGetDB( DB_SLAVE );
-               $limit = $opts['limit'];
-               $namespace = $opts['namespace'];
-               $invert = $opts['invert'];
-               $associated = $opts['associated'];
+               $dbr = $this->getDB();
+               $user = $this->getUser();
 
+               $tables = array( 'recentchanges' );
                $fields = RecentChange::selectFields();
+               $query_options = array();
+               $join_conds = array();
+
                // JOIN on watchlist for users
-               if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
+               if ( $user->getId() && $user->isAllowed( 'viewmywatchlist' ) ) {
                        $tables[] = 'watchlist';
                        $fields[] = 'wl_user';
                        $fields[] = 'wl_notificationtimestamp';
                        $join_conds['watchlist'] = array( 'LEFT JOIN', array(
-                               'wl_user' => $uid,
+                               'wl_user' => $user->getId(),
                                'wl_title=rc_title',
                                'wl_namespace=rc_namespace'
                        ) );
                }
-               if ( $this->getUser()->isAllowed( 'rollback' ) ) {
+
+               if ( $user->isAllowed( 'rollback' ) ) {
                        $tables[] = 'page';
                        $fields[] = 'page_latest';
                        $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
                }
-               // Tag stuff.
+
                ChangeTags::modifyDisplayQuery(
                        $tables,
                        $fields,
@@ -419,37 +227,50 @@ class SpecialRecentChanges extends SpecialPage {
 
                // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough
                // knowledge to use an index merge if it wants (it may use some other index though).
-               return $dbr->select(
+               $rows = $dbr->select(
                        $tables,
                        $fields,
                        $conds + array( 'rc_new' => array( 0, 1 ) ),
                        __METHOD__,
-                       array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ) + $query_options,
+                       array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $opts['limit'] ) + $query_options,
                        $join_conds
                );
+
+               // Build the final data
+               if ( $wgAllowCategorizedRecentChanges ) {
+                       $this->filterByCategories( $rows, $opts );
+               }
+
+               return $rows;
        }
 
        /**
-        * Send output to the OutputPage object, only called if not used feeds
+        * Output feed links.
+        */
+       public function outputFeedLinks() {
+               $feedQuery = $this->getFeedQuery();
+               if ( $feedQuery !== '' ) {
+                       $this->getOutput()->setFeedAppendQuery( $feedQuery );
+               } else {
+                       $this->getOutput()->setFeedAppendQuery( false );
+               }
+       }
+
+       /**
+        * Build and output the actual changes list.
         *
         * @param array $rows Database rows
         * @param FormOptions $opts
         */
-       public function webOutput( $rows, $opts ) {
-               global $wgRCShowWatchingUsers, $wgShowUpdatedMarker, $wgAllowCategorizedRecentChanges;
-
-               // Build the final data
-
-               if ( $wgAllowCategorizedRecentChanges ) {
-                       $this->filterByCategories( $rows, $opts );
-               }
+       public function outputChangesList( $rows, $opts ) {
+               global $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
 
                $limit = $opts['limit'];
 
                $showWatcherCount = $wgRCShowWatchingUsers && $this->getUser()->getOption( 'shownumberswatching' );
                $watcherCache = array();
 
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = $this->getDB();
 
                $counter = 1;
                $list = ChangesList::newFromContext( $this->getContext() );
@@ -493,21 +314,6 @@ class SpecialRecentChanges extends SpecialPage {
                }
                $rclistOutput .= $list->endRecentChangesList();
 
-               // Print things out
-
-               if ( !$this->including() ) {
-                       // Output options box
-                       $this->doHeader( $opts );
-               }
-
-               // And now for the content
-               $feedQuery = $this->getFeedQuery();
-               if ( $feedQuery !== '' ) {
-                       $this->getOutput()->setFeedAppendQuery( $feedQuery );
-               } else {
-                       $this->getOutput()->setFeedAppendQuery( false );
-               }
-
                if ( $rows->numRows() === 0 ) {
                        $this->getOutput()->addHtml(
                                '<div class="mw-changeslist-empty">' . $this->msg( 'recentchanges-noresult' )->parse() . '</div>'
@@ -517,28 +323,6 @@ class SpecialRecentChanges extends SpecialPage {
                }
        }
 
-       /**
-        * Get the query string to append to feed link URLs.
-        *
-        * @return string
-        */
-       public function getFeedQuery() {
-               global $wgFeedLimit;
-
-               $this->getOptions()->validateIntBounds( 'limit', 0, $wgFeedLimit );
-               $options = $this->getOptions()->getChangedValues();
-
-               // wfArrayToCgi() omits options set to null or false
-               foreach ( $options as &$value ) {
-                       if ( $value === false ) {
-                               $value = '0';
-                       }
-               }
-               unset( $value );
-
-               return wfArrayToCgi( $options );
-       }
-
        /**
         * Return the text to be displayed above the changes
         *
@@ -614,6 +398,27 @@ class SpecialRecentChanges extends SpecialPage {
                $this->setBottomText( $opts );
        }
 
+       /**
+        * Send the text to be displayed above the options
+        *
+        * @param FormOptions $opts Unused
+        */
+       function setTopText( FormOptions $opts ) {
+               global $wgContLang;
+
+               $message = $this->msg( 'recentchangestext' )->inContentLanguage();
+               if ( !$message->isDisabled() ) {
+                       $this->getOutput()->addWikiText(
+                               Html::rawElement( 'p',
+                                       array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
+                                       "\n" . $message->plain() . "\n"
+                               ),
+                               /* $lineStart */ false,
+                               /* $interface */ false
+                       );
+               }
+       }
+
        /**
         * Get options to be displayed in a form
         *
@@ -647,91 +452,72 @@ class SpecialRecentChanges extends SpecialPage {
        }
 
        /**
-        * Return the legend displayed within the fieldset.
-        *
-        * This method is also called from SpecialWatchlist.
+        * Add page-specific modules.
+        */
+       protected function addModules() {
+               parent::addModules();
+               $out = $this->getOutput();
+               $out->addModules( 'mediawiki.special.recentchanges' );
+       }
+
+       /**
+        * 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 $context the object available as $this in non-static functions
-        * @return string
+        * @param string $feedFormat
+        * @return string|bool
         */
-       public static function makeLegend( IContextSource $context ) {
-               global $wgRecentChangesFlags;
-               $user = $context->getUser();
-               # The legend showing what the letters and stuff mean
-               $legend = Xml::openElement( 'dl' ) . "\n";
-               # Iterates through them and gets the messages for both letter and tooltip
-               $legendItems = $wgRecentChangesFlags;
-               if ( !$user->useRCPatrol() ) {
-                       unset( $legendItems['unpatrolled'] );
-               }
-               foreach ( $legendItems as $key => $legendInfo ) { # generate items of the legend
-                       $label = $legendInfo['title'];
-                       $letter = $legendInfo['letter'];
-                       $cssClass = isset( $legendInfo['class'] ) ? $legendInfo['class'] : $key;
-
-                       $legend .= Xml::element( 'dt',
-                               array( 'class' => $cssClass ), $context->msg( $letter )->text()
-                       ) . "\n";
-                       if ( $key === 'newpage' ) {
-                               $legend .= Xml::openElement( 'dd' );
-                               $legend .= $context->msg( $label )->escaped();
-                               $legend .= ' ' . $context->msg( 'recentchanges-legend-newpage' )->parse();
-                               $legend .= Xml::closeElement( 'dd' ) . "\n";
-                       } else {
-                               $legend .= Xml::element( 'dd', array(),
-                                       $context->msg( $label )->text()
-                               ) . "\n";
+       public function checkLastModified( $feedFormat ) {
+               $dbr = $this->getDB();
+               $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__ );
+               if ( $feedFormat || !$this->getUser()->useRCPatrol() ) {
+                       if ( $lastmod && $this->getOutput()->checkLastModified( $lastmod ) ) {
+                               # Client cache fresh and headers sent, nothing more to do.
+                               return false;
                        }
                }
-               # (+-123)
-               $legend .= Xml::tags( 'dt',
-                       array( 'class' => 'mw-plusminus-pos' ),
-                       $context->msg( 'recentchanges-legend-plusminus' )->parse()
-               ) . "\n";
-               $legend .= Xml::element(
-                       'dd',
-                       array( 'class' => 'mw-changeslist-legend-plusminus' ),
-                       $context->msg( 'recentchanges-label-plusminus' )->text()
-               ) . "\n";
-               $legend .= Xml::closeElement( 'dl' ) . "\n";
-
-               # Collapsibility
-               $legend =
-                       '<div class="mw-changeslist-legend">' .
-                               $context->msg( 'recentchanges-legend-heading' )->parse() .
-                               '<div class="mw-collapsible-content">' . $legend . '</div>' .
-                       '</div>';
-
-               return $legend;
+
+               return $lastmod;
        }
 
        /**
-        * Send the text to be displayed above the options
+        * Return an array with a ChangesFeed object and ChannelFeed object.
         *
-        * @param FormOptions $opts Unused
+        * @param string $feedFormat Feed's format (either 'rss' or 'atom')
+        * @return array
         */
-       function setTopText( FormOptions $opts ) {
-               global $wgContLang;
+       public function getFeedObject( $feedFormat ) {
+               $changesFeed = new ChangesFeed( $feedFormat, 'rcfeed' );
+               $formatter = $changesFeed->getFeedObject(
+                       $this->msg( 'recentchanges' )->inContentLanguage()->text(),
+                       $this->msg( 'recentchanges-feed-description' )->inContentLanguage()->text(),
+                       $this->getPageTitle()->getFullURL()
+               );
 
-               $message = $this->msg( 'recentchangestext' )->inContentLanguage();
-               if ( !$message->isDisabled() ) {
-                       $this->getOutput()->addWikiText(
-                               Html::rawElement( 'p',
-                                       array( 'lang' => $wgContLang->getCode(), 'dir' => $wgContLang->getDir() ),
-                                       "\n" . $message->plain() . "\n"
-                               ),
-                               /* $lineStart */ false,
-                               /* $interface */ false
-                       );
-               }
+               return array( $changesFeed, $formatter );
        }
 
        /**
-        * Send the text to be displayed after the options, for use in subclasses.
+        * Get the query string to append to feed link URLs.
         *
-        * @param FormOptions $opts
+        * @return string
         */
-       function setBottomText( FormOptions $opts ) {
+       public function getFeedQuery() {
+               global $wgFeedLimit;
+
+               $this->getOptions()->validateIntBounds( 'limit', 0, $wgFeedLimit );
+               $options = $this->getOptions()->getChangedValues();
+
+               // wfArrayToCgi() omits options set to null or false
+               foreach ( $options as &$value ) {
+                       if ( $value === false ) {
+                               $value = '0';
+                       }
+               }
+               unset( $value );
+
+               return wfArrayToCgi( $options );
        }
 
        /**
@@ -779,7 +565,7 @@ class SpecialRecentChanges extends SpecialPage {
        /**
         * Filter $rows by categories set in $opts
         *
-        * @param array $rows Database rows
+        * @param ResultWrapper $rows Database rows
         * @param FormOptions $opts
         */
        function filterByCategories( &$rows, FormOptions $opts ) {
@@ -962,16 +748,7 @@ class SpecialRecentChanges extends SpecialPage {
                return "{$note}$rclinks<br />$rclistfrom";
        }
 
-       /**
-        * Add page-specific modules.
-        */
-       protected function addModules() {
-               $this->getOutput()->addModules( array(
-                       'mediawiki.special.recentchanges',
-               ) );
-       }
-
-       protected function getGroupName() {
-               return 'changes';
+       public function isIncludable() {
+               return true;
        }
 }