It helps when you don't break things...
[lhc/web/wiklou.git] / includes / SpecialWantedpages.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 * implements Special:Wantedpages
9 * @addtogroup SpecialPage
10 */
11 class WantedPagesPage extends QueryPage {
12 var $nlinks;
13
14 function WantedPagesPage( $inc = false, $nlinks = true ) {
15 $this->setListoutput( $inc );
16 $this->nlinks = $nlinks;
17 }
18
19 function getName() {
20 return 'Wantedpages';
21 }
22
23 function isExpensive() {
24 return true;
25 }
26 function isSyndicated() { return false; }
27
28 function getSQL() {
29 global $wgWantedPagesThreshold;
30 $count = $wgWantedPagesThreshold - 1;
31 $dbr = wfGetDB( DB_SLAVE );
32 $pagelinks = $dbr->tableName( 'pagelinks' );
33 $page = $dbr->tableName( 'page' );
34 return
35 "SELECT 'Wantedpages' AS type,
36 pl_namespace AS namespace,
37 pl_title AS title,
38 COUNT(*) AS value
39 FROM $pagelinks
40 LEFT JOIN $page AS pg1
41 ON pl_namespace = pg1.page_namespace AND pl_title = pg1.page_title
42 LEFT JOIN $page AS pg2
43 ON pl_from = pg2.page_id
44 WHERE pg1.page_namespace IS NULL
45 AND pl_namespace NOT IN ( 2, 3 )
46 AND pg2.page_namespace != 8
47 GROUP BY 1,2,3
48 HAVING COUNT(*) > $count";
49 }
50
51 /**
52 * Cache page existence for performance
53 */
54 function preprocessResults( &$db, &$res ) {
55 $batch = new LinkBatch;
56 while ( $row = $db->fetchObject( $res ) )
57 $batch->addObj( Title::makeTitleSafe( $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 /**
67 * Format an individual result
68 *
69 * @param Skin $skin Skin to use for UI elements
70 * @param object $result Result row
71 * @return string
72 */
73 public function formatResult( $skin, $result ) {
74 global $wgLang;
75 $title = Title::makeTitleSafe( $result->namespace, $result->title );
76 if( $title instanceof Title ) {
77 if( $this->isCached() ) {
78 $pageLink = $title->exists()
79 ? '<s>' . $skin->makeLinkObj( $title ) . '</s>'
80 : $skin->makeBrokenLinkObj( $title );
81 } else {
82 $pageLink = $skin->makeBrokenLinkObj( $title );
83 }
84 return wfSpecialList( $pageLink, $this->makeWlhLink( $title, $skin, $result ) );
85 } else {
86 $tsafe = htmlspecialchars( $result->title );
87 return "Invalid title in result set; {$tsafe}";
88 }
89 }
90
91 /**
92 * Make a "what links here" link for a specified result if required
93 *
94 * @param Title $title Title to make the link for
95 * @param Skin $skin Skin to use
96 * @param object $result Result row
97 * @return string
98 */
99 private function makeWlhLink( $title, $skin, $result ) {
100 global $wgLang;
101 if( $this->nlinks ) {
102 $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
103 $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
104 $wgLang->formatNum( $result->value ) );
105 return $skin->makeKnownLinkObj( $wlh, $label, 'target=' . $title->getPrefixedUrl() );
106 } else {
107 return null;
108 }
109 }
110
111 }
112
113 /**
114 * constructor
115 */
116 function wfSpecialWantedpages( $par = null, $specialPage ) {
117 $inc = $specialPage->including();
118
119 if ( $inc ) {
120 @list( $limit, $nlinks ) = explode( '/', $par, 2 );
121 $limit = (int)$limit;
122 $nlinks = $nlinks === 'nlinks';
123 $offset = 0;
124 } else {
125 list( $limit, $offset ) = wfCheckLimits();
126 $nlinks = true;
127 }
128
129 $wpp = new WantedPagesPage( $inc, $nlinks );
130
131 $wpp->doQuery( $offset, $limit, !$inc );
132 }
133
134