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', 'parse',
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 ) {
87 if( !is_array( $list ) ) {
88 $list = explode( "\n", $list );
89 if( !is_array( $list ) )
92 for( $i = 0; $i < count( $list ); $i++
) {
93 $list[$i] = Title
::newFromText( $list[$i] );
94 if( !$list[$i] instanceof Title
)
101 * Print out a list of linked titles
103 * @param array $titles
104 * @param OutputPage $output
107 private function showTitles( $titles, $output, $skin ) {
108 $talk = htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( NS_TALK
) );
109 // Do a batch existence check
110 $batch = new LinkBatch();
111 foreach( $titles as $title ) {
112 $batch->addObj( $title );
113 $batch->addObj( $title->getTalkPage() );
116 // Print out the list
117 $output->addHtml( "<ul>\n" );
118 foreach( $titles as $title )
119 $output->addHtml( "<li>" . $skin->makeLinkObj( $title )
120 . ' (' . $skin->makeLinkObj( $title->getTalkPage(), $talk ) . ")</li>\n" );
121 $output->addHtml( "</ul>\n" );
125 * Count the number of titles on a user's watchlist, excluding talk pages
130 private function countWatchlist( $user ) {
131 $dbr = wfGetDB( DB_SLAVE
);
132 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->getId() ), __METHOD__
);
133 $row = $dbr->fetchObject( $res );
134 return ceil( $row->count
/ 2 ); // Paranoia
138 * Get a list of titles on a user's watchlist, excluding talk pages,
139 * and return as a two-dimensional array with namespace, title and
145 private function getWatchlist( $user ) {
147 $dbr = wfGetDB( DB_SLAVE
);
148 $uid = intval( $user->getId() );
149 list( $watchlist, $page ) = $dbr->tableNamesN( 'watchlist', 'page' );
150 $sql = "SELECT wl_namespace, wl_title, page_id, page_is_redirect
151 FROM {$watchlist} LEFT JOIN {$page} ON ( wl_namespace = page_namespace
152 AND wl_title = page_title ) WHERE wl_user = {$uid}";
153 $res = $dbr->query( $sql, __METHOD__
);
154 if( $res && $dbr->numRows( $res ) > 0 ) {
155 $cache = LinkCache
::singleton();
156 while( $row = $dbr->fetchObject( $res ) ) {
157 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
158 if( $title instanceof Title
) {
159 // Update the link cache while we're at it
160 if( $row->page_id
) {
161 $cache->addGoodLinkObj( $row->page_id
, $title );
163 $cache->addBadLinkObj( $title );
166 if( !$title->isTalkPage() )
167 $titles[$row->wl_namespace
][$row->wl_title
] = $row->page_is_redirect
;
175 * Show a message indicating the number of items on the user's watchlist,
176 * and return this count for additional checking
178 * @param OutputPage $output
182 private function showItemCount( $output, $user ) {
183 if( ( $count = $this->countWatchlist( $user ) ) > 0 ) {
184 $output->addHtml( wfMsgExt( 'watchlistedit-numitems', 'parse',
185 $GLOBALS['wgLang']->formatNum( $count ) ) );
187 $output->addHtml( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
193 * Remove all titles from a user's watchlist
197 private function clearWatchlist( $user ) {
198 $dbw = wfGetDB( DB_MASTER
);
199 $dbw->delete( 'watchlist', array( 'wl_user' => $user->getId() ), __METHOD__
);
203 * Add a list of titles to a user's watchlist
205 * @param array $titles
208 private function watchTitles( $titles, $user ) {
209 $dbw = wfGetDB( DB_MASTER
);
211 foreach( $titles as $title ) {
213 'wl_user' => $user->getId(),
214 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
215 'wl_title' => $title->getDBkey(),
216 'wl_notificationtimestamp' => null,
219 'wl_user' => $user->getId(),
220 'wl_namespace' => ( $title->getNamespace() |
1 ),
221 'wl_title' => $title->getDBkey(),
222 'wl_notificationtimestamp' => null,
225 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
229 * Remove a list of titles from a user's watchlist
231 * @param array $titles
234 private function unwatchTitles( $titles, $user ) {
235 $dbw = wfGetDB( DB_MASTER
);
236 foreach( $titles as $title ) {
240 'wl_user' => $user->getId(),
241 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
242 'wl_title' => $title->getDBkey(),
249 'wl_user' => $user->getId(),
250 'wl_namespace' => ( $title->getNamespace() |
1 ),
251 'wl_title' => $title->getDBkey(),
259 * Show a confirmation form for users wishing to clear their watchlist
261 * @param OutputPage $output
264 private function showClearForm( $output, $user ) {
265 if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) {
266 $self = SpecialPage
::getTitleFor( 'Watchlist' );
267 $form = Xml
::openElement( 'form', array( 'method' => 'post',
268 'action' => $self->getLocalUrl( 'action=clear' ) ) );
269 $form .= Xml
::hidden( 'token', $user->editToken( 'watchlistedit' ) );
270 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-clear-legend' ) . '</legend>';
271 $form .= wfMsgExt( 'watchlistedit-clear-confirm', 'parse' );
272 $form .= '<p>' . Xml
::submitButton( wfMsg( 'watchlistedit-clear-submit' ) ) . '</p>';
273 $form .= '</fieldset></form>';
274 $output->addHtml( $form );
279 * Show the standard watchlist editing form
281 * @param OutputPage $output
284 private function showNormalForm( $output, $user ) {
285 if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) {
286 $self = SpecialPage
::getTitleFor( 'Watchlist' );
287 $form = Xml
::openElement( 'form', array( 'method' => 'post',
288 'action' => $self->getLocalUrl( 'action=edit' ) ) );
289 $form .= Xml
::hidden( 'token', $user->editToken( 'watchlistedit' ) );
290 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-normal-legend' ) . '</legend>';
291 $form .= wfMsgExt( 'watchlistedit-normal-explain', 'parse' );
292 foreach( $this->getWatchlist( $user ) as $namespace => $pages ) {
293 $form .= '<h2>' . $this->getNamespaceHeading( $namespace ) . '</h2>';
295 foreach( $pages as $dbkey => $redirect ) {
296 $title = Title
::makeTitleSafe( $namespace, $dbkey );
297 $form .= $this->buildRemoveLine( $title, $redirect, $user->getSkin() );
301 $form .= '<p>' . Xml
::submitButton( wfMsg( 'watchlistedit-normal-submit' ) ) . '</p>';
302 $form .= '</fieldset></form>';
303 $output->addHtml( $form );
308 * Get the correct "heading" for a namespace
310 * @param int $namespace
313 private function getNamespaceHeading( $namespace ) {
314 return $namespace == NS_MAIN
315 ?
wfMsgHtml( 'blanknamespace' )
316 : htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( $namespace ) );
320 * Build a single list item containing a check box selecting a title
321 * and a link to that title, with various additional bits
323 * @param Title $title
324 * @param bool $redirect
328 private function buildRemoveLine( $title, $redirect, $skin ) {
329 $link = $skin->makeLinkObj( $title );
331 $link = '<span class="watchlistredir">' . $link . '</span>';
332 $tools[] = $skin->makeLinkObj( $title->getTalkPage(),
333 htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( NS_TALK
) ) );
334 if( $title->exists() )
335 $tools[] = $skin->makeKnownLinkObj( $title, wfMsgHtml( 'history_short' ), 'action=history' );
337 . Xml
::check( 'titles[]', false, array( 'value' => $title->getPrefixedText() ) )
338 . $link . ' (' . implode( ' | ', $tools ) . ')' . '</li>';
342 * Show a form for editing the watchlist in "raw" mode
344 * @param OutputPage $output
347 public function showRawForm( $output, $user ) {
348 $this->showItemCount( $output, $user );
349 $self = SpecialPage
::getTitleFor( 'Watchlist' );
350 $form = Xml
::openElement( 'form', array( 'method' => 'post',
351 'action' => $self->getLocalUrl( 'action=raw' ) ) );
352 $form .= Xml
::hidden( 'token', $user->editToken( 'watchlistedit' ) );
353 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-raw-legend' ) . '</legend>';
354 $form .= wfMsgExt( 'watchlistedit-raw-explain', 'parse' );
355 $form .= Xml
::label( wfMsg( 'watchlistedit-raw-titles' ), 'titles' );
356 $form .= Xml
::openElement( 'textarea', array( 'id' => 'titles', 'name' => 'titles',
357 'rows' => 6, 'cols' => 80 ) );
358 foreach( $this->getWatchlist( $user ) as $namespace => $pages ) {
359 foreach( $pages as $dbkey => $redirect ) {
360 $title = Title
::makeTitleSafe( $namespace, $dbkey );
361 if( $title instanceof Title
)
362 $form .= htmlspecialchars( $title->getPrefixedText() ) . "\n";
365 $form .= '</textarea>';
366 $form .= '<p>' . Xml
::submitButton( wfMsg( 'watchlistedit-raw-submit' ) ) . '</p>';
367 $form .= '</fieldset></form>';
368 $output->addHtml( $form );
372 * Determine whether we are editing the watchlist, and if so, what
373 * kind of editing operation
375 * @param WebRequest $request
379 public static function getMode( $request, $par ) {
380 $mode = strtolower( $request->getVal( 'action', $par ) );
383 return self
::EDIT_CLEAR
;
385 return self
::EDIT_RAW
;
387 return self
::EDIT_NORMAL
;