6f7e727c8cb8e993149640097d6df92822ace033
[lhc/web/wiklou.git] / includes / specials / SpecialWantedpages.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
6
7 /**
8 * implements Special:Wantedpages
9 * @ingroup 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 $sql = "SELECT 'Wantedpages' AS type,
35 pl_namespace AS namespace,
36 pl_title AS title,
37 COUNT(*) AS value
38 FROM $pagelinks
39 LEFT JOIN $page AS pg1
40 ON pl_namespace = pg1.page_namespace AND pl_title = pg1.page_title
41 LEFT JOIN $page AS pg2
42 ON pl_from = pg2.page_id
43 WHERE pg1.page_namespace IS NULL
44 AND pl_namespace NOT IN ( 2, 3 )
45 AND pg2.page_namespace != 8
46 GROUP BY pl_namespace, pl_title
47 HAVING COUNT(*) > $count";
48
49 wfRunHooks( 'WantedPages::getSQL', array( &$this, &$sql ) );
50 return $sql;
51 }
52
53 /**
54 * Cache page existence for performance
55 */
56 function preprocessResults( $db, $res ) {
57 $batch = new LinkBatch;
58 while ( $row = $db->fetchObject( $res ) )
59 $batch->add( $row->namespace, $row->title );
60 $batch->execute();
61
62 // Back to start for display
63 if ( $db->numRows( $res ) > 0 )
64 // If there are no rows we get an error seeking.
65 $db->dataSeek( $res, 0 );
66 }
67
68 /**
69 * Format an individual result
70 *
71 * @param $skin Skin to use for UI elements
72 * @param $result Result row
73 * @return string
74 */
75 public function formatResult( $skin, $result ) {
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 to make the link for
96 * @param $skin Skin to use
97 * @param $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 }