From d59dd932e19e7a2e776983d76c8eaa1e22a1f3ab Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Wed, 15 Apr 2009 03:35:59 +0000 Subject: [PATCH] (bug 18151) Clean up duplicate code between Special:WantedPages, WantedFiles, WantedTemplates * New WantedQueryPage class for all of these Wanted*s, moved some identical methods to it * Removed sortDescending() because it was already defined as returning true @ QueryPage, no need to duplicate --- includes/AutoLoader.php | 1 + includes/QueryPage.php | 70 +++++++++++++++++++ includes/specials/SpecialWantedcategories.php | 27 +------ includes/specials/SpecialWantedfiles.php | 59 +--------------- includes/specials/SpecialWantedpages.php | 67 +----------------- includes/specials/SpecialWantedtemplates.php | 59 +--------------- 6 files changed, 75 insertions(+), 208 deletions(-) diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 8edefe54f5..12cbdab083 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -202,6 +202,7 @@ $wgAutoloadLocalClasses = array( 'UserArrayFromResult' => 'includes/UserArray.php', 'UserMailer' => 'includes/UserMailer.php', 'UserRightsProxy' => 'includes/UserRightsProxy.php', + 'WantedQueryPage' => 'includes/QueryPage.php', 'WatchedItem' => 'includes/WatchedItem.php', 'WatchlistEditor' => 'includes/WatchlistEditor.php', 'WebRequest' => 'includes/WebRequest.php', diff --git a/includes/QueryPage.php b/includes/QueryPage.php index cb333563d8..6d26b5fbd2 100644 --- a/includes/QueryPage.php +++ b/includes/QueryPage.php @@ -535,3 +535,73 @@ class QueryPage { return $title->getFullURL(); } } + +/** + * Class definition for a wanted query page like + * WantedPages, WantedTemplates, etc + */ +abstract class WantedQueryPage extends QueryPage { + + function isExpensive() { + return true; + } + + function isSyndicated() { + return false; + } + + /** + * Cache page existence for performance + */ + function preprocessResults( $db, $res ) { + $batch = new LinkBatch; + while ( $row = $db->fetchObject( $res ) ) + $batch->add( $row->namespace, $row->title ); + $batch->execute(); + + // Back to start for display + if ( $db->numRows( $res ) > 0 ) + // If there are no rows we get an error seeking. + $db->dataSeek( $res, 0 ); + } + + /** + * Format an individual result + * + * @param $skin Skin to use for UI elements + * @param $result Result row + * @return string + */ + public function formatResult( $skin, $result ) { + $title = Title::makeTitleSafe( $result->namespace, $result->title ); + if( $title instanceof Title ) { + if( $this->isCached() ) { + $pageLink = $title->exists() + ? '' . $skin->makeLinkObj( $title ) . '' + : $skin->makeBrokenLinkObj( $title ); + } else { + $pageLink = $skin->makeBrokenLinkObj( $title ); + } + return wfSpecialList( $pageLink, $this->makeWlhLink( $title, $skin, $result ) ); + } else { + $tsafe = htmlspecialchars( $result->title ); + return wfMsg( 'wantedpages-badtitle', $tsafe ); + } + } + + /** + * Make a "what links here" link for a given title + * + * @param Title $title Title to make the link for + * @param Skin $skin Skin to use + * @param object $result Result row + * @return string + */ + private function makeWlhLink( $title, $skin, $result ) { + global $wgLang; + $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' ); + $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ), + $wgLang->formatNum( $result->value ) ); + return $skin->link( $wlh, $label, array(), array( 'target' => $title->getPrefixedText() ) ); + } +} diff --git a/includes/specials/SpecialWantedcategories.php b/includes/specials/SpecialWantedcategories.php index 7497f9be47..9a28301a4f 100644 --- a/includes/specials/SpecialWantedcategories.php +++ b/includes/specials/SpecialWantedcategories.php @@ -13,20 +13,12 @@ * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later */ -class WantedCategoriesPage extends QueryPage { +class WantedCategoriesPage extends WantedQueryPage { function getName() { return 'Wantedcategories'; } - function isExpensive() { - return true; - } - - function isSyndicated() { - return false; - } - function getSQL() { $dbr = wfGetDB( DB_SLAVE ); list( $categorylinks, $page ) = $dbr->tableNamesN( 'categorylinks', 'page' ); @@ -45,23 +37,6 @@ class WantedCategoriesPage extends QueryPage { "; } - function sortDescending() { return true; } - - /** - * Fetch user page links and cache their existence - */ - function preprocessResults( $db, $res ) { - $batch = new LinkBatch; - while ( $row = $db->fetchObject( $res ) ) - $batch->add( $row->namespace, $row->title ); - $batch->execute(); - - // Back to start for display - if ( $db->numRows( $res ) > 0 ) - // If there are no rows we get an error seeking. - $db->dataSeek( $res, 0 ); - } - function formatResult( $skin, $result ) { global $wgLang, $wgContLang; diff --git a/includes/specials/SpecialWantedfiles.php b/includes/specials/SpecialWantedfiles.php index 4957531e6d..189b9d8bd9 100644 --- a/includes/specials/SpecialWantedfiles.php +++ b/includes/specials/SpecialWantedfiles.php @@ -13,20 +13,12 @@ * @copyright Copyright © 2008, Soxred93 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later */ -class WantedFilesPage extends QueryPage { +class WantedFilesPage extends WantedQueryPage { function getName() { return 'Wantedfiles'; } - function isExpensive() { - return true; - } - - function isSyndicated() { - return false; - } - function getSQL() { $dbr = wfGetDB( DB_SLAVE ); list( $imagelinks, $page ) = $dbr->tableNamesN( 'imagelinks', 'page' ); @@ -44,55 +36,6 @@ class WantedFilesPage extends QueryPage { GROUP BY il_to "; } - - function sortDescending() { return true; } - - /** - * Fetch user page links and cache their existence - */ - function preprocessResults( $db, $res ) { - $batch = new LinkBatch; - while ( $row = $db->fetchObject( $res ) ) - $batch->add( $row->namespace, $row->title ); - $batch->execute(); - - // Back to start for display - if ( $db->numRows( $res ) > 0 ) - // If there are no rows we get an error seeking. - $db->dataSeek( $res, 0 ); - } - - function formatResult( $skin, $result ) { - global $wgLang, $wgContLang; - - $nt = Title::makeTitle( $result->namespace, $result->title ); - $text = $wgContLang->convert( $nt->getText() ); - - $plink = $this->isCached() ? - $skin->makeLinkObj( $nt, htmlspecialchars( $text ) ) : - $skin->makeBrokenLinkObj( $nt, htmlspecialchars( $text ) ); - - return wfSpecialList( - $plink, - $this->makeWlhLink( $nt, $skin, $result ) - ); - } - - /** - * Make a "what links here" link for a given title - * - * @param Title $title Title to make the link for - * @param Skin $skin Skin to use - * @param object $result Result row - * @return string - */ - private function makeWlhLink( $title, $skin, $result ) { - global $wgLang; - $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' ); - $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ), - $wgLang->formatNum( $result->value ) ); - return $skin->link( $wlh, $label, array(), array( 'target' => $title->getPrefixedText() ) ); - } } /** diff --git a/includes/specials/SpecialWantedpages.php b/includes/specials/SpecialWantedpages.php index 7307b33594..b7e82cf28c 100644 --- a/includes/specials/SpecialWantedpages.php +++ b/includes/specials/SpecialWantedpages.php @@ -8,7 +8,7 @@ * implements Special:Wantedpages * @ingroup SpecialPage */ -class WantedPagesPage extends QueryPage { +class WantedPagesPage extends WantedQueryPage { var $nlinks; function WantedPagesPage( $inc = false, $nlinks = true ) { @@ -20,11 +20,6 @@ class WantedPagesPage extends QueryPage { return 'Wantedpages'; } - function isExpensive() { - return true; - } - function isSyndicated() { return false; } - function getSQL() { global $wgWantedPagesThreshold; $count = $wgWantedPagesThreshold - 1; @@ -49,66 +44,6 @@ class WantedPagesPage extends QueryPage { wfRunHooks( 'WantedPages::getSQL', array( &$this, &$sql ) ); return $sql; } - - /** - * Cache page existence for performance - */ - function preprocessResults( $db, $res ) { - $batch = new LinkBatch; - while ( $row = $db->fetchObject( $res ) ) - $batch->add( $row->namespace, $row->title ); - $batch->execute(); - - // Back to start for display - if ( $db->numRows( $res ) > 0 ) - // If there are no rows we get an error seeking. - $db->dataSeek( $res, 0 ); - } - - /** - * Format an individual result - * - * @param $skin Skin to use for UI elements - * @param $result Result row - * @return string - */ - public function formatResult( $skin, $result ) { - $title = Title::makeTitleSafe( $result->namespace, $result->title ); - if( $title instanceof Title ) { - if( $this->isCached() ) { - $pageLink = $title->exists() - ? '' . $skin->makeLinkObj( $title ) . '' - : $skin->makeBrokenLinkObj( $title ); - } else { - $pageLink = $skin->makeBrokenLinkObj( $title ); - } - return wfSpecialList( $pageLink, $this->makeWlhLink( $title, $skin, $result ) ); - } else { - $tsafe = htmlspecialchars( $result->title ); - return wfMsg( 'wantedpages-badtitle', $tsafe ); - } - } - - /** - * Make a "what links here" link for a specified result if required - * - * @param $title Title to make the link for - * @param $skin Skin to use - * @param $result Result row - * @return string - */ - private function makeWlhLink( $title, $skin, $result ) { - global $wgLang; - if( $this->nlinks ) { - $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' ); - $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ), - $wgLang->formatNum( $result->value ) ); - return $skin->makeKnownLinkObj( $wlh, $label, 'target=' . $title->getPrefixedUrl() ); - } else { - return null; - } - } - } /** diff --git a/includes/specials/SpecialWantedtemplates.php b/includes/specials/SpecialWantedtemplates.php index 7dd9a26208..329d7a3f49 100644 --- a/includes/specials/SpecialWantedtemplates.php +++ b/includes/specials/SpecialWantedtemplates.php @@ -15,20 +15,12 @@ * @copyright Copyright © 2008, Danny B. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later */ -class WantedTemplatesPage extends QueryPage { +class WantedTemplatesPage extends WantedQueryPage { function getName() { return 'Wantedtemplates'; } - function isExpensive() { - return true; - } - - function isSyndicated() { - return false; - } - function getSQL() { $dbr = wfGetDB( DB_SLAVE ); list( $templatelinks, $page ) = $dbr->tableNamesN( 'templatelinks', 'page' ); @@ -45,55 +37,6 @@ class WantedTemplatesPage extends QueryPage { GROUP BY tl_namespace, tl_title "; } - - function sortDescending() { return true; } - - /** - * Fetch user page links and cache their existence - */ - function preprocessResults( $db, $res ) { - $batch = new LinkBatch; - while ( $row = $db->fetchObject( $res ) ) - $batch->add( $row->namespace, $row->title ); - $batch->execute(); - - // Back to start for display - if ( $db->numRows( $res ) > 0 ) - // If there are no rows we get an error seeking. - $db->dataSeek( $res, 0 ); - } - - function formatResult( $skin, $result ) { - global $wgLang, $wgContLang; - - $nt = Title::makeTitle( $result->namespace, $result->title ); - $text = $wgContLang->convert( $nt->getText() ); - - $plink = $this->isCached() ? - $skin->makeLinkObj( $nt, htmlspecialchars( $text ) ) : - $skin->makeBrokenLinkObj( $nt, htmlspecialchars( $text ) ); - - return wfSpecialList( - $plink, - $this->makeWlhLink( $nt, $skin, $result ) - ); - } - - /** - * Make a "what links here" link for a given title - * - * @param Title $title Title to make the link for - * @param Skin $skin Skin to use - * @param object $result Result row - * @return string - */ - private function makeWlhLink( $title, $skin, $result ) { - global $wgLang; - $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' ); - $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ), - $wgLang->formatNum( $result->value ) ); - return $skin->link( $wlh, $label, array(), array( 'target' => $title->getPrefixedText() ) ); - } } /** -- 2.20.1