pages-from-category function for Special:Export
[lhc/web/wiklou.git] / includes / SpecialExport.php
1 <?php
2 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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 *
21 * @addtogroup SpecialPage
22 */
23
24 function wfExportGetPagesFromCategory( $title ) {
25 global $wgContLang;
26
27 $name = $title->getDBKey();
28
29 $dbr = wfGetDB( DB_SLAVE );
30
31 list( $page, $categorylinks ) = $dbr->tableNamesN( 'page', 'categorylinks' );
32 $sql = "SELECT page_namespace, page_title FROM $page " .
33 "JOIN $categorylinks ON cl_from = page_id " .
34 "WHERE cl_to = " . $dbr->addQuotes( $name );
35
36 $pages = array();
37 $res = $dbr->query( $sql, 'wfExportGetPagesFromCategory' );
38 while ( $row = $dbr->fetchObject( $res ) ) {
39 $n = $row->page_title;
40 if ($row->page_namespace) {
41 $ns = $wgContLang->getNsText( $row->page_namespace );
42 $n = $ns . ':' . $n;
43 }
44
45 $pages[] = $n;
46 }
47 $dbr->freeResult($res);
48
49 return $pages;
50 }
51
52 /**
53 *
54 */
55 function wfSpecialExport( $page = '' ) {
56 global $wgOut, $wgRequest, $wgExportAllowListContributors;
57 global $wgExportAllowHistory, $wgExportMaxHistory;
58
59 $curonly = true;
60 $doexport = false;
61 $page = NULL;
62
63 if ( $wgRequest->getCheck( 'addcat' ) ) {
64 $page = $wgRequest->getText( 'pages' );
65 $catname = $wgRequest->getText( 'catname' );
66
67 if ( $catname !== '' && $catname !== NULL && $catname !== false ) {
68 $t = Title::makeTitleSafe( NS_CATEGORY, $catname );
69 if ( $t ) {
70 $catpages = wfExportGetPagesFromCategory( $t );
71 if ( $catpages ) $page .= "\n" . implode( "\n", $catpages );
72 }
73 }
74 }
75 else if( $wgRequest->wasPosted() ) {
76 $page = $wgRequest->getText( 'pages' );
77 $curonly = $wgRequest->getCheck( 'curonly' );
78 $rawOffset = $wgRequest->getVal( 'offset' );
79 if( $rawOffset ) {
80 $offset = wfTimestamp( TS_MW, $rawOffset );
81 } else {
82 $offset = null;
83 }
84 $limit = $wgRequest->getInt( 'limit' );
85 $dir = $wgRequest->getVal( 'dir' );
86 $history = array(
87 'dir' => 'asc',
88 'offset' => false,
89 'limit' => $wgExportMaxHistory,
90 );
91 $historyCheck = $wgRequest->getCheck( 'history' );
92 if ( $curonly ) {
93 $history = WikiExporter::CURRENT;
94 } elseif ( !$historyCheck ) {
95 if ( $limit > 0 && $limit < $wgExportMaxHistory ) {
96 $history['limit'] = $limit;
97 }
98 if ( !is_null( $offset ) ) {
99 $history['offset'] = $offset;
100 }
101 if ( strtolower( $dir ) == 'desc' ) {
102 $history['dir'] = 'desc';
103 }
104 }
105
106 if( $page != '' ) $doexport = true;
107 } else {
108 // Default to current-only for GET requests
109 $page = $wgRequest->getText( 'pages', $page );
110 $historyCheck = $wgRequest->getCheck( 'history' );
111 if( $historyCheck ) {
112 $history = WikiExporter::FULL;
113 } else {
114 $history = WikiExporter::CURRENT;
115 }
116
117 if( $page != '' ) $doexport = true;
118 }
119
120 if( !$wgExportAllowHistory ) {
121 // Override
122 $history = WikiExporter::CURRENT;
123 }
124
125 $list_authors = $wgRequest->getCheck( 'listauthors' );
126 if ( !$curonly || !$wgExportAllowListContributors ) $list_authors = false ;
127
128 if ( $doexport ) {
129 $wgOut->disable();
130
131 // Cancel output buffering and gzipping if set
132 // This should provide safer streaming for pages with history
133 wfResetOutputBuffers();
134 header( "Content-type: application/xml; charset=utf-8" );
135 $pages = explode( "\n", $page );
136
137 $db = wfGetDB( DB_SLAVE );
138 $exporter = new WikiExporter( $db, $history );
139 $exporter->list_authors = $list_authors ;
140 $exporter->openStream();
141
142 foreach( $pages as $page ) {
143 /*
144 if( $wgExportMaxHistory && !$curonly ) {
145 $title = Title::newFromText( $page );
146 if( $title ) {
147 $count = Revision::countByTitle( $db, $title );
148 if( $count > $wgExportMaxHistory ) {
149 wfDebug( __FUNCTION__ .
150 ": Skipped $page, $count revisions too big\n" );
151 continue;
152 }
153 }
154 }*/
155
156 #Bug 8824: Only export pages the user can read
157 $title = Title::newFromText( $page );
158 if( is_null( $title ) ) continue; #TODO: perhaps output an <error> tag or something.
159 if( !$title->userCan( 'read' ) ) continue; #TODO: perhaps output an <error> tag or something.
160
161 $exporter->pageByTitle( $title );
162 }
163
164 $exporter->closeStream();
165 return;
166 }
167
168 $wgOut->addWikiText( wfMsg( "exporttext" ) );
169 $titleObj = SpecialPage::getTitleFor( "Export" );
170
171 $form = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalUrl() ) );
172
173 $form .= wfInputLabel( wfMsg( 'export-addcattext' ), 'catname', 'catname', 40 ) . ' ';
174 $form .= wfSubmitButton( wfMsg( 'export-addcat' ), array( 'name' => 'addcat' ) ) . '<br />';
175
176 $form .= wfOpenElement( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ) ) . htmlspecialchars($page). '</textarea><br />';
177
178 if( $wgExportAllowHistory ) {
179 $form .= wfCheck( 'curonly', true, array( 'value' => 'true', 'id' => 'curonly' ) );
180 $form .= wfLabel( wfMsg( 'exportcuronly' ), 'curonly' ) . '<br />';
181 } else {
182 $wgOut->addWikiText( wfMsg( 'exportnohistory' ) );
183 }
184 $form .= wfHidden( 'action', 'submit' );
185 $form .= wfSubmitButton( wfMsg( 'export-submit' ) ) . '</form>';
186 $wgOut->addHtml( $form );
187 }
188
189 ?>