From b0f510df565d206379d543efdc2b7550d4585107 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Sun, 25 Jan 2004 02:27:49 +0000 Subject: [PATCH] Ability to declare all query pages "expensive" --- includes/QueryPage.php | 152 +++++++++++++++---------------- includes/SpecialAncientpages.php | 54 +++++------ includes/SpecialLongpages.php | 40 ++++---- includes/SpecialNewpages.php | 76 ++++++++-------- includes/SpecialPopularpages.php | 38 ++++---- includes/SpecialShortpages.php | 50 +++++----- includes/SpecialWantedpages.php | 64 ++++++------- 7 files changed, 235 insertions(+), 239 deletions(-) diff --git a/includes/QueryPage.php b/includes/QueryPage.php index cbb9e7e40d..019533335b 100644 --- a/includes/QueryPage.php +++ b/includes/QueryPage.php @@ -7,90 +7,86 @@ include_once ( "LogPage.php" ) ; # subclasses derive from it. class QueryPage { + # Subclasses return their name here. Make sure the name is also + # specified in Language.php, both in the $wgValidSpecialPagesEn + # variable, and as a language message param. - # Subclasses return their name here. Make sure the name is also - # specified in Language.php, both in the $wgValidSpecialPagesEn - # variable, and as a language message param. - - function getName() { - return ""; - } - - # Subclasses return a SQL query here. - - function getSQL( $offset, $limit ) { - return ""; - } - - # Is this query expensive (for some definition of expensive)? Then we - # don't let it run in miser mode. The default is 0. Expensive - # subqueries should override this. - - function isExpensive( ) { - return 0; - } - - # Formats the results of the query for display. The skin is the current - # skin; you can use it for making links. The result is a single row of - # result data. You should be able to grab SQL results off of it. - - function formatResult( $skin, $result ) { - return ""; - } - - # This is the actual workhorse. It does everything needed to make a - # real, honest-to-gosh query page. - - function doQuery( $offset, $limit ) { - - global $wgUser, $wgOut, $wgLang, $wgMiserMode; - - $sname = $this->getName(); - $fname = get_class($this) . "::doQuery"; - - if ( $this->isExpensive( ) ) { - - $vsp = $wgLang->getValidSpecialPages(); - $logpage = new LogPage( $vsp[$sname] ); - $logpage->mUpdateRecentChanges = false; - - if ( $wgMiserMode ) { - $logpage->showAsDisabledPage(); - return; - } + function getName() { + return ""; } - $sql = $this->getSQL( $offset, $limit+1 ); + # Subclasses return a SQL query here. - $res = wfQuery( $sql, DB_READ, $fname ); - - $sk = $wgUser->getSkin( ); + function getSQL( $offset, $limit ) { + return ""; + } - $top = wfShowingResults( $offset, $limit ); - $wgOut->addHTML( "

{$top}\n" ); + # Is this query expensive (for some definition of expensive)? Then we + # don't let it run in miser mode. $wgDisableQueryPages causes all query + # pages to be declared expensive. Some query pages are always expensive. + function isExpensive( ) { + global $wgDisableQueryPages; + return $wgDisableQueryPages; + } - $sl = wfViewPrevNext( $offset, $limit, $wgLang->specialPage( $sname ), "", - wfNumRows( $res ) < ($limit+1) ); - $wgOut->addHTML( "
{$sl}\n" ); + # Formats the results of the query for display. The skin is the current + # skin; you can use it for making links. The result is a single row of + # result data. You should be able to grab SQL results off of it. - $s = "

    "; - $i = 0; - while ( ($i<$limit) && ($obj = wfFetchObject( $res )) ) { - $format = $this->formatResult( $sk, $obj ); - $s .= "
  1. {$format}
  2. \n"; - $i++; + function formatResult( $skin, $result ) { + return ""; } - wfFreeResult( $res ); - $s .= "
"; - $wgOut->addHTML( $s ); - $wgOut->addHTML( "

{$sl}\n" ); - - # Saving cache - - if ( $this->isExpensive() && $offset == 0 && $limit >= 50 ) { - $logpage->replaceContent( $s ); + + # This is the actual workhorse. It does everything needed to make a + # real, honest-to-gosh query page. + + function doQuery( $offset, $limit ) { + + global $wgUser, $wgOut, $wgLang, $wgMiserMode; + + $sname = $this->getName(); + $fname = get_class($this) . "::doQuery"; + + if ( $this->isExpensive( ) ) { + + $vsp = $wgLang->getValidSpecialPages(); + $logpage = new LogPage( "!" . $vsp[$sname] ); + $logpage->mUpdateRecentChanges = false; + + if ( $wgMiserMode ) { + $logpage->showAsDisabledPage(); + return; + } + } + + $sql = $this->getSQL( $offset, $limit ); + + $res = wfQuery( $sql, DB_READ, $fname ); + + $sk = $wgUser->getSkin( ); + + $top = wfShowingResults( $offset, $limit ); + $wgOut->addHTML( "

