r23895 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23894‎ | r23895 | r23896 >
Date:10:59, 9 July 2007
Author:robchurch
Status:old
Tags:
Comment:
Make WatchlistEditor use arrays of strings, which are far less expensive than Title objects, so Brion can now add 5000 pages to his watchlist without sapping up all his memory. Also, use the current user's preference for raw edit box size, and while we're on the subject, use $wgUser to make the difference explicit.
Modified paths:
  • /trunk/phase3/includes/WatchlistEditor.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/WatchlistEditor.php
@@ -25,6 +25,7 @@
2626 * @param int $mode
2727 */
2828 public function execute( $user, $output, $request, $mode ) {
 29+ global $wgUser;
2930 if( wfReadOnly() ) {
3031 $output->readOnlyPage();
3132 return;
@@ -32,7 +33,7 @@
3334 switch( $mode ) {
3435 case self::EDIT_CLEAR:
3536 $output->setPageTitle( wfMsg( 'watchlistedit-clear-title' ) );
36 - if( $request->wasPosted() && $this->checkToken( $request, $user ) ) {
 37+ if( $request->wasPosted() && $this->checkToken( $request, $wgUser ) ) {
3738 $this->clearWatchlist( $user );
3839 $user->invalidateCache();
3940 $output->addHtml( wfMsgExt( 'watchlistedit-clear-done', 'parse' ) );
@@ -42,7 +43,7 @@
4344 break;
4445 case self::EDIT_RAW:
4546 $output->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
46 - if( $request->wasPosted() && $this->checkToken( $request, $user ) ) {
 47+ if( $request->wasPosted() && $this->checkToken( $request, $wgUser ) ) {
4748 $current = $this->getWatchlist( $user );
4849 $wanted = $this->extractTitles( $request->getText( 'titles' ) );
4950 $toWatch = array_diff( $wanted, $current );
@@ -54,24 +55,24 @@
5556 $output->addHtml( wfMsgExt( 'watchlistedit-raw-done', 'parse' ) );
5657 if( ( $count = count( $toWatch ) ) > 0 ) {
5758 $output->addHtml( wfMsgExt( 'watchlistedit-raw-added', 'parse', $count ) );
58 - $this->showTitles( $toWatch, $output, $user->getSkin() );
 59+ $this->showTitles( $toWatch, $output, $wgUser->getSkin() );
5960 }
6061 if( ( $count = count( $toUnwatch ) ) > 0 ) {
6162 $output->addHtml( wfMsgExt( 'watchlistedit-raw-removed', 'parse', $count ) );
62 - $this->showTitles( $toUnwatch, $output, $user->getSkin() );
 63+ $this->showTitles( $toUnwatch, $output, $wgUser->getSkin() );
6364 }
6465 }
6566 $this->showRawForm( $output, $user );
6667 break;
6768 case self::EDIT_NORMAL:
6869 $output->setPageTitle( wfMsg( 'watchlistedit-normal-title' ) );
69 - if( $request->wasPosted() && $this->checkToken( $request, $user ) ) {
 70+ if( $request->wasPosted() && $this->checkToken( $request, $wgUser ) ) {
7071 $titles = $this->extractTitles( $request->getArray( 'titles' ) );
7172 $this->unwatchTitles( $titles, $user );
7273 $user->invalidateCache();
7374 $output->addHtml( wfMsgExt( 'watchlistedit-normal-done', 'parse',
7475 $GLOBALS['wgLang']->formatNum( count( $titles ) ) ) );
75 - $this->showTitles( $titles, $output, $user->getSkin() );
 76+ $this->showTitles( $titles, $output, $wgUser->getSkin() );
7677 }
7778 $this->showNormalForm( $output, $user );
7879 }
@@ -89,8 +90,8 @@
9091 }
9192
9293 /**
93 - * Extract a list of titles from a text list; if we're given
94 - * an array, convert each item into a Title (ignores unwatchable titles)
 94+ * Extract a list of titles from a blob of text, returning
 95+ * (prefixed) strings; unwatchable titles are ignored
9596 *
9697 * @param mixed $list
9798 * @return array
@@ -107,7 +108,7 @@
108109 if( strlen( $text ) > 0 ) {
109110 $title = Title::newFromText( $text );
110111 if( $title instanceof Title && $title->isWatchable() )
111 - $titles[] = $title;
 112+ $titles[] = $title->getPrefixedText();
112113 }
113114 }
114115 return $titles;
@@ -116,7 +117,10 @@
117118 /**
118119 * Print out a list of linked titles
119120 *
120 - * @param array $titles
 121+ * $titles can be an array of strings or Title objects; the former
 122+ * is preferred, since Titles are very memory-heavy
 123+ *
 124+ * @param array $titles An array of strings, or Title objects
121125 * @param OutputPage $output
122126 * @param Skin $skin
123127 */
@@ -125,15 +129,24 @@
126130 // Do a batch existence check
127131 $batch = new LinkBatch();
128132 foreach( $titles as $title ) {
129 - $batch->addObj( $title );
130 - $batch->addObj( $title->getTalkPage() );
 133+ if( !$title instanceof Title )
 134+ $title = Title::newFromText( $title );
 135+ if( $title instanceof Title ) {
 136+ $batch->addObj( $title );
 137+ $batch->addObj( $title->getTalkPage() );
 138+ }
131139 }
132140 $batch->execute();
133141 // Print out the list
134142 $output->addHtml( "<ul>\n" );
135 - foreach( $titles as $title )
136 - $output->addHtml( "<li>" . $skin->makeLinkObj( $title )
137 - . ' (' . $skin->makeLinkObj( $title->getTalkPage(), $talk ) . ")</li>\n" );
 143+ foreach( $titles as $title ) {
 144+ if( !$title instanceof Title )
 145+ $title = Title::newFromText( $title );
 146+ if( $title instanceof Title ) {
 147+ $output->addHtml( "<li>" . $skin->makeLinkObj( $title )
 148+ . ' (' . $skin->makeLinkObj( $title->getTalkPage(), $talk ) . ")</li>\n" );
 149+ }
 150+ }
138151 $output->addHtml( "</ul>\n" );
139152 }
140153
@@ -151,24 +164,32 @@
152165 }
153166
154167 /**
155 - * Get a list of titles on a user's watchlist, excluding talk pages
 168+ * Prepare a list of titles on a user's watchlist (excluding talk pages)
 169+ * and return an array of (prefixed) strings
156170 *
157171 * @param User $user
158172 * @return array
159173 */
160174 private function getWatchlist( $user ) {
161 - $titles = array();
 175+ $list = array();
162176 $dbr = wfGetDB( DB_SLAVE );
163 - $res = $dbr->select( 'watchlist', '*', array( 'wl_user' => $user->getId() ), __METHOD__ );
164 - if( $res && $dbr->numRows( $res ) > 0 ) {
165 - while( $row = $dbr->fetchObject( $res ) ) {
 177+ $res = $dbr->select(
 178+ 'watchlist',
 179+ '*',
 180+ array(
 181+ 'wl_user' => $user->getId(),
 182+ ),
 183+ __METHOD__
 184+ );
 185+ if( $res->numRows() > 0 ) {
 186+ while( $row = $res->fetchObject() ) {
166187 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
167188 if( $title instanceof Title && !$title->isTalkPage() )
168 - $titles[] = $title;
 189+ $list[] = $title->getPrefixedText();
169190 }
170 - $dbr->freeResult( $res );
 191+ $res->free();
171192 }
172 - return $titles;
 193+ return $list;
173194 }
174195
175196 /**
@@ -239,25 +260,32 @@
240261 /**
241262 * Add a list of titles to a user's watchlist
242263 *
243 - * @param array $titles
 264+ * $titles can be an array of strings or Title objects; the former
 265+ * is preferred, since Titles are very memory-heavy
 266+ *
 267+ * @param array $titles An array of strings, or Title objects
244268 * @param User $user
245269 */
246270 private function watchTitles( $titles, $user ) {
247271 $dbw = wfGetDB( DB_MASTER );
248272 $rows = array();
249273 foreach( $titles as $title ) {
250 - $rows[] = array(
251 - 'wl_user' => $user->getId(),
252 - 'wl_namespace' => ( $title->getNamespace() & ~1 ),
253 - 'wl_title' => $title->getDBkey(),
254 - 'wl_notificationtimestamp' => null,
255 - );
256 - $rows[] = array(
257 - 'wl_user' => $user->getId(),
258 - 'wl_namespace' => ( $title->getNamespace() | 1 ),
259 - 'wl_title' => $title->getDBkey(),
260 - 'wl_notificationtimestamp' => null,
261 - );
 274+ if( !$title instanceof Title )
 275+ $title = Title::newFromText( $title );
 276+ if( $title instanceof Title ) {
 277+ $rows[] = array(
 278+ 'wl_user' => $user->getId(),
 279+ 'wl_namespace' => ( $title->getNamespace() & ~1 ),
 280+ 'wl_title' => $title->getDBkey(),
 281+ 'wl_notificationtimestamp' => null,
 282+ );
 283+ $rows[] = array(
 284+ 'wl_user' => $user->getId(),
 285+ 'wl_namespace' => ( $title->getNamespace() | 1 ),
 286+ 'wl_title' => $title->getDBkey(),
 287+ 'wl_notificationtimestamp' => null,
 288+ );
 289+ }
262290 }
263291 $dbw->insert( 'watchlist', $rows, __METHOD__, 'IGNORE' );
264292 }
@@ -265,30 +293,37 @@
266294 /**
267295 * Remove a list of titles from a user's watchlist
268296 *
269 - * @param array $titles
 297+ * $titles can be an array of strings or Title objects; the former
 298+ * is preferred, since Titles are very memory-heavy
 299+ *
 300+ * @param array $titles An array of strings, or Title objects
270301 * @param User $user
271302 */
272303 private function unwatchTitles( $titles, $user ) {
273304 $dbw = wfGetDB( DB_MASTER );
274305 foreach( $titles as $title ) {
275 - $dbw->delete(
276 - 'watchlist',
277 - array(
278 - 'wl_user' => $user->getId(),
279 - 'wl_namespace' => ( $title->getNamespace() & ~1 ),
280 - 'wl_title' => $title->getDBkey(),
281 - ),
282 - __METHOD__
283 - );
284 - $dbw->delete(
285 - 'watchlist',
286 - array(
287 - 'wl_user' => $user->getId(),
288 - 'wl_namespace' => ( $title->getNamespace() | 1 ),
289 - 'wl_title' => $title->getDBkey(),
290 - ),
291 - __METHOD__
292 - );
 306+ if( !$title instanceof Title )
 307+ $title = Title::newFromText( $title );
 308+ if( $title instanceof Title ) {
 309+ $dbw->delete(
 310+ 'watchlist',
 311+ array(
 312+ 'wl_user' => $user->getId(),
 313+ 'wl_namespace' => ( $title->getNamespace() & ~1 ),
 314+ 'wl_title' => $title->getDBkey(),
 315+ ),
 316+ __METHOD__
 317+ );
 318+ $dbw->delete(
 319+ 'watchlist',
 320+ array(
 321+ 'wl_user' => $user->getId(),
 322+ 'wl_namespace' => ( $title->getNamespace() | 1 ),
 323+ 'wl_title' => $title->getDBkey(),
 324+ ),
 325+ __METHOD__
 326+ );
 327+ }
293328 }
294329 }
295330
@@ -299,11 +334,12 @@
300335 * @param User $user
301336 */
302337 private function showClearForm( $output, $user ) {
 338+ global $wgUser;
303339 if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) {
304340 $self = SpecialPage::getTitleFor( 'Watchlist' );
305341 $form = Xml::openElement( 'form', array( 'method' => 'post',
306342 'action' => $self->getLocalUrl( 'action=clear' ) ) );
307 - $form .= Xml::hidden( 'token', $user->editToken( 'watchlistedit' ) );
 343+ $form .= Xml::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
308344 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-clear-legend' ) . '</legend>';
309345 $form .= wfMsgExt( 'watchlistedit-clear-confirm', 'parse' );
310346 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-clear-submit' ) ) . '</p>';
@@ -319,11 +355,12 @@
320356 * @param User $user
321357 */
322358 private function showNormalForm( $output, $user ) {
 359+ global $wgUser;
323360 if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) {
324361 $self = SpecialPage::getTitleFor( 'Watchlist' );
325362 $form = Xml::openElement( 'form', array( 'method' => 'post',
326363 'action' => $self->getLocalUrl( 'action=edit' ) ) );
327 - $form .= Xml::hidden( 'token', $user->editToken( 'watchlistedit' ) );
 364+ $form .= Xml::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
328365 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-normal-legend' ) . '</legend>';
329366 $form .= wfMsgExt( 'watchlistedit-normal-explain', 'parse' );
330367 foreach( $this->getWatchlistInfo( $user ) as $namespace => $pages ) {
@@ -331,7 +368,7 @@
332369 $form .= '<ul>';
333370 foreach( $pages as $dbkey => $redirect ) {
334371 $title = Title::makeTitleSafe( $namespace, $dbkey );
335 - $form .= $this->buildRemoveLine( $title, $redirect, $user->getSkin() );
 372+ $form .= $this->buildRemoveLine( $title, $redirect, $wgUser->getSkin() );
336373 }
337374 $form .= '</ul>';
338375 }
@@ -382,18 +419,20 @@
383420 * @param User $user
384421 */
385422 public function showRawForm( $output, $user ) {
 423+ global $wgUser;
386424 $this->showItemCount( $output, $user );
387425 $self = SpecialPage::getTitleFor( 'Watchlist' );
388426 $form = Xml::openElement( 'form', array( 'method' => 'post',
389427 'action' => $self->getLocalUrl( 'action=raw' ) ) );
390 - $form .= Xml::hidden( 'token', $user->editToken( 'watchlistedit' ) );
 428+ $form .= Xml::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
391429 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-raw-legend' ) . '</legend>';
392430 $form .= wfMsgExt( 'watchlistedit-raw-explain', 'parse' );
393431 $form .= Xml::label( wfMsg( 'watchlistedit-raw-titles' ), 'titles' );
394432 $form .= Xml::openElement( 'textarea', array( 'id' => 'titles', 'name' => 'titles',
395 - 'rows' => 6, 'cols' => 80 ) );
396 - foreach( $this->getWatchlist( $user ) as $title )
397 - $form .= htmlspecialchars( $title->getPrefixedText() ) . "\n";
 433+ 'rows' => $wgUser->getIntOption( 'rows' ), 'cols' => $wgUser->getIntOption( 'cols' ) ) );
 434+ $titles = $this->getWatchlist( $user );
 435+ foreach( $titles as $title )
 436+ $form .= htmlspecialchars( $title ) . "\n";
398437 $form .= '</textarea>';
399438 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-raw-submit' ) ) . '</p>';
400439 $form .= '</fieldset></form>';

Follow-up revisions

RevisionCommit summaryAuthorDate
r23912Merged revisions 23662-23909 via svnmerge from...david18:11, 9 July 2007

Status & tagging log