94547ff414e7f83fbfbf4b0183fcb340d258f629
[lhc/web/wiklou.git] / includes / SpecialSpecialpages.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 * Main function
9 */
10 function wfSpecialSpecialpages() {
11 global $wgOut, $wgUser, $wgMessageCache;
12
13 $wgMessageCache->loadAllMessages();
14
15 $wgOut->setRobotpolicy( 'noindex,nofollow' ); # Is this really needed?
16
17 # Read the special pages
18 $pagesRegular = SpecialPage::getRegularPages();
19 $pagesRestricted = SpecialPage::getRestrictedPages();
20 if( count( $pagesRegular ) == 0 && count( $pagesRestricted ) == 0 ) {
21 # Yeah, that was pointless. Thanks for coming.
22 return;
23 }
24
25 # Put regular and restricted special pages into sortable arrays
26 $unsortedPagesRegular = wfSpecialSpecialpages_read( $pagesRegular );
27 $unsortedPagesRestricted = wfSpecialSpecialpages_read( $pagesRestricted );
28
29 # Read the template
30 $tpl = wfMsg( 'specialpages-tpl' );
31 $newSpecialPage = '';
32
33 # Parse the template and create a localized wikitext page
34 foreach ( explode( "\n", $tpl ) as $line ) {
35 # Look for 'special:' in the line
36 $pos = strpos( strtolower( $line ), 'special:' );
37 if( $pos >= 0 ) {
38
39 # Preserve the line start
40 $lineStart = ( $pos > 0 ) ? substr( $line, 0, $pos ) : '';
41
42 # Get the canonical special page name
43 $canonical = strtolower( trim( substr( $line, $pos + strlen( 'special:' ) ) ) );
44
45 # Check if it is a valid regular special page name
46 # Restricted pages will be added at the end
47 if( isset( $unsortedPagesRegular[$canonical] ) ) {
48 # Add a new line
49 $newSpecialPage .= $lineStart . "[[special:" . $canonical . "|" . $unsortedPagesRegular[$canonical] . "]]\n";
50 # Delete this regular special page from the array to avoid double output
51 unset( $unsortedPagesRegular[$canonical] );
52 } else {
53 # Ok, no valid special page, but add the line to the output
54 $newSpecialPage .= $line . "\n";
55 }
56 } else {
57 $newSpecialPage .= $line . "\n";
58 }
59 }
60
61 # Add the rest
62 $newSpecialPage .= wfSpecialSpecialpages_out( 'specialpages-other', $unsortedPagesRegular );
63 # Add the restricted special pages
64 $newSpecialPage .= wfSpecialSpecialpages_out( 'restrictedpheading', $unsortedPagesRestricted );
65
66 # Output the customized special pages list
67 $wgOut->addWikiText( $newSpecialPage );
68
69 if ( $wgUser->isAllowed( 'editinterface' ) ) {
70 # Output a nice link to edit the template
71 $wgOut->addHtml( wfSpecialSpecialpages_edit() );
72 }
73 }
74
75 /**
76 * Output the rest special pages as wikitext
77 * @param string $heading Message name for the heading
78 * @param array $sortedPages List of other special pages
79 * @return string $rest Wikitext
80 */
81 function wfSpecialSpecialpages_out( $heading, $pages ) {
82 global $wgOut, $wgSortSpecialPages;
83
84 if( count( $pages ) == 0 ) {
85 # Yeah, that was pointless. Thanks for coming.
86 return;
87 }
88
89 # Sort
90 if ( $wgSortSpecialPages ) {
91 asort( $pages );
92 }
93
94 # Now output the rest special pages as wikitext
95 $rest = '== ' . wfMsgExt( $heading, array( 'parseinline' ) ) . " ==\n";
96 foreach ( $pages as $title => $desc ) {
97 $rest .= "* [[special:" . $title . "|" . $desc . "]]\n";
98 }
99 return $rest;
100 }
101
102 /**
103 * Create array with descriptions and names of special pages
104 * @param array $pagelist List of special pages
105 * @return array $unsortedPagesList Wikitext
106 */
107 function wfSpecialSpecialpages_read( $pagelist ) {
108 $unsortedPagesList = array();
109 foreach ( $pagelist as $page ) {
110 if ( $page->isListed() ) {
111 $name = strtolower( $page->getName() );
112 $unsortedPagesList[$name] = $page->getDescription();
113 }
114 }
115 return $unsortedPagesList;
116 }
117
118 /**
119 * Build edit link to MediaWiki:specialpages-tpl
120 *
121 * @return string
122 */
123 function wfSpecialSpecialpages_edit() {
124 global $wgUser, $wgContLang;
125 $align = $wgContLang->isRtl() ? 'left' : 'right';
126 $skin = $wgUser->getSkin();
127 $link = $skin->makeLink ( 'MediaWiki:specialpages-tpl', wfMsgHtml( 'specialpages-edit' ) );
128 return '<p style="float:' . $align . ';" class="mw-specialspecialpages-edit">' . $link . '</p>';
129 }