Re-implement r54638 in a higher-level way. Allows extensions to modify selection...
authorAlex Z <mrzman@users.mediawiki.org>
Mon, 17 Aug 2009 15:23:51 +0000 (15:23 +0000)
committerAlex Z <mrzman@users.mediawiki.org>
Mon, 17 Aug 2009 15:23:51 +0000 (15:23 +0000)
RELEASE-NOTES
docs/hooks.txt
includes/DefaultSettings.php
includes/specials/SpecialRandompage.php
includes/specials/SpecialRandomredirect.php

index 58ad369..f755547 100644 (file)
@@ -79,6 +79,8 @@ this. Was used when mwEmbed was going to be an extension.
   to control which external domains may access the API via cross-site AJAX.
 * $wgMaintenanceScripts for extensions to add their scripts to the default list
 * $wgMemoryLimit has been added, default value '50M'
+* $wgExtraRandompageSQL is deprecated, the SpecialRandomGetRandomTitle hook 
+  should be used instead
 
 === New features in 1.16 ===
 
@@ -195,6 +197,9 @@ this. Was used when mwEmbed was going to be an extension.
    output by omitting some things like quotation marks where HTML 5 allows.
 * Added crop for inline images.
 * The description message in $wgExtensionCredits can be an array with parameters
+* New hook SpecialRandomGetRandomTitle allows extensions to modify the selection
+  criteria used by Special:Random and subclasses, or substitute a custom result,
+  deprecating the $wgExtraRandompageSQL config variable
 
 === Bug fixes in 1.16 ===
 
index 2362262..0b64a3c 100644 (file)
@@ -1376,6 +1376,15 @@ $newTitle: new title (object)
 hook to remove a core special page
 $list: list (array) of core special pages
 
+'SpecialRandomGetRandomTitle': called during the execution of Special:Random,
+use this to change some selection criteria or substitute a different title
+&$randstr: The random number from wfRandom()
+&$isRedir: Boolean, whether to select a redirect or non-redirect 
+&$namespaces: An array of namespace indexes to get the title from
+&$extra: An array of extra SQL statements 
+&$title: If the hook returns false, a Title object to use instead of the
+result from the normal query
+
 'SpecialRecentChangesPanel': called when building form options in
 SpecialRecentChanges
 &$extraOpts: array of added items, to which can be added
index fe76935..8c99e0c 100644 (file)
@@ -2820,7 +2820,11 @@ $wgUseSiteJs = true;
 /** Use the site's Cascading Style Sheets (CSS)? */
 $wgUseSiteCss = true;
 
-/** Filter for Special:Randompage. Part of a WHERE clause */
+/** 
+ * Filter for Special:Randompage. Part of a WHERE clause 
+ * @deprecated as of 1.16, use the SpecialRandomGetRandomTitle hook
+*/
+
 $wgExtraRandompageSQL = false;
 
 /** Allow the "info" action, very inefficient at the moment */
index ce2a8c0..fd3f17f 100644 (file)
@@ -9,6 +9,8 @@
  */
 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
 
        public function __construct( $name = 'Randompage' ){
                global $wgContentNamespaces;
@@ -26,9 +28,8 @@ class RandomPage extends SpecialPage {
        }
 
        // select redirects instead of normal pages?
-       // Overriden by SpecialRandomredirect
        public function isRedirect(){
-               return false;
+               return $this->isRedir;
        }
 
        public function execute( $par ) {
@@ -75,6 +76,10 @@ class RandomPage extends SpecialPage {
         */
        public function getRandomTitle() {
                $randstr = wfRandom();
+               $title = null;
+               if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$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
@@ -102,9 +107,17 @@ class RandomPage extends SpecialPage {
 
                $ns = implode( ",", $this->namespaces );
                $redirect = $this->isRedirect() ? 1 : 0;
-
-               $extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : "";
-               $extra .= $this->addExtraSQL() ? "AND (".$this->addExtraSQL().")" : "";
+               
+               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 )
@@ -118,8 +131,10 @@ class RandomPage extends SpecialPage {
                return $dbr->fetchObject( $res );
        }
 
-       // an alternative to $wgExtraRandompageSQL so extensions
-       // can add their own SQL by overriding this function
+       /* 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 '';
        }
index 629d5b3..28cb2aa 100644 (file)
 class SpecialRandomredirect extends RandomPage {
        function __construct(){
                parent::__construct( 'Randomredirect' );
+               $this->isRedir = true;
        }
 
-       // Override parent::isRedirect()
-       public function isRedirect(){
-               return true;
-       }
 }