Jdlrobson has uploaded a new change for review.
https://gerrit.wikimedia.org/r/183756
Change subject: Add saveToDatabase method to WatchlistPageCollection
......................................................................
Add saveToDatabase method to WatchlistPageCollection
Use the WatchlistPageCollection to manage saving and removing
t
* Add methods pop
Change-Id: I7796c9e61c43291b0491810bf4f542f38d05308b
---
M includes/collections/PageCollection.php
M includes/collections/WatchlistPageCollection.php
M includes/specials/SpecialEditWatchlist.php
3 files changed, 84 insertions(+), 63 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/56/183756/1
diff --git a/includes/collections/PageCollection.php
b/includes/collections/PageCollection.php
index 1103dff..e4e51b5 100644
--- a/includes/collections/PageCollection.php
+++ b/includes/collections/PageCollection.php
@@ -9,6 +9,13 @@
*/
class PageCollection implements IteratorAggregate, Countable {
/**
+ * The internal collection of removed pages.
+ *
+ * @var PageCollectionItem[]
+ */
+ protected $removedItems;
+
+ /**
* The user who owns the collection.
*
* @var User $user
@@ -65,6 +72,15 @@
$this->items = array();
}
+ /**
+ * Pop the last title from the collection
+ */
+ public function pop() {
+ $item = array_pop( $this->items );
+ $this->removedItems[] = $item;
+ return $item;
+ }
+
/** @inheritdoc */
public function getIterator() {
return new ArrayIterator( $this->items );
diff --git a/includes/collections/WatchlistPageCollection.php
b/includes/collections/WatchlistPageCollection.php
index 1a1e36e..03fa98e 100644
--- a/includes/collections/WatchlistPageCollection.php
+++ b/includes/collections/WatchlistPageCollection.php
@@ -8,7 +8,6 @@
* A collection of items representing a set of pages that the user is watching.
*/
class WatchlistPageCollection extends PageCollection {
-
/**
* Get the database associated with the Watchlist.
* @return DatabaseBase
@@ -67,4 +66,62 @@
__METHOD__
);
}
+
+ /**
+ * Save to database any changes to the model.
+ */
+ public function saveToDatabase() {
+ $dbw = $this->getDatabase();
+ $rows = array();
+
+ foreach ( $this->items as $item ) {
+ $title = $item->getTitle();
+ // add page
+ $rows[] = array(
+ 'wl_user' => $this->getOwner()->getId(),
+ 'wl_namespace' => MWNamespace::getSubject(
$title->getNamespace() ),
+ 'wl_title' => $title->getDBkey(),
+ 'wl_notificationtimestamp' => null,
+ );
+ // add talk page
+ $rows[] = array(
+ 'wl_user' => $this->getOwner()->getId(),
+ 'wl_namespace' => MWNamespace::getTalk(
$title->getNamespace() ),
+ 'wl_title' => $title->getDBkey(),
+ 'wl_notificationtimestamp' => null,
+ );
+ }
+
+ if ( count( $rows ) > 0 ) {
+ $dbw->insert( 'watchlist', $rows, __METHOD__, 'IGNORE'
);
+ }
+
+ foreach ( $this->removedItems as $item ) {
+ $title = $item->getTitle();
+ // delete page
+ $dbw->delete(
+ 'watchlist',
+ array(
+ 'wl_user' => $this->getOwner()->getId(),
+ 'wl_namespace' =>
MWNamespace::getSubject( $title->getNamespace() ),
+ 'wl_title' => $title->getDBkey(),
+ ),
+ __METHOD__
+ );
+
+ // delete talk page
+ $dbw->delete(
+ 'watchlist',
+ array(
+ 'wl_user' => $this->getOwner()->getId(),
+ 'wl_namespace' => MWNamespace::getTalk(
$title->getNamespace() ),
+ 'wl_title' => $title->getDBkey(),
+ ),
+ __METHOD__
+ );
+
+ $page = WikiPage::factory( $title );
+ Hooks::run( 'UnwatchArticleComplete', array(
$this->getOwner(), &$page ) );
+ }
+ }
}
diff --git a/includes/specials/SpecialEditWatchlist.php
b/includes/specials/SpecialEditWatchlist.php
index 51f6f8b..bbac933 100644
--- a/includes/specials/SpecialEditWatchlist.php
+++ b/includes/specials/SpecialEditWatchlist.php
@@ -193,8 +193,7 @@
if ( count( $wanted ) > 0 ) {
$toWatch = array_diff( $wanted, $current );
$toUnwatch = array_diff( $current, $wanted );
- $this->watchTitles( $toWatch );
- $this->unwatchTitles( $toUnwatch );
+ $this->updateCollectionInDatabase( $toWatch, $toUnwatch
);
$this->getUser()->invalidateCache();
if ( count( $toWatch ) > 0 || count( $toUnwatch ) > 0 )
{
@@ -431,83 +430,32 @@
* is preferred, since Titles are very memory-heavy
*
* @param array $titles Array of strings, or Title objects
+ * @param array $titlesToRemove Array of strings, or Title objects to
remove
*/
- private function watchTitles( $titles ) {
- $dbw = wfGetDB( DB_MASTER );
- $rows = array();
-
+ private function updateCollectionInDatabase( $titles, $titlesToRemove )
{
+ $collection = $this->getWatchlistPageCollection();
foreach ( $titles as $title ) {
if ( !$title instanceof Title ) {
$title = Title::newFromText( $title );
}
-
- if ( $title instanceof Title ) {
- $rows[] = array(
- 'wl_user' => $this->getUser()->getId(),
- 'wl_namespace' =>
MWNamespace::getSubject( $title->getNamespace() ),
- 'wl_title' => $title->getDBkey(),
- 'wl_notificationtimestamp' => null,
- );
- $rows[] = array(
- 'wl_user' => $this->getUser()->getId(),
- 'wl_namespace' => MWNamespace::getTalk(
$title->getNamespace() ),
- 'wl_title' => $title->getDBkey(),
- 'wl_notificationtimestamp' => null,
- );
- }
+ $collection->add( new PageCollectionItem( $title ) );
}
- $dbw->insert( 'watchlist', $rows, __METHOD__, 'IGNORE' );
- }
-
- /**
- * Remove a list of titles from a user's watchlist
- *
- * $titles can be an array of strings or Title objects; the former
- * is preferred, since Titles are very memory-heavy
- *
- * @param array $titles Array of strings, or Title objects
- */
- private function unwatchTitles( $titles ) {
- $dbw = wfGetDB( DB_MASTER );
-
- foreach ( $titles as $title ) {
+ foreach ( $titlesToRemove as $title ) {
if ( !$title instanceof Title ) {
$title = Title::newFromText( $title );
}
-
- if ( $title instanceof Title ) {
- $dbw->delete(
- 'watchlist',
- array(
- 'wl_user' =>
$this->getUser()->getId(),
- 'wl_namespace' =>
MWNamespace::getSubject( $title->getNamespace() ),
- 'wl_title' =>
$title->getDBkey(),
- ),
- __METHOD__
- );
-
- $dbw->delete(
- 'watchlist',
- array(
- 'wl_user' =>
$this->getUser()->getId(),
- 'wl_namespace' =>
MWNamespace::getTalk( $title->getNamespace() ),
- 'wl_title' =>
$title->getDBkey(),
- ),
- __METHOD__
- );
-
- $page = WikiPage::factory( $title );
- Hooks::run( 'UnwatchArticleComplete', array(
$this->getUser(), &$page ) );
- }
+ $collection->add( new PageCollectionItem( $title ) );
+ $collection->pop();
}
+ $collection->saveToDatabase();
}
public function submitNormal( $data ) {
$removed = array();
foreach ( $data as $titles ) {
- $this->unwatchTitles( $titles );
+ $this->updateCollectionInDatabase( array(), $titles );
$removed = array_merge( $removed, $titles );
}
--
To view, visit https://gerrit.wikimedia.org/r/183756
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I7796c9e61c43291b0491810bf4f542f38d05308b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits