Paranoid page table check
[lhc/web/wiklou.git] / includes / specials / SpecialWantedtemplates.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
6
7 /**
8 * A querypage to list the most wanted templates - implements Special:Wantedtemplates
9 * based on SpecialWantedcategories.php by Ævar Arnfjörð Bjarmason <avarab@gmail.com>
10 * makeWlhLink() taken from SpecialMostlinkedtemplates by Rob Church <robchur@gmail.com>
11 *
12 * @ingroup SpecialPage
13 *
14 * @author Danny B.
15 * @copyright Copyright © 2008, Danny B.
16 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
17 */
18 class WantedTemplatesPage extends QueryPage {
19
20 function getName() {
21 return 'Wantedtemplates';
22 }
23
24 function isExpensive() {
25 return true;
26 }
27
28 function isSyndicated() {
29 return false;
30 }
31
32 function getSQL() {
33 $dbr = wfGetDB( DB_SLAVE );
34 list( $templatelinks, $page ) = $dbr->tableNamesN( 'templatelinks', 'page' );
35 $name = $dbr->addQuotes( $this->getName() );
36 return
37 "
38 SELECT $name as type,
39 tl_namespace as namespace,
40 tl_title as title,
41 COUNT(*) as value
42 FROM $templatelinks LEFT JOIN
43 $page ON tl_title = page_title AND tl_namespace = page_namespace
44 WHERE page_title IS NULL AND tl_namespace = ". NS_TEMPLATE ."
45 GROUP BY tl_title
46 ";
47 }
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 $nlinks = wfMsgExt( 'nmembers', array( 'parsemag', 'escape'),
77 $wgLang->formatNum( $result->value ) );
78 return wfSpecialList(
79 $plink,
80 $this->makeWlhLink( $nt, $skin, $result )
81 );
82 }
83
84 /**
85 * Make a "what links here" link for a given title
86 *
87 * @param Title $title Title to make the link for
88 * @param Skin $skin Skin to use
89 * @param object $result Result row
90 * @return string
91 */
92 private function makeWlhLink( $title, $skin, $result ) {
93 global $wgLang;
94 $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
95 $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
96 $wgLang->formatNum( $result->value ) );
97 return $skin->link( $wlh, $label, array(), array( 'target' => $title->getPrefixedText() ) );
98 }
99 }
100
101 /**
102 * constructor
103 */
104 function wfSpecialWantedTemplates() {
105 list( $limit, $offset ) = wfCheckLimits();
106
107 $wpp = new WantedTemplatesPage();
108
109 $wpp->doQuery( $offset, $limit );
110 }