00001 <?php
00002
00010 class WatchlistEditor {
00011
00015 const EDIT_CLEAR = 1;
00016 const EDIT_RAW = 2;
00017 const EDIT_NORMAL = 3;
00018
00027 public function execute( $user, $output, $request, $mode ) {
00028 global $wgUser;
00029 if( wfReadOnly() ) {
00030 $output->readOnlyPage();
00031 return;
00032 }
00033 switch( $mode ) {
00034 case self::EDIT_CLEAR:
00035
00036
00037 case self::EDIT_RAW:
00038 $output->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
00039 if( $request->wasPosted() && $this->checkToken( $request, $wgUser ) ) {
00040 $wanted = $this->extractTitles( $request->getText( 'titles' ) );
00041 $current = $this->getWatchlist( $user );
00042 if( count( $wanted ) > 0 ) {
00043 $toWatch = array_diff( $wanted, $current );
00044 $toUnwatch = array_diff( $current, $wanted );
00045 $this->watchTitles( $toWatch, $user );
00046 $this->unwatchTitles( $toUnwatch, $user );
00047 $user->invalidateCache();
00048 if( count( $toWatch ) > 0 || count( $toUnwatch ) > 0 )
00049 $output->addHTML( wfMsgExt( 'watchlistedit-raw-done', 'parse' ) );
00050 if( ( $count = count( $toWatch ) ) > 0 ) {
00051 $output->addHTML( wfMsgExt( 'watchlistedit-raw-added', 'parse', $count ) );
00052 $this->showTitles( $toWatch, $output, $wgUser->getSkin() );
00053 }
00054 if( ( $count = count( $toUnwatch ) ) > 0 ) {
00055 $output->addHTML( wfMsgExt( 'watchlistedit-raw-removed', 'parse', $count ) );
00056 $this->showTitles( $toUnwatch, $output, $wgUser->getSkin() );
00057 }
00058 } else {
00059 $this->clearWatchlist( $user );
00060 $user->invalidateCache();
00061 $output->addHTML( wfMsgExt( 'watchlistedit-raw-removed', 'parse', count( $current ) ) );
00062 $this->showTitles( $current, $output, $wgUser->getSkin() );
00063 }
00064 }
00065 $this->showRawForm( $output, $user );
00066 break;
00067 case self::EDIT_NORMAL:
00068 $output->setPageTitle( wfMsg( 'watchlistedit-normal-title' ) );
00069 if( $request->wasPosted() && $this->checkToken( $request, $wgUser ) ) {
00070 $titles = $this->extractTitles( $request->getArray( 'titles' ) );
00071 $this->unwatchTitles( $titles, $user );
00072 $user->invalidateCache();
00073 $output->addHTML( wfMsgExt( 'watchlistedit-normal-done', 'parse',
00074 $GLOBALS['wgLang']->formatNum( count( $titles ) ) ) );
00075 $this->showTitles( $titles, $output, $wgUser->getSkin() );
00076 }
00077 $this->showNormalForm( $output, $user );
00078 }
00079 }
00080
00088 private function checkToken( $request, $user ) {
00089 return $user->matchEditToken( $request->getVal( 'token' ), 'watchlistedit' );
00090 }
00091
00099 private function extractTitles( $list ) {
00100 $titles = array();
00101 if( !is_array( $list ) ) {
00102 $list = explode( "\n", trim( $list ) );
00103 if( !is_array( $list ) )
00104 return array();
00105 }
00106 foreach( $list as $text ) {
00107 $text = trim( $text );
00108 if( strlen( $text ) > 0 ) {
00109 $title = Title::newFromText( $text );
00110 if( $title instanceof Title && $title->isWatchable() )
00111 $titles[] = $title->getPrefixedText();
00112 }
00113 }
00114 return array_unique( $titles );
00115 }
00116
00127 private function showTitles( $titles, $output, $skin ) {
00128 $talk = wfMsgHtml( 'talkpagelinktext' );
00129
00130 $batch = new LinkBatch();
00131 foreach( $titles as $title ) {
00132 if( !$title instanceof Title )
00133 $title = Title::newFromText( $title );
00134 if( $title instanceof Title ) {
00135 $batch->addObj( $title );
00136 $batch->addObj( $title->getTalkPage() );
00137 }
00138 }
00139 $batch->execute();
00140
00141 $output->addHTML( "<ul>\n" );
00142 foreach( $titles as $title ) {
00143 if( !$title instanceof Title )
00144 $title = Title::newFromText( $title );
00145 if( $title instanceof Title ) {
00146 $output->addHTML( "<li>" . $skin->makeLinkObj( $title )
00147 . ' (' . $skin->makeLinkObj( $title->getTalkPage(), $talk ) . ")</li>\n" );
00148 }
00149 }
00150 $output->addHTML( "</ul>\n" );
00151 }
00152
00159 private function countWatchlist( $user ) {
00160 $dbr = wfGetDB( DB_MASTER );
00161 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->getId() ), __METHOD__ );
00162 $row = $dbr->fetchObject( $res );
00163 return ceil( $row->count / 2 );
00164 }
00165
00173 private function getWatchlist( $user ) {
00174 $list = array();
00175 $dbr = wfGetDB( DB_MASTER );
00176 $res = $dbr->select(
00177 'watchlist',
00178 '*',
00179 array(
00180 'wl_user' => $user->getId(),
00181 ),
00182 __METHOD__
00183 );
00184 if( $res->numRows() > 0 ) {
00185 while( $row = $res->fetchObject() ) {
00186 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
00187 if( $title instanceof Title && !$title->isTalkPage() )
00188 $list[] = $title->getPrefixedText();
00189 }
00190 $res->free();
00191 }
00192 return $list;
00193 }
00194
00203 private function getWatchlistInfo( $user ) {
00204 $titles = array();
00205 $dbr = wfGetDB( DB_MASTER );
00206 $uid = intval( $user->getId() );
00207 list( $watchlist, $page ) = $dbr->tableNamesN( 'watchlist', 'page' );
00208 $sql = "SELECT wl_namespace, wl_title, page_id, page_len, page_is_redirect
00209 FROM {$watchlist} LEFT JOIN {$page} ON ( wl_namespace = page_namespace
00210 AND wl_title = page_title ) WHERE wl_user = {$uid}";
00211 $res = $dbr->query( $sql, __METHOD__ );
00212 if( $res && $dbr->numRows( $res ) > 0 ) {
00213 $cache = LinkCache::singleton();
00214 while( $row = $dbr->fetchObject( $res ) ) {
00215 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
00216 if( $title instanceof Title ) {
00217
00218 if( $row->page_id ) {
00219 $cache->addGoodLinkObj( $row->page_id, $title, $row->page_len, $row->page_is_redirect );
00220 } else {
00221 $cache->addBadLinkObj( $title );
00222 }
00223
00224 if( !$title->isTalkPage() )
00225 $titles[$row->wl_namespace][$row->wl_title] = $row->page_is_redirect;
00226 }
00227 }
00228 }
00229 return $titles;
00230 }
00231
00240 private function showItemCount( $output, $user ) {
00241 if( ( $count = $this->countWatchlist( $user ) ) > 0 ) {
00242 $output->addHTML( wfMsgExt( 'watchlistedit-numitems', 'parse',
00243 $GLOBALS['wgLang']->formatNum( $count ) ) );
00244 } else {
00245 $output->addHTML( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
00246 }
00247 return $count;
00248 }
00249
00255 private function clearWatchlist( $user ) {
00256 $dbw = wfGetDB( DB_MASTER );
00257 $dbw->delete( 'watchlist', array( 'wl_user' => $user->getId() ), __METHOD__ );
00258 }
00259
00269 private function watchTitles( $titles, $user ) {
00270 $dbw = wfGetDB( DB_MASTER );
00271 $rows = array();
00272 foreach( $titles as $title ) {
00273 if( !$title instanceof Title )
00274 $title = Title::newFromText( $title );
00275 if( $title instanceof Title ) {
00276 $rows[] = array(
00277 'wl_user' => $user->getId(),
00278 'wl_namespace' => ( $title->getNamespace() & ~1 ),
00279 'wl_title' => $title->getDBkey(),
00280 'wl_notificationtimestamp' => null,
00281 );
00282 $rows[] = array(
00283 'wl_user' => $user->getId(),
00284 'wl_namespace' => ( $title->getNamespace() | 1 ),
00285 'wl_title' => $title->getDBkey(),
00286 'wl_notificationtimestamp' => null,
00287 );
00288 }
00289 }
00290 $dbw->insert( 'watchlist', $rows, __METHOD__, 'IGNORE' );
00291 }
00292
00302 private function unwatchTitles( $titles, $user ) {
00303 $dbw = wfGetDB( DB_MASTER );
00304 foreach( $titles as $title ) {
00305 if( !$title instanceof Title )
00306 $title = Title::newFromText( $title );
00307 if( $title instanceof Title ) {
00308 $dbw->delete(
00309 'watchlist',
00310 array(
00311 'wl_user' => $user->getId(),
00312 'wl_namespace' => ( $title->getNamespace() & ~1 ),
00313 'wl_title' => $title->getDBkey(),
00314 ),
00315 __METHOD__
00316 );
00317 $dbw->delete(
00318 'watchlist',
00319 array(
00320 'wl_user' => $user->getId(),
00321 'wl_namespace' => ( $title->getNamespace() | 1 ),
00322 'wl_title' => $title->getDBkey(),
00323 ),
00324 __METHOD__
00325 );
00326 $article = new Article($title);
00327 wfRunHooks('UnwatchArticleComplete',array(&$user,&$article));
00328 }
00329 }
00330 }
00331
00338 private function showNormalForm( $output, $user ) {
00339 global $wgUser;
00340 if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) {
00341 $self = SpecialPage::getTitleFor( 'Watchlist' );
00342 $form = Xml::openElement( 'form', array( 'method' => 'post',
00343 'action' => $self->getLocalUrl( 'action=edit' ) ) );
00344 $form .= Xml::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
00345 $form .= "<fieldset>\n<legend>" . wfMsgHtml( 'watchlistedit-normal-legend' ) . "</legend>";
00346 $form .= wfMsgExt( 'watchlistedit-normal-explain', 'parse' );
00347 $form .= $this->buildRemoveList( $user, $wgUser->getSkin() );
00348 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-normal-submit' ) ) . '</p>';
00349 $form .= '</fieldset></form>';
00350 $output->addHTML( $form );
00351 }
00352 }
00353
00362 private function buildRemoveList( $user, $skin ) {
00363 $list = "";
00364 $toc = $skin->tocIndent();
00365 $tocLength = 0;
00366 foreach( $this->getWatchlistInfo( $user ) as $namespace => $pages ) {
00367 $tocLength++;
00368 $heading = htmlspecialchars( $this->getNamespaceHeading( $namespace ) );
00369 $anchor = "editwatchlist-ns" . $namespace;
00370
00371 $list .= $skin->makeHeadLine( 2, ">", $anchor, $heading, "" );
00372 $toc .= $skin->tocLine( $anchor, $heading, $tocLength, 1 ) . $skin->tocLineEnd();
00373
00374 $list .= "<ul>\n";
00375 foreach( $pages as $dbkey => $redirect ) {
00376 $title = Title::makeTitleSafe( $namespace, $dbkey );
00377 $list .= $this->buildRemoveLine( $title, $redirect, $skin );
00378 }
00379 $list .= "</ul>\n";
00380 }
00381
00382 if( $tocLength > 1 ) {
00383 $list = $skin->tocList( $toc ) . $list;
00384 }
00385 return $list;
00386 }
00387
00394 private function getNamespaceHeading( $namespace ) {
00395 return $namespace == NS_MAIN
00396 ? wfMsgHtml( 'blanknamespace' )
00397 : htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( $namespace ) );
00398 }
00399
00409 private function buildRemoveLine( $title, $redirect, $skin ) {
00410 global $wgLang;
00411
00412 $link = $skin->makeLinkObj( $title );
00413 if( $redirect )
00414 $link = '<span class="watchlistredir">' . $link . '</span>';
00415 $tools[] = $skin->makeLinkObj( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
00416 if( $title->exists() ) {
00417 $tools[] = $skin->makeKnownLinkObj( $title, wfMsgHtml( 'history_short' ), 'action=history' );
00418 }
00419 if( $title->getNamespace() == NS_USER && !$title->isSubpage() ) {
00420 $tools[] = $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Contributions', $title->getText() ), wfMsgHtml( 'contributions' ) );
00421 }
00422 return "<li>"
00423 . Xml::check( 'titles[]', false, array( 'value' => $title->getPrefixedText() ) )
00424 . $link . " (" . $wgLang->pipeList( $tools ) . ")" . "</li>\n";
00425 }
00426
00433 public function showRawForm( $output, $user ) {
00434 global $wgUser;
00435 $this->showItemCount( $output, $user );
00436 $self = SpecialPage::getTitleFor( 'Watchlist' );
00437 $form = Xml::openElement( 'form', array( 'method' => 'post',
00438 'action' => $self->getLocalUrl( 'action=raw' ) ) );
00439 $form .= Xml::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
00440 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-raw-legend' ) . '</legend>';
00441 $form .= wfMsgExt( 'watchlistedit-raw-explain', 'parse' );
00442 $form .= Xml::label( wfMsg( 'watchlistedit-raw-titles' ), 'titles' );
00443 $form .= "<br />\n";
00444 $form .= Xml::openElement( 'textarea', array( 'id' => 'titles', 'name' => 'titles',
00445 'rows' => $wgUser->getIntOption( 'rows' ), 'cols' => $wgUser->getIntOption( 'cols' ) ) );
00446 $titles = $this->getWatchlist( $user );
00447 foreach( $titles as $title )
00448 $form .= htmlspecialchars( $title ) . "\n";
00449 $form .= '</textarea>';
00450 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-raw-submit' ) ) . '</p>';
00451 $form .= '</fieldset></form>';
00452 $output->addHTML( $form );
00453 }
00454
00463 public static function getMode( $request, $par ) {
00464 $mode = strtolower( $request->getVal( 'action', $par ) );
00465 switch( $mode ) {
00466 case 'clear':
00467 return self::EDIT_CLEAR;
00468 case 'raw':
00469 return self::EDIT_RAW;
00470 case 'edit':
00471 return self::EDIT_NORMAL;
00472 default:
00473 return false;
00474 }
00475 }
00476
00484 public static function buildTools( $skin ) {
00485 global $wgLang;
00486
00487 $tools = array();
00488 $modes = array( 'view' => false, 'edit' => 'edit', 'raw' => 'raw' );
00489 foreach( $modes as $mode => $subpage ) {
00490 $tools[] = $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Watchlist', $subpage ), wfMsgHtml( "watchlisttools-{$mode}" ) );
00491 }
00492 return $wgLang->pipeList( $tools );
00493 }
00494 }