Make WatchlistEditor::extractTitles() more robust when dealing with blank lines
[lhc/web/wiklou.git] / includes / WatchlistEditor.php
1 <?php
2
3 /**
4 * Provides the UI through which users can perform editing
5 * operations on their watchlist
6 *
7 * @addtogroup Watchlist
8 * @author Rob Church <robchur@gmail.com>
9 */
10 class WatchlistEditor {
11
12 /**
13 * Editing modes
14 */
15 const EDIT_CLEAR = 1;
16 const EDIT_RAW = 2;
17 const EDIT_NORMAL = 3;
18
19 /**
20 * Main execution point
21 *
22 * @param User $user
23 * @param OutputPage $output
24 * @param WebRequest $request
25 * @param int $mode
26 */
27 public function execute( $user, $output, $request, $mode ) {
28 if( wfReadOnly() ) {
29 $output->readOnlyPage();
30 return;
31 }
32 switch( $mode ) {
33 case self::EDIT_CLEAR:
34 $output->setPageTitle( wfMsg( 'watchlistedit-clear-title' ) );
35 if( $request->wasPosted() && $this->checkToken( $request, $user ) ) {
36 $this->clearWatchlist( $user );
37 $user->invalidateCache();
38 $output->addHtml( wfMsgExt( 'watchlistedit-clear-done', 'parse' ) );
39 } else {
40 $this->showClearForm( $output, $user );
41 }
42 break;
43 case self::EDIT_RAW:
44 $output->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
45 if( $request->wasPosted() && $this->checkToken( $request, $user ) ) {
46 $titles = $this->extractTitles( $request->getText( 'titles' ) );
47 $this->clearWatchlist( $user );
48 $this->watchTitles( $titles, $user );
49 $user->invalidateCache();
50 $output->addHtml( wfMsgExt( 'watchlistedit-raw-done', 'parse' ) );
51 }
52 $this->showRawForm( $output, $user );
53 break;
54 case self::EDIT_NORMAL:
55 $output->setPageTitle( wfMsg( 'watchlistedit-normal-title' ) );
56 if( $request->wasPosted() && $this->checkToken( $request, $user ) ) {
57 $titles = $this->extractTitles( $request->getArray( 'titles' ) );
58 $this->unwatchTitles( $titles, $user );
59 $user->invalidateCache();
60 $output->addHtml( wfMsgExt( 'watchlistedit-normal-done', 'parsemag',
61 $GLOBALS['wgLang']->formatNum( count( $titles ) ) ) );
62 $this->showTitles( $titles, $output, $user->getSkin() );
63 }
64 $this->showNormalForm( $output, $user );
65 }
66 }
67
68 /**
69 * Check the edit token from a form submission
70 *
71 * @param WebRequest $request
72 * @param User $user
73 * @return bool
74 */
75 private function checkToken( $request, $user ) {
76 return $user->matchEditToken( $request->getVal( 'token' ), 'watchlistedit' );
77 }
78
79 /**
80 * Extract a list of titles from a text list; if we're given
81 * an array, convert each item into a Title
82 *
83 * @param mixed $list
84 * @return array
85 */
86 private function extractTitles( $list ) {
87 $titles = array();
88 if( !is_array( $list ) ) {
89 $list = explode( "\n", trim( $list ) );
90 if( !is_array( $list ) )
91 return array();
92 }
93 foreach( $list as $text ) {
94 $text = trim( $text );
95 if( strlen( $text ) > 0 ) {
96 $title = Title::newFromText( $text );
97 if( $title instanceof Title )
98 $titles[] = $title;
99 }
100 }
101 return $titles;
102 }
103
104 /**
105 * Print out a list of linked titles
106 *
107 * @param array $titles
108 * @param OutputPage $output
109 * @param Skin $skin
110 */
111 private function showTitles( $titles, $output, $skin ) {
112 $talk = htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( NS_TALK ) );
113 // Do a batch existence check
114 $batch = new LinkBatch();
115 foreach( $titles as $title ) {
116 $batch->addObj( $title );
117 $batch->addObj( $title->getTalkPage() );
118 }
119 $batch->execute();
120 // Print out the list
121 $output->addHtml( "<ul>\n" );
122 foreach( $titles as $title )
123 $output->addHtml( "<li>" . $skin->makeLinkObj( $title )
124 . ' (' . $skin->makeLinkObj( $title->getTalkPage(), $talk ) . ")</li>\n" );
125 $output->addHtml( "</ul>\n" );
126 }
127
128 /**
129 * Count the number of titles on a user's watchlist, excluding talk pages
130 *
131 * @param User $user
132 * @return int
133 */
134 private function countWatchlist( $user ) {
135 $dbr = wfGetDB( DB_SLAVE );
136 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->getId() ), __METHOD__ );
137 $row = $dbr->fetchObject( $res );
138 return ceil( $row->count / 2 ); // Paranoia
139 }
140
141 /**
142 * Get a list of titles on a user's watchlist, excluding talk pages,
143 * and return as a two-dimensional array with namespace, title and
144 * redirect status
145 *
146 * @param User $user
147 * @return array
148 */
149 private function getWatchlist( $user ) {
150 $titles = array();
151 $dbr = wfGetDB( DB_SLAVE );
152 $uid = intval( $user->getId() );
153 list( $watchlist, $page ) = $dbr->tableNamesN( 'watchlist', 'page' );
154 $sql = "SELECT wl_namespace, wl_title, page_id, page_is_redirect
155 FROM {$watchlist} LEFT JOIN {$page} ON ( wl_namespace = page_namespace
156 AND wl_title = page_title ) WHERE wl_user = {$uid}";
157 $res = $dbr->query( $sql, __METHOD__ );
158 if( $res && $dbr->numRows( $res ) > 0 ) {
159 $cache = LinkCache::singleton();
160 while( $row = $dbr->fetchObject( $res ) ) {
161 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
162 if( $title instanceof Title ) {
163 // Update the link cache while we're at it
164 if( $row->page_id ) {
165 $cache->addGoodLinkObj( $row->page_id, $title );
166 } else {
167 $cache->addBadLinkObj( $title );
168 }
169 // Ignore non-talk
170 if( !$title->isTalkPage() )
171 $titles[$row->wl_namespace][$row->wl_title] = $row->page_is_redirect;
172 }
173 }
174 }
175 return $titles;
176 }
177
178 /**
179 * Show a message indicating the number of items on the user's watchlist,
180 * and return this count for additional checking
181 *
182 * @param OutputPage $output
183 * @param User $user
184 * @return int
185 */
186 private function showItemCount( $output, $user ) {
187 if( ( $count = $this->countWatchlist( $user ) ) > 0 ) {
188 $output->addHtml( wfMsgExt( 'watchlistedit-numitems', 'parsemag',
189 $GLOBALS['wgLang']->formatNum( $count ) ) );
190 } else {
191 $output->addHtml( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
192 }
193 return $count;
194 }
195
196 /**
197 * Remove all titles from a user's watchlist
198 *
199 * @param User $user
200 */
201 private function clearWatchlist( $user ) {
202 $dbw = wfGetDB( DB_MASTER );
203 $dbw->delete( 'watchlist', array( 'wl_user' => $user->getId() ), __METHOD__ );
204 }
205
206 /**
207 * Add a list of titles to a user's watchlist
208 *
209 * @param array $titles
210 * @param User $user
211 */
212 private function watchTitles( $titles, $user ) {
213 $dbw = wfGetDB( DB_MASTER );
214 $rows = array();
215 foreach( $titles as $title ) {
216 $rows[] = array(
217 'wl_user' => $user->getId(),
218 'wl_namespace' => ( $title->getNamespace() & ~1 ),
219 'wl_title' => $title->getDBkey(),
220 'wl_notificationtimestamp' => null,
221 );
222 $rows[] = array(
223 'wl_user' => $user->getId(),
224 'wl_namespace' => ( $title->getNamespace() | 1 ),
225 'wl_title' => $title->getDBkey(),
226 'wl_notificationtimestamp' => null,
227 );
228 }
229 $dbw->insert( 'watchlist', $rows, __METHOD__, 'IGNORE' );
230 }
231
232 /**
233 * Remove a list of titles from a user's watchlist
234 *
235 * @param array $titles
236 * @param User $user
237 */
238 private function unwatchTitles( $titles, $user ) {
239 $dbw = wfGetDB( DB_MASTER );
240 foreach( $titles as $title ) {
241 $dbw->delete(
242 'watchlist',
243 array(
244 'wl_user' => $user->getId(),
245 'wl_namespace' => ( $title->getNamespace() & ~1 ),
246 'wl_title' => $title->getDBkey(),
247 ),
248 __METHOD__
249 );
250 $dbw->delete(
251 'watchlist',
252 array(
253 'wl_user' => $user->getId(),
254 'wl_namespace' => ( $title->getNamespace() | 1 ),
255 'wl_title' => $title->getDBkey(),
256 ),
257 __METHOD__
258 );
259 }
260 }
261
262 /**
263 * Show a confirmation form for users wishing to clear their watchlist
264 *
265 * @param OutputPage $output
266 * @param User $user
267 */
268 private function showClearForm( $output, $user ) {
269 if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) {
270 $self = SpecialPage::getTitleFor( 'Watchlist' );
271 $form = Xml::openElement( 'form', array( 'method' => 'post',
272 'action' => $self->getLocalUrl( 'action=clear' ) ) );
273 $form .= Xml::hidden( 'token', $user->editToken( 'watchlistedit' ) );
274 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-clear-legend' ) . '</legend>';
275 $form .= wfMsgExt( 'watchlistedit-clear-confirm', 'parse' );
276 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-clear-submit' ) ) . '</p>';
277 $form .= '</fieldset></form>';
278 $output->addHtml( $form );
279 }
280 }
281
282 /**
283 * Show the standard watchlist editing form
284 *
285 * @param OutputPage $output
286 * @param User $user
287 */
288 private function showNormalForm( $output, $user ) {
289 if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) {
290 $self = SpecialPage::getTitleFor( 'Watchlist' );
291 $form = Xml::openElement( 'form', array( 'method' => 'post',
292 'action' => $self->getLocalUrl( 'action=edit' ) ) );
293 $form .= Xml::hidden( 'token', $user->editToken( 'watchlistedit' ) );
294 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-normal-legend' ) . '</legend>';
295 $form .= wfMsgExt( 'watchlistedit-normal-explain', 'parse' );
296 foreach( $this->getWatchlist( $user ) as $namespace => $pages ) {
297 $form .= '<h2>' . $this->getNamespaceHeading( $namespace ) . '</h2>';
298 $form .= '<ul>';
299 foreach( $pages as $dbkey => $redirect ) {
300 $title = Title::makeTitleSafe( $namespace, $dbkey );
301 $form .= $this->buildRemoveLine( $title, $redirect, $user->getSkin() );
302 }
303 $form .= '</ul>';
304 }
305 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-normal-submit' ) ) . '</p>';
306 $form .= '</fieldset></form>';
307 $output->addHtml( $form );
308 }
309 }
310
311 /**
312 * Get the correct "heading" for a namespace
313 *
314 * @param int $namespace
315 * @return string
316 */
317 private function getNamespaceHeading( $namespace ) {
318 return $namespace == NS_MAIN
319 ? wfMsgHtml( 'blanknamespace' )
320 : htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( $namespace ) );
321 }
322
323 /**
324 * Build a single list item containing a check box selecting a title
325 * and a link to that title, with various additional bits
326 *
327 * @param Title $title
328 * @param bool $redirect
329 * @param Skin $skin
330 * @return string
331 */
332 private function buildRemoveLine( $title, $redirect, $skin ) {
333 $link = $skin->makeLinkObj( $title );
334 if( $redirect )
335 $link = '<span class="watchlistredir">' . $link . '</span>';
336 $tools[] = $skin->makeLinkObj( $title->getTalkPage(),
337 htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( NS_TALK ) ) );
338 if( $title->exists() )
339 $tools[] = $skin->makeKnownLinkObj( $title, wfMsgHtml( 'history_short' ), 'action=history' );
340 return '<li>'
341 . Xml::check( 'titles[]', false, array( 'value' => $title->getPrefixedText() ) )
342 . $link . ' (' . implode( ' | ', $tools ) . ')' . '</li>';
343 }
344
345 /**
346 * Show a form for editing the watchlist in "raw" mode
347 *
348 * @param OutputPage $output
349 * @param User $user
350 */
351 public function showRawForm( $output, $user ) {
352 $this->showItemCount( $output, $user );
353 $self = SpecialPage::getTitleFor( 'Watchlist' );
354 $form = Xml::openElement( 'form', array( 'method' => 'post',
355 'action' => $self->getLocalUrl( 'action=raw' ) ) );
356 $form .= Xml::hidden( 'token', $user->editToken( 'watchlistedit' ) );
357 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-raw-legend' ) . '</legend>';
358 $form .= wfMsgExt( 'watchlistedit-raw-explain', 'parse' );
359 $form .= Xml::label( wfMsg( 'watchlistedit-raw-titles' ), 'titles' );
360 $form .= Xml::openElement( 'textarea', array( 'id' => 'titles', 'name' => 'titles',
361 'rows' => 6, 'cols' => 80 ) );
362 foreach( $this->getWatchlist( $user ) as $namespace => $pages ) {
363 foreach( $pages as $dbkey => $redirect ) {
364 $title = Title::makeTitleSafe( $namespace, $dbkey );
365 if( $title instanceof Title )
366 $form .= htmlspecialchars( $title->getPrefixedText() ) . "\n";
367 }
368 }
369 $form .= '</textarea>';
370 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-raw-submit' ) ) . '</p>';
371 $form .= '</fieldset></form>';
372 $output->addHtml( $form );
373 }
374
375 /**
376 * Determine whether we are editing the watchlist, and if so, what
377 * kind of editing operation
378 *
379 * @param WebRequest $request
380 * @param mixed $par
381 * @return int
382 */
383 public static function getMode( $request, $par ) {
384 $mode = strtolower( $request->getVal( 'action', $par ) );
385 switch( $mode ) {
386 case 'clear':
387 return self::EDIT_CLEAR;
388 case 'raw':
389 return self::EDIT_RAW;
390 case 'edit':
391 return self::EDIT_NORMAL;
392 default:
393 return false;
394 }
395 }
396
397 }