4 * Provides the UI through which users can perform editing
5 * operations on their watchlist
7 * @addtogroup Watchlist
8 * @author Rob Church <robchur@gmail.com>
10 class WatchlistEditor
{
17 const EDIT_NORMAL
= 3;
20 * Main execution point
23 * @param OutputPage $output
24 * @param WebRequest $request
27 public function execute( $user, $output, $request, $mode ) {
29 $output->readOnlyPage();
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' ) );
40 $this->showClearForm( $output, $user );
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' ) );
52 $this->showRawForm( $output, $user );
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() );
64 $this->showNormalForm( $output, $user );
69 * Check the edit token from a form submission
71 * @param WebRequest $request
75 private function checkToken( $request, $user ) {
76 return $user->matchEditToken( $request->getVal( 'token' ), 'watchlistedit' );
80 * Extract a list of titles from a text list; if we're given
81 * an array, convert each item into a Title
86 private function extractTitles( $list ) {
88 if( !is_array( $list ) ) {
89 $list = explode( "\n", trim( $list ) );
90 if( !is_array( $list ) )
93 foreach( $list as $text ) {
94 $text = trim( $text );
95 if( strlen( $text ) > 0 ) {
96 $title = Title
::newFromText( $text );
97 if( $title instanceof Title
)
105 * Print out a list of linked titles
107 * @param array $titles
108 * @param OutputPage $output
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() );
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" );
129 * Count the number of titles on a user's watchlist, excluding talk pages
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
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
149 private function getWatchlist( $user ) {
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 );
167 $cache->addBadLinkObj( $title );
170 if( !$title->isTalkPage() )
171 $titles[$row->wl_namespace
][$row->wl_title
] = $row->page_is_redirect
;
179 * Show a message indicating the number of items on the user's watchlist,
180 * and return this count for additional checking
182 * @param OutputPage $output
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 ) ) );
191 $output->addHtml( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
197 * Remove all titles from a user's watchlist
201 private function clearWatchlist( $user ) {
202 $dbw = wfGetDB( DB_MASTER
);
203 $dbw->delete( 'watchlist', array( 'wl_user' => $user->getId() ), __METHOD__
);
207 * Add a list of titles to a user's watchlist
209 * @param array $titles
212 private function watchTitles( $titles, $user ) {
213 $dbw = wfGetDB( DB_MASTER
);
215 foreach( $titles as $title ) {
217 'wl_user' => $user->getId(),
218 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
219 'wl_title' => $title->getDBkey(),
220 'wl_notificationtimestamp' => null,
223 'wl_user' => $user->getId(),
224 'wl_namespace' => ( $title->getNamespace() |
1 ),
225 'wl_title' => $title->getDBkey(),
226 'wl_notificationtimestamp' => null,
229 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
233 * Remove a list of titles from a user's watchlist
235 * @param array $titles
238 private function unwatchTitles( $titles, $user ) {
239 $dbw = wfGetDB( DB_MASTER
);
240 foreach( $titles as $title ) {
244 'wl_user' => $user->getId(),
245 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
246 'wl_title' => $title->getDBkey(),
253 'wl_user' => $user->getId(),
254 'wl_namespace' => ( $title->getNamespace() |
1 ),
255 'wl_title' => $title->getDBkey(),
263 * Show a confirmation form for users wishing to clear their watchlist
265 * @param OutputPage $output
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 );
283 * Show the standard watchlist editing form
285 * @param OutputPage $output
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>';
299 foreach( $pages as $dbkey => $redirect ) {
300 $title = Title
::makeTitleSafe( $namespace, $dbkey );
301 $form .= $this->buildRemoveLine( $title, $redirect, $user->getSkin() );
305 $form .= '<p>' . Xml
::submitButton( wfMsg( 'watchlistedit-normal-submit' ) ) . '</p>';
306 $form .= '</fieldset></form>';
307 $output->addHtml( $form );
312 * Get the correct "heading" for a namespace
314 * @param int $namespace
317 private function getNamespaceHeading( $namespace ) {
318 return $namespace == NS_MAIN
319 ?
wfMsgHtml( 'blanknamespace' )
320 : htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( $namespace ) );
324 * Build a single list item containing a check box selecting a title
325 * and a link to that title, with various additional bits
327 * @param Title $title
328 * @param bool $redirect
332 private function buildRemoveLine( $title, $redirect, $skin ) {
333 $link = $skin->makeLinkObj( $title );
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' );
341 . Xml
::check( 'titles[]', false, array( 'value' => $title->getPrefixedText() ) )
342 . $link . ' (' . implode( ' | ', $tools ) . ')' . '</li>';
346 * Show a form for editing the watchlist in "raw" mode
348 * @param OutputPage $output
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";
369 $form .= '</textarea>';
370 $form .= '<p>' . Xml
::submitButton( wfMsg( 'watchlistedit-raw-submit' ) ) . '</p>';
371 $form .= '</fieldset></form>';
372 $output->addHtml( $form );
376 * Determine whether we are editing the watchlist, and if so, what
377 * kind of editing operation
379 * @param WebRequest $request
383 public static function getMode( $request, $par ) {
384 $mode = strtolower( $request->getVal( 'action', $par ) );
387 return self
::EDIT_CLEAR
;
389 return self
::EDIT_RAW
;
391 return self
::EDIT_NORMAL
;