r49489 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r49488‎ | r49489 | r49490 >
Date:03:35, 15 April 2009
Author:demon
Status:ok (Comments)
Tags:
Comment:
(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
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/QueryPage.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialWantedcategories.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialWantedfiles.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialWantedpages.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialWantedtemplates.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/AutoLoader.php
@@ -202,6 +202,7 @@
203203 'UserArrayFromResult' => 'includes/UserArray.php',
204204 'UserMailer' => 'includes/UserMailer.php',
205205 'UserRightsProxy' => 'includes/UserRightsProxy.php',
 206+ 'WantedQueryPage' => 'includes/QueryPage.php',
206207 'WatchedItem' => 'includes/WatchedItem.php',
207208 'WatchlistEditor' => 'includes/WatchlistEditor.php',
208209 'WebRequest' => 'includes/WebRequest.php',
Index: trunk/phase3/includes/specials/SpecialWantedtemplates.php
@@ -15,20 +15,12 @@
1616 * @copyright Copyright © 2008, Danny B.
1717 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1818 */
19 -class WantedTemplatesPage extends QueryPage {
 19+class WantedTemplatesPage extends WantedQueryPage {
2020
2121 function getName() {
2222 return 'Wantedtemplates';
2323 }
2424
25 - function isExpensive() {
26 - return true;
27 - }
28 -
29 - function isSyndicated() {
30 - return false;
31 - }
32 -
3325 function getSQL() {
3426 $dbr = wfGetDB( DB_SLAVE );
3527 list( $templatelinks, $page ) = $dbr->tableNamesN( 'templatelinks', 'page' );
@@ -45,55 +37,6 @@
4638 GROUP BY tl_namespace, tl_title
4739 ";
4840 }
49 -
50 - function sortDescending() { return true; }
51 -
52 - /**
53 - * Fetch user page links and cache their existence
54 - */
55 - function preprocessResults( $db, $res ) {
56 - $batch = new LinkBatch;
57 - while ( $row = $db->fetchObject( $res ) )
58 - $batch->add( $row->namespace, $row->title );
59 - $batch->execute();
60 -
61 - // Back to start for display
62 - if ( $db->numRows( $res ) > 0 )
63 - // If there are no rows we get an error seeking.
64 - $db->dataSeek( $res, 0 );
65 - }
66 -
67 - function formatResult( $skin, $result ) {
68 - global $wgLang, $wgContLang;
69 -
70 - $nt = Title::makeTitle( $result->namespace, $result->title );
71 - $text = $wgContLang->convert( $nt->getText() );
72 -
73 - $plink = $this->isCached() ?
74 - $skin->makeLinkObj( $nt, htmlspecialchars( $text ) ) :
75 - $skin->makeBrokenLinkObj( $nt, htmlspecialchars( $text ) );
76 -
77 - return wfSpecialList(
78 - $plink,
79 - $this->makeWlhLink( $nt, $skin, $result )
80 - );
81 - }
82 -
83 - /**
84 - * Make a "what links here" link for a given title
85 - *
86 - * @param Title $title Title to make the link for
87 - * @param Skin $skin Skin to use
88 - * @param object $result Result row
89 - * @return string
90 - */
91 - private function makeWlhLink( $title, $skin, $result ) {
92 - global $wgLang;
93 - $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
94 - $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
95 - $wgLang->formatNum( $result->value ) );
96 - return $skin->link( $wlh, $label, array(), array( 'target' => $title->getPrefixedText() ) );
97 - }
9841 }
9942
10043 /**
Index: trunk/phase3/includes/specials/SpecialWantedfiles.php
@@ -13,20 +13,12 @@
1414 * @copyright Copyright © 2008, Soxred93
1515 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1616 */
17 -class WantedFilesPage extends QueryPage {
 17+class WantedFilesPage extends WantedQueryPage {
1818
1919 function getName() {
2020 return 'Wantedfiles';
2121 }
2222
23 - function isExpensive() {
24 - return true;
25 - }
26 -
27 - function isSyndicated() {
28 - return false;
29 - }
30 -
3123 function getSQL() {
3224 $dbr = wfGetDB( DB_SLAVE );
3325 list( $imagelinks, $page ) = $dbr->tableNamesN( 'imagelinks', 'page' );
@@ -44,55 +36,6 @@
4537 GROUP BY il_to
4638 ";
4739 }
48 -
49 - function sortDescending() { return true; }
50 -
51 - /**
52 - * Fetch user page links and cache their existence
53 - */
54 - function preprocessResults( $db, $res ) {
55 - $batch = new LinkBatch;
56 - while ( $row = $db->fetchObject( $res ) )
57 - $batch->add( $row->namespace, $row->title );
58 - $batch->execute();
59 -
60 - // Back to start for display
61 - if ( $db->numRows( $res ) > 0 )
62 - // If there are no rows we get an error seeking.
63 - $db->dataSeek( $res, 0 );
64 - }
65 -
66 - function formatResult( $skin, $result ) {
67 - global $wgLang, $wgContLang;
68 -
69 - $nt = Title::makeTitle( $result->namespace, $result->title );
70 - $text = $wgContLang->convert( $nt->getText() );
71 -
72 - $plink = $this->isCached() ?
73 - $skin->makeLinkObj( $nt, htmlspecialchars( $text ) ) :
74 - $skin->makeBrokenLinkObj( $nt, htmlspecialchars( $text ) );
75 -
76 - return wfSpecialList(
77 - $plink,
78 - $this->makeWlhLink( $nt, $skin, $result )
79 - );
80 - }
81 -
82 - /**
83 - * Make a "what links here" link for a given title
84 - *
85 - * @param Title $title Title to make the link for
86 - * @param Skin $skin Skin to use
87 - * @param object $result Result row
88 - * @return string
89 - */
90 - private function makeWlhLink( $title, $skin, $result ) {
91 - global $wgLang;
92 - $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
93 - $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
94 - $wgLang->formatNum( $result->value ) );
95 - return $skin->link( $wlh, $label, array(), array( 'target' => $title->getPrefixedText() ) );
96 - }
9740 }
9841
9942 /**
Index: trunk/phase3/includes/specials/SpecialWantedcategories.php
@@ -13,20 +13,12 @@
1414 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
1515 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1616 */
17 -class WantedCategoriesPage extends QueryPage {
 17+class WantedCategoriesPage extends WantedQueryPage {
1818
1919 function getName() {
2020 return 'Wantedcategories';
2121 }
2222
23 - function isExpensive() {
24 - return true;
25 - }
26 -
27 - function isSyndicated() {
28 - return false;
29 - }
30 -
3123 function getSQL() {
3224 $dbr = wfGetDB( DB_SLAVE );
3325 list( $categorylinks, $page ) = $dbr->tableNamesN( 'categorylinks', 'page' );
@@ -45,23 +37,6 @@
4638 ";
4739 }
4840
49 - function sortDescending() { return true; }
50 -
51 - /**
52 - * Fetch user page links and cache their existence
53 - */
54 - function preprocessResults( $db, $res ) {
55 - $batch = new LinkBatch;
56 - while ( $row = $db->fetchObject( $res ) )
57 - $batch->add( $row->namespace, $row->title );
58 - $batch->execute();
59 -
60 - // Back to start for display
61 - if ( $db->numRows( $res ) > 0 )
62 - // If there are no rows we get an error seeking.
63 - $db->dataSeek( $res, 0 );
64 - }
65 -
6641 function formatResult( $skin, $result ) {
6742 global $wgLang, $wgContLang;
6843
Index: trunk/phase3/includes/specials/SpecialWantedpages.php
@@ -8,7 +8,7 @@
99 * implements Special:Wantedpages
1010 * @ingroup SpecialPage
1111 */
12 -class WantedPagesPage extends QueryPage {
 12+class WantedPagesPage extends WantedQueryPage {
1313 var $nlinks;
1414
1515 function WantedPagesPage( $inc = false, $nlinks = true ) {
@@ -20,11 +20,6 @@
2121 return 'Wantedpages';
2222 }
2323
24 - function isExpensive() {
25 - return true;
26 - }
27 - function isSyndicated() { return false; }
28 -
2924 function getSQL() {
3025 global $wgWantedPagesThreshold;
3126 $count = $wgWantedPagesThreshold - 1;
@@ -49,66 +44,6 @@
5045 wfRunHooks( 'WantedPages::getSQL', array( &$this, &$sql ) );
5146 return $sql;
5247 }
53 -
54 - /**
55 - * Cache page existence for performance
56 - */
57 - function preprocessResults( $db, $res ) {
58 - $batch = new LinkBatch;
59 - while ( $row = $db->fetchObject( $res ) )
60 - $batch->add( $row->namespace, $row->title );
61 - $batch->execute();
62 -
63 - // Back to start for display
64 - if ( $db->numRows( $res ) > 0 )
65 - // If there are no rows we get an error seeking.
66 - $db->dataSeek( $res, 0 );
67 - }
68 -
69 - /**
70 - * Format an individual result
71 - *
72 - * @param $skin Skin to use for UI elements
73 - * @param $result Result row
74 - * @return string
75 - */
76 - public function formatResult( $skin, $result ) {
77 - $title = Title::makeTitleSafe( $result->namespace, $result->title );
78 - if( $title instanceof Title ) {
79 - if( $this->isCached() ) {
80 - $pageLink = $title->exists()
81 - ? '<s>' . $skin->makeLinkObj( $title ) . '</s>'
82 - : $skin->makeBrokenLinkObj( $title );
83 - } else {
84 - $pageLink = $skin->makeBrokenLinkObj( $title );
85 - }
86 - return wfSpecialList( $pageLink, $this->makeWlhLink( $title, $skin, $result ) );
87 - } else {
88 - $tsafe = htmlspecialchars( $result->title );
89 - return wfMsg( 'wantedpages-badtitle', $tsafe );
90 - }
91 - }
92 -
93 - /**
94 - * Make a "what links here" link for a specified result if required
95 - *
96 - * @param $title Title to make the link for
97 - * @param $skin Skin to use
98 - * @param $result Result row
99 - * @return string
100 - */
101 - private function makeWlhLink( $title, $skin, $result ) {
102 - global $wgLang;
103 - if( $this->nlinks ) {
104 - $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
105 - $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
106 - $wgLang->formatNum( $result->value ) );
107 - return $skin->makeKnownLinkObj( $wlh, $label, 'target=' . $title->getPrefixedUrl() );
108 - } else {
109 - return null;
110 - }
111 - }
112 -
11348 }
11449
11550 /**
Index: trunk/phase3/includes/QueryPage.php
@@ -535,3 +535,73 @@
536536 return $title->getFullURL();
537537 }
538538 }
 539+
 540+/**
 541+ * Class definition for a wanted query page like
 542+ * WantedPages, WantedTemplates, etc
 543+ */
 544+abstract class WantedQueryPage extends QueryPage {
 545+
 546+ function isExpensive() {
 547+ return true;
 548+ }
 549+
 550+ function isSyndicated() {
 551+ return false;
 552+ }
 553+
 554+ /**
 555+ * Cache page existence for performance
 556+ */
 557+ function preprocessResults( $db, $res ) {
 558+ $batch = new LinkBatch;
 559+ while ( $row = $db->fetchObject( $res ) )
 560+ $batch->add( $row->namespace, $row->title );
 561+ $batch->execute();
 562+
 563+ // Back to start for display
 564+ if ( $db->numRows( $res ) > 0 )
 565+ // If there are no rows we get an error seeking.
 566+ $db->dataSeek( $res, 0 );
 567+ }
 568+
 569+ /**
 570+ * Format an individual result
 571+ *
 572+ * @param $skin Skin to use for UI elements
 573+ * @param $result Result row
 574+ * @return string
 575+ */
 576+ public function formatResult( $skin, $result ) {
 577+ $title = Title::makeTitleSafe( $result->namespace, $result->title );
 578+ if( $title instanceof Title ) {
 579+ if( $this->isCached() ) {
 580+ $pageLink = $title->exists()
 581+ ? '<s>' . $skin->makeLinkObj( $title ) . '</s>'
 582+ : $skin->makeBrokenLinkObj( $title );
 583+ } else {
 584+ $pageLink = $skin->makeBrokenLinkObj( $title );
 585+ }
 586+ return wfSpecialList( $pageLink, $this->makeWlhLink( $title, $skin, $result ) );
 587+ } else {
 588+ $tsafe = htmlspecialchars( $result->title );
 589+ return wfMsg( 'wantedpages-badtitle', $tsafe );
 590+ }
 591+ }
 592+
 593+ /**
 594+ * Make a "what links here" link for a given title
 595+ *
 596+ * @param Title $title Title to make the link for
 597+ * @param Skin $skin Skin to use
 598+ * @param object $result Result row
 599+ * @return string
 600+ */
 601+ private function makeWlhLink( $title, $skin, $result ) {
 602+ global $wgLang;
 603+ $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
 604+ $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
 605+ $wgLang->formatNum( $result->value ) );
 606+ return $skin->link( $wlh, $label, array(), array( 'target' => $title->getPrefixedText() ) );
 607+ }
 608+}

Follow-up revisions

RevisionCommit summaryAuthorDate
r49527querypage-work: Merge r49489 from trunkcatrope21:04, 15 April 2009

Comments

#Comment by Tim Starling (talk | contribs)   12:52, 19 May 2009

<3 refactoring

Status & tagging log