0081331ae798d52b62a6eccea16410cefb31a318
[lhc/web/wiklou.git] / includes / specials / SpecialDisambiguations.php
1 <?php
2 /**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20 /**
21 * @file
22 * @ingroup SpecialPage
23 */
24 class DisambiguationsPage extends PageQueryPage {
25
26 function getName() {
27 return 'Disambiguations';
28 }
29
30 function isExpensive( ) { return true; }
31 function isSyndicated() { return false; }
32
33
34 function getPageHeader( ) {
35 return wfMsgExt( 'disambiguations-text', array( 'parse' ) );
36 }
37
38 function getSQL() {
39 global $wgContentNamespaces;
40
41 $dbr = wfGetDB( DB_SLAVE );
42
43 $dMsgText = wfMsgForContent('disambiguationspage');
44
45 $linkBatch = new LinkBatch;
46
47 # If the text can be treated as a title, use it verbatim.
48 # Otherwise, pull the titles from the links table
49 $dp = Title::newFromText($dMsgText);
50 if( $dp ) {
51 if($dp->getNamespace() != NS_TEMPLATE) {
52 # FIXME we assume the disambiguation message is a template but
53 # the page can potentially be from another namespace :/
54 wfDebug("Mediawiki:disambiguationspage message does not refer to a template!\n");
55 }
56 $linkBatch->addObj( $dp );
57 } else {
58 # Get all the templates linked from the Mediawiki:Disambiguationspage
59 $disPageObj = Title::makeTitleSafe( NS_MEDIAWIKI, 'disambiguationspage' );
60 $res = $dbr->select(
61 array('pagelinks', 'page'),
62 'pl_title',
63 array('page_id = pl_from', 'pl_namespace' => NS_TEMPLATE,
64 'page_namespace' => $disPageObj->getNamespace(), 'page_title' => $disPageObj->getDBkey()),
65 __METHOD__ );
66
67 while ( $row = $dbr->fetchObject( $res ) ) {
68 $linkBatch->addObj( Title::makeTitle( NS_TEMPLATE, $row->pl_title ));
69 }
70 }
71
72 $set = $linkBatch->constructSet( 'lb.tl', $dbr );
73 if( $set === false ) {
74 # We must always return a valid sql query, but this way DB will always quicly return an empty result
75 $set = 'FALSE';
76 wfDebug("Mediawiki:disambiguationspage message does not link to any templates!\n");
77 }
78
79 list( $page, $pagelinks, $templatelinks) = $dbr->tableNamesN( 'page', 'pagelinks', 'templatelinks' );
80
81 if ( $wgContentNamespaces ) {
82 $nsclause = 'IN (' . $dbr->makeList( $wgContentNamespaces ) . ')';
83 } else {
84 $nsclause = '= ' . NS_MAIN;
85 }
86
87 $sql = "SELECT 'Disambiguations' AS \"type\", pb.page_namespace AS namespace,"
88 ." pb.page_title AS title, la.pl_from AS value"
89 ." FROM {$templatelinks} AS lb, {$page} AS pb, {$pagelinks} AS la, {$page} AS pa"
90 ." WHERE $set" # disambiguation template(s)
91 .' AND pa.page_id = la.pl_from'
92 .' AND pa.page_namespace ' . $nsclause
93 .' AND pb.page_id = lb.tl_from'
94 .' AND pb.page_namespace = la.pl_namespace'
95 .' AND pb.page_title = la.pl_title'
96 .' ORDER BY lb.tl_namespace, lb.tl_title';
97
98 return $sql;
99 }
100
101 function getOrder() {
102 return '';
103 }
104
105 function formatResult( $skin, $result ) {
106 global $wgContLang;
107 $title = Title::newFromID( $result->value );
108 $dp = Title::makeTitle( $result->namespace, $result->title );
109
110 $from = $skin->link( $title );
111 $edit = $skin->link( $title, wfMsgExt( 'parentheses', array( 'escape' ), wfMsg( 'editlink' ) ) , array(), array( 'redirect' => 'no', 'action' => 'edit' ) );
112 $arr = $wgContLang->getArrow();
113 $to = $skin->link( $dp );
114
115 return "$from $edit $arr $to";
116 }
117 }
118
119 /**
120 * Constructor
121 */
122 function wfSpecialDisambiguations() {
123 list( $limit, $offset ) = wfCheckLimits();
124
125 $sd = new DisambiguationsPage();
126
127 return $sd->doQuery( $offset, $limit );
128 }