Add an order by to the list of watched pages.
[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 * @ingroup 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 $output OutputPage
24 * @param $request WebRequest
25 * @param $mode int
26 */
27 public function execute( $user, $output, $request, $mode ) {
28 global $wgUser;
29 if( wfReadOnly() ) {
30 $output->readOnlyPage();
31 return;
32 }
33 switch( $mode ) {
34 case self::EDIT_CLEAR:
35 // The "Clear" link scared people too much.
36 // Pass on to the raw editor, from which it's very easy to clear.
37 case self::EDIT_RAW:
38 $output->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
39 if( $request->wasPosted() && $this->checkToken( $request, $wgUser ) ) {
40 $wanted = $this->extractTitles( $request->getText( 'titles' ) );
41 $current = $this->getWatchlist( $user );
42 if( count( $wanted ) > 0 ) {
43 $toWatch = array_diff( $wanted, $current );
44 $toUnwatch = array_diff( $current, $wanted );
45 $this->watchTitles( $toWatch, $user );
46 $this->unwatchTitles( $toUnwatch, $user );
47 $user->invalidateCache();
48 if( count( $toWatch ) > 0 || count( $toUnwatch ) > 0 )
49 $output->addHtml( wfMsgExt( 'watchlistedit-raw-done', 'parse' ) );
50 if( ( $count = count( $toWatch ) ) > 0 ) {
51 $output->addHtml( wfMsgExt( 'watchlistedit-raw-added', 'parse', $count ) );
52 $this->showTitles( $toWatch, $output, $wgUser->getSkin() );
53 }
54 if( ( $count = count( $toUnwatch ) ) > 0 ) {
55 $output->addHtml( wfMsgExt( 'watchlistedit-raw-removed', 'parse', $count ) );
56 $this->showTitles( $toUnwatch, $output, $wgUser->getSkin() );
57 }
58 } else {
59 $this->clearWatchlist( $user );
60 $user->invalidateCache();
61 $output->addHtml( wfMsgExt( 'watchlistedit-raw-removed', 'parse', count( $current ) ) );
62 $this->showTitles( $current, $output, $wgUser->getSkin() );
63 }
64 }
65 $this->showRawForm( $output, $user );
66 break;
67 case self::EDIT_NORMAL:
68 $output->setPageTitle( wfMsg( 'watchlistedit-normal-title' ) );
69 if( $request->wasPosted() && $this->checkToken( $request, $wgUser ) ) {
70 $titles = $this->extractTitles( $request->getArray( 'titles' ) );
71 $this->unwatchTitles( $titles, $user );
72 $user->invalidateCache();
73 $output->addHtml( wfMsgExt( 'watchlistedit-normal-done', 'parse',
74 $GLOBALS['wgLang']->formatNum( count( $titles ) ) ) );
75 $this->showTitles( $titles, $output, $wgUser->getSkin() );
76 }
77 $this->showNormalForm( $output, $user );
78 }
79 }
80
81 /**
82 * Check the edit token from a form submission
83 *
84 * @param $request WebRequest
85 * @param $user User
86 * @return bool
87 */
88 private function checkToken( $request, $user ) {
89 return $user->matchEditToken( $request->getVal( 'token' ), 'watchlistedit' );
90 }
91
92 /**
93 * Extract a list of titles from a blob of text, returning
94 * (prefixed) strings; unwatchable titles are ignored
95 *
96 * @param $list mixed
97 * @return array
98 */
99 private function extractTitles( $list ) {
100 $titles = array();
101 if( !is_array( $list ) ) {
102 $list = explode( "\n", trim( $list ) );
103 if( !is_array( $list ) )
104 return array();
105 }
106 foreach( $list as $text ) {
107 $text = trim( $text );
108 if( strlen( $text ) > 0 ) {
109 $title = Title::newFromText( $text );
110 if( $title instanceof Title && $title->isWatchable() )
111 $titles[] = $title->getPrefixedText();
112 }
113 }
114 return array_unique( $titles );
115 }
116
117 /**
118 * Print out a list of linked titles
119 *
120 * $titles can be an array of strings or Title objects; the former
121 * is preferred, since Titles are very memory-heavy
122 *
123 * @param $titles An array of strings, or Title objects
124 * @param $output OutputPage
125 * @param $skin Skin
126 */
127 private function showTitles( $titles, $output, $skin ) {
128 $talk = wfMsgHtml( 'talkpagelinktext' );
129 // Do a batch existence check
130 $batch = new LinkBatch();
131 foreach( $titles as $title ) {
132 if( !$title instanceof Title )
133 $title = Title::newFromText( $title );
134 if( $title instanceof Title ) {
135 $batch->addObj( $title );
136 $batch->addObj( $title->getTalkPage() );
137 }
138 }
139 $batch->execute();
140 // Print out the list
141 $output->addHtml( "<ul>\n" );
142 foreach( $titles as $title ) {
143 if( !$title instanceof Title )
144 $title = Title::newFromText( $title );
145 if( $title instanceof Title ) {
146 $output->addHtml( "<li>" . $skin->makeLinkObj( $title )
147 . ' (' . $skin->makeLinkObj( $title->getTalkPage(), $talk ) . ")</li>\n" );
148 }
149 }
150 $output->addHtml( "</ul>\n" );
151 }
152
153 /**
154 * Count the number of titles on a user's watchlist, excluding talk pages
155 *
156 * @param $user User
157 * @return int
158 */
159 private function countWatchlist( $user ) {
160 $dbr = wfGetDB( DB_MASTER );
161 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->getId() ), __METHOD__ );
162 $row = $dbr->fetchObject( $res );
163 return ceil( $row->count / 2 ); // Paranoia
164 }
165
166 /**
167 * Prepare a list of titles on a user's watchlist (excluding talk pages)
168 * and return an array of (prefixed) strings
169 *
170 * @param $user User
171 * @return array
172 */
173 private function getWatchlist( $user ) {
174 $list = array();
175 $dbr = wfGetDB( DB_MASTER );
176 $res = $dbr->select(
177 'watchlist',
178 '*',
179 array(
180 'wl_user' => $user->getId(),
181 ),
182 __METHOD__
183 );
184 if( $res->numRows() > 0 ) {
185 while( $row = $res->fetchObject() ) {
186 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
187 if( $title instanceof Title && !$title->isTalkPage() )
188 $list[] = $title->getPrefixedText();
189 }
190 $res->free();
191 }
192 return $list;
193 }
194
195 /**
196 * Get a list of titles on a user's watchlist, excluding talk pages,
197 * and return as a two-dimensional array with namespace, title and
198 * redirect status
199 *
200 * @param $user User
201 * @return array
202 */
203 private function getWatchlistInfo( $user ) {
204 $titles = array();
205 $dbr = wfGetDB( DB_MASTER );
206 $uid = intval( $user->getId() );
207 list( $watchlist, $page ) = $dbr->tableNamesN( 'watchlist', 'page' );
208 $sql = "SELECT wl_namespace, wl_title, page_id, page_len, page_is_redirect
209 FROM {$watchlist} LEFT JOIN {$page} ON ( wl_namespace = page_namespace
210 AND wl_title = page_title ) WHERE wl_user = {$uid}";
211 if ( ! $dbr->implicitOrderby() ) {
212 $sql .= "ORDER BY wl_title';
213 }
214 $res = $dbr->query( $sql, __METHOD__ );
215 if( $res && $dbr->numRows( $res ) > 0 ) {
216 $cache = LinkCache::singleton();
217 while( $row = $dbr->fetchObject( $res ) ) {
218 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
219 if( $title instanceof Title ) {
220 // Update the link cache while we're at it
221 if( $row->page_id ) {
222 $cache->addGoodLinkObj( $row->page_id, $title, $row->page_len, $row->page_is_redirect );
223 } else {
224 $cache->addBadLinkObj( $title );
225 }
226 // Ignore non-talk
227 if( !$title->isTalkPage() )
228 $titles[$row->wl_namespace][$row->wl_title] = $row->page_is_redirect;
229 }
230 }
231 }
232 return $titles;
233 }
234
235 /**
236 * Show a message indicating the number of items on the user's watchlist,
237 * and return this count for additional checking
238 *
239 * @param $output OutputPage
240 * @param $user User
241 * @return int
242 */
243 private function showItemCount( $output, $user ) {
244 if( ( $count = $this->countWatchlist( $user ) ) > 0 ) {
245 $output->addHtml( wfMsgExt( 'watchlistedit-numitems', 'parse',
246 $GLOBALS['wgLang']->formatNum( $count ) ) );
247 } else {
248 $output->addHtml( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
249 }
250 return $count;
251 }
252
253 /**
254 * Remove all titles from a user's watchlist
255 *
256 * @param $user User
257 */
258 private function clearWatchlist( $user ) {
259 $dbw = wfGetDB( DB_MASTER );
260 $dbw->delete( 'watchlist', array( 'wl_user' => $user->getId() ), __METHOD__ );
261 }
262
263 /**
264 * Add a list of titles to a user's watchlist
265 *
266 * $titles can be an array of strings or Title objects; the former
267 * is preferred, since Titles are very memory-heavy
268 *
269 * @param $titles An array of strings, or Title objects
270 * @param $user User
271 */
272 private function watchTitles( $titles, $user ) {
273 $dbw = wfGetDB( DB_MASTER );
274 $rows = array();
275 foreach( $titles as $title ) {
276 if( !$title instanceof Title )
277 $title = Title::newFromText( $title );
278 if( $title instanceof Title ) {
279 $rows[] = array(
280 'wl_user' => $user->getId(),
281 'wl_namespace' => ( $title->getNamespace() & ~1 ),
282 'wl_title' => $title->getDBkey(),
283 'wl_notificationtimestamp' => null,
284 );
285 $rows[] = array(
286 'wl_user' => $user->getId(),
287 'wl_namespace' => ( $title->getNamespace() | 1 ),
288 'wl_title' => $title->getDBkey(),
289 'wl_notificationtimestamp' => null,
290 );
291 }
292 }
293 $dbw->insert( 'watchlist', $rows, __METHOD__, 'IGNORE' );
294 }
295
296 /**
297 * Remove a list of titles from a user's watchlist
298 *
299 * $titles can be an array of strings or Title objects; the former
300 * is preferred, since Titles are very memory-heavy
301 *
302 * @param $titles An array of strings, or Title objects
303 * @param $user User
304 */
305 private function unwatchTitles( $titles, $user ) {
306 $dbw = wfGetDB( DB_MASTER );
307 foreach( $titles as $title ) {
308 if( !$title instanceof Title )
309 $title = Title::newFromText( $title );
310 if( $title instanceof Title ) {
311 $dbw->delete(
312 'watchlist',
313 array(
314 'wl_user' => $user->getId(),
315 'wl_namespace' => ( $title->getNamespace() & ~1 ),
316 'wl_title' => $title->getDBkey(),
317 ),
318 __METHOD__
319 );
320 $dbw->delete(
321 'watchlist',
322 array(
323 'wl_user' => $user->getId(),
324 'wl_namespace' => ( $title->getNamespace() | 1 ),
325 'wl_title' => $title->getDBkey(),
326 ),
327 __METHOD__
328 );
329 }
330 }
331 }
332
333 /**
334 * Show the standard watchlist editing form
335 *
336 * @param $output OutputPage
337 * @param $user User
338 */
339 private function showNormalForm( $output, $user ) {
340 global $wgUser;
341 if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) {
342 $self = SpecialPage::getTitleFor( 'Watchlist' );
343 $form = Xml::openElement( 'form', array( 'method' => 'post',
344 'action' => $self->getLocalUrl( 'action=edit' ) ) );
345 $form .= Xml::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
346 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-normal-legend' ) . '</legend>';
347 $form .= wfMsgExt( 'watchlistedit-normal-explain', 'parse' );
348 foreach( $this->getWatchlistInfo( $user ) as $namespace => $pages ) {
349 $form .= '<h2>' . $this->getNamespaceHeading( $namespace ) . '</h2>';
350 $form .= '<ul>';
351 foreach( $pages as $dbkey => $redirect ) {
352 $title = Title::makeTitleSafe( $namespace, $dbkey );
353 $form .= $this->buildRemoveLine( $title, $redirect, $wgUser->getSkin() );
354 }
355 $form .= '</ul>';
356 }
357 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-normal-submit' ) ) . '</p>';
358 $form .= '</fieldset></form>';
359 $output->addHtml( $form );
360 }
361 }
362
363 /**
364 * Get the correct "heading" for a namespace
365 *
366 * @param $namespace int
367 * @return string
368 */
369 private function getNamespaceHeading( $namespace ) {
370 return $namespace == NS_MAIN
371 ? wfMsgHtml( 'blanknamespace' )
372 : htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( $namespace ) );
373 }
374
375 /**
376 * Build a single list item containing a check box selecting a title
377 * and a link to that title, with various additional bits
378 *
379 * @param $title Title
380 * @param $redirect bool
381 * @param $skin Skin
382 * @return string
383 */
384 private function buildRemoveLine( $title, $redirect, $skin ) {
385 $link = $skin->makeLinkObj( $title );
386 if( $redirect )
387 $link = '<span class="watchlistredir">' . $link . '</span>';
388 $tools[] = $skin->makeLinkObj( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
389 if( $title->exists() ) {
390 $tools[] = $skin->makeKnownLinkObj( $title, wfMsgHtml( 'history_short' ), 'action=history' );
391 }
392 if( $title->getNamespace() == NS_USER && !$title->isSubpage() ) {
393 $tools[] = $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Contributions', $title->getText() ), wfMsgHtml( 'contributions' ) );
394 }
395 return '<li>'
396 . Xml::check( 'titles[]', false, array( 'value' => $title->getPrefixedText() ) )
397 . $link . ' (' . implode( ' | ', $tools ) . ')' . '</li>';
398 }
399
400 /**
401 * Show a form for editing the watchlist in "raw" mode
402 *
403 * @param $output OutputPage
404 * @param $user User
405 */
406 public function showRawForm( $output, $user ) {
407 global $wgUser;
408 $this->showItemCount( $output, $user );
409 $self = SpecialPage::getTitleFor( 'Watchlist' );
410 $form = Xml::openElement( 'form', array( 'method' => 'post',
411 'action' => $self->getLocalUrl( 'action=raw' ) ) );
412 $form .= Xml::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
413 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-raw-legend' ) . '</legend>';
414 $form .= wfMsgExt( 'watchlistedit-raw-explain', 'parse' );
415 $form .= Xml::label( wfMsg( 'watchlistedit-raw-titles' ), 'titles' );
416 $form .= "<br />\n";
417 $form .= Xml::openElement( 'textarea', array( 'id' => 'titles', 'name' => 'titles',
418 'rows' => $wgUser->getIntOption( 'rows' ), 'cols' => $wgUser->getIntOption( 'cols' ) ) );
419 $titles = $this->getWatchlist( $user );
420 foreach( $titles as $title )
421 $form .= htmlspecialchars( $title ) . "\n";
422 $form .= '</textarea>';
423 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-raw-submit' ) ) . '</p>';
424 $form .= '</fieldset></form>';
425 $output->addHtml( $form );
426 }
427
428 /**
429 * Determine whether we are editing the watchlist, and if so, what
430 * kind of editing operation
431 *
432 * @param $request WebRequest
433 * @param $par mixed
434 * @return int
435 */
436 public static function getMode( $request, $par ) {
437 $mode = strtolower( $request->getVal( 'action', $par ) );
438 switch( $mode ) {
439 case 'clear':
440 return self::EDIT_CLEAR;
441 case 'raw':
442 return self::EDIT_RAW;
443 case 'edit':
444 return self::EDIT_NORMAL;
445 default:
446 return false;
447 }
448 }
449
450 /**
451 * Build a set of links for convenient navigation
452 * between watchlist viewing and editing modes
453 *
454 * @param $skin Skin to use
455 * @return string
456 */
457 public static function buildTools( $skin ) {
458 $tools = array();
459 $modes = array( 'view' => false, 'edit' => 'edit', 'raw' => 'raw' );
460 foreach( $modes as $mode => $subpage ) {
461 $tools[] = $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Watchlist', $subpage ), wfMsgHtml( "watchlisttools-{$mode}" ) );
462 }
463 return implode( ' | ', $tools );
464 }
465 }