{$top}\n" ); + + $sl = wfViewPrevNext( $offset, $limit, $wgLang->specialPage( $sname ) ); + $wgOut->addHTML( "
{$sl}\n" ); + + $s = "

    "; + while ( $obj = wfFetchObject( $res ) ) { + $format = $this->formatResult( $sk, $obj ); + $s .= "
  1. {$format}
  2. \n"; + } + wfFreeResult( $res ); + $s .= "
"; + $wgOut->addHTML( $s ); + $wgOut->addHTML( "

{$sl}\n" ); + + # Saving cache + + if ( $this->isExpensive() && $offset == 0 && $limit >= 50 ) { + $logpage->replaceContent( $s ); + } } - } } # This is a subclass for very simple queries that are just looking for page @@ -99,9 +95,9 @@ class QueryPage { class PageQueryPage extends QueryPage { - function formatResult( $skin, $result ) { - return $skin->makeKnownLink( $result->cur_title, "" ); - } + function formatResult( $skin, $result ) { + return $skin->makeKnownLink( $result->cur_title, "" ); + } } ?> diff --git a/includes/SpecialAncientpages.php b/includes/SpecialAncientpages.php index a93a4eb961..aadf79b0e8 100644 --- a/includes/SpecialAncientpages.php +++ b/includes/SpecialAncientpages.php @@ -4,37 +4,37 @@ include_once( "QueryPage.php" ); class AncientPagesPage extends QueryPage { - function getName() { - return "Ancientpages"; - } - - function isExpensive() { - return 0; - } - - function getSQL( $offset, $limit ) { - return "SELECT cur_title, cur_timestamp " . - "FROM cur USE INDEX (cur_timestamp) " . - "WHERE cur_namespace=0 AND cur_is_redirect=0 " . - " ORDER BY cur_timestamp LIMIT {$offset}, {$limit}"; - } - - function formatResult( $skin, $result ) { - global $wgLang; - - $d = $wgLang->timeanddate( $result->cur_timestamp, true ); - $link = $skin->makeKnownLink( $result->cur_title, "" ); - return "{$link} ({$d})"; - } + function getName() { + return "Ancientpages"; + } + + function isExpensive() { + return parent::isExpensive() ; + } + + function getSQL( $offset, $limit ) { + return "SELECT cur_title, cur_timestamp " . + "FROM cur USE INDEX (cur_timestamp) " . + "WHERE cur_namespace=0 AND cur_is_redirect=0 " . + " ORDER BY cur_timestamp LIMIT {$offset}, {$limit}"; + } + + function formatResult( $skin, $result ) { + global $wgLang; + + $d = $wgLang->timeanddate( $result->cur_timestamp, true ); + $link = $skin->makeKnownLink( $result->cur_title, "" ); + return "{$link} ({$d})"; + } } function wfSpecialAncientpages() { - list( $limit, $offset ) = wfCheckLimits(); - - $app = new AncientPagesPage(); - - $app->doQuery( $offset, $limit ); + list( $limit, $offset ) = wfCheckLimits(); + + $app = new AncientPagesPage(); + + $app->doQuery( $offset, $limit ); } ?> diff --git a/includes/SpecialLongpages.php b/includes/SpecialLongpages.php index 44fa88afd9..e4c0ed7999 100644 --- a/includes/SpecialLongpages.php +++ b/includes/SpecialLongpages.php @@ -3,26 +3,26 @@ include_once( "QueryPage.php" ); class LongPagesPage extends QueryPage { - - function getName() { - return "Longpages"; - } - - function isExpensive() { - return 1; - } - - function getSQL( $offset, $limit ) { - return "SELECT cur_title, LENGTH(cur_text) AS len FROM cur " . - "WHERE cur_namespace=0 AND cur_is_redirect=0 ORDER BY " . - "LENGTH(cur_text) DESC LIMIT {$offset}, {$limit}"; - } - - function formatResult( $skin, $result ) { - $nb = wfMsg( "nbytes", $result->len ); - $link = $skin->makeKnownLink( $result->cur_title, "" ); - return "{$link} ({$nb})"; - } + + function getName() { + return "Longpages"; + } + + function isExpensive() { + return 1; + } + + function getSQL( $offset, $limit ) { + return "SELECT cur_title, LENGTH(cur_text) AS len FROM cur " . + "WHERE cur_namespace=0 AND cur_is_redirect=0 ORDER BY " . + "LENGTH(cur_text) DESC LIMIT {$offset}, {$limit}"; + } + + function formatResult( $skin, $result ) { + $nb = wfMsg( "nbytes", $result->len ); + $link = $skin->makeKnownLink( $result->cur_title, "" ); + return "{$link} ({$nb})"; + } } function wfSpecialLongpages() diff --git a/includes/SpecialNewpages.php b/includes/SpecialNewpages.php index ddeafa23e7..8dbff5be48 100644 --- a/includes/SpecialNewpages.php +++ b/includes/SpecialNewpages.php @@ -4,48 +4,48 @@ include_once( "QueryPage.php" ); class NewPagesPage extends QueryPage { - function getName() { - return "Newpages"; - } - - function isExpensive() { - return 0; - } - - function getSQL( $offset, $limit ) { - return "SELECT rc_title AS cur_title,rc_user AS cur_user,rc_user_text AS cur_user_text,rc_comment as cur_comment," . - "rc_timestamp AS cur_timestamp,length(cur_text) as cur_length FROM recentchanges,cur " . - "WHERE rc_cur_id=cur_id AND rc_new=1 AND rc_namespace=0 AND cur_is_redirect=0 " . - "ORDER BY rc_timestamp DESC LIMIT {$offset}, {$limit}"; - } - - function formatResult( $skin, $result ) { - - global $wgLang; - - $u = $result->cur_user; - $ut = $result->cur_user_text; - - $length = wfmsg( "nbytes", $result->cur_length ); - $c = wfEscapeHTML( $result->cur_comment ); - - if ( 0 == $u ) { # not by a logged-in user - $ul = $ut; + function getName() { + return "Newpages"; } - else { - $ul = $skin->makeLink( $wgLang->getNsText(2) . ":{$ut}", $ut ); + + function isExpensive() { + return parent::isExpensive(); } - - $d = $wgLang->timeanddate( $result->cur_timestamp, true ); - $link = $skin->makeKnownLink( $result->cur_title, "" ); - $s = "{$d} {$link} ({$length}) . . {$ul}"; - - if ( "" != $c && "*" != $c ) { - $s .= " ({$c})"; + + function getSQL( $offset, $limit ) { + return "SELECT rc_title AS cur_title,rc_user AS cur_user,rc_user_text AS cur_user_text,rc_comment as cur_comment," . + "rc_timestamp AS cur_timestamp,length(cur_text) as cur_length FROM recentchanges,cur " . + "WHERE rc_cur_id=cur_id AND rc_new=1 AND rc_namespace=0 AND cur_is_redirect=0 " . + "ORDER BY rc_timestamp DESC LIMIT {$offset}, {$limit}"; } - return $s; - } + function formatResult( $skin, $result ) { + + global $wgLang; + + $u = $result->cur_user; + $ut = $result->cur_user_text; + + $length = wfmsg( "nbytes", $result->cur_length ); + $c = wfEscapeHTML( $result->cur_comment ); + + if ( 0 == $u ) { # not by a logged-in user + $ul = $ut; + } + else { + $ul = $skin->makeLink( $wgLang->getNsText(2) . ":{$ut}", $ut ); + } + + $d = $wgLang->timeanddate( $result->cur_timestamp, true ); + $link = $skin->makeKnownLink( $result->cur_title, "" ); + $s = "{$d} {$link} ({$length}) . . {$ul}"; + + if ( "" != $c && "*" != $c ) { + $s .= " ({$c})"; + } + + return $s; + } } function wfSpecialNewpages() diff --git a/includes/SpecialPopularpages.php b/includes/SpecialPopularpages.php index 1e0db4e7a2..5053750441 100644 --- a/includes/SpecialPopularpages.php +++ b/includes/SpecialPopularpages.php @@ -4,25 +4,25 @@ include_once( "QueryPage.php" ); class PopularPagesPage extends QueryPage { - function getName() { - return "Popularpages"; - } - - function isExpensive() { - return 1; - } - - function getSQL( $offset, $limit ) { - return "SELECT DISTINCT cur_title, cur_counter FROM cur " . - "WHERE cur_namespace=0 AND cur_is_redirect=0 ORDER BY " . - "cur_counter DESC LIMIT {$offset}, {$limit}"; - } - - function formatResult( $skin, $result ) { - $link = $skin->makeKnownLink( $result->cur_title, "" ); - $nv = wfMsg( "nviews", $result->cur_counter ); - return "{$link} ({$nv})"; - } + function getName() { + return "Popularpages"; + } + + function isExpensive() { + return 1; + } + + function getSQL( $offset, $limit ) { + return "SELECT DISTINCT cur_title, cur_counter FROM cur " . + "WHERE cur_namespace=0 AND cur_is_redirect=0 ORDER BY " . + "cur_counter DESC LIMIT {$offset}, {$limit}"; + } + + function formatResult( $skin, $result ) { + $link = $skin->makeKnownLink( $result->cur_title, "" ); + $nv = wfMsg( "nviews", $result->cur_counter ); + return "{$link} ({$nv})"; + } } function wfSpecialPopularpages() diff --git a/includes/SpecialShortpages.php b/includes/SpecialShortpages.php index c74158f262..c68e072c0a 100644 --- a/includes/SpecialShortpages.php +++ b/includes/SpecialShortpages.php @@ -3,35 +3,35 @@ include_once("QueryPage.php"); class ShortPagesPage extends QueryPage { - - function getName() { - return "Shortpages"; - } - - function isExpensive() { - return 1; - } - - function getSQL( $offset, $limit ) { - return "SELECT cur_title, LENGTH(cur_text) AS len FROM cur " . - "WHERE cur_namespace=0 AND cur_is_redirect=0 ORDER BY " . - "LENGTH(cur_text) LIMIT {$offset}, {$limit}"; - } - - function formatResult( $skin, $result ) { - $nb = wfMsg( "nbytes", $result->len ); - $link = $skin->makeKnownLink( $result->cur_title, "" ); - return "{$link} ({$nb})"; - } + + function getName() { + return "Shortpages"; + } + + function isExpensive() { + return 1; + } + + function getSQL( $offset, $limit ) { + return "SELECT cur_title, LENGTH(cur_text) AS len FROM cur " . + "WHERE cur_namespace=0 AND cur_is_redirect=0 ORDER BY " . + "LENGTH(cur_text) LIMIT {$offset}, {$limit}"; + } + + function formatResult( $skin, $result ) { + $nb = wfMsg( "nbytes", $result->len ); + $link = $skin->makeKnownLink( $result->cur_title, "" ); + return "{$link} ({$nb})"; + } } function wfSpecialShortpages() { - list( $limit, $offset ) = wfCheckLimits(); - - $spp = new ShortPagesPage(); - - return $spp->doQuery( $offset, $limit ); + list( $limit, $offset ) = wfCheckLimits(); + + $spp = new ShortPagesPage(); + + return $spp->doQuery( $offset, $limit ); } ?> diff --git a/includes/SpecialWantedpages.php b/includes/SpecialWantedpages.php index acb11c655f..6bc82a57e0 100644 --- a/includes/SpecialWantedpages.php +++ b/includes/SpecialWantedpages.php @@ -3,42 +3,42 @@ include_once ( "QueryPage.php" ) ; class WantedPagesPage extends QueryPage { - - function getName() { - return "Wantedpages"; - } - - function isExpensive() { - return 1; - } - - function getSQL( $offset, $limit ) { - return "SELECT bl_to, COUNT( DISTINCT bl_from ) as nlinks " . - "FROM brokenlinks GROUP BY bl_to HAVING nlinks > 1 " . - "ORDER BY nlinks DESC LIMIT {$offset}, {$limit}"; - } - - function formatResult( $skin, $result ) { - global $wgLang; - - $nt = Title::newFromDBkey( $result->bl_to ); - - $plink = $skin->makeBrokenLink( $nt->getPrefixedText(), "" ); - $nl = wfMsg( "nlinks", $result->nlinks ); - $nlink = $skin->makeKnownLink( $wgLang->specialPage( "Whatlinkshere" ), $nl, - "target=" . $nt->getPrefixedURL() ); - - return "{$plink} ({$nlink})"; - } + + function getName() { + return "Wantedpages"; + } + + function isExpensive() { + return 1; + } + + function getSQL( $offset, $limit ) { + return "SELECT bl_to, COUNT( DISTINCT bl_from ) as nlinks " . + "FROM brokenlinks GROUP BY bl_to HAVING nlinks > 1 " . + "ORDER BY nlinks DESC LIMIT {$offset}, {$limit}"; + } + + function formatResult( $skin, $result ) { + global $wgLang; + + $nt = Title::newFromDBkey( $result->bl_to ); + + $plink = $skin->makeBrokenLink( $nt->getPrefixedText(), "" ); + $nl = wfMsg( "nlinks", $result->nlinks ); + $nlink = $skin->makeKnownLink( $wgLang->specialPage( "Whatlinkshere" ), $nl, + "target=" . $nt->getPrefixedURL() ); + + return "{$plink} ({$nlink})"; + } } function wfSpecialWantedpages() { - list( $limit, $offset ) = wfCheckLimits(); - - $wpp = new WantedPagesPage(); - - $wpp->doQuery( $offset, $limit ); + list( $limit, $offset ) = wfCheckLimits(); + + $wpp = new WantedPagesPage(); + + $wpp->doQuery( $offset, $limit ); } ?> -- 2.20.1