r23372 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23371‎ | r23372 | r23373 >
Date:08:55, 25 June 2007
Author:robchurch
Status:old
Tags:
Comment:
With one hand, Rob giveth, with the other, he taketh...
Modified paths:
  • /branches/robchurch/reports/includes/SpecialWantedpages.php (deleted) (history)
  • /branches/robchurch/reports/includes/reports/WantedPagesReport.php (added) (history)

Diff [purge]

Index: branches/robchurch/reports/includes/SpecialWantedpages.php
@@ -1,134 +0,0 @@
2 -<?php
3 -/**
4 - *
5 - * @addtogroup SpecialPage
6 - */
7 -
8 -/**
9 - * implements Special:Wantedpages
10 - * @addtogroup SpecialPage
11 - */
12 -class WantedPagesPage extends QueryPage {
13 - var $nlinks;
14 -
15 - function WantedPagesPage( $inc = false, $nlinks = true ) {
16 - $this->setListoutput( $inc );
17 - $this->nlinks = $nlinks;
18 - }
19 -
20 - function getName() {
21 - return 'Wantedpages';
22 - }
23 -
24 - function isExpensive() {
25 - return true;
26 - }
27 - function isSyndicated() { return false; }
28 -
29 - function getSQL() {
30 - global $wgWantedPagesThreshold;
31 - $count = $wgWantedPagesThreshold - 1;
32 - $dbr = wfGetDB( DB_SLAVE );
33 - $pagelinks = $dbr->tableName( 'pagelinks' );
34 - $page = $dbr->tableName( 'page' );
35 - return
36 - "SELECT 'Wantedpages' AS type,
37 - pl_namespace AS namespace,
38 - pl_title AS title,
39 - COUNT(*) AS value
40 - FROM $pagelinks
41 - LEFT JOIN $page AS pg1
42 - ON pl_namespace = pg1.page_namespace AND pl_title = pg1.page_title
43 - LEFT JOIN $page AS pg2
44 - ON pl_from = pg2.page_id
45 - WHERE pg1.page_namespace IS NULL
46 - AND pl_namespace NOT IN ( 2, 3 )
47 - AND pg2.page_namespace != 8
48 - GROUP BY 1,2,3
49 - HAVING COUNT(*) > $count";
50 - }
51 -
52 - /**
53 - * Cache page existence for performance
54 - */
55 - function preprocessResults( &$db, &$res ) {
56 - $batch = new LinkBatch;
57 - while ( $row = $db->fetchObject( $res ) )
58 - $batch->addObj( Title::makeTitleSafe( $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 - /**
68 - * Format an individual result
69 - *
70 - * @param Skin $skin Skin to use for UI elements
71 - * @param object $result Result row
72 - * @return string
73 - */
74 - public function formatResult( $skin, $result ) {
75 - global $wgLang;
76 - $title = Title::makeTitleSafe( $result->namespace, $result->title );
77 - if( $title instanceof Title ) {
78 - if( $this->isCached() ) {
79 - $pageLink = $title->exists()
80 - ? '<s>' . $skin->makeLinkObj( $title ) . '</s>'
81 - : $skin->makeBrokenLinkObj( $title );
82 - } else {
83 - $pageLink = $skin->makeBrokenLinkObj( $title );
84 - }
85 - return wfSpecialList( $pageLink, $this->makeWlhLink( $title, $skin, $result ) );
86 - } else {
87 - $tsafe = htmlspecialchars( $result->title );
88 - return "Invalid title in result set; {$tsafe}";
89 - }
90 - }
91 -
92 - /**
93 - * Make a "what links here" link for a specified result if required
94 - *
95 - * @param Title $title Title to make the link for
96 - * @param Skin $skin Skin to use
97 - * @param object $result Result row
98 - * @return string
99 - */
100 - private function makeWlhLink( $title, $skin, $result ) {
101 - global $wgLang;
102 - if( $this->nlinks ) {
103 - $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
104 - $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
105 - $wgLang->formatNum( $result->value ) );
106 - return $skin->makeKnownLinkObj( $wlh, $label, 'target=' . $title->getPrefixedUrl() );
107 - } else {
108 - return null;
109 - }
110 - }
111 -
112 -}
113 -
114 -/**
115 - * constructor
116 - */
117 -function wfSpecialWantedpages( $par = null, $specialPage ) {
118 - $inc = $specialPage->including();
119 -
120 - if ( $inc ) {
121 - @list( $limit, $nlinks ) = explode( '/', $par, 2 );
122 - $limit = (int)$limit;
123 - $nlinks = $nlinks === 'nlinks';
124 - $offset = 0;
125 - } else {
126 - list( $limit, $offset ) = wfCheckLimits();
127 - $nlinks = true;
128 - }
129 -
130 - $wpp = new WantedPagesPage( $inc, $nlinks );
131 -
132 - $wpp->doQuery( $offset, $limit, !$inc );
133 -}
134 -
135 -?>
Index: branches/robchurch/reports/includes/reports/WantedPagesReport.php
@@ -0,0 +1,214 @@
 2+<?php
 3+
 4+/**
 5+ * Report lists all pages which don't exist but have
 6+ * incoming links
 7+ *
 8+ * @addtogroup Reports
 9+ * @author Rob Church <robchur@gmail.com>
 10+ */
 11+class WantedPagesReport extends Report {
 12+
 13+ /**
 14+ * Constructor
 15+ */
 16+ public function __construct() {
 17+ parent::__construct();
 18+ }
 19+
 20+ /**
 21+ * Get the name of the report
 22+ *
 23+ * @return string
 24+ */
 25+ public function getName() {
 26+ return 'Wantedpages';
 27+ }
 28+
 29+ /**
 30+ * Get a HTML header for the top of the page
 31+ *
 32+ * @return string
 33+ */
 34+ public function getHeader() {
 35+ global $wgLang, $wgWantedPagesThreshold;
 36+ return wfMsgExt( 'wantedpages-header', 'parse',
 37+ $wgLang->formatNum( $wgWantedPagesThreshold ) );
 38+ }
 39+
 40+ /**
 41+ * Is it appropriate to allow filtering redirects?
 42+ *
 43+ * @return bool
 44+ */
 45+ public function allowRedirectFilter() {
 46+ return false;
 47+ }
 48+
 49+ /**
 50+ * Should redirects be filtered from results?
 51+ *
 52+ * @return bool
 53+ */
 54+ public function excludeRedirects() {
 55+ return false;
 56+ }
 57+
 58+ /**
 59+ * Is it appropriate to allow filtering namespaces?
 60+ *
 61+ * @return bool
 62+ */
 63+ public function allowNamespaceFilter() {
 64+ return true;
 65+ }
 66+
 67+ /**
 68+ * Get a list of namespaces this report can be run
 69+ * against - false indicates *all* namespaces
 70+ *
 71+ * @return mixed
 72+ */
 73+ public function getApplicableNamespaces() {
 74+ return array(
 75+ NS_MAIN,
 76+ ) + $GLOBALS['wgContentNamespaces'];
 77+ }
 78+
 79+ /**
 80+ * Return base SQL for the report
 81+ *
 82+ * @param Database $dbr Database object being queried
 83+ * @return string
 84+ */
 85+ public function getBaseSql( $dbr ) {
 86+ list( $page, $pagelinks ) = $dbr->tableNamesN( 'page', 'pagelinks' );
 87+ # Note: We have no page identifier, but we can use pagelinks.pl_title
 88+ # instead, since pagers can handle it \o/
 89+ return
 90+ "SELECT
 91+ pl_title AS rp_id,
 92+ pl_namespace AS rp_namespace,
 93+ pl_title AS rp_title,
 94+ 0 AS rp_redirect,
 95+ COUNT(*) AS count
 96+ FROM {$pagelinks}
 97+ LEFT JOIN {$page} AS pg1 ON
 98+ pl_namespace = pg1.page_namespace
 99+ AND pl_title = pg1.page_title
 100+ LEFT JOIN {$page} AS pg2 ON
 101+ pl_from = pg2.page_id";
 102+ }
 103+
 104+ /**
 105+ * Return additional WHERE clauses and other conditions
 106+ * to which the paging clauses will be appened when
 107+ * the report runs live
 108+ *
 109+ * @param Database $dbr Database object being queried
 110+ * @return array
 111+ */
 112+ public function getExtraConditions( $dbr ) {
 113+ return array(
 114+ 'pg1.page_namespace IS NULL',
 115+ // Excludes links from the MediaWiki namespace; works around
 116+ // an old bug which causes pages like "$1" to appear on the list
 117+ 'pg2.page_namespace != 8',
 118+ );
 119+ }
 120+
 121+ /**
 122+ * Get the column used for paging when the report is run live
 123+ *
 124+ * @return string
 125+ */
 126+ public function getPagingColumn() {
 127+ return 'pl_title';
 128+ }
 129+
 130+ /**
 131+ * Get a partial WHERE clause to filter on namespace when
 132+ * the report is run live
 133+ *
 134+ * @param int $namespace Namespace to limit to
 135+ * @return string
 136+ */
 137+ public function getNamespaceClause( $namespace ) {
 138+ return "pl_namespace = {$namespace}";
 139+ }
 140+
 141+ /**
 142+ * Get additional SQL to be inserted between the
 143+ * conditions and ORDER clauses when the report is run live
 144+ *
 145+ * @param Database $dbr Database object being queried
 146+ * @return string
 147+ */
 148+ public function getExtraSql( $dbr ) {
 149+ $count = intval( $GLOBALS['wgWantedPagesThreshold'] ) - 1;
 150+ return
 151+ " GROUP BY 1, 2, 3
 152+ HAVING count > {$count}";
 153+ }
 154+
 155+ /**
 156+ * Get ORDER BY clauses to be applied when the
 157+ * report is run live
 158+ *
 159+ * @return array
 160+ */
 161+ public function getOrderingClauses() {
 162+ return array(
 163+ 'count DESC',
 164+ );
 165+ }
 166+
 167+ /**
 168+ * Given a result object, extract additional parameters
 169+ * as a dictionary for later use
 170+ *
 171+ * @param object $row Result row
 172+ * @return array
 173+ */
 174+ public function extractParameters( $row ) {
 175+ return array(
 176+ 'count' => $row->count,
 177+ );
 178+ }
 179+
 180+ /**
 181+ * Format an individual result row
 182+ *
 183+ * @param Title $title Result title
 184+ * @param object $row Result row
 185+ * @param array $params Result parameters
 186+ * @param Skin $skin User skin
 187+ * @return string
 188+ */
 189+ public function formatRow( $title, $row, $params, $skin ) {
 190+ global $wgLang;
 191+ $links = $this->makeLinksLink(
 192+ $title,
 193+ wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ), $params['count'] ),
 194+ $skin
 195+ );
 196+ return "<li>" . $skin->makeLinkObj( $title ) . " ({$links})</li>\n";
 197+ }
 198+
 199+ /**
 200+ * Build a "what links here" link with the
 201+ * specified title as a target
 202+ *
 203+ * @param Title $target Title to show links to
 204+ * @param string $label Link label
 205+ * @param Skin $skin Skin to use
 206+ * @return string
 207+ */
 208+ private function makeLinksLink( $target, $label, $skin ) {
 209+ $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
 210+ return $skin->makeKnownLinkObj( $wlh, $label, 'target=' . $target->getPrefixedUrl() );
 211+ }
 212+
 213+}
 214+
 215+?>
\ No newline at end of file
Property changes on: branches/robchurch/reports/includes/reports/WantedPagesReport.php
___________________________________________________________________
Added: svn:eol-style
1216 + native

Status & tagging log