]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - includes/specials/SpecialRandompage.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / specials / SpecialRandompage.php
index 6299f3841a8e1a5364a5b272fbceabcaa8f9c16e..e3b567d7fe69c28a9235ee93745794d876661e88 100644 (file)
  * @ingroup SpecialPage
  */
 class RandomPage extends SpecialPage {
-       private $namespaces;  // namespaces to select pages from
+       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' ){
-               global $wgContentNamespaces;
-               $this->namespaces = $wgContentNamespaces;
+       public function __construct( $name = 'Randompage' ) {
+               $this->namespaces = MWNamespace::getContentNamespaces();
                parent::__construct( $name );
        }
 
@@ -42,66 +41,78 @@ class RandomPage extends SpecialPage {
                return $this->namespaces;
        }
 
-       public function setNamespace ( $ns ) {
-               if( !$ns || $ns < NS_MAIN ) $ns = NS_MAIN;
-               $this->namespaces = array( $ns );
+       public function setNamespace( $ns ) {
+               if ( !$ns || $ns < NS_MAIN ) {
+                       $ns = NS_MAIN;
+               }
+               $this->namespaces = [ $ns ];
        }
 
        // select redirects instead of normal pages?
-       public function isRedirect(){
+       public function isRedirect() {
                return $this->isRedir;
        }
 
        public function execute( $par ) {
-               global $wgOut, $wgContLang, $wgRequest;
+               global $wgContLang;
 
-               if ($par) {
+               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 ) );
                }
 
                $title = $this->getRandomTitle();
 
-               if( is_null( $title ) ) {
+               if ( is_null( $title ) ) {
                        $this->setHeaders();
-                       $wgOut->addWikiMsg( strtolower( $this->mName ) . '-nopages', 
+                       // Message: randompage-nopages, randomredirect-nopages
+                       $this->getOutput()->addWikiMsg( strtolower( $this->getName() ) . '-nopages',
                                $this->getNsList(), count( $this->namespaces ) );
+
                        return;
                }
 
-               $redirectParam = $this->isRedirect() ? array( 'redirect' => 'no' ) : array();
-               $query = array_merge( $wgRequest->getValues(), $redirectParam );
+               $redirectParam = $this->isRedirect() ? [ 'redirect' => 'no' ] : [];
+               $query = array_merge( $this->getRequest()->getValues(), $redirectParam );
                unset( $query['title'] );
-               $wgOut->redirect( $title->getFullUrl( $query ) );
+               $this->getOutput()->redirect( $title->getFullURL( $query ) );
        }
 
        /**
         * Get a comma-delimited list of namespaces we don't have
         * any pages in
-        * @return String
+        * @return string
         */
        private function getNsList() {
                global $wgContLang;
-               $nsNames = array();
-               foreach( $this->namespaces as $n ) {
-                       if( $n === NS_MAIN )
-                               $nsNames[] = wfMsgForContent( 'blanknamespace' );
-                       else
+               $nsNames = [];
+               foreach ( $this->namespaces as $n ) {
+                       if ( $n === NS_MAIN ) {
+                               $nsNames[] = $this->msg( 'blanknamespace' )->plain();
+                       } else {
                                $nsNames[] = $wgContLang->getNsText( $n );
+                       }
                }
+
                return $wgContLang->commaList( $nsNames );
        }
 
-
        /**
         * Choose a random title.
-        * @return Title object (or null if nothing to choose from)
+        * @return Title|null Title object (or null if nothing to choose from)
         */
        public function getRandomTitle() {
                $randstr = wfRandom();
                $title = null;
-               if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$randstr, &$this->isRedir, &$this->namespaces, &$this->extra, &$title ) ) ) {
+
+               if ( !Hooks::run(
+                       'SpecialRandomGetRandomTitle',
+                       [ &$randstr, &$this->isRedir, &$this->namespaces, &$this->extra, &$title ]
+               ) ) {
                        return $title;
                }
+
                $row = $this->selectRandomPageFromDB( $randstr );
 
                /* If we picked a value that was higher than any in
@@ -111,53 +122,59 @@ class RandomPage extends SpecialPage {
                 * any more bias than what the page_random scheme
                 * causes anyway.  Trust me, I'm a mathematician. :)
                 */
-               if( !$row )
+               if ( !$row ) {
                        $row = $this->selectRandomPageFromDB( "0" );
+               }
 
-               if( $row )
+               if ( $row ) {
                        return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
-               else
-                       return null;
+               }
+
+               return null;
        }
 
-       private function selectRandomPageFromDB( $randstr ) {
-               global $wgExtraRandompageSQL;
-               $dbr = wfGetDB( DB_SLAVE );
+       protected function getQueryInfo( $randstr ) {
+               $redirect = $this->isRedirect() ? 1 : 0;
+               $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' => $joinConds
+               ];
+       }
 
-               $use_index = $dbr->useIndexClause( 'page_random' );
-               $page = $dbr->tableName( 'page' );
+       private function selectRandomPageFromDB( $randstr, $fname = __METHOD__ ) {
+               $dbr = wfGetDB( DB_REPLICA );
+
+               $query = $this->getQueryInfo( $randstr );
+               $res = $dbr->select(
+                       $query['tables'],
+                       $query['fields'],
+                       $query['conds'],
+                       $fname,
+                       $query['options'],
+                       $query['join_conds']
+               );
 
-               $ns = implode( ",", $this->namespaces );
-               $redirect = $this->isRedirect() ? 1 : 0;
-               
-               if ( $wgExtraRandompageSQL ) {
-                       $this->extra[] = $wgExtraRandompageSQL;
-               }
-               if ( $this->addExtraSQL() ) {
-                       $this->extra[] = $this->addExtraSQL();
-               }
-               $extra = '';
-               if ( $this->extra ) {
-                       $extra = 'AND (' . implode( ') AND (', $this->extra ) . ')';
-               }
-               $sql = "SELECT page_title, page_namespace
-                       FROM $page $use_index
-                       WHERE page_namespace IN ( $ns )
-                       AND page_is_redirect = $redirect
-                       AND page_random >= $randstr
-                       $extra
-                       ORDER BY page_random";
-
-               $sql = $dbr->limitResult( $sql, 1, 0 );
-               $res = $dbr->query( $sql, __METHOD__ );
                return $dbr->fetchObject( $res );
        }
 
-       /* an alternative to $wgExtraRandompageSQL so subclasses
-        * can add their own SQL by overriding this function
-        * @deprecated, append to $this->extra instead
-        */
-       public function addExtraSQL() {
-               return '';
+       protected function getGroupName() {
+               return 'redirects';
        }
 }