Update/add documentation
[lhc/web/wiklou.git] / includes / specials / SpecialDisambiguations.php
1 <?php
2 /**
3 * Implements Special:Disambiguations
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * A special page that lists pages containing links to disambiguations pages
26 *
27 * @ingroup SpecialPage
28 */
29 class DisambiguationsPage extends QueryPage {
30 function __construct( $name = 'Disambiguations' ) {
31 parent::__construct( $name );
32 }
33
34 function isExpensive() {
35 return true;
36 }
37
38 function isSyndicated() {
39 return false;
40 }
41
42 function getPageHeader() {
43 return $this->msg( 'disambiguations-text' )->parseAsBlock();
44 }
45
46 /**
47 * @return string|bool False on failure
48 */
49 function getQueryFromLinkBatch() {
50 $dbr = wfGetDB( DB_SLAVE );
51 $dMsgText = $this->msg( 'disambiguationspage' )->inContentLanguage()->text();
52 $linkBatch = new LinkBatch;
53
54 # If the text can be treated as a title, use it verbatim.
55 # Otherwise, pull the titles from the links table
56 $dp = Title::newFromText( $dMsgText );
57 if( $dp ) {
58 if( $dp->getNamespace() != NS_TEMPLATE ) {
59 # @todo FIXME: We assume the disambiguation message is a template but
60 # the page can potentially be from another namespace :/
61 wfDebug( "Mediawiki:disambiguationspage message does not refer to a template!\n" );
62 }
63 $linkBatch->addObj( $dp );
64 } else {
65 # Get all the templates linked from the Mediawiki:Disambiguationspage
66 $disPageObj = Title::makeTitleSafe( NS_MEDIAWIKI, 'disambiguationspage' );
67 $res = $dbr->select(
68 array( 'pagelinks', 'page' ),
69 'pl_title',
70 array( 'page_id = pl_from',
71 'pl_namespace' => NS_TEMPLATE,
72 'page_namespace' => $disPageObj->getNamespace(),
73 'page_title' => $disPageObj->getDBkey()
74 ), __METHOD__ );
75
76 foreach ( $res as $row ) {
77 $linkBatch->addObj( Title::makeTitle( NS_TEMPLATE, $row->pl_title ));
78 }
79 }
80 $set = $linkBatch->constructSet( 'tl', $dbr );
81
82 if( $set === false ) {
83 # We must always return a valid SQL query, but this way
84 # the DB will always quickly return an empty result
85 $set = 'FALSE';
86 wfDebug( "Mediawiki:disambiguationspage message does not link to any templates!\n" );
87 }
88
89 return $set;
90 }
91
92 function getQueryInfo() {
93 // @todo FIXME: What are pagelinks and p2 doing here?
94 return array (
95 'tables' => array(
96 'templatelinks',
97 'p1' => 'page',
98 'pagelinks',
99 'p2' => 'page'
100 ),
101 'fields' => array(
102 'namespace' => 'p1.page_namespace',
103 'title' => 'p1.page_title',
104 'value' => 'pl_from'
105 ),
106 'conds' => array(
107 $this->getQueryFromLinkBatch(),
108 'p1.page_id = tl_from',
109 'pl_namespace = p1.page_namespace',
110 'pl_title = p1.page_title',
111 'p2.page_id = pl_from',
112 'p2.page_namespace' => MWNamespace::getContentNamespaces()
113 )
114 );
115 }
116
117 function getOrderFields() {
118 return array( 'tl_namespace', 'tl_title', 'value' );
119 }
120
121 function sortDescending() {
122 return false;
123 }
124
125 /**
126 * Fetch links and cache their existence
127 *
128 * @param DatabaseBase $db
129 * @param ResultWrapper $res
130 */
131 function preprocessResults( $db, $res ) {
132 if ( !$res->numRows() ) {
133 return;
134 }
135
136 $batch = new LinkBatch;
137 foreach ( $res as $row ) {
138 $batch->add( $row->namespace, $row->title );
139 }
140 $batch->execute();
141
142 $res->seek( 0 );
143 }
144
145 /**
146 * @param Skin $skin
147 * @param object $result Result row
148 * @return string
149 */
150 function formatResult( $skin, $result ) {
151 $title = Title::newFromID( $result->value );
152 $dp = Title::makeTitle( $result->namespace, $result->title );
153
154 $from = Linker::link( $title );
155 $edit = Linker::link(
156 $title,
157 $this->msg( 'parentheses', $this->msg( 'editlink' )->text() )->escaped(),
158 array(),
159 array( 'redirect' => 'no', 'action' => 'edit' )
160 );
161 $arr = $this->getLanguage()->getArrow();
162 $to = Linker::link( $dp );
163
164 return "$from $edit $arr $to";
165 }
166
167 protected function getGroupName() {
168 return 'pages';
169 }
170 }