Hello Chad, Reedy, C. Scott Ananian, Addshore, Legoktm, MaxSem, jenkins-bot, Siebrand, MarkAHershberger, Thiemo Mättig (WMDE), Jforrester,
I'd like you to do a code review. Please visit https://gerrit.wikimedia.org/r/332929 to review the following change. Change subject: Move all functions into CollectionAjaxFunctions file in preparation for extension.json ...................................................................... Move all functions into CollectionAjaxFunctions file in preparation for extension.json Change-Id: I240b65f68d3023d21be3f281deaaaf07e1105391 --- M Collection.php A CollectionAjaxFunctions.php 2 files changed, 258 insertions(+), 253 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Collection refs/changes/29/332929/1 diff --git a/Collection.php b/Collection.php index 8587dfd..80852a9 100644 --- a/Collection.php +++ b/Collection.php @@ -176,6 +176,7 @@ $wgAutoloadClasses['SpecialCollection'] = __DIR__ . '/Collection.body.php'; $wgAutoloadClasses['CollectionSession'] = __DIR__ . '/Collection.session.php'; $wgAutoloadClasses['CollectionHooks'] = __DIR__ . '/Collection.hooks.php'; +$wgAutoloadClasses['CollectionAjaxFunctions'] = __DIR__ . '/CollectionAjaxFunctions.php'; $wgAutoloadClasses['CollectionSuggest'] = __DIR__ . '/Collection.suggest.php'; $wgAutoloadClasses['CollectionProposals'] = __DIR__ . '/Collection.suggest.php'; @@ -250,275 +251,42 @@ # register global Ajax functions: -function wfAjaxGetCollection() { - if ( isset( $_SESSION['wsCollection'] ) ) { - $collection = $_SESSION['wsCollection']; - } else { - $collection = array(); - } - $r = new AjaxResponse( FormatJson::encode( array( 'collection' => $collection ) ) ); - $r->setContentType( 'application/json' ); - return $r; -} +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxGetCollection'; -$wgAjaxExportList[] = 'wfAjaxGetCollection'; +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxPostCollection'; -function wfAjaxPostCollection( $collection = '', $redirect = '' ) { - if ( session_id() == '' ) { - wfSetupSession(); - } - $collection = FormatJson::decode( $collection, true ); - $collection['enabled'] = true; - $_SESSION['wsCollection'] = $collection; - $r = new AjaxResponse(); - if ( $redirect ) { - $title = Title::newFromText( $redirect ); - $redirecturl = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT ); - $r->setResponseCode( 302 ); - header( 'Location: ' . $redirecturl ); - } else { - $title = SpecialPage::getTitleFor( 'Book' ); - $redirecturl = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT ); - $r->setContentType( 'application/json' ); - $r->addText( FormatJson::encode( array( 'redirect_url' => $redirecturl ) ) ); - } - return $r; -} +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxGetMWServeStatus'; -$wgAjaxExportList[] = 'wfAjaxPostCollection'; +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionAddArticle'; -function wfAjaxGetMWServeStatus( $collection_id = '', $writer = 'rl' ) { - $response = CollectionRenderingAPI::instance( $writer ) - ->getRenderStatus( $collection_id ); - $result = $response->response; - if ( isset( $result['status']['progress'] ) ) { - $result['status']['progress'] = number_format( $result['status']['progress'], 2, '.', '' ); - } - $r = new AjaxResponse( FormatJson::encode( $result ) ); - $r->setContentType( 'application/json' ); - return $r; -} +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionRemoveArticle'; -$wgAjaxExportList[] = 'wfAjaxGetMWServeStatus'; +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionAddCategory'; -function wfAjaxCollectionAddArticle( $namespace = 0, $title = '', $oldid = '' ) { - SpecialCollection::addArticleFromName( $namespace, $title, $oldid ); - return wfAjaxCollectionGetItemList(); -} +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionGetBookCreatorBoxContent'; -$wgAjaxExportList[] = 'wfAjaxCollectionAddArticle'; +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionGetItemList'; -function wfAjaxCollectionRemoveArticle( $namespace = 0, $title = '', $oldid = '' ) { - SpecialCollection::removeArticleFromName( $namespace, $title, $oldid ); - return wfAjaxCollectionGetItemList(); -} +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionRemoveItem'; -$wgAjaxExportList[] = 'wfAjaxCollectionRemoveArticle'; +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionAddChapter'; -function wfAjaxCollectionAddCategory( $title = '' ) { - SpecialCollection::addCategoryFromName( $title ); - return wfAjaxCollectionGetItemList(); -} +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionRenameChapter'; -$wgAjaxExportList[] = 'wfAjaxCollectionAddCategory'; +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionSetTitles'; -function wfAjaxCollectionGetBookCreatorBoxContent( - $ajaxHint = '', - $oldid = null, - $pageName = null -) { - if ( !is_null( $oldid ) ) { - $oldid = intval( $oldid ); - } +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionSetSorting'; - $title = null; - if ( !is_null( $pageName ) ) { - $title = Title::newFromText( $pageName ); - } - if ( is_null( $title ) ) { - $title = Title::newMainPage(); - } +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionClear'; - $html = CollectionHooks::getBookCreatorBoxContent( $title, $ajaxHint, $oldid ); +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionGetPopupData'; - $result = array(); - $result['html'] = $html; - $r = new AjaxResponse( FormatJson::encode( $result ) ); - $r->setContentType( 'application/json' ); - return $r; -} +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionSuggestBanArticle'; -$wgAjaxExportList[] = 'wfAjaxCollectionGetBookCreatorBoxContent'; +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionSuggestAddArticle'; -function wfAjaxCollectionGetItemList() { - $collection = $_SESSION['wsCollection']; +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionSuggestRemoveArticle'; - $template = new CollectionListTemplate(); - $template->set( 'collection', $collection ); - $template->set( 'is_ajax', true ); - ob_start(); - $template->execute(); - $html = ob_get_contents(); - ob_end_clean(); +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionSuggestUndoArticle'; - $result = array(); - $result['html'] = $html; - $result['collection'] = $collection; - $r = new AjaxResponse( FormatJson::encode( $result ) ); - $r->setContentType( 'application/json' ); - return $r; -} - -$wgAjaxExportList[] = 'wfAjaxCollectionGetItemList'; - -function wfAjaxCollectionRemoveItem( $index ) { - SpecialCollection::removeItem( (int)$index ); - return wfAjaxCollectionGetItemList(); -} - -$wgAjaxExportList[] = 'wfAjaxCollectionRemoveItem'; - -function wfAjaxCollectionAddChapter( $name ) { - SpecialCollection::addChapter( $name ); - return wfAjaxCollectionGetItemList(); -} - -$wgAjaxExportList[] = 'wfAjaxCollectionAddChapter'; - -function wfAjaxCollectionRenameChapter( $index, $name ) { - SpecialCollection::renameChapter( (int)$index, $name ); - return wfAjaxCollectionGetItemList(); -} - -$wgAjaxExportList[] = 'wfAjaxCollectionRenameChapter'; - -function wfAjaxCollectionSetTitles( $title, $subtitle, $settings = '' ) { - SpecialCollection::setTitles( $title, $subtitle ); - $settings = FormatJson::decode( $settings, true ); - if ( is_array( $settings ) ) { - SpecialCollection::setSettings( $settings ); - } - return wfAjaxCollectionGetItemList(); -} - -$wgAjaxExportList[] = 'wfAjaxCollectionSetTitles'; - -function wfAjaxCollectionSetSorting( $items_string ) { - $parsed = array(); - parse_str( $items_string, $parsed ); - $items = array(); - foreach ( $parsed['item'] as $s ) { - if ( is_numeric( $s ) ) { - $items[] = intval( $s ); - } - } - SpecialCollection::setSorting( $items ); - return wfAjaxCollectionGetItemList(); -} - -$wgAjaxExportList[] = 'wfAjaxCollectionSetSorting'; - -function wfAjaxCollectionClear() { - CollectionSession::clearCollection(); - CollectionSuggest::clear(); - return wfAjaxCollectionGetItemList(); -} - -$wgAjaxExportList[] = 'wfAjaxCollectionClear'; - -function wfAjaxCollectionGetPopupData( $title ) { - global $wgExtensionAssetsPath; - - $result = array(); - $imagePath = "$wgExtensionAssetsPath/Collection/images"; - $t = Title::newFromText( $title ); - if ( $t && $t->isRedirect() ) { - $wikiPage = WikiPage::factory( $t ); - $t = $wikiPage->followRedirect(); - if ( $t instanceof Title ) { - $title = $t->getPrefixedText(); - } - } - if ( CollectionSession::findArticle( $title ) == - 1 ) { - $result['action'] = 'add'; - $result['text'] = wfMessage( 'coll-add_linked_article' )->text(); - $result['img'] = "$imagePath/silk-add.png"; - } else { - $result['action'] = 'remove'; - $result['text'] = wfMessage( 'coll-remove_linked_article' )->text(); - $result['img'] = "$imagePath/silk-remove.png"; - } - $result['title'] = $title; - $r = new AjaxResponse( FormatJson::encode( $result ) ); - $r->setContentType( 'application/json' ); - - return $r; -} - -$wgAjaxExportList[] = 'wfAjaxCollectionGetPopupData'; - -/** - * Backend of several following SAJAX function handlers... - * @param String $action provided by the specific handlers internally - * @param String $article title passed in from client - * @return AjaxResponse with JSON-encoded array including HTML fragment. - */ -function wfCollectionSuggestAction( $action, $article ) { - $result = CollectionSuggest::refresh( $action, $article ); - $undoLink = Xml::element( 'a', - array( - 'href' => SkinTemplate::makeSpecialUrl( - 'Book', - array( 'bookcmd' => 'suggest', 'undo' => $action, 'arttitle' => $article ) - ), - 'onclick' => "collectionSuggestCall('UndoArticle'," . - Xml::encodeJsVar( array( $action, $article ) ) . "); return false;", - 'title' => wfMessage( 'coll-suggest_undo_tooltip' )->text(), - ), - wfMessage( 'coll-suggest_undo' )->text() - ); - // Message keys used: - // coll-suggest_article_ban - // coll-suggest_article_add - // coll-suggest_article_remove - $result['last_action'] = wfMessage( "coll-suggest_article_$action", $article ) - ->rawParams( $undoLink )->parse(); - $result['collection'] = CollectionSession::getCollection(); - $r = new AjaxResponse( FormatJson::encode( $result ) ); - $r->setContentType( 'application/json' ); - return $r; -} - -function wfAjaxCollectionSuggestBanArticle( $article ) { - return wfCollectionSuggestAction( 'ban', $article ); -} - -$wgAjaxExportList[] = 'wfAjaxCollectionSuggestBanArticle'; - -function wfAjaxCollectionSuggestAddArticle( $article ) { - return wfCollectionSuggestAction( 'add', $article ); -} - -$wgAjaxExportList[] = 'wfAjaxCollectionSuggestAddArticle'; - -function wfAjaxCollectionSuggestRemoveArticle( $article ) { - return wfCollectionSuggestAction( 'remove', $article ); -} - -$wgAjaxExportList[] = 'wfAjaxCollectionSuggestRemoveArticle'; - -function wfAjaxCollectionSuggestUndoArticle( $lastAction, $article ) { - $result = CollectionSuggest::undo( $lastAction, $article ); - $r = new AjaxResponse( FormatJson::encode( $result ) ); - $r->setContentType( 'application/json' ); - return $r; -} - -$wgAjaxExportList[] = 'wfAjaxCollectionSuggestUndoArticle'; - -function wfAjaxCollectionSortItems() { - SpecialCollection::sortItems(); - return wfAjaxCollectionGetItemList(); -} - -$wgAjaxExportList[] = 'wfAjaxCollectionSortItems'; +$wgAjaxExportList[] = 'CollectionAjaxFunctions::onAjaxCollectionSortItems'; diff --git a/CollectionAjaxFunctions.php b/CollectionAjaxFunctions.php new file mode 100644 index 0000000..9c9951b --- /dev/null +++ b/CollectionAjaxFunctions.php @@ -0,0 +1,237 @@ +<?php + +class CollectionAjaxFunctions { + + public static function onAjaxGetCollection() { + if ( isset( $_SESSION['wsCollection'] ) ) { + $collection = $_SESSION['wsCollection']; + } else { + $collection = array(); + } + $r = new AjaxResponse( FormatJson::encode( array( 'collection' => $collection ) ) ); + $r->setContentType( 'application/json' ); + return $r; + } + + public static function onAjaxPostCollection( $collection = '', $redirect = '' ) { + if ( session_id() == '' ) { + wfSetupSession(); + } + $collection = FormatJson::decode( $collection, true ); + $collection['enabled'] = true; + $_SESSION['wsCollection'] = $collection; + $r = new AjaxResponse(); + if ( $redirect ) { + $title = Title::newFromText( $redirect ); + $redirecturl = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT ); + $r->setResponseCode( 302 ); + header( 'Location: ' . $redirecturl ); + } else { + $title = SpecialPage::getTitleFor( 'Book' ); + $redirecturl = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT ); + $r->setContentType( 'application/json' ); + $r->addText( FormatJson::encode( array( 'redirect_url' => $redirecturl ) ) ); + } + return $r; + } + + public static function onAjaxGetMWServeStatus( $collection_id = '', $writer = 'rl' ) { + $response = CollectionRenderingAPI::instance( $writer ) + ->getRenderStatus( $collection_id ); + $result = $response->response; + if ( isset( $result['status']['progress'] ) ) { + $result['status']['progress'] = number_format( $result['status']['progress'], 2, '.', '' ); + } + $r = new AjaxResponse( FormatJson::encode( $result ) ); + $r->setContentType( 'application/json' ); + return $r; + } + + public static function onAjaxCollectionAddArticle( $namespace = 0, $title = '', $oldid = '' ) { + SpecialCollection::addArticleFromName( $namespace, $title, $oldid ); + return self::onAjaxCollectionGetItemList(); + } + + public static function onAjaxCollectionRemoveArticle( $namespace = 0, $title = '', $oldid = '' ) { + SpecialCollection::removeArticleFromName( $namespace, $title, $oldid ); + return self::onAjaxCollectionGetItemList(); + } + + public static function onAjaxCollectionAddCategory( $title = '' ) { + SpecialCollection::addCategoryFromName( $title ); + return self::onAjaxCollectionGetItemList(); + } + + public static function onAjaxCollectionGetBookCreatorBoxContent( + $ajaxHint = '', + $oldid = null, + $pageName = null + ) { + if ( !is_null( $oldid ) ) { + $oldid = intval( $oldid ); + } + + $title = null; + if ( !is_null( $pageName ) ) { + $title = Title::newFromText( $pageName ); + } + if ( is_null( $title ) ) { + $title = Title::newMainPage(); + } + + $html = CollectionHooks::getBookCreatorBoxContent( $title, $ajaxHint, $oldid ); + + $result = array(); + $result['html'] = $html; + $r = new AjaxResponse( FormatJson::encode( $result ) ); + $r->setContentType( 'application/json' ); + return $r; + } + + public static function onAjaxCollectionGetItemList() { + $collection = $_SESSION['wsCollection']; + + $template = new CollectionListTemplate(); + $template->set( 'collection', $collection ); + $template->set( 'is_ajax', true ); + ob_start(); + $template->execute(); + $html = ob_get_contents(); + ob_end_clean(); + + $result = array(); + $result['html'] = $html; + $result['collection'] = $collection; + $r = new AjaxResponse( FormatJson::encode( $result ) ); + $r->setContentType( 'application/json' ); + return $r; + } + + public static function onAjaxCollectionRemoveItem( $index ) { + SpecialCollection::removeItem( (int)$index ); + return self::onAjaxCollectionGetItemList(); + } + + public static function onAjaxCollectionAddChapter( $name ) { + SpecialCollection::addChapter( $name ); + return self::onAjaxCollectionGetItemList(); + } + + public static function onAjaxCollectionRenameChapter( $index, $name ) { + SpecialCollection::renameChapter( (int)$index, $name ); + return self::onAjaxCollectionGetItemList(); + } + + public static function onAjaxCollectionSetTitles( $title, $subtitle, $settings = '' ) { + SpecialCollection::setTitles( $title, $subtitle ); + $settings = FormatJson::decode( $settings, true ); + if ( is_array( $settings ) ) { + SpecialCollection::setSettings( $settings ); + } + return self::onAjaxCollectionGetItemList(); + } + + public static function onAjaxCollectionSetSorting( $items_string ) { + $parsed = array(); + parse_str( $items_string, $parsed ); + $items = array(); + foreach ( $parsed['item'] as $s ) { + if ( is_numeric( $s ) ) { + $items[] = intval( $s ); + } + } + SpecialCollection::setSorting( $items ); + return self::onAjaxCollectionGetItemList(); + } + + public static function onAjaxCollectionClear() { + CollectionSession::clearCollection(); + CollectionSuggest::clear(); + return self::onAjaxCollectionGetItemList(); + } + + public static function onAjaxCollectionGetPopupData( $title ) { + global $wgExtensionAssetsPath; + + $result = array(); + $imagePath = "$wgExtensionAssetsPath/Collection/images"; + $t = Title::newFromText( $title ); + if ( $t && $t->isRedirect() ) { + $wikiPage = WikiPage::factory( $t ); + $t = $wikiPage->followRedirect(); + if ( $t instanceof Title ) { + $title = $t->getPrefixedText(); + } + } + if ( CollectionSession::findArticle( $title ) == - 1 ) { + $result['action'] = 'add'; + $result['text'] = wfMessage( 'coll-add_linked_article' )->text(); + $result['img'] = "$imagePath/silk-add.png"; + } else { + $result['action'] = 'remove'; + $result['text'] = wfMessage( 'coll-remove_linked_article' )->text(); + $result['img'] = "$imagePath/silk-remove.png"; + } + $result['title'] = $title; + $r = new AjaxResponse( FormatJson::encode( $result ) ); + $r->setContentType( 'application/json' ); + + return $r; + } + + /** + * Backend of several following SAJAX function handlers... + * @param String $action provided by the specific handlers internally + * @param String $article title passed in from client + * @return AjaxResponse with JSON-encoded array including HTML fragment. + */ + public static function onCollectionSuggestAction( $action, $article ) { + $result = CollectionSuggest::refresh( $action, $article ); + $undoLink = Xml::element( 'a', + array( + 'href' => SkinTemplate::makeSpecialUrl( + 'Book', + array( 'bookcmd' => 'suggest', 'undo' => $action, 'arttitle' => $article ) + ), + 'onclick' => "collectionSuggestCall('UndoArticle'," . + Xml::encodeJsVar( array( $action, $article ) ) . "); return false;", + 'title' => wfMessage( 'coll-suggest_undo_tooltip' )->text(), + ), + wfMessage( 'coll-suggest_undo' )->text() + ); + // Message keys used: + // coll-suggest_article_ban + // coll-suggest_article_add + // coll-suggest_article_remove + $result['last_action'] = wfMessage( "coll-suggest_article_$action", $article ) + ->rawParams( $undoLink )->parse(); + $result['collection'] = CollectionSession::getCollection(); + $r = new AjaxResponse( FormatJson::encode( $result ) ); + $r->setContentType( 'application/json' ); + return $r; + } + + public static function onAjaxCollectionSuggestBanArticle( $article ) { + return self::onCollectionSuggestAction( 'ban', $article ); + } + + public static function onAjaxCollectionSuggestAddArticle( $article ) { + return self::onCollectionSuggestAction( 'add', $article ); + } + + public static function onAjaxCollectionSuggestRemoveArticle( $article ) { + return self::onCollectionSuggestAction( 'remove', $article ); + } + + public static function onAjaxCollectionSuggestUndoArticle( $lastAction, $article ) { + $result = CollectionSuggest::undo( $lastAction, $article ); + $r = new AjaxResponse( FormatJson::encode( $result ) ); + $r->setContentType( 'application/json' ); + return $r; + } + + public static function onAjaxCollectionSortItems() { + SpecialCollection::sortItems(); + return self::onAjaxCollectionGetItemList(); + } +} -- To view, visit https://gerrit.wikimedia.org/r/332929 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I240b65f68d3023d21be3f281deaaaf07e1105391 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Collection Gerrit-Branch: master Gerrit-Owner: Paladox <thomasmulhall...@yahoo.com> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: C. Scott Ananian <canan...@wikimedia.org> Gerrit-Reviewer: Chad <ch...@wikimedia.org> Gerrit-Reviewer: Jforrester <jforres...@wikimedia.org> Gerrit-Reviewer: Legoktm <lego...@member.fsf.org> Gerrit-Reviewer: MarkAHershberger <m...@nichework.com> Gerrit-Reviewer: MaxSem <maxsem.w...@gmail.com> Gerrit-Reviewer: Reedy <re...@wikimedia.org> Gerrit-Reviewer: Siebrand <siebr...@kitano.nl> Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits