Merge "Mention relevant tasks in comment (follow-up to c8733333)"
[lhc/web/wiklou.git] / includes / specials / SpecialRandompage.php
index 73a88b9..c4ea005 100644 (file)
@@ -22,6 +22,8 @@
  * @author Rob Church <robchur@gmail.com>, Ilmari Karonen
  */
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * Special page to direct the user to a random page
  *
 class RandomPage extends SpecialPage {
        private $namespaces; // namespaces to select pages from
        protected $isRedir = false; // should the result be a redirect?
-       protected $extra = array(); // Extra SQL statements
+       protected $extra = []; // Extra SQL statements
 
        public function __construct( $name = 'Randompage' ) {
-               $this->namespaces = MWNamespace::getContentNamespaces();
+               $this->namespaces = MediaWikiServices::getInstance()->getNamespaceInfo()->
+                       getContentNamespaces();
                parent::__construct( $name );
        }
 
@@ -45,7 +48,7 @@ class RandomPage extends SpecialPage {
                if ( !$ns || $ns < NS_MAIN ) {
                        $ns = NS_MAIN;
                }
-               $this->namespaces = array( $ns );
+               $this->namespaces = [ $ns ];
        }
 
        // select redirects instead of normal pages?
@@ -54,12 +57,11 @@ class RandomPage extends SpecialPage {
        }
 
        public function execute( $par ) {
-               global $wgContLang;
-
                if ( is_string( $par ) ) {
                        // Testing for stringiness since we want to catch
                        // the empty string to mean main namespace only.
-                       $this->setNamespace( $wgContLang->getNsIndex( $par ) );
+                       $this->setNamespace(
+                               MediaWikiServices::getInstance()->getContentLanguage()->getNsIndex( $par ) );
                }
 
                $title = $this->getRandomTitle();
@@ -73,7 +75,7 @@ class RandomPage extends SpecialPage {
                        return;
                }
 
-               $redirectParam = $this->isRedirect() ? array( 'redirect' => 'no' ) : array();
+               $redirectParam = $this->isRedirect() ? [ 'redirect' => 'no' ] : [];
                $query = array_merge( $this->getRequest()->getValues(), $redirectParam );
                unset( $query['title'] );
                $this->getOutput()->redirect( $title->getFullURL( $query ) );
@@ -85,17 +87,17 @@ class RandomPage extends SpecialPage {
         * @return string
         */
        private function getNsList() {
-               global $wgContLang;
-               $nsNames = array();
+               $contLang = MediaWikiServices::getInstance()->getContentLanguage();
+               $nsNames = [];
                foreach ( $this->namespaces as $n ) {
                        if ( $n === NS_MAIN ) {
                                $nsNames[] = $this->msg( 'blanknamespace' )->plain();
                        } else {
-                               $nsNames[] = $wgContLang->getNsText( $n );
+                               $nsNames[] = $contLang->getNsText( $n );
                        }
                }
 
-               return $wgContLang->commaList( $nsNames );
+               return $contLang->commaList( $nsNames );
        }
 
        /**
@@ -108,7 +110,7 @@ class RandomPage extends SpecialPage {
 
                if ( !Hooks::run(
                        'SpecialRandomGetRandomTitle',
-                       array( &$randstr, &$this->isRedir, &$this->namespaces, &$this->extra, &$title )
+                       [ &$randstr, &$this->isRedir, &$this->namespaces, &$this->extra, &$title ]
                ) ) {
                        return $title;
                }
@@ -135,25 +137,31 @@ class RandomPage extends SpecialPage {
 
        protected function getQueryInfo( $randstr ) {
                $redirect = $this->isRedirect() ? 1 : 0;
-
-               return array(
-                       'tables' => array( 'page' ),
-                       'fields' => array( 'page_title', 'page_namespace' ),
-                       'conds' => array_merge( array(
-                               'page_namespace' => $this->namespaces,
-                               'page_is_redirect' => $redirect,
-                               'page_random >= ' . $randstr
-                       ), $this->extra ),
-                       'options' => array(
+               $tables = [ 'page' ];
+               $conds = array_merge( [
+                       'page_namespace' => $this->namespaces,
+                       'page_is_redirect' => $redirect,
+                       'page_random >= ' . $randstr
+               ], $this->extra );
+               $joinConds = [];
+
+               // Allow extensions to modify the query
+               Hooks::run( 'RandomPageQuery', [ &$tables, &$conds, &$joinConds ] );
+
+               return [
+                       'tables' => $tables,
+                       'fields' => [ 'page_title', 'page_namespace' ],
+                       'conds' => $conds,
+                       'options' => [
                                'ORDER BY' => 'page_random',
                                'LIMIT' => 1,
-                       ),
-                       'join_conds' => array()
-               );
+                       ],
+                       'join_conds' => $joinConds
+               ];
        }
 
        private function selectRandomPageFromDB( $randstr, $fname = __METHOD__ ) {
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_REPLICA );
 
                $query = $this->getQueryInfo( $randstr );
                $res = $dbr->select(