From 195157b2c2cfa5dcb9cfabf6c1e30f4f1c1ae4a3 Mon Sep 17 00:00:00 2001 From: Alex Z Date: Mon, 17 Aug 2009 15:23:51 +0000 Subject: [PATCH] Re-implement r54638 in a higher-level way. Allows extensions to modify selection criteria for Special:Random or subsititute their own result. --- RELEASE-NOTES | 5 ++++ docs/hooks.txt | 9 +++++++ includes/DefaultSettings.php | 6 ++++- includes/specials/SpecialRandompage.php | 29 ++++++++++++++++----- includes/specials/SpecialRandomredirect.php | 5 +--- 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 58ad369291..f755547cc4 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -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 === diff --git a/docs/hooks.txt b/docs/hooks.txt index 2362262d08..0b64a3c639 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -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 diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index fe7693588b..8c99e0c23a 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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 */ diff --git a/includes/specials/SpecialRandompage.php b/includes/specials/SpecialRandompage.php index ce2a8c015c..fd3f17f2ff 100644 --- a/includes/specials/SpecialRandompage.php +++ b/includes/specials/SpecialRandompage.php @@ -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 ''; } diff --git a/includes/specials/SpecialRandomredirect.php b/includes/specials/SpecialRandomredirect.php index 629d5b3c85..28cb2aae29 100644 --- a/includes/specials/SpecialRandomredirect.php +++ b/includes/specials/SpecialRandomredirect.php @@ -10,10 +10,7 @@ class SpecialRandomredirect extends RandomPage { function __construct(){ parent::__construct( 'Randomredirect' ); + $this->isRedir = true; } - // Override parent::isRedirect() - public function isRedirect(){ - return true; - } } -- 2.20.1